//go:build linux package host import ( "context" "encoding/json" "errors" "net" "os" "path/filepath" "testing" "github.com/carlosframework/platform/internal/hostbox" "github.com/carlosframework/platform/internal/hostclaim" execlease "github.com/carlosframework/platform/internal/hostexec" "github.com/carlosframework/platform/internal/hostguard" "github.com/carlosframework/platform/internal/rootparticipation" ) func productionExecutionFixture(t *testing.T, kind string) (*recoveryFixture, ClaimRootHTTP, *launchParticipationDisk, *claimExecutionRuntime, *productionClaimLocal) { t.Helper() f, root, _ := plan14InstallRegistration(t, kind, "checking") if err := os.Mkdir(filepath.Join(root.Roots["data"], ".hostexec"), 0700); err != nil { t.Fatal(err) } root.Reconcile.Census.SharedPaths["data"] = append(root.Reconcile.Census.SharedPaths["data"], ".hostexec") disk := &launchParticipationDisk{&rollbackDisk{dir: t.TempDir()}} raw, _ := json.Marshal(rootparticipation.Census{Schema: 1, Box: "box-1", Enrollment: "en", Migration: 1, Revision: 1, LaunchRevision: 1, Complete: true}) if err := disk.Write("census.json", raw); err != nil { t.Fatal(err) } boot, err := execlease.BootID() if err != nil { t.Fatal(err) } start, err := claimLaunchProcessStart("/proc", os.Getpid()) if err != nil { t.Fatal(err) } root.Stop.Authenticate = func(context.Context, net.Conn) (StopPeer, error) { return StopPeer{UID: 1001, PID: os.Getpid(), ProcessStart: start, BootID: boot, ControllerID: "agent"}, nil } root.Stop.Coordinator.Kernel = stopKernelFunc(func(context.Context, hostguard.FenceTarget, StopInventory, DrainAcknowledgement) (StopKernelEvidence, error) { return StopKernelEvidence{}, ErrClaimStopInventory }) runtime, err := newClaimExecutionRuntime(root, disk) if err != nil { t.Fatal(err) } root.Execution, root.executionRuntime = runtime.broker, runtime t.Cleanup(func() { _ = runtime.Close(context.Background()) }) socket := serveClaimRoot(t, root) p := &productionClaimLocal{Resources: ClaimRuntimeResources{Mode: "agent", DataDir: root.Roots["data"]}, Store: f.claims, lifetime: t.Context(), assembly: &ClaimAssembly{Claims: f.claims, Guard: root.Stop.Coordinator.Guard, Wake: &ClaimWakeAssembly{}, Launches: root.Launches, Reconcile: &root.Reconcile}, executionCall: rootExecutionClient(socket), slots: make(map[string]*claimParticipation)} t.Cleanup(func() { _ = p.Close(context.Background()) }) return f, root, disk, runtime, p } func TestPlan14ExecutionProductionClientSelection(t *testing.T) { f, root, disk, runtime, local := productionExecutionFixture(t, hostclaim.KindLanding) actor, _ := root.InventoryActor(t.Context()) o := f.read() if err := local.prepareWith(t.Context(), actor, o); err != nil { t.Fatal(err) } if _, ok := local.slots[o.Host].Controller.(*execlease.RootClient); !ok { t.Fatal("agent selected a second lease owner") } raw, err := disk.Read(o.Host + ".json") if err != nil { t.Fatal(err) } var record execlease.ExecutionRecord if err := rootparticipation.Decode(raw, &record); err != nil { t.Fatal(err) } if record.Status != "active" || !record.Authority.ParticipationOnly || record.LeaseETag == "" { t.Fatalf("root did not acquire participation: %+v", record) } selection := hostbox.Selection{Box: actor.Box, Region: actor.Region, Captured: hostguard.Captured{Incarnation: o.Claim.Holder.Incarnation, Stamp: o.Claim.Holder.Stamp}} if err := local.Check(t.Context(), actor, o.Claim, selection); err != nil { t.Fatal(err) } if err := local.prepareWith(t.Context(), actor, o); err != nil { t.Fatal(err) } if _, err := execlease.ReadRoot(t.Context(), disk, execlease.SystemClock{}, record.Request, false); err == nil { t.Fatal("preinstallation participation granted root effects") } if err := runtime.Close(t.Context()); err != nil { t.Fatal(err) } if err := local.prepareWith(t.Context(), actor, o); err == nil { t.Fatal("closed broker admission remained active") } if err := rootparticipation.Decode(mustExecutionRead(t, disk, o.Host), &record); err != nil || record.Status != "lost" { t.Fatalf("shutdown discarded obligation: %+v %v", record, err) } } func mustExecutionRead(t *testing.T, disk *launchParticipationDisk, host string) []byte { t.Helper() raw, err := disk.Read(host + ".json") if err != nil { t.Fatal(err) } return raw } func TestPlan14ExecutionProductionServingBeforeInstallation(t *testing.T) { f, root, disk, _, local := productionExecutionFixture(t, hostclaim.KindServing) actor, _ := root.InventoryActor(t.Context()) o := f.read() selection := hostbox.Selection{Box: actor.Box, Region: actor.Region, Captured: hostguard.Captured{Incarnation: o.Claim.Holder.Incarnation, Stamp: o.Claim.Holder.Stamp}} if err := local.Check(t.Context(), actor, o.Claim, selection); err == nil { t.Fatal("serving had no root observation") } if err := local.prepareWith(t.Context(), actor, o); err != nil { t.Fatal(err) } if err := local.Check(t.Context(), actor, o.Claim, selection); err != nil { t.Fatal(err) } if _, err := disk.Read(o.Host + ".json"); !errors.Is(err, os.ErrNotExist) { t.Fatal("serving acquired an operation lease", err) } if err := disk.Write("update.json", []byte(`{}`)); err != nil { t.Fatal(err) } if err := local.prepareWith(t.Context(), actor, o); err == nil { t.Fatal("update marker did not revoke preparation") } if err := local.Check(t.Context(), actor, o.Claim, selection); err == nil { t.Fatal("failed preparation retained serving admission") } } func TestPlan14ExecutionProductionSettlement(t *testing.T) { f, root, disk, _, local := productionExecutionFixture(t, hostclaim.KindLanding) actor, _ := root.InventoryActor(t.Context()) if err := local.prepareWith(t.Context(), actor, f.read()); err != nil { t.Fatal(err) } c := local.slots[f.read().Host].Controller if err := c.Close(t.Context()); err == nil { t.Fatal("CC9 cleared participation while still a participant") } var r execlease.ExecutionRecord if err := rootparticipation.Decode(mustExecutionRead(t, disk, f.read().Host), &r); err != nil || r.Status != "lost" { t.Fatalf("CC9 failure lost denial: %+v %v", r, err) } _, err := hostclaim.Update(t.Context(), f.claims, f.read(), func(c *hostclaim.Claim) error { c.Op = hostclaim.Op{Kind: hostclaim.KindServing, Exec: actor.Box} return nil }) if err != nil { t.Fatal(err) } if err := local.prepareWith(t.Context(), actor, f.read()); err != nil { t.Fatal(err) } if err := rootparticipation.Decode(mustExecutionRead(t, disk, f.read().Host), &r); err != nil || r.Status != "settled" { t.Fatalf("CC9 failed to settle: %+v %v", r, err) } }