package build import ( "context" "strings" "testing" "git.novox.be/novox/mesh-sdk/go/delivery" ) // trunkModule writes the trunk's record of the module m at a fingerprint of its own, so that the candidate's fingerprint (fp1) // is one the trunk does not carry. func trunkModule(t *testing.T, r *rig) { t.Helper() data, err := delivery.EncodeModule(delivery.Module{Repository: "novox/mesh-catalog", Commit: "7a74cee83f88e6585a5511c3cc53bdf0f5303d20", FP: "src1:" + hex64('f'), FP2: "src2:" + hex64('e'), Manifest: mdigest('a'), Path: "modules/m", Toolchains: agreedToolchains}) if err != nil { t.Fatal(err) } r.b.Module(context.Background(), "m", data) r.b.Wait() r.bm.mu.Lock() r.bm.calls = nil r.bm.mu.Unlock() } func candidateValue(t *testing.T, group string, number int, head string) []byte { t.Helper() pulls := []delivery.CandidatePull{{Head: delivery.Head{Repository: "novox/mesh-catalog", Number: number, Head: head}, Modules: []string{"m"}, Fps: map[string]delivery.CandidateModule{"m": {FP2: fp1, Manifest: mdigest('a')}}}} hash, err := delivery.HeadsHash([]delivery.Head{pulls[0].Head}) if err != nil { t.Fatal(err) } data, err := delivery.EncodeCandidate(delivery.Candidate{V: delivery.Version, Group: group, HeadsHash: hash, Pulls: pulls, FPSet: delivery.FingerprintSet(pulls)}) if err != nil { t.Fatal(err) } return data } const ( headX = "1111111111111111111111111111111111111111" headY = "2222222222222222222222222222222222222222" ) func TestACandidatesFingerprintIsBuiltOnceAsAKeyOfItsOwnFromTheHeadMergedOntoTheTrunk(t *testing.T) { // N7/AC1, N7/AC2 (issue 1684) r := newRig(t, goBundle) trunkModule(t, r) r.b.Candidate(context.Background(), "feat%2Fx", candidateValue(t, "feat/x", 7, headX)) r.b.Wait() v := valueOf(t, r.st, r.key) if v.State != delivery.BuildDone || len(v.Artifacts) != 1 { t.Fatalf("build = %+v, want the candidate's fingerprint built", v) } fetches := r.bm.callsOf("build_go_fetch") if len(fetches) != 1 { // one node per fingerprint (ADR 0478 clause 4) t.Fatalf("%d fetches, want one", len(fetches)) } f := fetches[0].body.(map[string]any) if f["commit"] != headX || f["ref"] != "refs/pull/7/head" || f["base"] != "7a74cee83f88e6585a5511c3cc53bdf0f5303d20" || f["repository"] != "novox/mesh-catalog" { t.Errorf("fetch args = %+v, want the head, its ref and the trunk commit to merge onto", f) } if b := r.bm.callsOf("build_go_build")[0].body.(map[string]any); b["module"] != "modules/m" { t.Errorf("build args = %+v", b) } // Two heads carrying one fingerprint are one build. r.b.Candidate(context.Background(), "feat%2Fy", candidateValue(t, "feat/y", 8, headY)) r.b.Wait() if n := len(r.bm.callsOf("build_go_fetch")); n != 1 { t.Errorf("%d fetches after a second head with the same fingerprint, want no more", n) } } func TestACandidateWhoseModuleHasNoTrunkRecordIsNotBuilt(t *testing.T) { r := newRig(t, goBundle) r.b.Candidate(context.Background(), "feat%2Fx", candidateValue(t, "feat/x", 7, headX)) r.b.Wait() if _, ok := r.st.vals[r.key]; ok { t.Error("a build key was written for a module the builder knows no directory of") } } func TestAFingerprintTheTrunkCarriesIsBuiltFromTheTrunksCommit(t *testing.T) { // the trunk's source wins r := newRig(t, goBundle) r.b.Candidate(context.Background(), "feat%2Fx", candidateValue(t, "feat/x", 7, headX)) r.b.Module(context.Background(), "m", moduleValue(t, "modules/m")) // the trunk now carries fp1 r.b.Wait() for _, c := range r.bm.callsOf("build_go_fetch") { if f := c.body.(map[string]any); f["ref"] != nil || f["base"] != nil { t.Errorf("fetch args = %+v: a trunk fingerprint is built from the trunk's commit alone", f) } } if v := valueOf(t, r.st, r.key); v.State != delivery.BuildDone { t.Errorf("build = %+v", v) } } func TestACandidateWhoseHeadDoesNotMergeIsFailedAndNoTrunkBuildIsStoredUnderIt(t *testing.T) { // review D4 for name, set := range map[string]func(*rig){ "conflict": func(r *rig) { r.bm.conflicts = "go.mod" }, "no merge": func(r *rig) { r.bm.noMerge = true }, } { r := newRig(t, goBundle) trunkModule(t, r) set(r) r.b.Candidate(context.Background(), "feat%2Fx", candidateValue(t, "feat/x", 7, headX)) r.b.Wait() v := valueOf(t, r.st, r.key) if v.State != delivery.BuildFailed || (name == "conflict" && !strings.Contains(v.Error, "go.mod")) || len(r.bm.callsOf("build_go_build")) != 0 { t.Errorf("%s: build = %+v, builds %d, want failed and nothing built", name, v, len(r.bm.callsOf("build_go_build"))) } } }