| 1 | package activator |
| 2 | |
| 3 | import ( |
| 4 | "context" |
| 5 | "errors" |
| 6 | "os" |
| 7 | "os/exec" |
| 8 | "path/filepath" |
| 9 | "testing" |
| 10 | |
| 11 | "github.com/carlosframework/platform/internal/cgroup" |
| 12 | "github.com/carlosframework/platform/internal/hostbox" |
| 13 | "github.com/carlosframework/platform/internal/hostclaim" |
| 14 | "github.com/carlosframework/platform/internal/hostguard" |
| 15 | "github.com/carlosframework/platform/internal/hostwrite" |
| 16 | ) |
| 17 | |
| 18 | func TestClaimRoleLaunchRefusesWithoutIsolatedRoot(t *testing.T) { |
| 19 | for _, role := range []string{"main", "seed", "sidecar", "unknown"} { |
| 20 | t.Run(role, func(t *testing.T) { |
| 21 | journal, err := hostbox.OpenLaunchJournal(filepath.Join(t.TempDir(), "launch.db")) |
| 22 | if err != nil { |
| 23 | t.Fatal(err) |
| 24 | } |
| 25 | defer journal.Close() |
| 26 | target := hostguard.FenceTarget{Host: "tenant.test", Incarnation: "inc", Stamp: 1, Session: 1, Box: "box", Enrollment: "enroll", BootID: "boot", ControllerID: "ctrl"} |
| 27 | work, err := (hostwrite.Config{}).Select(t.Context(), target.Host, "spawn") |
| 28 | if err != nil { |
| 29 | t.Fatal(err) |
| 30 | } |
| 31 | launch := RoleLaunch{Parent: &cgroup.Prepared{}, ParentPath: "parent", Work: work, Target: target, Journal: journal, Executable: func(context.Context, *exec.Cmd) (*os.File, error) { |
| 32 | t.Fatal("unsupported launch inspected executable") |
| 33 | return nil, nil |
| 34 | }, Initialize: func(context.Context) error { |
| 35 | t.Fatal("unsupported launch initialized state") |
| 36 | return nil |
| 37 | }, RegisterProducer: func(context.Context, string) (RoleProducer, error) { |
| 38 | t.Fatal("unsupported launch registered a producer") |
| 39 | return RoleProducer{}, nil |
| 40 | }} |
| 41 | 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 { |
| 42 | t.Fatal("agent built tenant command without isolated root support") |
| 43 | return nil |
| 44 | }, func() error { t.Fatal("unsupported launch reached before-start effect"); return nil }) |
| 45 | if !errors.Is(err, hostbox.ErrUnsupported) || cmd != nil || child != nil || settled != nil || producer.Context != nil || producer.Finish != nil { |
| 46 | t.Fatalf("launch returned execution capability: cmd=%v child=%v err=%v", cmd, child, err) |
| 47 | } |
| 48 | }) |
| 49 | } |
| 50 | } |
| 51 | |
| 52 | func TestClaimSpawnDisabledKeepsLegacyBuild(t *testing.T) { |
| 53 | calls, builds := 0, 0 |
| 54 | dir := t.TempDir() |
| 55 | cmd, prepared, err := spawnClaimedBefore(t.Context(), false, nil, "tenant.test", cgroup.TierDefault, claimSpawnWork{true, &calls}, hostwrite.Identity{}, func() *exec.Cmd { |
| 56 | builds++ |
| 57 | cmd := exec.Command(filepath.Join(dir, "absent-executable")) |
| 58 | cmd.Dir, cmd.Env = dir, []string{} |
| 59 | return cmd |
| 60 | }, func() error { t.Fatal("disabled helper invoked claim callback"); return nil }) |
| 61 | if builds != 1 || calls != 0 || cmd == nil || prepared != nil || !errors.Is(err, os.ErrNotExist) { |
| 62 | t.Fatalf("legacy dispatch changed: builds=%d guards=%d cmd=%v prepared=%v err=%v", builds, calls, cmd, prepared, err) |
| 63 | } |
| 64 | } |
| 65 | |