//go:build linux package host import ( "context" "github.com/carlosframework/platform/internal/cgroup" "github.com/carlosframework/platform/internal/hostbox" "os" "path/filepath" "testing" ) type fix1HostLaunches struct { records []hostbox.LaunchInventory } func (f fix1HostLaunches) HostLaunches(context.Context, string) ([]hostbox.LaunchInventory, error) { return f.records, nil } func TestPlan11Fix1NoProcessHistoricalInstallations(t *testing.T) { for _, shape := range []string{"old-incarnation", "old-stamp", "current"} { t.Run(shape, func(t *testing.T) { fx := newWritersKernelFixture(t) fx.dropAgentHolds(t) old := fx.target switch shape { case "old-incarnation": old.Incarnation = "old" case "old-stamp": old.Stamp-- } records := fix1HostLaunches{records: []hostbox.LaunchInventory{{Target: old}}} target := fx.target target.Session = 0 _, err := verifyNoProcessAt(t.Context(), fx.roots(), records, target, fx.inv) if shape == "current" { if err == nil { t.Fatal("current installation journal accepted") } return } if err != nil { t.Fatalf("old installation blocks witness: %v", err) } }) } } func TestPlan11Fix1NoProcessWalksClaimChildren(t *testing.T) { for _, populated := range []string{"0", "1"} { t.Run(populated, func(t *testing.T) { fx := newWritersKernelFixture(t) fx.dropAgentHolds(t) old := fx.target old.Stamp-- name := cgroup.ExecName(claimContainmentName(old)) dir := filepath.Join(fx.agentCgroup, name) child := filepath.Join(dir, "role-child") if err := os.MkdirAll(child, 0700); err != nil { t.Fatal(err) } for path, value := range map[string]string{dir: "0", child: populated} { if err := os.WriteFile(filepath.Join(path, "cgroup.events"), []byte("populated "+value+"\n"), 0600); err != nil { t.Fatal(err) } } records := fix1HostLaunches{records: []hostbox.LaunchInventory{{Target: old}}} target := fx.target target.Session = 0 ev, err := verifyNoProcessAt(t.Context(), fx.roots(), records, target, fx.inv) if populated == "1" { if err == nil { t.Fatal("populated claim child missed") } return } if err != nil || len(ev.CgroupIdentities) != 3 { t.Fatalf("claim group not witnessed: %+v %v", ev, err) } }) } } func TestPlan11Fix1NoProcessReadsRootJournal(t *testing.T) { f := newLaunchFixture(t, servingOp()) fx := newWritersKernelFixture(t) fx.dropAgentHolds(t) target := fx.target target.Session = 0 target.Stamp++ if _, err := verifyNoProcessAt(t.Context(), fx.roots(), f.journal, target, fx.inv); err != nil { t.Fatalf("historical persisted launch blocks new installation: %v", err) } target.Stamp-- if _, err := verifyNoProcessAt(t.Context(), fx.roots(), f.journal, target, fx.inv); err == nil { t.Fatal("persisted current installation accepted") } }