containment: name the source-reading tests TestSource
A test binary run outside the source tree (in a VM) skips '^TestSource'. Rename the namespace version test, move the ci-box.json comparison out of TestBubblewrapPins into its own TestSource test, and add TestSourceTestsAreNamed: it parses the package test files and fails when a function outside the prefix opens a relative path literal.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
3 files changed,
+106
−6
docs/superpowers/plans/HOSTED-SLICE1-PROGRESS.md+10 −0internal/runner/containment/bubblewrap_pin_test.go+9 −5internal/runner/containment/helper_version_test.go+87 −1
diff --git a/docs/superpowers/plans/HOSTED-SLICE1-PROGRESS.md b/docs/superpowers/plans/HOSTED-SLICE1-PROGRESS.md| index a284f75e..cd25bbe2 100644 |
| --- a/docs/superpowers/plans/HOSTED-SLICE1-PROGRESS.md |
| +++ b/docs/superpowers/plans/HOSTED-SLICE1-PROGRESS.md |
| @@ -8,3 +8,13 @@ Retry `poll` on `EINTR` on Linux and Darwin (`internal/runner/containment/poll_u |
| | --- | --- | --- | --- | |
| | `pollRetry` returns on the first `EINTR` (`if e != unix.EINTR \|\| true`) | `go test ./internal/runner/containment -run 'TestPollRetriesEINTR' -count=1` | `poll_unix_test.go:23: interrupted poll not retried: n=0 err=interrupted system call calls=1` | yes, `-run 'TestPollRetr'` passes | |
| | Extra: the expired deadline reports the `EINTR` instead of a timeout (`return 0, e`) | `go test ./internal/runner/containment -run 'TestPollRetryKeepsDeadline' -count=1` | `poll_unix_test.go:36: retries outlived the timeout: n=0 err=interrupted system call after 120.617911ms` | yes, `-run 'TestPollRetr'` passes | |
| + |
| +## Task 2 |
| + |
| +Tests in `internal/runner/containment` that read the source tree carry the `TestSource` prefix, so a compiled test binary run outside the tree skips them with `-test.skip '^TestSource'`. `TestSourceTestsAreNamed` parses every `*_test.go` in the package and fails if a function outside the prefix passes a relative string literal (directly or as the first part of a `filepath.Join`) to `os.ReadFile`, `os.Open`, `os.OpenFile`, `os.Stat`, `os.Lstat`, `os.ReadDir`, `os.DirFS` or `filepath.Glob`. |
| + |
| +| Mutation | Command | Observed failure | Restored | |
| +| --- | --- | --- | --- | |
| +| `TestBubblewrapPins` reads `../../../scripts/ci-box.json` again | `go test ./internal/runner/containment -run 'TestSourceTestsAreNamed' -count=1` | `helper_version_test.go:83: tests outside the TestSource prefix open relative paths: bubblewrap_pin_test.go:63 TestBubblewrapPins` | yes, `-run 'TestSourceTestsAreNamed'` passes | |
| + |
| +Compiled binary outside the tree: `go test -c` into `mktemp -d "$TMPDIR/hosted-XXXX"`, run from there with `-test.run TestBubblewrapPins -test.skip '^TestSource'`: `--- PASS: TestBubblewrapPins`. Directory removed. |
diff --git a/internal/runner/containment/bubblewrap_pin_test.go b/internal/runner/containment/bubblewrap_pin_test.go| index 862abde0..ae3770e2 100644 |
| --- a/internal/runner/containment/bubblewrap_pin_test.go |
| +++ b/internal/runner/containment/bubblewrap_pin_test.go |
| @@ -42,8 +42,7 @@ func TestNamespaceBubblewrapDigest(t *testing.T) { |
| } |
| // The release pin is the manifest's: the amd64 build a release runs on. |
| -// The arm64 build is another digest, for tests on an arm64 machine, and |
| -// the CI box installs exactly these two (scripts/ci-box.json). |
| +// The arm64 build is another digest, for tests on an arm64 machine. |
| func TestBubblewrapPins(t *testing.T) { |
| pinned := "" |
| for _, b := range release.BuildManifest().Binaries { |
| @@ -61,6 +60,14 @@ func TestBubblewrapPins(t *testing.T) { |
| if _, ok := BubblewrapPin("riscv64"); ok { |
| t.Fatal("a pin for a machine with no pinned build") |
| } |
| + if _, ok := BubblewrapPin(runtime.GOARCH); !ok { |
| + t.Skip("no pinned bubblewrap for this machine") |
| + } |
| +} |
| + |
| +// The CI box installs exactly the two pinned builds (scripts/ci-box.json). |
| +// It reads the source tree, so it is a TestSource test. |
| +func TestSourceBubblewrapPinsMatchCIBox(t *testing.T) { |
| raw, err := os.ReadFile("../../../scripts/ci-box.json") |
| if err != nil { |
| t.Fatal(err) |
| @@ -80,7 +87,4 @@ func TestBubblewrapPins(t *testing.T) { |
| t.Fatalf("the CI box installs another %s bubblewrap than the pin: %s", arch, tool.SHA256) |
| } |
| } |
| - if _, ok := BubblewrapPin(runtime.GOARCH); !ok { |
| - t.Skip("no pinned bubblewrap for this machine") |
| - } |
| } |
diff --git a/internal/runner/containment/helper_version_test.go b/internal/runner/containment/helper_version_test.go| index d37c23cc..40e67ee0 100644 |
| --- a/internal/runner/containment/helper_version_test.go |
| +++ b/internal/runner/containment/helper_version_test.go |
| @@ -8,8 +8,12 @@ import ( |
| "crypto/sha256" |
| "encoding/json" |
| "fmt" |
| + "go/ast" |
| + "go/parser" |
| + "go/token" |
| "os" |
| "path/filepath" |
| + "strconv" |
| "strings" |
| "testing" |
| ) |
| @@ -39,7 +43,7 @@ func TestNamespaceHelperProtocol(t *testing.T) { |
| } |
| } |
| -func TestNamespaceVersionTracksImplementation(t *testing.T) { |
| +func TestSourceNamespaceVersionTracksImplementation(t *testing.T) { |
| raw, err := os.ReadFile("linux.go") |
| if err != nil { |
| t.Fatal(err) |
| @@ -54,6 +58,88 @@ func TestNamespaceVersionTracksImplementation(t *testing.T) { |
| } |
| } |
| +// A test binary can run outside the source tree, in a VM with only the |
| +// binary, so it skips '^TestSource'. Every function in the package's test |
| +// files that opens a relative path literal must therefore be a TestSource |
| +// test, or that run would fail on a missing file. |
| +func TestSourceTestsAreNamed(t *testing.T) { |
| + planted := "package p\nimport (\"os\"; fp \"path/filepath\")\nfunc TestA() { os.ReadFile(\"/abs\"); os.ReadFile(dir) }\nfunc TestSourceB() { os.ReadFile(\"x.go\") }\nfunc TestC() { os.Stat(fp.Join(\"..\", \"x.json\")) }\nfunc helper() { os.Open(\"y\") }\n" |
| + if got := relativeOpens(t, "planted_test.go", []byte(planted)); strings.Join(got, ",") != "planted_test.go:5 TestC,planted_test.go:6 helper" { |
| + t.Fatalf("the check misreads a planted file: %q", got) |
| + } |
| + names, err := filepath.Glob("*_test.go") |
| + if err != nil || len(names) < 2 { |
| + t.Fatalf("no test files to check: %v %v", names, err) |
| + } |
| + var bad []string |
| + for _, name := range names { |
| + raw, err := os.ReadFile(name) |
| + if err != nil { |
| + t.Fatal(err) |
| + } |
| + bad = append(bad, relativeOpens(t, name, raw)...) |
| + } |
| + if len(bad) > 0 { |
| + t.Fatalf("tests outside the TestSource prefix open relative paths: %s", strings.Join(bad, ", ")) |
| + } |
| +} |
| + |
| +// relativeOpens lists the functions, other than TestSource tests, that pass |
| +// a relative string literal (directly or as the first part of a Join) to a |
| +// call that opens or inspects a path. |
| +func relativeOpens(t *testing.T, name string, src []byte) []string { |
| + t.Helper() |
| + fset := token.NewFileSet() |
| + file, err := parser.ParseFile(fset, name, src, 0) |
| + if err != nil { |
| + t.Fatal(err) |
| + } |
| + pkgs := map[string]string{} |
| + for _, imp := range file.Imports { |
| + p := strings.Trim(imp.Path.Value, `"`) |
| + local := p[strings.LastIndex(p, "/")+1:] |
| + if imp.Name != nil { |
| + local = imp.Name.Name |
| + } |
| + pkgs[local] = p |
| + } |
| + opens := map[string]bool{"os.ReadFile": true, "os.Open": true, "os.OpenFile": true, "os.Stat": true, "os.Lstat": true, "os.ReadDir": true, "os.DirFS": true, "path/filepath.Glob": true} |
| + callee := func(e ast.Expr) string { |
| + if sel, ok := e.(*ast.SelectorExpr); ok { |
| + if id, ok := sel.X.(*ast.Ident); ok { |
| + return pkgs[id.Name] + "." + sel.Sel.Name |
| + } |
| + } |
| + return "" |
| + } |
| + var relative func(ast.Expr) bool |
| + relative = func(e ast.Expr) bool { |
| + switch v := e.(type) { |
| + case *ast.BasicLit: |
| + s, err := strconv.Unquote(v.Value) |
| + return v.Kind == token.STRING && err == nil && !filepath.IsAbs(s) |
| + case *ast.CallExpr: |
| + c := callee(v.Fun) |
| + return (c == "path/filepath.Join" || c == "path.Join") && len(v.Args) > 0 && relative(v.Args[0]) |
| + } |
| + return false |
| + } |
| + var bad []string |
| + for _, decl := range file.Decls { |
| + fn, ok := decl.(*ast.FuncDecl) |
| + if !ok || fn.Body == nil || strings.HasPrefix(fn.Name.Name, "TestSource") { |
| + continue |
| + } |
| + ast.Inspect(fn.Body, func(n ast.Node) bool { |
| + if call, ok := n.(*ast.CallExpr); ok && opens[callee(call.Fun)] && len(call.Args) > 0 && relative(call.Args[0]) { |
| + bad = append(bad, fmt.Sprintf("%s:%d %s", name, fset.Position(call.Pos()).Line, fn.Name.Name)) |
| + } |
| + return true |
| + }) |
| + } |
| + return bad |
| +} |
| + |
| func TestBwrapRunsPinnedDescriptor(t *testing.T) { |
| m, l := fixture(t) |
| path := filepath.Join(t.TempDir(), "bwrap") |