| 1 | package thin |
| 2 | |
| 3 | import ( |
| 4 | "context" |
| 5 | "errors" |
| 6 | "fmt" |
| 7 | "go/version" |
| 8 | "io" |
| 9 | "os" |
| 10 | "os/exec" |
| 11 | "path/filepath" |
| 12 | "strings" |
| 13 | "testing" |
| 14 | |
| 15 | "amadan.net/oficina/equipo/internal/runner/checks" |
| 16 | "amadan.net/oficina/equipo/internal/runner/containment" |
| 17 | "amadan.net/oficina/equipo/internal/runner/provider" |
| 18 | "amadan.net/oficina/equipo/internal/wire" |
| 19 | ) |
| 20 | |
| 21 | // TestChecksGoToolchainAndDependencies (Astra, review of f03d79bb): the |
| 22 | // runner's Go is held to the base's requirement before any gate runs, |
| 23 | // and only Equipo's own dependency stage can make a failed gate "needs |
| 24 | // network"; the candidate's output can only keep a failure a failure. |
| 25 | func TestChecksGoToolchainAndDependencies(t *testing.T) { |
| 26 | out, err := exec.Command("go", "env", "GOROOT").Output() |
| 27 | if err != nil { |
| 28 | t.Skip("go is not installed") |
| 29 | } |
| 30 | goRoot := strings.TrimSpace(string(out)) |
| 31 | local, err := checks.RunnerGoVersion(goRoot) |
| 32 | if err != nil { |
| 33 | t.Skip("the test's Go has no release version") |
| 34 | } |
| 35 | other := "go1.20.1" |
| 36 | if version.Lang(local) == "go1.20" { |
| 37 | other = "go1.21.1" |
| 38 | } |
| 39 | const sum = "example.invalid/dep v1.0.0 h1:AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA=\nexample.invalid/dep v1.0.0/go.mod h1:AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA=\n" |
| 40 | needsDep := map[string]string{"go.mod": "module example.com/x\n\ngo 1.21\n\nrequire example.invalid/dep v1.0.0\n", "go.sum": sum, "x.go": "package x\n\nimport _ \"example.invalid/dep\"\n"} |
| 41 | plain := map[string]string{"go.mod": "module example.com/x\n\ngo 1.21\n", "x.go": "package x\n"} |
| 42 | with := func(files map[string]string, name, body string) map[string]string { |
| 43 | out := map[string]string{name: body} |
| 44 | for k, v := range files { |
| 45 | if k != name { |
| 46 | out[k] = v |
| 47 | } |
| 48 | } |
| 49 | return out |
| 50 | } |
| 51 | for _, tc := range []struct { |
| 52 | name string |
| 53 | base, candidate map[string]string |
| 54 | outcome, reason string |
| 55 | }{ |
| 56 | {"newer base requirement", with(plain, "go.mod", "module example.com/x\n\ngo 1.99.0\n"), nil, wire.ChecksCannotRun, "checks_unavailable"}, |
| 57 | {"version-specific base build", with(plain, "Makefile", "export GOTOOLCHAIN := "+other+"\nci:\n\ttrue\n"), nil, wire.ChecksCannotRun, "checks_unavailable"}, |
| 58 | {"candidate raises the requirement", plain, map[string]string{"go.mod": "module example.com/x\n\ngo 1.99.0\n"}, wire.ChecksFailed, ""}, |
| 59 | {"pinned patch of this minor", with(plain, "Makefile", "export GOTOOLCHAIN := "+version.Lang(local)+".0\nci:\n\tgo vet ./...\n"), nil, wire.ChecksPassed, ""}, |
| 60 | {"offline dependency", with(needsDep, "Makefile", "ci:\n\tgo vet ./...\n"), nil, wire.ChecksCannotRun, "needs_network"}, |
| 61 | {"mixed failure", with(needsDep, "Makefile", "ci:\n\t@echo '--- FAIL: TestDoor (0.00s)'; go vet ./...\n"), nil, wire.ChecksFailed, ""}, |
| 62 | {"json failure", with(needsDep, "Makefile", "ci:\n\t@echo '{\"Action\":\"fail\",\"Package\":\"example.com/x\",\"Test\":\"TestDoor\"}'; go vet ./...\n"), nil, wire.ChecksFailed, ""}, |
| 63 | {"failure beyond the tail", with(needsDep, "Makefile", "ci:\n\t@echo '--- FAIL: TestDoor (0.00s)'; seq 1000; go vet ./...\n"), nil, wire.ChecksFailed, ""}, |
| 64 | {"quoted diagnostic", with(plain, "Makefile", "ci:\n\t@echo 'go: golang.org/toolchain@v0.0.1-go1.26.5.linux-amd64: verifying module: checksum database disabled by GOSUMDB=off'; echo 'go: example.invalid/dep@v1.0.0: module lookup disabled by GOPROXY=off'; exit 1\n"), nil, wire.ChecksFailed, ""}, |
| 65 | } { |
| 66 | t.Run(tc.name, func(t *testing.T) { |
| 67 | e, f, r, _ := hubRig(t) |
| 68 | seed := filepath.Join(filepath.Dir(f.dir), "seed") |
| 69 | if _, ok := tc.base["Makefile"]; !ok { |
| 70 | tc.base = with(tc.base, "Makefile", "ci:\n\tgo vet ./...\n") |
| 71 | } |
| 72 | for name, body := range tc.base { |
| 73 | writeFile(t, seed, name, body) |
| 74 | } |
| 75 | gitOut(t, seed, "add", "-A") |
| 76 | gitOut(t, seed, "commit", "-qm", "base") |
| 77 | r.BaseSHA = gitOut(t, seed, "rev-parse", "HEAD") |
| 78 | gitOut(t, seed, "checkout", "-q", "-B", r.Branch) |
| 79 | writeFile(t, seed, "NEWS", "candidate\n") |
| 80 | for name, body := range tc.candidate { |
| 81 | writeFile(t, seed, name, body) |
| 82 | } |
| 83 | gitOut(t, seed, "add", "-A") |
| 84 | gitOut(t, seed, "commit", "-qm", "candidate") |
| 85 | r.CandidateSHA = gitOut(t, seed, "rev-parse", "HEAD") |
| 86 | gitOut(t, seed, "push", "-q", "--force", f.dir, "main", r.Branch) |
| 87 | r.Specialist = wire.SpecialistChecks |
| 88 | scratch := filepath.Join(filepath.Dir(f.dir), "sandbox") |
| 89 | _ = os.Mkdir(scratch, 0o700) |
| 90 | self, _ := os.Executable() |
| 91 | m, err := containment.New(containment.Config{Binary: "/usr/bin/bwrap", RunnerBinary: self, ScratchRoot: scratch}) |
| 92 | if err != nil { |
| 93 | t.Fatal(err) |
| 94 | } |
| 95 | // A module cache as a runner has one, without the module. |
| 96 | cache := t.TempDir() |
| 97 | _ = os.MkdirAll(filepath.Join(cache, "cache/download"), 0o700) |
| 98 | e.O.Manager, e.O.Runner.GoRoot, e.O.Runner.ModuleCache = m, goRoot, cache |
| 99 | if err := e.O.State.AcceptDispatch(context.Background(), "d", r.DispatchCommand); err != nil { |
| 100 | t.Fatal(err) |
| 101 | } |
| 102 | rep := e.Exec(context.Background(), r, func(wire.StepReport) {}) |
| 103 | if rep.Checks != tc.outcome || rep.Reason != tc.reason { |
| 104 | t.Fatalf("checks %q (%q), want %q (%q)\n%s", rep.Checks, rep.Reason, tc.outcome, tc.reason, fmt.Sprint(rep.Output)) |
| 105 | } |
| 106 | if tc.reason == "checks_unavailable" && (rep.ExitCode != nil || rep.Output != "") { |
| 107 | t.Fatalf("a gate ran for a toolchain this runner lacks: %+v", rep) |
| 108 | } |
| 109 | }) |
| 110 | } |
| 111 | } |
| 112 | |
| 113 | // TestChecksStopWhenTheStageExitIsUnverified (Astra, review of 4e0d2497): |
| 114 | // when the dependency stage's namespace exit cannot be verified, the step |
| 115 | // stops there, unterminated, with that namespace kept for recovery. No |
| 116 | // gate launches over it and nothing is reported checked. |
| 117 | func TestChecksStopWhenTheStageExitIsUnverified(t *testing.T) { |
| 118 | out, err := exec.Command("go", "env", "GOROOT").Output() |
| 119 | if err != nil { |
| 120 | t.Skip("go is not installed") |
| 121 | } |
| 122 | e, f, r, _ := hubRig(t) |
| 123 | seed := filepath.Join(filepath.Dir(f.dir), "seed") |
| 124 | writeFile(t, seed, "go.mod", "module example.com/x\n\ngo 1.21\n") |
| 125 | writeFile(t, seed, "Makefile", "ci:\n\ttrue\n") |
| 126 | gitOut(t, seed, "add", "-A") |
| 127 | gitOut(t, seed, "commit", "-qm", "base") |
| 128 | r.BaseSHA = gitOut(t, seed, "rev-parse", "HEAD") |
| 129 | gitOut(t, seed, "checkout", "-q", "-B", r.Branch) |
| 130 | writeFile(t, seed, "NEWS", "candidate\n") |
| 131 | gitOut(t, seed, "commit", "-qam", "candidate") |
| 132 | r.CandidateSHA = gitOut(t, seed, "rev-parse", "HEAD") |
| 133 | gitOut(t, seed, "push", "-q", "--force", f.dir, "main", r.Branch) |
| 134 | r.Specialist = wire.SpecialistChecks |
| 135 | cache := t.TempDir() |
| 136 | starter := &unprovenStarter{} |
| 137 | e.checkStarter, e.O.Runner.GoRoot, e.O.Runner.ModuleCache = starter, strings.TrimSpace(string(out)), cache |
| 138 | if err := e.O.State.AcceptDispatch(context.Background(), "d", r.DispatchCommand); err != nil { |
| 139 | t.Fatal(err) |
| 140 | } |
| 141 | rep := e.Exec(context.Background(), r, func(wire.StepReport) {}) |
| 142 | var ns string |
| 143 | e.O.State.db.QueryRow("SELECT ns_id FROM steps WHERE step_id=? AND attempt=?", r.StepID, r.Attempt).Scan(&ns) |
| 144 | if rep.State != wire.StepFailed || rep.Termination || rep.Checks != "" || rep.ExitCode != nil || starter.launches != 1 || ns != "ns-1" { |
| 145 | t.Fatalf("report %+v after %d launches, namespace %q", rep, starter.launches, ns) |
| 146 | } |
| 147 | } |
| 148 | |
| 149 | // unprovenStarter launches processes whose exit is never verified. |
| 150 | type unprovenStarter struct{ launches int } |
| 151 | |
| 152 | func (s *unprovenStarter) StartRecorded(ctx context.Context, _ provider.Launch, _ []string, record func(context.Context, containment.Record) error) (provider.Process, error) { |
| 153 | s.launches++ |
| 154 | if err := record(ctx, containment.Record{ID: fmt.Sprintf("ns-%d", s.launches)}); err != nil { |
| 155 | return nil, err |
| 156 | } |
| 157 | return unprovenProcess{}, nil |
| 158 | } |
| 159 | |
| 160 | type unprovenProcess struct{} |
| 161 | |
| 162 | func (unprovenProcess) Stdin() io.WriteCloser { return nopWriter{} } |
| 163 | func (unprovenProcess) Stdout() io.ReadCloser { return io.NopCloser(strings.NewReader("")) } |
| 164 | func (unprovenProcess) Stderr() io.ReadCloser { return io.NopCloser(strings.NewReader("")) } |
| 165 | func (unprovenProcess) ID() string { return "unproven" } |
| 166 | func (unprovenProcess) Wait(context.Context) (provider.Exit, error) { |
| 167 | return provider.Exit{Code: 1}, errors.New("namespace exit not proven") |
| 168 | } |
| 169 | |
| 170 | type nopWriter struct{} |
| 171 | |
| 172 | func (nopWriter) Write(p []byte) (int, error) { return len(p), nil } |
| 173 | func (nopWriter) Close() error { return nil } |
| 174 | |