package thin import ( "context" "errors" "fmt" "go/version" "io" "os" "os/exec" "path/filepath" "strings" "testing" "amadan.net/oficina/equipo/internal/runner/checks" "amadan.net/oficina/equipo/internal/runner/containment" "amadan.net/oficina/equipo/internal/runner/provider" "amadan.net/oficina/equipo/internal/wire" ) // TestChecksGoToolchainAndDependencies (Astra, review of f03d79bb): the // runner's Go is held to the base's requirement before any gate runs, // and only Equipo's own dependency stage can make a failed gate "needs // network"; the candidate's output can only keep a failure a failure. func TestChecksGoToolchainAndDependencies(t *testing.T) { out, err := exec.Command("go", "env", "GOROOT").Output() if err != nil { t.Skip("go is not installed") } goRoot := strings.TrimSpace(string(out)) local, err := checks.RunnerGoVersion(goRoot) if err != nil { t.Skip("the test's Go has no release version") } other := "go1.20.1" if version.Lang(local) == "go1.20" { other = "go1.21.1" } const sum = "example.invalid/dep v1.0.0 h1:AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA=\nexample.invalid/dep v1.0.0/go.mod h1:AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA=\n" 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"} plain := map[string]string{"go.mod": "module example.com/x\n\ngo 1.21\n", "x.go": "package x\n"} with := func(files map[string]string, name, body string) map[string]string { out := map[string]string{name: body} for k, v := range files { if k != name { out[k] = v } } return out } for _, tc := range []struct { name string base, candidate map[string]string outcome, reason string }{ {"newer base requirement", with(plain, "go.mod", "module example.com/x\n\ngo 1.99.0\n"), nil, wire.ChecksCannotRun, "checks_unavailable"}, {"version-specific base build", with(plain, "Makefile", "export GOTOOLCHAIN := "+other+"\nci:\n\ttrue\n"), nil, wire.ChecksCannotRun, "checks_unavailable"}, {"candidate raises the requirement", plain, map[string]string{"go.mod": "module example.com/x\n\ngo 1.99.0\n"}, wire.ChecksFailed, ""}, {"pinned patch of this minor", with(plain, "Makefile", "export GOTOOLCHAIN := "+version.Lang(local)+".0\nci:\n\tgo vet ./...\n"), nil, wire.ChecksPassed, ""}, {"offline dependency", with(needsDep, "Makefile", "ci:\n\tgo vet ./...\n"), nil, wire.ChecksCannotRun, "needs_network"}, {"mixed failure", with(needsDep, "Makefile", "ci:\n\t@echo '--- FAIL: TestDoor (0.00s)'; go vet ./...\n"), nil, wire.ChecksFailed, ""}, {"json failure", with(needsDep, "Makefile", "ci:\n\t@echo '{\"Action\":\"fail\",\"Package\":\"example.com/x\",\"Test\":\"TestDoor\"}'; go vet ./...\n"), nil, wire.ChecksFailed, ""}, {"failure beyond the tail", with(needsDep, "Makefile", "ci:\n\t@echo '--- FAIL: TestDoor (0.00s)'; seq 1000; go vet ./...\n"), nil, wire.ChecksFailed, ""}, {"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, ""}, } { t.Run(tc.name, func(t *testing.T) { e, f, r, _ := hubRig(t) seed := filepath.Join(filepath.Dir(f.dir), "seed") if _, ok := tc.base["Makefile"]; !ok { tc.base = with(tc.base, "Makefile", "ci:\n\tgo vet ./...\n") } for name, body := range tc.base { writeFile(t, seed, name, body) } gitOut(t, seed, "add", "-A") gitOut(t, seed, "commit", "-qm", "base") r.BaseSHA = gitOut(t, seed, "rev-parse", "HEAD") gitOut(t, seed, "checkout", "-q", "-B", r.Branch) writeFile(t, seed, "NEWS", "candidate\n") for name, body := range tc.candidate { writeFile(t, seed, name, body) } gitOut(t, seed, "add", "-A") gitOut(t, seed, "commit", "-qm", "candidate") r.CandidateSHA = gitOut(t, seed, "rev-parse", "HEAD") gitOut(t, seed, "push", "-q", "--force", f.dir, "main", r.Branch) r.Specialist = wire.SpecialistChecks scratch := filepath.Join(filepath.Dir(f.dir), "sandbox") _ = os.Mkdir(scratch, 0o700) self, _ := os.Executable() m, err := containment.New(containment.Config{Binary: "/usr/bin/bwrap", RunnerBinary: self, ScratchRoot: scratch}) if err != nil { t.Fatal(err) } // A module cache as a runner has one, without the module. cache := t.TempDir() _ = os.MkdirAll(filepath.Join(cache, "cache/download"), 0o700) e.O.Manager, e.O.Runner.GoRoot, e.O.Runner.ModuleCache = m, goRoot, cache if err := e.O.State.AcceptDispatch(context.Background(), "d", r.DispatchCommand); err != nil { t.Fatal(err) } rep := e.Exec(context.Background(), r, func(wire.StepReport) {}) if rep.Checks != tc.outcome || rep.Reason != tc.reason { t.Fatalf("checks %q (%q), want %q (%q)\n%s", rep.Checks, rep.Reason, tc.outcome, tc.reason, fmt.Sprint(rep.Output)) } if tc.reason == "checks_unavailable" && (rep.ExitCode != nil || rep.Output != "") { t.Fatalf("a gate ran for a toolchain this runner lacks: %+v", rep) } }) } } // TestChecksStopWhenTheStageExitIsUnverified (Astra, review of 4e0d2497): // when the dependency stage's namespace exit cannot be verified, the step // stops there, unterminated, with that namespace kept for recovery. No // gate launches over it and nothing is reported checked. func TestChecksStopWhenTheStageExitIsUnverified(t *testing.T) { out, err := exec.Command("go", "env", "GOROOT").Output() if err != nil { t.Skip("go is not installed") } e, f, r, _ := hubRig(t) seed := filepath.Join(filepath.Dir(f.dir), "seed") writeFile(t, seed, "go.mod", "module example.com/x\n\ngo 1.21\n") writeFile(t, seed, "Makefile", "ci:\n\ttrue\n") gitOut(t, seed, "add", "-A") gitOut(t, seed, "commit", "-qm", "base") r.BaseSHA = gitOut(t, seed, "rev-parse", "HEAD") gitOut(t, seed, "checkout", "-q", "-B", r.Branch) writeFile(t, seed, "NEWS", "candidate\n") gitOut(t, seed, "commit", "-qam", "candidate") r.CandidateSHA = gitOut(t, seed, "rev-parse", "HEAD") gitOut(t, seed, "push", "-q", "--force", f.dir, "main", r.Branch) r.Specialist = wire.SpecialistChecks cache := t.TempDir() starter := &unprovenStarter{} e.checkStarter, e.O.Runner.GoRoot, e.O.Runner.ModuleCache = starter, strings.TrimSpace(string(out)), cache if err := e.O.State.AcceptDispatch(context.Background(), "d", r.DispatchCommand); err != nil { t.Fatal(err) } rep := e.Exec(context.Background(), r, func(wire.StepReport) {}) var ns string e.O.State.db.QueryRow("SELECT ns_id FROM steps WHERE step_id=? AND attempt=?", r.StepID, r.Attempt).Scan(&ns) if rep.State != wire.StepFailed || rep.Termination || rep.Checks != "" || rep.ExitCode != nil || starter.launches != 1 || ns != "ns-1" { t.Fatalf("report %+v after %d launches, namespace %q", rep, starter.launches, ns) } } // unprovenStarter launches processes whose exit is never verified. type unprovenStarter struct{ launches int } func (s *unprovenStarter) StartRecorded(ctx context.Context, _ provider.Launch, _ []string, record func(context.Context, containment.Record) error) (provider.Process, error) { s.launches++ if err := record(ctx, containment.Record{ID: fmt.Sprintf("ns-%d", s.launches)}); err != nil { return nil, err } return unprovenProcess{}, nil } type unprovenProcess struct{} func (unprovenProcess) Stdin() io.WriteCloser { return nopWriter{} } func (unprovenProcess) Stdout() io.ReadCloser { return io.NopCloser(strings.NewReader("")) } func (unprovenProcess) Stderr() io.ReadCloser { return io.NopCloser(strings.NewReader("")) } func (unprovenProcess) ID() string { return "unproven" } func (unprovenProcess) Wait(context.Context) (provider.Exit, error) { return provider.Exit{Code: 1}, errors.New("namespace exit not proven") } type nopWriter struct{} func (nopWriter) Write(p []byte) (int, error) { return len(p), nil } func (nopWriter) Close() error { return nil }