Files
mesh-catalog/modules/mesh-build-go/cmd/mesh-build-go/candidate_test.go
T

263 lines
11 KiB
Go

package main
import (
"context"
"encoding/json"
"os"
"path/filepath"
"slices"
"strings"
"testing"
sdkstep "git.novox.be/novox/mesh-delivery/sdk/buildstep"
"git.novox.be/novox/mesh-sdk/go/forgekey"
)
// Issue 1898 K1, K2 (ADR 0535): asked with the runner workspace, the digest and test steps compile the workspace's own
// mesh-build-go shut off and run the step as it, in one shut-off process; the answer says which program ran.
const candidateSHA = "fedcba9876543210fedcba9876543210fedcba98"
// candidateRig is a module over a workspace whose tree holds modules/mesh-build-go, with a runner standing for git, go
// and the candidate's program; what ran shut off is in seen, every command in ran.
type candidateRig struct {
m *Module
states *keptStates
seen []shutOff
ran []string
compile forgekey.Ran // what go build answers; Status 0 writes the program
answer func(spec levelSpec) levelAnswer // what the candidate's program answers
specs []levelSpec
compiled int
}
func newCandidateRig(t *testing.T) *candidateRig {
t.Helper()
cr := &candidateRig{states: newStates()}
cr.m = moduleWith(cr.states, "go1.27.1")
cr.m.Home, cr.m.Work, cr.m.Cache = t.TempDir(), t.TempDir(), t.TempDir()
cr.m.Executable = "/opt/bundle/mesh-build-go"
dir := filepath.Join(cr.m.Work, "ws1", "tree", candidateModule)
if err := os.MkdirAll(dir, 0o755); err != nil {
t.Fatal(err)
}
if err := os.WriteFile(filepath.Join(dir, "go.mod"), []byte("module meshbuildgo\n\ngo 1.26.0\n"), 0o644); err != nil {
t.Fatal(err)
}
cr.answer = func(spec levelSpec) levelAnswer {
if spec.Step == stepDigest {
return levelAnswer{Digest: &DigestResult{Level: spec.Level, Packages: map[string]string{"meshbuildgo/cmd/mesh-build-go": "mark"}}}
}
return levelAnswer{Result: &TestResult{Level: spec.Step, Passed: true, Tests: 42, Packages: []PackageResult{}, Why: "mark"}}
}
exe := filepath.Join(cr.m.Work, "ws1", "candidate", "mesh-build-go")
cr.m.Run = throughBubblewrap(t, func(ctx context.Context, env []string, name string, args ...string) forgekey.Ran {
line := name + " " + strings.Join(args, " ")
cr.ran = append(cr.ran, line)
switch {
case name == "git" && slices.Contains(args, "rev-parse"):
return forgekey.Ran{Stdout: candidateSHA + "\n"}
case name == "git" && slices.Contains(args, "ls-files"):
return forgekey.Ran{Stdout: "go.mod\n"}
case name == "postgres":
return forgekey.Ran{Stdout: "postgres (PostgreSQL) 18.6\n"}
case name == "go" && slices.Contains(args, "build"):
cr.compiled++
for _, want := range []string{"GOPROXY=off", "GOTOOLCHAIN=local", "GOFLAGS=-mod=mod"} {
if !slices.Contains(env, want) {
t.Errorf("the candidate compiled without %s: %v", want, env)
}
}
if cr.compile.Status == 0 && cr.compile.Err == "" {
if err := os.WriteFile(args[slices.Index(args, "-o")+1], []byte("program"), 0o755); err != nil {
t.Fatal(err)
}
}
return cr.compile
case name == exe && len(args) == 2 && args[0] == levelArg:
var spec levelSpec
if err := json.Unmarshal([]byte(args[1]), &spec); err != nil {
t.Errorf("the spec %s: %v", args[1], err)
}
cr.specs = append(cr.specs, spec)
raw, _ := json.Marshal(cr.answer(spec))
return forgekey.Ran{Stdout: string(raw) + "\n"}
}
return forgekey.Ran{Status: 1, Err: "not expected here: " + line}
}, &cr.seen)
return cr
}
func (cr *candidateRig) test(t *testing.T, args map[string]any) (TestResult, sdkstep.Step) {
t.Helper()
if _, err := stepTool(t, cr.m, "build_go_test")(args); err != nil {
t.Fatal(err)
}
st := cr.states.waitEnd(t)
var res TestResult
if st.State == sdkstep.StepPassed {
if err := json.Unmarshal(st.Result, &res); err != nil {
t.Fatalf("result %s: %v", st.Result, err)
}
}
return res, st
}
func TestTheDigestAndTestToolsAnnounceTheRunner(t *testing.T) { // 1898 K1, ADR 0535 §2
m := moduleWith(newStates(), "go1.27.1")
for _, s := range m.StepTools() {
if s.Name != "build_go_digest" && s.Name != "build_go_test" {
continue
}
p, _ := s.Input["properties"].(map[string]any)["runner"].(map[string]any)
if p == nil || !slices.Equal(p["enum"].([]string), []string{RunnerInstalled, RunnerWorkspace}) {
t.Errorf("%s announces the runner %v, want installed and workspace", s.Name, p)
}
}
}
func TestTheUnitLevelUnderTheWorkspaceRunnerRunsTheCandidateShutOff(t *testing.T) { // 1898 K1, K2 (AC4's unit part)
cr := newCandidateRig(t)
res, st := cr.test(t, map[string]any{"workspace": "ws1", "level": LevelUnit, "runner": RunnerWorkspace, "packages": []any{"./..."}})
if st.State != sdkstep.StepPassed {
t.Fatalf("the step ended %s: %s", st.State, st.Reason)
}
if res.Tests != 42 || res.Why != "mark" || res.JudgedBy != "candidate "+candidateSHA {
t.Errorf("result = %+v, want the candidate's mark judged by candidate %s", res, candidateSHA)
}
if len(cr.specs) != 1 || cr.specs[0].Step != stepUnit || cr.specs[0].Workspace != "ws1" || !slices.Equal(cr.specs[0].Packages, []string{"./..."}) || cr.specs[0].Bound != UnitBound {
t.Errorf("the candidate was asked %+v, want the unit level of ws1 with its packages and bound", cr.specs)
}
for _, l := range cr.ran {
if strings.HasPrefix(l, "go ") && strings.Contains(l, " test ") {
t.Errorf("the installed program ran %q: the level is the candidate's to run", l)
}
}
// Two commands shut off: the compile, then the candidate's process; each as a run step is (ADR 0448 §2).
if len(cr.seen) != 2 || !strings.Contains(cr.seen[0].command, " build ") || !strings.Contains(cr.seen[0].command, "-C "+filepath.Join(cr.m.Work, "ws1", "tree", candidateModule)) ||
!strings.HasPrefix(cr.seen[1].command, filepath.Join(cr.m.Work, "ws1", "candidate", "mesh-build-go")+" level ") {
t.Fatalf("shut off %+v, want the compile and the candidate's process", cr.seen)
}
for _, s := range cr.seen {
checkShutOff(t, cr.m, s, false)
}
}
func TestTheIntegrationLevelUnderTheWorkspaceRunnerRunsTheCandidate(t *testing.T) { // 1898 K2
cr := newCandidateRig(t)
res, st := cr.test(t, map[string]any{"workspace": "ws1", "level": LevelIntegration, "runner": RunnerWorkspace})
if st.State != sdkstep.StepPassed || res.JudgedBy != "candidate "+candidateSHA {
t.Fatalf("the step ended %s (%s): %+v", st.State, st.Reason, res)
}
if len(cr.specs) != 1 || cr.specs[0].Step != stepIntegration || cr.specs[0].Bound != IntegrationRun || cr.specs[0].Setup != IntegrationSetup {
t.Errorf("the candidate was asked %+v, want the integration level with its bounds", cr.specs)
}
}
func TestTheDigestUnderTheWorkspaceRunnerIsTheCandidatesAndTheCandidateIsCompiledOnce(t *testing.T) { // 1898 K1, K2
cr := newCandidateRig(t)
out, err := stepTool(t, cr.m, "build_go_digest")(map[string]any{"workspace": "ws1", "level": LevelUnit, "runner": RunnerWorkspace})
if err != nil {
t.Fatal(err)
}
d := out.(DigestResult)
if d.Packages["meshbuildgo/cmd/mesh-build-go"] != "mark" || d.JudgedBy != "candidate "+candidateSHA {
t.Errorf("digest = %+v, want the candidate's, judged by candidate %s", d, candidateSHA)
}
if len(cr.specs) != 1 || cr.specs[0].Step != stepDigest || cr.specs[0].Level != LevelUnit {
t.Errorf("the candidate was asked %+v", cr.specs)
}
cr.test(t, map[string]any{"workspace": "ws1", "level": LevelUnit, "runner": RunnerWorkspace})
if cr.compiled != 1 {
t.Errorf("compiled the candidate %d times for two steps of one workspace, want once", cr.compiled)
}
}
func TestACandidateThatDoesNotCompileFailsTheStepWithTheCompilersFirstErrorAndRunsNoLevel(t *testing.T) { // 1898/AC5
cr := newCandidateRig(t)
cr.compile = forgekey.Ran{Status: 1, Stderr: "# meshbuildgo/cmd/mesh-build-go\ncmd/mesh-build-go/digest.go:12:2: undefined: x\ncmd/mesh-build-go/digest.go:13:2: undefined: y\n"}
_, st := cr.test(t, map[string]any{"workspace": "ws1", "level": LevelUnit, "runner": RunnerWorkspace})
if st.State == sdkstep.StepPassed || !strings.Contains(st.Reason, "does not compile: cmd/mesh-build-go/digest.go:12:2: undefined: x") || strings.Contains(st.Reason, "undefined: y") {
t.Fatalf("the step ended %s: %q, want it failed naming the first compile error", st.State, st.Reason)
}
if len(cr.specs) != 0 {
t.Errorf("the candidate ran %+v after it failed to compile", cr.specs)
}
for _, l := range cr.ran {
if strings.Contains(l, " test ") {
t.Errorf("a level ran: %q", l)
}
}
if _, err := stepTool(t, cr.m, "build_go_digest")(map[string]any{"workspace": "ws1", "level": LevelUnit, "runner": RunnerWorkspace}); err == nil ||
!strings.Contains(err.Error(), "undefined: x") {
t.Errorf("the digest answered %v, want the compile error", err)
}
}
func TestTheInstalledProgramSaysItJudged(t *testing.T) { // 1898 K1, ADR 0535 §5
m, states, ws, _ := workspaceFor(t, "slow")
for _, runner := range []any{nil, RunnerInstalled} {
args := map[string]any{"workspace": ws, "level": LevelUnit}
if runner != nil {
args["runner"] = runner
}
res, st := runUnitWith(t, m, states, args)
if st.State != sdkstep.StepPassed || res.JudgedBy != RunnerInstalled {
t.Errorf("runner %v: the step ended %s, judged by %q, want installed", runner, st.State, res.JudgedBy)
}
}
dm, _ := digestFixture(t)
if d := digests(t, dm, LevelUnit); d.JudgedBy != RunnerInstalled {
t.Errorf("the installed digest is judged by %q, want installed", d.JudgedBy)
}
}
func TestAnUnknownRunnerIsRefused(t *testing.T) { // 1898 K1
cr := newCandidateRig(t)
if _, err := cr.m.digest(context.Background(), map[string]any{"workspace": "ws1", "level": LevelUnit, "runner": "elsewhere"}); err == nil {
t.Error("the digest took the runner elsewhere")
}
if _, err := cr.m.test(context.Background(), map[string]any{"workspace": "ws1", "level": LevelUnit, "runner": "elsewhere"}); err == nil {
t.Error("the test step took the runner elsewhere")
}
}
func TestTheShutOffProcessRunsTheStepItIsAsked(t *testing.T) { // 1898 K2: the candidate's side of the spec
m, _, ws, calls := workspaceFor(t, "slow")
m.Inside = true
a := m.answer(context.Background(), levelSpec{Step: stepUnit, Workspace: ws, Packages: []string{"./..."}, Bound: UnitBound})
if a.Error != "" || a.Result == nil || a.Result.Level != LevelUnit || a.Result.Tests == 0 {
t.Fatalf("the unit step answered %+v", a)
}
ranTest := false
for _, c := range *calls {
ranTest = ranTest || strings.HasPrefix(c.line, "go -C ") && strings.Contains(c.line, " test -json ")
}
if !ranTest {
t.Errorf("the process ran no go test: %+v", *calls)
}
dm, _ := digestFixture(t)
dm.Inside = true
if a := dm.answer(context.Background(), levelSpec{Step: stepDigest, Level: LevelUnit, Workspace: "ws1"}); a.Error != "" || a.Digest == nil || len(a.Digest.Packages) == 0 {
t.Errorf("the digest step answered %+v", a)
}
if a := m.answer(context.Background(), levelSpec{Step: "deploy", Workspace: ws}); a.Error == "" {
t.Error("the process took a step that is not one")
}
}
func runUnitWith(t *testing.T, m *Module, states *keptStates, args map[string]any) (TestResult, sdkstep.Step) {
t.Helper()
if _, err := stepTool(t, m, "build_go_test")(args); err != nil {
t.Fatal(err)
}
st := states.waitEnd(t)
var res TestResult
if st.State == sdkstep.StepPassed {
if err := json.Unmarshal(st.Result, &res); err != nil {
t.Fatalf("result %s: %v", st.Result, err)
}
}
return res, st
}