package instancectl import ( "strings" "testing" "github.com/carlosframework/platform/internal/provision" ) // Escape follows systemd-escape(1) for unit instance names. The cases are // the ones our identifiers can produce (hosts are [a-z0-9.-]; a hashed // identifier adds a leading underscore), plus the general rules so a // surprise character can never yield an unloadable unit name. func TestEscapeFollowsSystemd(t *testing.T) { cases := map[string]string{ "bill.go.tito.io": "bill.go.tito.io", "bill-2.example.com": `bill\x2d2.example.com`, "_aaaa-1234abcd": `_aaaa\x2d1234abcd`, "a/b": "a-b", ".hidden": `\x2ehidden`, "x y": `x\x20y`, "": "", "UPPER_ok:1": "UPPER_ok:1", } for in, want := range cases { if got := Escape(in); got != want { t.Errorf("Escape(%q) = %q, want %q", in, got, want) } } } func TestUnitAndSliceNamesAreKeyedByEscapedInstanceID(t *testing.T) { host := "bill-2.example.com" esc := `bill\x2d2.example.com` if got := UnitName(RoleRun, host); got != "carlos-instance@"+esc+".service" { t.Errorf("run unit = %q", got) } if got := UnitName(RoleSeed, host); got != "carlos-instance-seed@"+esc+".service" { t.Errorf("seed unit = %q", got) } if got := UnitName(RoleSidecar, host); got != "carlos-instance-sidecar@"+esc+".service" { t.Errorf("sidecar unit = %q", got) } if got := SliceName(host); got != "carlos-instances-"+esc+".slice" { t.Errorf("slice = %q", got) } long := strings.Repeat("a", 60) + ".example.com" if got := UnitName(RoleRun, long); !strings.HasPrefix(got, "carlos-instance@_") || !strings.HasSuffix(got, ".service") { t.Errorf("long host unit = %q, want the hashed identifier", got) } if got, want := UnitName(RoleRun, long), "carlos-instance@"+Escape(provision.InstanceID(long))+".service"; got != want { t.Errorf("long host unit = %q, want %q", got, want) } } func TestParseUnitNameRoundTrips(t *testing.T) { for _, role := range []Role{RoleRun, RoleSeed, RoleSidecar, RoleCmd} { u := UnitName(role, "bill-2.example.com") gotRole, esc, ok := ParseUnitName(u) if !ok || gotRole != role || esc != `bill\x2d2.example.com` { t.Errorf("ParseUnitName(%q) = %q %q %v", u, gotRole, esc, ok) } } if got := UnitName(RoleCmd, "bill-2.example.com"); got != `carlos-instance-cmd@bill\x2d2.example.com.service` { t.Errorf("cmd unit = %q", got) } for _, bad := range []string{"carlos-edge.service", "carlos-app@x.service", "carlos-instance@x.socket", "carlos-instance@.service", "carlos-instance-cmd@.service", "carlos-instances-x.slice"} { if _, _, ok := ParseUnitName(bad); ok { t.Errorf("ParseUnitName(%q) accepted", bad) } } } func TestParseRole(t *testing.T) { for _, s := range []string{"run", "seed", "sidecar", "cmd"} { if r, ok := ParseRole(s); !ok || string(r) != s { t.Errorf("ParseRole(%q) = %q %v", s, r, ok) } } for _, s := range []string{"", "Run", "exec", "run ", "Cmd", "cmd-err"} { if _, ok := ParseRole(s); ok { t.Errorf("ParseRole(%q) accepted", s) } } } func TestFileNames(t *testing.T) { if got := LaunchFileName(RoleSidecar); got != "launch.sidecar.json" { t.Errorf("LaunchFileName = %q", got) } if got := ExitRecordName(RoleRun, "abc123"); got != "exit.run.abc123.json" { t.Errorf("ExitRecordName = %q", got) } if provision.ValidHostShape(ProbeDirName) { t.Errorf("ProbeDirName %q is a valid host; it must never collide with one", ProbeDirName) } }