package activator import ( "context" "errors" "os" "os/exec" "path/filepath" "testing" "github.com/carlosframework/platform/internal/cgroup" "github.com/carlosframework/platform/internal/hostbox" "github.com/carlosframework/platform/internal/hostclaim" "github.com/carlosframework/platform/internal/hostguard" "github.com/carlosframework/platform/internal/hostwrite" ) func TestClaimRoleLaunchRefusesWithoutIsolatedRoot(t *testing.T) { for _, role := range []string{"main", "seed", "sidecar", "unknown"} { t.Run(role, func(t *testing.T) { journal, err := hostbox.OpenLaunchJournal(filepath.Join(t.TempDir(), "launch.db")) if err != nil { t.Fatal(err) } defer journal.Close() target := hostguard.FenceTarget{Host: "tenant.test", Incarnation: "inc", Stamp: 1, Session: 1, Box: "box", Enrollment: "enroll", BootID: "boot", ControllerID: "ctrl"} work, err := (hostwrite.Config{}).Select(t.Context(), target.Host, "spawn") if err != nil { t.Fatal(err) } launch := RoleLaunch{Parent: &cgroup.Prepared{}, ParentPath: "parent", Work: work, Target: target, Journal: journal, Executable: func(context.Context, *exec.Cmd) (*os.File, error) { t.Fatal("unsupported launch inspected executable") return nil, nil }, Initialize: func(context.Context) error { t.Fatal("unsupported launch initialized state") return nil }, RegisterProducer: func(context.Context, string) (RoleProducer, error) { t.Fatal("unsupported launch registered a producer") return RoleProducer{}, nil }} cmd, child, settled, producer, err := launch.Start(t.Context(), role, hostclaim.WakeRetryAttempt{ID: "a", InvocationID: "inv", LaunchInputs: "inputs", Session: 1, Box: "box", Enrollment: "enroll", BootID: "boot", ControllerID: "ctrl"}, func() *exec.Cmd { t.Fatal("agent built tenant command without isolated root support") return nil }, func() error { t.Fatal("unsupported launch reached before-start effect"); return nil }) if !errors.Is(err, hostbox.ErrUnsupported) || cmd != nil || child != nil || settled != nil || producer.Context != nil || producer.Finish != nil { t.Fatalf("launch returned execution capability: cmd=%v child=%v err=%v", cmd, child, err) } }) } } func TestClaimSpawnDisabledKeepsLegacyBuild(t *testing.T) { calls, builds := 0, 0 dir := t.TempDir() cmd, prepared, err := spawnClaimedBefore(t.Context(), false, nil, "tenant.test", cgroup.TierDefault, claimSpawnWork{true, &calls}, hostwrite.Identity{}, func() *exec.Cmd { builds++ cmd := exec.Command(filepath.Join(dir, "absent-executable")) cmd.Dir, cmd.Env = dir, []string{} return cmd }, func() error { t.Fatal("disabled helper invoked claim callback"); return nil }) if builds != 1 || calls != 0 || cmd == nil || prepared != nil || !errors.Is(err, os.ErrNotExist) { t.Fatalf("legacy dispatch changed: builds=%d guards=%d cmd=%v prepared=%v err=%v", builds, calls, cmd, prepared, err) } }