| 1 | //go:build linux |
| 2 | |
| 3 | package host |
| 4 | |
| 5 | import ( |
| 6 | "context" |
| 7 | "github.com/carlosframework/platform/internal/cgroup" |
| 8 | "github.com/carlosframework/platform/internal/hostbox" |
| 9 | "os" |
| 10 | "path/filepath" |
| 11 | "testing" |
| 12 | ) |
| 13 | |
| 14 | type fix1HostLaunches struct { |
| 15 | records []hostbox.LaunchInventory |
| 16 | } |
| 17 | |
| 18 | func (f fix1HostLaunches) HostLaunches(context.Context, string) ([]hostbox.LaunchInventory, error) { |
| 19 | return f.records, nil |
| 20 | } |
| 21 | |
| 22 | func TestPlan11Fix1NoProcessHistoricalInstallations(t *testing.T) { |
| 23 | for _, shape := range []string{"old-incarnation", "old-stamp", "current"} { |
| 24 | t.Run(shape, func(t *testing.T) { |
| 25 | fx := newWritersKernelFixture(t) |
| 26 | fx.dropAgentHolds(t) |
| 27 | old := fx.target |
| 28 | switch shape { |
| 29 | case "old-incarnation": |
| 30 | old.Incarnation = "old" |
| 31 | case "old-stamp": |
| 32 | old.Stamp-- |
| 33 | } |
| 34 | records := fix1HostLaunches{records: []hostbox.LaunchInventory{{Target: old}}} |
| 35 | target := fx.target |
| 36 | target.Session = 0 |
| 37 | _, err := verifyNoProcessAt(t.Context(), fx.roots(), records, target, fx.inv) |
| 38 | if shape == "current" { |
| 39 | if err == nil { |
| 40 | t.Fatal("current installation journal accepted") |
| 41 | } |
| 42 | return |
| 43 | } |
| 44 | if err != nil { |
| 45 | t.Fatalf("old installation blocks witness: %v", err) |
| 46 | } |
| 47 | }) |
| 48 | } |
| 49 | } |
| 50 | |
| 51 | func TestPlan11Fix1NoProcessWalksClaimChildren(t *testing.T) { |
| 52 | for _, populated := range []string{"0", "1"} { |
| 53 | t.Run(populated, func(t *testing.T) { |
| 54 | fx := newWritersKernelFixture(t) |
| 55 | fx.dropAgentHolds(t) |
| 56 | old := fx.target |
| 57 | old.Stamp-- |
| 58 | name := cgroup.ExecName(claimContainmentName(old)) |
| 59 | dir := filepath.Join(fx.agentCgroup, name) |
| 60 | child := filepath.Join(dir, "role-child") |
| 61 | if err := os.MkdirAll(child, 0700); err != nil { |
| 62 | t.Fatal(err) |
| 63 | } |
| 64 | for path, value := range map[string]string{dir: "0", child: populated} { |
| 65 | if err := os.WriteFile(filepath.Join(path, "cgroup.events"), []byte("populated "+value+"\n"), 0600); err != nil { |
| 66 | t.Fatal(err) |
| 67 | } |
| 68 | } |
| 69 | records := fix1HostLaunches{records: []hostbox.LaunchInventory{{Target: old}}} |
| 70 | target := fx.target |
| 71 | target.Session = 0 |
| 72 | ev, err := verifyNoProcessAt(t.Context(), fx.roots(), records, target, fx.inv) |
| 73 | if populated == "1" { |
| 74 | if err == nil { |
| 75 | t.Fatal("populated claim child missed") |
| 76 | } |
| 77 | return |
| 78 | } |
| 79 | if err != nil || len(ev.CgroupIdentities) != 3 { |
| 80 | t.Fatalf("claim group not witnessed: %+v %v", ev, err) |
| 81 | } |
| 82 | }) |
| 83 | } |
| 84 | } |
| 85 | |
| 86 | func TestPlan11Fix1NoProcessReadsRootJournal(t *testing.T) { |
| 87 | f := newLaunchFixture(t, servingOp()) |
| 88 | fx := newWritersKernelFixture(t) |
| 89 | fx.dropAgentHolds(t) |
| 90 | target := fx.target |
| 91 | target.Session = 0 |
| 92 | target.Stamp++ |
| 93 | if _, err := verifyNoProcessAt(t.Context(), fx.roots(), f.journal, target, fx.inv); err != nil { |
| 94 | t.Fatalf("historical persisted launch blocks new installation: %v", err) |
| 95 | } |
| 96 | target.Stamp-- |
| 97 | if _, err := verifyNoProcessAt(t.Context(), fx.roots(), f.journal, target, fx.inv); err == nil { |
| 98 | t.Fatal("persisted current installation accepted") |
| 99 | } |
| 100 | } |
| 101 | |