package build import ( "context" "errors" "strings" "testing" "time" "git.novox.be/novox/mesh-delivery/modules/mesh-delivery-builder/builds" "git.novox.be/novox/mesh-delivery/modules/mesh-delivery-contracts/go/seats/meshbuilds" "git.novox.be/novox/mesh-delivery/modules/mesh-delivery-contracts/go/seats/meshcatalog" "git.novox.be/novox/mesh-sdk/go/catalogue" "mesh-delivery-builder/internal/versions" ) // The runner's build step ask is the builder's one entry point for a candidate (design 47 G A-97): each entry is read by its // fingerprint, built through the one claim, and the step's end says passed only for a build that is done. func TestABuildStepAnswersItsEnd(t *testing.T) { r := newRig(t, catalogue.Artifact{Name: "files", Kind: "archive"}) entryOf := func(fp, commit string) meshcatalog.CatalogEntry { return meshcatalog.CatalogEntry{Module: "m", Repository: "novox/m", Commit: commit, Fp: fp, ManifestDigest: mdigest('a'), Path: "."} } r.b.StepEntry = func(_ context.Context, fp string) (meshcatalog.CatalogEntry, error) { if fp == fp1 { return entryOf(fp1, "c1"), nil } return meshcatalog.CatalogEntry{}, errors.New("not found") } ask := func(entries ...meshbuilds.BuildEntry) meshbuilds.StepEnded { return r.b.BuildStep(t.Context(), meshbuilds.StepRun{Pipeline: "main.1.m", Step: meshbuilds.StepRunStepBuild, Entries: entries}) } if end := ask(); end.Outcome != meshbuilds.StepEndedOutcomeNothingToRun { t.Errorf("an ask naming no entry: %+v", end) } if end := ask(meshbuilds.BuildEntry{Module: "m", Fingerprint: "src2:" + strings.Repeat("2", 64)}); end.Outcome != meshbuilds.StepEndedOutcomeCouldNotRun || !strings.Contains(end.Said, "cannot be read") { t.Errorf("an entry the seat does not hold: %+v", end) } if end := ask(meshbuilds.BuildEntry{Module: "m", Fingerprint: fp1, Commit: "other"}); end.Outcome != meshbuilds.StepEndedOutcomeCouldNotRun { t.Errorf("an entry at another commit: %+v", end) } // A build already done is passed at once, naming each entry as it was asked. done, _ := builds.EncodeBuilderBuild(builds.BuilderBuild{State: builds.BuildDone, Manifest: mdigest('a')}) if _, err := r.st.Create(t.Context(), r.key, done); err != nil { t.Fatal(err) } entries := []meshbuilds.BuildEntry{{Module: "m", Fingerprint: fp1, Commit: "c1"}} if end := ask(entries...); end.Outcome != meshbuilds.StepEndedOutcomePassed || end.Step != meshbuilds.StepEndedStepBuild || len(end.Entries) != 1 || end.Entries[0].Commit != "c1" || end.Pipeline != "main.1.m" { t.Errorf("a done build: %+v", end) } } // A step ask waits for the end of its build without a timer: the log entry of that end wakes it (design 47 G A-97). func TestAStepAskIsWokenByItsBuildsEnd(t *testing.T) { r := newRig(t) key := r.key ch := r.b.awaitEnd(key) r.b.stepEnded(builds.BuildLogEntry{Event: builds.BuildStartedEvent, Module: "m", FP: fp1}) // a start wakes nothing select { case <-ch: t.Fatal("a build's start woke the wait") default: } r.b.stepEnded(builds.BuildLogEntry{Event: builds.BuildEndedEvent, Module: "m", FP: fp1, Outcome: builds.BuildDone}) select { case e := <-ch: if e.Outcome != builds.BuildDone { t.Errorf("end = %+v", e) } case <-time.After(time.Second): t.Fatal("the end of the build did not wake the wait") } r.b.dropEnd(key, ch) if len(r.b.stepWaits) != 0 { t.Errorf("waits left: %v", r.b.stepWaits) } } // A build step passes a build that reserves a version only when a version is reserved for its fingerprint (design 47 G A-456): // a trunk build that ended done with none fails naming builder_reserve_done, and passes once one is reserved for it; a build // that reserves none (a manifest of before G, version "1") passes as it did. func TestABuildStepPassesAReservingBuildOnlyWithItsReservedVersion(t *testing.T) { for _, c := range []struct { name, version string reserve, released, unreadable bool want, said string }{ {"no version reserved", "0.1", false, false, false, meshbuilds.StepEndedOutcomeFailed, "built at " + fp1 + ", but no version is reserved for it"}, {"a version reserved", "0.1", true, false, false, meshbuilds.StepEndedOutcomePassed, ""}, {"a released build whose reservation is gone", "0.1", false, true, false, meshbuilds.StepEndedOutcomePassed, ""}, {"the plan cannot be read", "0.1", false, false, true, meshbuilds.StepEndedOutcomeCouldNotRun, "plan cannot be read"}, {"a series of before G", "1", false, false, false, meshbuilds.StepEndedOutcomePassed, ""}, } { t.Run(c.name, func(t *testing.T) { r := newRig(t, goBundle) r.mf.byDigest[mdigest('a')] = catalogue.Manifest{Module: "m", Version: c.version, Build: &catalogue.Build{Artifacts: []catalogue.Artifact{goBundle}}} res := &fakeReservations{filled: true, released: map[string]bool{fp1: c.released}} if v := r.consider(t, "modules/m"); v.State != builds.BuildDone { // built while the builder reserved nothing, as the live key was t.Fatalf("build = %+v, want done", v) } r.b.Reserved = res if c.reserve { res.sources = append(res.sources, versions.Source{Fingerprint: fp1}) } r.b.StepEntry = func(context.Context, string) (meshcatalog.CatalogEntry, error) { return meshcatalog.CatalogEntry{Module: "m", Repository: "novox/m", Commit: "c1", Fp: fp1, ManifestDigest: digestOf(c.unreadable), Path: "modules/m"}, nil } end := r.b.BuildStep(t.Context(), meshbuilds.StepRun{Pipeline: "main.1.m", Step: meshbuilds.StepRunStepBuild, Entries: []meshbuilds.BuildEntry{{Module: "m", Fingerprint: fp1}}}) if end.Outcome != c.want || !strings.Contains(end.Said, c.said) || (c.want == meshbuilds.StepEndedOutcomeFailed && !strings.Contains(end.Said, "builder_reserve_done")) { t.Errorf("end = %+v, want %s saying %q", end, c.want, c.said) } }) } } // digestOf is the manifest digest a step's entry names: the rig's manifest, or one no store holds, whose plan cannot be read. func digestOf(unreadable bool) string { if unreadable { return mdigest('z') } return mdigest('a') }