package resolution import ( "strings" "testing" mcat "git.novox.be/novox/mesh-sdk/go/catalogue" ) // The catalogue's module: /etc/hostname is the operator's `hostname` setting. Without it the module is // left out, naming the key — the mesh never renames a machine on its own — and a mesh-wide setting // naming ${machine:name} gives every machine its mesh name. func TestTheMachinesNameIsItsSetting(t *testing.T) { cat := map[string]mcat.Manifest{"hostname": catalogueManifest(t, "hostname")} got, err := Resolve(cat, []string{"hostname"}, mcat.Node{Name: "ace", At: "ace.internal"}, mcat.World{}) if err != nil { t.Fatal(err) } machines := map[string]string{"ace.internal": "10.42.0.2"} nameOf := func(settings mcat.SettingsBy) (string, string) { t.Helper() composed, err := Compose(got, mcat.Rendering{Names: machines, Machines: machines, Suffix: "internal", Settings: settings}) if err != nil { t.Fatal(err) } if why := composed.LeftOut["hostname"]; why != "" { return "", why } for _, r := range composed.Resources { if r["path"] == "/etc/hostname" { return r["content"].(string), "" } } t.Fatal("no /etc/hostname composed") return "", "" } if _, why := nameOf(nil); !strings.Contains(why, "hostname") { t.Errorf("with no setting the machine's name was written, or left out for another reason: %q", why) } if name, why := nameOf(mcat.SettingsBy{"hostname": {{From: "ace", Values: map[string]any{"hostname": "Ace"}}}}); name != "Ace\n" { t.Errorf("the operator's name for the machine gave %q (%s)", name, why) } mesh := mcat.Layer{From: "the mesh", Values: map[string]any{"hostname": "${machine:name}"}} if name, why := nameOf(mcat.SettingsBy{"hostname": {mesh}}); name != "ace\n" { t.Errorf("a mesh-wide ${machine:name} gave %q (%s)", name, why) } node := mcat.Layer{From: "ace", Values: map[string]any{"hostname": "Ace"}} if name, why := nameOf(mcat.SettingsBy{"hostname": {mesh, node}}); name != "Ace\n" { t.Errorf("a machine's own name over the mesh-wide one gave %q (%s)", name, why) } }