package activator import ( "context" "errors" "fmt" "os/exec" "strings" "testing" "github.com/carlosframework/platform/internal/cgroup" "github.com/carlosframework/platform/internal/instancectl" "github.com/carlosframework/platform/internal/registry" ) func TestPlan14GenerationBackendDirectBoundary(t *testing.T) { for _, claimed := range []bool{false, true} { for _, strict := range []string{"unset", "ordinary", "strict"} { for _, child := range []string{"", "g7", "role-seed", "role-sidecar"} { t.Run(fmt.Sprintf("claim=%t/%s/%s", claimed, strict, child), func(t *testing.T) { b := testExecBackend(t) b.Cfg.HostWrites.Enabled = claimed prepared, built, gates := 0, 0, 0 prepareErr := errors.New("strict preparation reached") if strict != "unset" { b.Strict = func(string) bool { return strict == "strict" } b.prepareStrict = func(string, cgroup.Limit) (strictProc, error) { prepared++ return nil, prepareErr } } build := func() *exec.Cmd { built++; return exec.Command("true") } before := func() error { gates++; return nil } var cmd *exec.Cmd var settle func() var err error if child == "" { cmd, settle, err = b.spawnInCgroupGated(registry.Route{Host: "direct.example"}, cgroup.Limit{}, build, before) } else { cmd, settle, err = b.spawnInCgroupAtTier(registry.Route{Host: "direct.example"}, child, true, cgroup.Limit{}, build, before) } if cmd != nil { if wait := cmd.Wait(); wait != nil { t.Fatal(wait) } } if settle != nil { settle() } if claimed { if !errors.Is(err, ErrClaimIsolationUnavailable) || cmd != nil || prepared != 0 || built != 0 || gates != 0 { t.Fatalf("unreserved direct launch: cmd=%v prepared=%d built=%d gates=%d err=%v", cmd, prepared, built, gates, err) } } else if strict == "strict" { if !errors.Is(err, ErrStrictRequired) || built != 0 || gates != 0 { t.Fatalf("legacy strict boundary: %v", err) } } else if err != nil || cmd == nil || built != 1 || gates != 1 { t.Fatalf("legacy spawn: cmd=%v built=%d gates=%d err=%v", cmd, built, gates, err) } }) } } } } type generationBackendControl struct{ identityControl } func (c generationBackendControl) StartGen(_ context.Context, host string, n uint64, max int64) (string, error) { c.rec(fmt.Sprintf("startgen %s %d %d", host, n, max)) return c.inv, nil } func TestPlan14GenerationBackendControlBoundary(t *testing.T) { for _, claimed := range []bool{false, true} { for _, gen := range []uint64{0, 7} { for _, pinned := range []bool{false, true} { t.Run(fmt.Sprintf("claim=%t/gen=%d/pinned=%t", claimed, gen, pinned), func(t *testing.T) { ctl := &fakeControl{inv: "inv1"} b := testExecBackend(t) b.Cfg.HostWrites.Enabled = claimed b.Control = generationBackendControl{identityControl{ctl}} var exe *instancectl.StagedExe if pinned { exe = &instancectl.StagedExe{Path: "/unused/staged", Version: "v1", Digest: strings.Repeat("a", 64)} } inv, err := b.startRole(t.Context(), registry.Route{Host: "direct.example"}, instancectl.RoleRun, gen, exe, cgroup.Limit{}) if claimed { if !errors.Is(err, ErrClaimIsolationUnavailable) || inv != "" || ctl.joined() != "" { t.Fatalf("unreserved control launch: inv=%q calls=%q err=%v", inv, ctl.joined(), err) } } else if err != nil || inv != "inv1" || ctl.joined() == "" { t.Fatalf("legacy control launch: inv=%q calls=%q err=%v", inv, ctl.joined(), err) } }) } } } } func TestPlan14GenerationBackendAuxiliaryBoundary(t *testing.T) { t.Parallel() for _, claimed := range []bool{false, true} { for _, role := range []string{"seed", "sidecar"} { for _, pinned := range []bool{false, true} { t.Run(fmt.Sprintf("claim=%t/%s/pinned=%t", claimed, role, pinned), func(t *testing.T) { ctl := &fakeControl{inv: "inv1", seed: instancectl.SeedResponse{InvocationID: "s1", Result: "success", Status: "0", Code: "exited"}} unit := &fakeUnit{} b, rt := nestedBackend(t, ctl, unit) b.Cfg.HostWrites.Enabled = claimed b.Control = identityControl{ctl} rt.Seed = `["seed"]` stageVersion(t, b, rt.Host, "v1", "exit 0\n") id, err := b.LiveIdentity(t.Context(), rt) if err != nil { t.Fatal(err) } var exe *instancectl.StagedExe if pinned { exe = stagedExeOf(id) } var p Proc if role == "seed" { err = b.seedUnit(t.Context(), rt, exe, cgroup.Limit{}) } else { p, err = b.startSidecarUnitFrom(t.Context(), rt, exe, cgroup.Limit{}) } calls := ctl.joined() if p != nil { unit.set("inactive", "inv1", false, false) waitFor(t, "sidecar exit", p.Exited) if stop := p.Stop(t.Context()); stop != nil { t.Fatal(stop) } } if claimed { if !errors.Is(err, ErrClaimIsolationUnavailable) || p != nil || calls != "" { t.Fatalf("unreserved auxiliary launch: proc=%v calls=%q err=%v", p, calls, err) } } else if err != nil || calls == "" { t.Fatalf("legacy auxiliary launch: calls=%q err=%v", calls, err) } }) } } } }