| 1 | package checks |
| 2 | |
| 3 | import ( |
| 4 | "os" |
| 5 | "os/exec" |
| 6 | "path/filepath" |
| 7 | "slices" |
| 8 | "strings" |
| 9 | "testing" |
| 10 | |
| 11 | "golang.org/x/mod/module" |
| 12 | modzip "golang.org/x/mod/zip" |
| 13 | ) |
| 14 | |
| 15 | const ( |
| 16 | toolchainRefused = "go: golang.org/toolchain@v0.0.1-go1.26.5.linux-amd64: verifying module: checksum database disabled by GOSUMDB=off" |
| 17 | moduleRefused = "go: amadan.net/oficina/home/handoff@v0.0.0-20261120140933-17cdcaff3345: module lookup disabled by GOPROXY=off" |
| 18 | ) |
| 19 | |
| 20 | // TestNeedsNetworkNeedsTheDependencyStage: only Equipo's own dependency |
| 21 | // stage can say a gate needed the network: it failed, and everything Go |
| 22 | // printed there is its refusal to download a module or a Go release |
| 23 | // offline. The gate's output is the candidate's: read whole, as it is |
| 24 | // produced, it can only keep a failure a failure, and evidence that is |
| 25 | // not complete keeps it a failure too (Astra, reviews of f03d79bb and |
| 26 | // 4e0d2497). |
| 27 | func TestNeedsNetworkNeedsTheDependencyStage(t *testing.T) { |
| 28 | run := func(code int, out string) Run { return Run{Code: code, Output: out, Evidence: Scan(out)} } |
| 29 | offline := run(1, moduleRefused+"\n"+toolchainRefused+"\n") |
| 30 | deps := "go mod download in . failed (the hub's git rate limit?); retrying in 5s\napp.go:32:2: module lookup disabled by GOPROXY=off\nx.go:3:8: mkdir /opt/go-mod/cache/download/example.invalid: read-only file system\nFAIL\tamadan.net/oficina/home [setup failed]\nFAIL\nmake: *** [Makefile:82: deps] Error 1\n" |
| 31 | gateDeps := run(2, deps) |
| 32 | if !NeedsNetwork(offline, gateDeps) { |
| 33 | t.Fatal("an offline dependency stage and a gate failing on dependencies is not needs-network") |
| 34 | } |
| 35 | noise := strings.Repeat("noise\n", 5000) |
| 36 | for name, tc := range map[string]struct{ stage, gate Run }{ |
| 37 | "checksum mismatch": {run(1, "verifying example.com/m@v1.0.0: checksum mismatch\n\tdownloaded: h1:aaa=\n\tgo.sum: h1:bbb=\n\nSECURITY ERROR\nThis download does NOT match an earlier download recorded in go.sum.\n"), gateDeps}, |
| 38 | "corrupt archive": {run(1, "go: golang.org/toolchain@v0.0.1-go1.26.5.linux-amd64: zip: not a valid zip file\n"), gateDeps}, |
| 39 | "quoted diagnostic": {run(0, ""), run(1, " door_test.go:12: got \""+toolchainRefused+"\"\n"+toolchainRefused+"\n"+moduleRefused+"\nFAIL\n")}, |
| 40 | "stage passed": {run(0, toolchainRefused), gateDeps}, |
| 41 | "mixed test": {offline, run(1, deps+"--- FAIL: TestDoorPages (0.01s)\n")}, |
| 42 | "mixed compile": {offline, run(1, deps+"./calma.go:12:2: undefined: doorView\n")}, |
| 43 | "mixed panic": {offline, run(1, deps+"panic: runtime error: index out of range\n")}, |
| 44 | "mixed other write": {offline, run(1, deps+"x.go:3:8: open /workspace/gen.go: read-only file system\n")}, |
| 45 | "json test failure": {offline, run(1, deps+`{"Time":"2026-11-23T10:00:00Z","Action":"fail","Package":"example.com/x","Test":"TestDoor","Elapsed":0}`+"\n")}, |
| 46 | "json build failure": {offline, run(1, deps+`{"ImportPath":"example.com/x","Action":"build-fail"}`+"\n")}, |
| 47 | "failure beyond tail": {offline, run(1, "--- FAIL: TestDoor (0.00s)\n"+noise+deps)}, |
| 48 | "stage other line": {run(1, moduleRefused+"\nbash: cd: handoff: No such file or directory\n"), gateDeps}, |
| 49 | "stage refusal beyond tail": {run(1, "bash: cd: handoff: No such file or directory\n"+strings.Repeat(moduleRefused+"\n", 500)), gateDeps}, |
| 50 | "stage empty": {run(1, ""), gateDeps}, |
| 51 | "stage only progress": {run(1, "go: no module dependencies to download\n"), gateDeps}, |
| 52 | "gate passed": {offline, run(0, "")}, |
| 53 | "newer go.mod": {run(1, "go: go.mod requires go >= 1.27.0 (running go 1.26.10; GOTOOLCHAIN=local)\n"), gateDeps}, |
| 54 | "no gate evidence": {offline, Run{Code: 2, Output: deps}}, |
| 55 | "no stage evidence": {Run{Code: 1, Output: moduleRefused}, gateDeps}, |
| 56 | } { |
| 57 | if NeedsNetwork(tc.stage, tc.gate) { |
| 58 | t.Errorf("%s: counted as needs-network", name) |
| 59 | } |
| 60 | } |
| 61 | // Incomplete evidence is a failure: a stream never finished, one cut |
| 62 | // short, or a line too long to read whole. |
| 63 | incomplete := map[string]func() *Evidence{ |
| 64 | "stream never closed": func() *Evidence { |
| 65 | e, w := NewEvidence(2) |
| 66 | w[0].Write([]byte(deps)) |
| 67 | w[0].Close() |
| 68 | return e |
| 69 | }, |
| 70 | "stream cut short": func() *Evidence { |
| 71 | e, w := NewEvidence(1) |
| 72 | w[0].Write([]byte(deps)) |
| 73 | w[0].Abort() |
| 74 | return e |
| 75 | }, |
| 76 | "line too long": func() *Evidence { |
| 77 | e, w := NewEvidence(1) |
| 78 | w[0].Write([]byte(deps + strings.Repeat("x", 1<<20) + "\n")) |
| 79 | w[0].Close() |
| 80 | return e |
| 81 | }, |
| 82 | } |
| 83 | for name, ev := range incomplete { |
| 84 | if NeedsNetwork(offline, Run{Code: 2, Output: deps, Evidence: ev()}) { |
| 85 | t.Errorf("%s: counted as needs-network", name) |
| 86 | } |
| 87 | } |
| 88 | stageEv, stageW := NewEvidence(2) |
| 89 | stageW[0].Write([]byte(moduleRefused + "\n")) |
| 90 | stageW[0].Close() |
| 91 | if NeedsNetwork(Run{Code: 1, Evidence: stageEv}, gateDeps) { |
| 92 | t.Error("an unfinished dependency stage counted as needs-network") |
| 93 | } |
| 94 | // Written in pieces, from two streams at once, the same lines read the same. |
| 95 | e, w := NewEvidence(2) |
| 96 | for _, piece := range strings.SplitAfter(deps, "o") { |
| 97 | w[0].Write([]byte(piece)) |
| 98 | w[1].Write([]byte(piece)) |
| 99 | } |
| 100 | w[0].Close() |
| 101 | w[1].Close() |
| 102 | if !NeedsNetwork(offline, Run{Code: 2, Evidence: e}) { |
| 103 | t.Fatal("streamed evidence read differently") |
| 104 | } |
| 105 | } |
| 106 | |
| 107 | // TestGoRequirement: the runner's Go must meet what the base's own |
| 108 | // go.mod files and gate files ask. go and toolchain directives are |
| 109 | // minimums; a release a Makefile pins is honoured by the same minor |
| 110 | // release at that patch or newer (make runs with GOTOOLCHAIN=local); a |
| 111 | // release any other gate file pins is honoured only exactly. |
| 112 | func TestGoRequirement(t *testing.T) { |
| 113 | home := map[string]string{"go.mod": "module amadan.net/oficina/home\n\ngo 1.26.0\n\ntoolchain go1.26.5\n", "handoff/go.mod": "module x\n\ngo 1.25.0\n", "Makefile": "export GOTOOLCHAIN := go1.26.5\nbuild:\n\tgo build ./...\n"} |
| 114 | for _, tc := range []struct { |
| 115 | name string |
| 116 | files map[string]string |
| 117 | met bool |
| 118 | }{ |
| 119 | {"home", home, true}, |
| 120 | {"newer go directive", map[string]string{"go.mod": "module x\n\ngo 1.27.0\n"}, false}, |
| 121 | {"newer toolchain directive", map[string]string{"go.mod": "module x\n\ngo 1.26.0\ntoolchain go1.27.1\n"}, false}, |
| 122 | {"newer submodule", map[string]string{"go.mod": "module x\n\ngo 1.26.0\n", "tools/go.mod": "module y\n\ngo 1.26.11\n"}, false}, |
| 123 | {"vendored go.mod ignored", map[string]string{"go.mod": "module x\n\ngo 1.26.0\n", "vendor/example.com/y/go.mod": "module y\n\ngo 1.30\n", "internal/testdata/go.mod": "module z\n\ngo 1.30\n"}, true}, |
| 124 | {"make pins an older minor", map[string]string{"Makefile": "export GOTOOLCHAIN := go1.25.4\n"}, false}, |
| 125 | {"make pins a newer patch", map[string]string{"build/go.mk": "GOTOOLCHAIN ?= go1.26.11\n"}, false}, |
| 126 | {"make pins a minimum", map[string]string{"GNUmakefile": "export GOTOOLCHAIN=go1.26.3+auto\n"}, true}, |
| 127 | {"make pins a newer minimum", map[string]string{"Makefile": "export GOTOOLCHAIN = go1.27.0+auto\n"}, false}, |
| 128 | {"make asks for local", map[string]string{"Makefile": "export GOTOOLCHAIN := local\n"}, true}, |
| 129 | {"script pins another patch", map[string]string{".amadan/ci.d/10-vet": "#!/bin/sh\nexport GOTOOLCHAIN=go1.26.5\nexec go vet ./...\n"}, false}, |
| 130 | {"script pins this release", map[string]string{".amadan/ci": "GOTOOLCHAIN='go1.26.10' go test ./...\n"}, true}, |
| 131 | {"no Go", map[string]string{"package.json": "{}"}, true}, |
| 132 | } { |
| 133 | files := map[string][]byte{} |
| 134 | for name, body := range tc.files { |
| 135 | if !GoRequirementFile(name) && name != "package.json" && name != "vendor/example.com/y/go.mod" && name != "internal/testdata/go.mod" { |
| 136 | t.Fatalf("%s: %s not read", tc.name, name) |
| 137 | } |
| 138 | if GoRequirementFile(name) { |
| 139 | files[name] = []byte(body) |
| 140 | } |
| 141 | } |
| 142 | if got := ReadGoRequirement(files).Unmet("go1.26.10"); got == tc.met { |
| 143 | t.Errorf("%s: unmet=%v", tc.name, got) |
| 144 | } |
| 145 | } |
| 146 | } |
| 147 | |
| 148 | func TestRunnerGoVersion(t *testing.T) { |
| 149 | root := t.TempDir() |
| 150 | if _, err := RunnerGoVersion(root); err == nil { |
| 151 | t.Fatal("missing VERSION accepted") |
| 152 | } |
| 153 | os.WriteFile(filepath.Join(root, "VERSION"), []byte("go1.26.10\ntime 2026-11-05T00:00:00Z\n"), 0o600) |
| 154 | if v, err := RunnerGoVersion(root); err != nil || v != "go1.26.10" { |
| 155 | t.Fatalf("%q %v", v, err) |
| 156 | } |
| 157 | os.WriteFile(filepath.Join(root, "VERSION"), []byte("devel +abc\n"), 0o600) |
| 158 | if _, err := RunnerGoVersion(root); err == nil { |
| 159 | t.Fatal("unversioned toolchain accepted") |
| 160 | } |
| 161 | } |
| 162 | |
| 163 | // TestDependencyStage: the stage downloads each module's dependencies |
| 164 | // from the read-only cache, in Go's own words, and leaves the tree's |
| 165 | // go.mod and go.sum exactly as the candidate wrote them. |
| 166 | func TestDependencyStage(t *testing.T) { |
| 167 | goBin, err := exec.LookPath("go") |
| 168 | if err != nil { |
| 169 | t.Skip("go is not installed") |
| 170 | } |
| 171 | // Create child state first so cleanup removes it after the source tree. |
| 172 | state := t.TempDir() |
| 173 | for _, dir := range []string{"home", "tmp", "mod", "cache"} { |
| 174 | if err := os.MkdirAll(filepath.Join(state, dir), 0o700); err != nil { |
| 175 | t.Fatal(err) |
| 176 | } |
| 177 | } |
| 178 | // Go ignores GOTELEMETRY as a setting: its mode file controls the |
| 179 | // detached sidecar. Reuse the sandbox's off configuration and Go's |
| 180 | // test-directory override so this is also hermetic on macOS. |
| 181 | xdg, err := telemetryOffConfig(state) |
| 182 | if err != nil { |
| 183 | t.Fatal(err) |
| 184 | } |
| 185 | tree := t.TempDir() |
| 186 | write := func(name, body string) { |
| 187 | p := filepath.Join(tree, name) |
| 188 | os.MkdirAll(filepath.Dir(p), 0o700) |
| 189 | if err := os.WriteFile(p, []byte(body), 0o600); err != nil { |
| 190 | t.Fatal(err) |
| 191 | } |
| 192 | } |
| 193 | write("go.mod", "module example.com/app\n\ngo 1.21\n") |
| 194 | write("app.go", "package app\n") |
| 195 | write("dep/go.mod", "module example.com/dep\n\ngo 1.21\n\nrequire example.invalid/missing v1.0.0\n") |
| 196 | write("dep/go.sum", "") |
| 197 | write("vendored/go.mod", "module example.com/vendored\n\ngo 1.21\n\nrequire example.invalid/v v1.0.0\n") |
| 198 | write("vendored/vendor/modules.txt", "# example.invalid/v v1.0.0\n") |
| 199 | for _, skipped := range []string{"vendor/x/go.mod", "testdata/go.mod", ".hidden/go.mod", "_old/go.mod"} { |
| 200 | write(skipped, "module skipped\n") |
| 201 | } |
| 202 | dirs := GoModuleDirs(tree) |
| 203 | if !slices.Equal(dirs, []string{".", "dep"}) { |
| 204 | t.Fatalf("module dirs %q", dirs) |
| 205 | } |
| 206 | proxy := "off" |
| 207 | run := func(dirs []string) Run { |
| 208 | argv := DependencyStage(dirs) |
| 209 | cmd := exec.Command(argv[0], argv[1:]...) |
| 210 | cmd.Dir = tree |
| 211 | cmd.Env = []string{"PATH=" + filepath.Dir(goBin) + ":/usr/bin:/bin", "HOME=" + filepath.Join(state, "home"), "TMPDIR=" + filepath.Join(state, "tmp"), "GOMODCACHE=" + filepath.Join(state, "mod"), "GOCACHE=" + filepath.Join(state, "cache"), "GOPROXY=" + proxy, "GOSUMDB=off", "GOTOOLCHAIN=local", "GOFLAGS=-modcacherw", "GOWORK=off", "GOENV=off", "GOTELEMETRY=off", "TEST_TELEMETRY_DIR=" + filepath.Join(xdg, "go", "telemetry")} |
| 212 | // An environment variable alone does not set Go's telemetry mode. |
| 213 | // Check the actual child configuration before it can spawn a sidecar. |
| 214 | mode := exec.Command(goBin, "env", "GOTELEMETRY") |
| 215 | mode.Env = cmd.Env |
| 216 | if out, err := mode.CombinedOutput(); err != nil || strings.TrimSpace(string(out)) != "off" { |
| 217 | t.Fatalf("dependency child telemetry: %q, %v", out, err) |
| 218 | } |
| 219 | out, err := cmd.CombinedOutput() |
| 220 | if err != nil { |
| 221 | t.Logf("dependency stage: %v", err) |
| 222 | } |
| 223 | return Run{Code: cmd.ProcessState.ExitCode(), Output: string(out), Evidence: Scan(string(out))} |
| 224 | } |
| 225 | if res := run([]string{"."}); res.Code != 0 { |
| 226 | t.Fatalf("a module with nothing to download failed: %+v", res) |
| 227 | } |
| 228 | res := run(dirs) |
| 229 | if res.Code == 0 || !NeedsNetwork(res, Run{Code: 1, Evidence: Scan("make: *** [ci] Error 1\n")}) { |
| 230 | t.Fatalf("missing module not refused offline in Go's words: %+v", res) |
| 231 | } |
| 232 | // A download that succeeds (from a local proxy here) records its sums |
| 233 | // in the stage's copy, never in the candidate's go.sum. |
| 234 | dir := t.TempDir() |
| 235 | lib := filepath.Join(dir, "lib") |
| 236 | os.MkdirAll(lib, 0o700) |
| 237 | os.WriteFile(filepath.Join(lib, "go.mod"), []byte("module example.com/lib\n\ngo 1.21\n"), 0o600) |
| 238 | os.WriteFile(filepath.Join(lib, "lib.go"), []byte("package lib\n"), 0o600) |
| 239 | at := filepath.Join(dir, "proxy/example.com/lib/@v") |
| 240 | os.MkdirAll(at, 0o700) |
| 241 | os.WriteFile(filepath.Join(at, "list"), []byte("v1.0.0\n"), 0o600) |
| 242 | os.WriteFile(filepath.Join(at, "v1.0.0.info"), []byte(`{"Version":"v1.0.0"}`), 0o600) |
| 243 | os.WriteFile(filepath.Join(at, "v1.0.0.mod"), []byte("module example.com/lib\n\ngo 1.21\n"), 0o600) |
| 244 | z, err := os.Create(filepath.Join(at, "v1.0.0.zip")) |
| 245 | if err != nil { |
| 246 | t.Fatal(err) |
| 247 | } |
| 248 | if err := modzip.CreateFromDir(z, module.Version{Path: "example.com/lib", Version: "v1.0.0"}, lib); err != nil { |
| 249 | t.Fatal(err) |
| 250 | } |
| 251 | z.Close() |
| 252 | write("uses/go.mod", "module example.com/uses\n\ngo 1.21\n\nrequire example.com/lib v1.0.0\n") |
| 253 | write("uses/go.sum", "") |
| 254 | write("uses/uses.go", "package uses\n\nimport _ \"example.com/lib\"\n") |
| 255 | proxy = "file://" + filepath.Join(dir, "proxy") |
| 256 | if res := run([]string{"uses"}); res.Code != 0 { |
| 257 | t.Fatalf("download from the proxy failed: %+v", res) |
| 258 | } |
| 259 | for _, name := range []string{"dep/go.sum", "uses/go.sum"} { |
| 260 | if raw, _ := os.ReadFile(filepath.Join(tree, name)); len(raw) != 0 { |
| 261 | t.Fatalf("the stage wrote the candidate's %s: %q", name, raw) |
| 262 | } |
| 263 | } |
| 264 | } |
| 265 | |