gvisor: test each probe cleanup entry point on its own
Astra P2: the probe gate test shared one call log between proveGVisorProbeGone and finishGVisorProbe and dropped cleanup's error. Each entry point now has its own test. Cleanup asserts the stop, the error and whether files are kept, for these cases: unloaded, listed+active with no drop-in, failed stop, and populated cgroup. A mutation that drops the cleanup gate fails the test.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
1 file changed,
+105
−35
internal/instancectl/gvisor_never_loaded_test.go+105 −35
CI passed — run details
diff --git a/internal/instancectl/gvisor_never_loaded_test.go b/internal/instancectl/gvisor_never_loaded_test.go| index 863cc9989..fcbf36a16 100644 |
| --- a/internal/instancectl/gvisor_never_loaded_test.go |
| +++ b/internal/instancectl/gvisor_never_loaded_test.go |
| @@ -122,61 +122,131 @@ func TestGVisorSiblingNeverLoadedBestEffortAndDemo(t *testing.T) { |
| // systemctl only makes systemd log "Refusing" (three lines a probe on |
| // v0.34.37 dev demo box-1). Unloaded, with no cgroup and no drop-in file, it |
| // is proven stopped without its name; anything else keeps the full proof. |
| -func TestGVisorProbeNeverLoadedIsNotNamed(t *testing.T) { |
| +// Each entry point is tested on its own server and call log. |
| +type probeGateFixture struct { |
| + s *Server |
| + calls []string |
| + removed []string |
| + active bool |
| + stopErr error |
| +} |
| + |
| +func newProbeGateFixture(t *testing.T, listing string, listErr error) *probeGateFixture { |
| + f := &probeGateFixture{} |
| + f.s = &Server{CgroupRoot: t.TempDir(), DropInDir: t.TempDir(), SandboxEffective: execsandbox.EffectiveGVisor} |
| + f.s.removeSandbox = func(id string) error { f.removed = append(f.removed, id); return nil } |
| + f.s.Systemctl = func(_ context.Context, args ...string) (string, error) { |
| + f.calls = append(f.calls, strings.Join(args, " ")) |
| + switch args[0] { |
| + case "list-units": |
| + return listing, listErr |
| + case "stop": |
| + if f.stopErr != nil { |
| + return "", f.stopErr |
| + } |
| + f.active = false |
| + } |
| + out := systemd257Stopped(args[len(args)-1]) |
| + if f.active { |
| + out = strings.NewReplacer("ActiveState=inactive", "ActiveState=active", "SubState=dead", "SubState=running", "MainPID=0", "MainPID=42").Replace(out) |
| + } |
| + return out, nil |
| + } |
| + return f |
| +} |
| + |
| +func (f *probeGateFixture) named() bool { |
| + for _, c := range f.calls { |
| + if !strings.HasPrefix(c, "list-units") && strings.Contains(c, gvisorProbeUnit) { |
| + return true |
| + } |
| + } |
| + return false |
| +} |
| + |
| +func (f *probeGateFixture) stopped() bool { |
| + for _, c := range f.calls { |
| + if c == "stop "+gvisorProbeUnit { |
| + return true |
| + } |
| + } |
| + return false |
| +} |
| + |
| +func TestGVisorProbeGoneSkipsAnUnloadedProbe(t *testing.T) { |
| for _, tc := range []struct { |
| - name string |
| - listing string |
| - listErr error |
| - cgroup bool |
| - dropIn bool |
| - dropInDir bool |
| - wantNamed bool |
| + name, listing string |
| + listErr error |
| + dropIn bool |
| + dropInDir bool |
| + wantNamed bool |
| }{ |
| - {name: "unloaded", listing: ""}, |
| + {name: "unloaded"}, |
| {name: "empty drop-in directory", dropInDir: true}, |
| {name: "listed", listing: gvisorProbeUnit + " loaded inactive dead x\n", wantNamed: true}, |
| - {name: "cgroup present", cgroup: true, wantNamed: true}, |
| {name: "drop-in present", dropIn: true, wantNamed: true}, |
| {name: "listing error", listErr: errors.New("bus"), wantNamed: true}, |
| } { |
| t.Run(tc.name, func(t *testing.T) { |
| - var calls []string |
| - s := &Server{CgroupRoot: t.TempDir(), DropInDir: t.TempDir(), SandboxEffective: execsandbox.EffectiveGVisor} |
| - s.removeSandbox = func(string) error { return nil } |
| - s.Systemctl = func(_ context.Context, args ...string) (string, error) { |
| - calls = append(calls, strings.Join(args, " ")) |
| - if args[0] == "list-units" { |
| - return tc.listing, tc.listErr |
| - } |
| - return systemd257Stopped(args[len(args)-1]), nil |
| - } |
| - if tc.cgroup { |
| - if err := os.MkdirAll(filepath.Join(s.CgroupRoot, parentSlices, gvisorProbeUnit), 0o755); err != nil { |
| - t.Fatal(err) |
| - } |
| - } |
| + f := newProbeGateFixture(t, tc.listing, tc.listErr) |
| if tc.dropIn || tc.dropInDir { |
| - if err := os.MkdirAll(filepath.Join(s.DropInDir, gvisorProbeUnit+".d"), 0o755); err != nil { |
| + if err := os.MkdirAll(filepath.Join(f.s.DropInDir, gvisorProbeUnit+".d"), 0o755); err != nil { |
| t.Fatal(err) |
| } |
| } |
| if tc.dropIn { |
| - if err := os.WriteFile(filepath.Join(s.DropInDir, gvisorProbeUnit+".d", gvisorDropInName), []byte("[Service]\n"), 0o644); err != nil { |
| + if err := os.WriteFile(filepath.Join(f.s.DropInDir, gvisorProbeUnit+".d", gvisorDropInName), []byte("[Service]\n"), 0o644); err != nil { |
| t.Fatal(err) |
| } |
| } |
| - if err := s.proveGVisorProbeGone(context.Background()); err != nil && !tc.cgroup { |
| + if err := f.s.proveGVisorProbeGone(context.Background()); err != nil { |
| t.Fatal(err) |
| } |
| - _ = s.finishGVisorProbe(context.Background()) |
| - named := false |
| - for _, c := range calls { |
| - if !strings.HasPrefix(c, "list-units") && strings.Contains(c, gvisorProbeUnit) { |
| - named = true |
| + if f.named() != tc.wantNamed { |
| + t.Fatalf("named = %v, want %v\n%s", f.named(), tc.wantNamed, strings.Join(f.calls, "\n")) |
| + } |
| + }) |
| + } |
| +} |
| + |
| +func TestGVisorProbeFinishKeepsFilesUntilStopIsProven(t *testing.T) { |
| + listed := gvisorProbeUnit + " loaded active running x\n" |
| + for _, tc := range []struct { |
| + name string |
| + listing string |
| + active bool |
| + stopErr error |
| + populated bool |
| + wantErr bool |
| + wantNamed bool |
| + wantStop bool |
| + }{ |
| + {name: "unloaded: files removed without the name"}, |
| + {name: "listed and active, no drop-in: stopped, then removed", listing: listed, active: true, wantNamed: true, wantStop: true}, |
| + {name: "stop fails: files kept", listing: listed, active: true, stopErr: errors.New("bus"), wantErr: true, wantNamed: true, wantStop: true}, |
| + {name: "populated cgroup: files kept", populated: true, wantErr: true, wantNamed: true}, |
| + } { |
| + t.Run(tc.name, func(t *testing.T) { |
| + f := newProbeGateFixture(t, tc.listing, nil) |
| + f.active, f.stopErr = tc.active, tc.stopErr |
| + if tc.populated { |
| + dir := filepath.Join(f.s.CgroupRoot, parentSlices, gvisorProbeUnit) |
| + if err := os.MkdirAll(dir, 0o755); err != nil { |
| + t.Fatal(err) |
| + } |
| + if err := os.WriteFile(filepath.Join(dir, "cgroup.events"), []byte("populated 1\nfrozen 0\n"), 0o644); err != nil { |
| + t.Fatal(err) |
| } |
| } |
| - if named != tc.wantNamed { |
| - t.Fatalf("probe unit named = %v, want %v\n%s", named, tc.wantNamed, strings.Join(calls, "\n")) |
| + err := f.s.finishGVisorProbe(context.Background()) |
| + if (err != nil) != tc.wantErr { |
| + t.Fatalf("err = %v\n%s", err, strings.Join(f.calls, "\n")) |
| + } |
| + if f.named() != tc.wantNamed || f.stopped() != tc.wantStop { |
| + t.Fatalf("named=%v stopped=%v\n%s", f.named(), f.stopped(), strings.Join(f.calls, "\n")) |
| + } |
| + if removed := len(f.removed) > 0; removed == tc.wantErr { |
| + t.Fatalf("files removed = %v with err %v", removed, err) |
| } |
| }) |
| } |