package instancectl import ( "math" "strings" "testing" "github.com/carlosframework/platform/internal/provision" ) func TestGenerationUnitNames(t *testing.T) { const host = "bill-2.example.com" if got := GenInstance(host, 7); got != "bill-2.example.com:7" { t.Errorf("GenInstance = %q", got) } if got := GenUnitName(host, 7); got != `carlos-instance-gen@bill\x2d2.example.com:7.service` { t.Errorf("GenUnitName = %q", got) } esc, n, ok := ParseGenUnitName(GenUnitName(host, 7)) if !ok || esc != `bill\x2d2.example.com` || n != 7 { t.Errorf("ParseGenUnitName = %q %d %v", esc, n, ok) } // The sweep's parser knows the generation template, as a run unit in // the host's slice. role, esc, ok := ParseUnitName(GenUnitName(host, 7)) if !ok || role != RoleRun || esc != `bill\x2d2.example.com` { t.Errorf("ParseUnitName(gen) = %q %q %v", role, esc, ok) } // Flag off: the original names are unchanged. if got := UnitName(RoleRun, host); got != `carlos-instance@bill\x2d2.example.com.service` { t.Errorf("UnitName(run) = %q", got) } if role, esc, ok := ParseUnitName(`carlos-instance@bill\x2d2.example.com.service`); !ok || role != RoleRun || esc != `bill\x2d2.example.com` { t.Errorf("ParseUnitName(run) = %q %q %v", role, esc, ok) } for _, bad := range []string{"carlos-instance-gen@.service", `carlos-instance-gen@bill.example.com.service`, "carlos-instance-gen@bill.example.com:0.service"} { if _, _, ok := ParseUnitName(bad); ok { t.Errorf("ParseUnitName(%q) accepted", bad) } } } func TestParseGenInstanceIsStrict(t *testing.T) { if iid, n, ok := ParseGenInstance("_abc-123:42"); !ok || iid != "_abc-123" || n != 42 { t.Errorf("ParseGenInstance = %q %d %v", iid, n, ok) } for _, bad := range []string{"x", "x:", ":7", "x:0", "x:07", "x:7a", "a/b:7", "..:7", "x:18446744073709551616", "a:b:7"} { if _, _, ok := ParseGenInstance(bad); ok { t.Errorf("ParseGenInstance(%q) accepted", bad) } } } func TestGenerationFileNames(t *testing.T) { const host = "bill-2.example.com" for got, want := range map[string]string{ GenKey(host, 7): "bill-2.example.com@7", GenLogSocket(host, 7): "/run/carlos/logs/bill-2.example.com@7.run.sock", GenLaunchFileName(7): "launch.gen.7.json", GenExitRecordName(7, "abc"): "exit.gen.7.abc.json", GenExitPrefix(7): "exit.gen.7.", } { if got != want { t.Errorf("got %q, want %q", got, want) } } // Never under a legacy prefix: prepare's stale sweep for the run role // removes "exit.run.*" and must not touch a live generation's record. if strings.HasPrefix(GenExitRecordName(7, "abc"), "exit.run.") || GenLaunchFileName(7) == LaunchFileName(RoleRun) { t.Error("a generation file shares a legacy name") } } // D2: no instance ID holds '@', so a generation key never equals a legacy // instance ID, whatever the host. func TestGenKeyNeverMeetsALegacyName(t *testing.T) { for _, host := range []string{"shop.example.com", "shop.example.com.g7", strings.Repeat("x", 60) + ".example.com"} { if iid := provision.InstanceID(host); strings.Contains(iid, "@") { t.Fatalf("InstanceID(%q) = %q holds '@'", host, iid) } if strings.Count(GenKey(host, 7), "@") != 1 { t.Errorf("GenKey(%q) = %q", host, GenKey(host, 7)) } } if GenLogSocket("shop.example.com", 7) == provision.LogSocket("shop.example.com.g7", "run") { t.Error("a generation log socket met a legacy one") } } // Review focus: the longest host and the largest generation stay inside // sockaddr_un and systemd's unit-name limit (UNIT_NAME_MAX 256 with NUL). func TestGenerationNamesStayBoundedForTheLongestHost(t *testing.T) { host := strings.Repeat("a-b.", 60) + "example.com" n := uint64(math.MaxUint64) if got := GenLogSocket(host, n); len(got) > provision.MaxUnixPath { t.Errorf("GenLogSocket is %d bytes: %q", len(got), got) } unit := GenUnitName(host, n) if len(unit) > 255 { t.Errorf("GenUnitName is %d bytes: %q", len(unit), unit) } if !strings.HasPrefix(GenInstance(host, n), "_") { t.Errorf("GenInstance(long) = %q, want the hashed instance ID", GenInstance(host, n)) } esc, m, ok := ParseGenUnitName(unit) iid, uok := Unescape(esc) if !ok || m != n || !uok || iid != provision.InstanceID(host) { t.Errorf("round trip: %q %d %v -> %q %v", esc, m, ok, iid, uok) } } func TestUnescapeInvertsEscape(t *testing.T) { for _, s := range []string{"bill-2.example.com", "_abcdefghijklmnopqrstuv-0123456789abcdef", "a.b"} { if got, ok := Unescape(Escape(s)); !ok || got != s { t.Errorf("Unescape(Escape(%q)) = %q %v", s, got, ok) } } for _, bad := range []string{`a\x2`, `a\y2d`, `a\xzz`} { if _, ok := Unescape(bad); ok { t.Errorf("Unescape(%q) accepted", bad) } } } // ParseGenKey reads a generation's exe directory name back (Task 15's // sweep): it inverts GenKey, for a long host's hashed identifier too, and // refuses a host's own directory and every name GenKey never makes. func TestParseGenKeyInvertsGenKey(t *testing.T) { for _, host := range []string{"bill-2.example.com", strings.Repeat("a", 60) + ".example.com"} { iid, n, ok := ParseGenKey(GenKey(host, 7)) if !ok || iid != provision.InstanceID(host) || n != 7 { t.Errorf("ParseGenKey(GenKey(%q, 7)) = %q, %d, %v", host, iid, n, ok) } if got := GenUnitNameOf(iid, n); got != GenUnitName(host, 7) { t.Errorf("GenUnitNameOf = %q, want %q", got, GenUnitName(host, 7)) } if unit, ok := genUnitOfExeKey(GenKey(host, 7)); !ok || unit != GenUnitName(host, 7) { t.Errorf("genUnitOfExeKey(GenKey(%q, 7)) = %q, %v", host, unit, ok) } } for _, bad := range []string{"bill-2.example.com", "@7", "a@0", "a@07", "a@x", "a@", "a@b@7", "a:b@7", "a@7:1", ".@7", "..@7"} { if iid, n, ok := ParseGenKey(bad); ok { t.Errorf("ParseGenKey(%q) = %q, %d, true; want refused", bad, iid, n) } } }