57 lines
2.3 KiB
Go
57 lines
2.3 KiB
Go
package catalogue
|
|
|
|
import (
|
|
"strings"
|
|
"testing"
|
|
)
|
|
|
|
// novox/hq ADR 0223 part 3: a machine's names are one seat's. The `hosts` module holding
|
|
// `node-hosts-file` became `hostname` holding `node-hostname`, which writes /etc/hostname beside the
|
|
// machine's own lines in /etc/hosts. The seat was renamed (ADR 0122), so what claims the old name — a
|
|
// manifest registered before the rename — still holds the one seat.
|
|
|
|
// 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]Manifest{"hostname": catalogueManifest(t, "hostname")}
|
|
got, err := Resolve(cat, []string{"hostname"}, Node{Name: "ace", At: "ace.internal"}, World{})
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
machines := map[string]string{"ace.internal": "10.42.0.2"}
|
|
nameOf := func(settings SettingsBy) (string, string) {
|
|
t.Helper()
|
|
composed, err := got.Compose(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(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 := Layer{From: "the mesh", Values: map[string]any{"hostname": "${machine:name}"}}
|
|
if name, why := nameOf(SettingsBy{"hostname": {mesh}}); name != "ace\n" {
|
|
t.Errorf("a mesh-wide ${machine:name} gave %q (%s)", name, why)
|
|
}
|
|
node := Layer{From: "ace", Values: map[string]any{"hostname": "Ace"}}
|
|
if name, why := nameOf(SettingsBy{"hostname": {mesh, node}}); name != "Ace\n" {
|
|
t.Errorf("a machine's own name over the mesh-wide one gave %q (%s)", name, why)
|
|
}
|
|
}
|