296 lines
11 KiB
Go
296 lines
11 KiB
Go
package build
|
|
|
|
import (
|
|
"context"
|
|
"encoding/json"
|
|
"fmt"
|
|
"strings"
|
|
"sync"
|
|
"testing"
|
|
"time"
|
|
|
|
"git.novox.be/novox/mesh-sdk/go/catalogue"
|
|
"git.novox.be/novox/mesh-sdk/go/delivery"
|
|
)
|
|
|
|
// buildModules simulates the two build modules on the nodes: a long tool answers a step and key at once and writes the
|
|
// step's end into the steps (as mesh-build-go and mesh-build-node do), with a digest by node and artifact, or a failure.
|
|
type buildModules struct {
|
|
mu sync.Mutex
|
|
n int
|
|
calls []call
|
|
steps *fakeSteps
|
|
digests map[string]string // `<node>/<artifact>` -> digest; the default is `digest`
|
|
fails map[string]string // `<node>/<tool>` -> the reason the step fails with
|
|
}
|
|
|
|
func newBuildModules() *buildModules {
|
|
return &buildModules{steps: &fakeSteps{steps: map[string]delivery.Step{}}, digests: map[string]string{}, fails: map[string]string{}}
|
|
}
|
|
|
|
func (b *buildModules) CallOn(node, key string, body any) (json.RawMessage, error) {
|
|
b.mu.Lock()
|
|
defer b.mu.Unlock()
|
|
b.calls = append(b.calls, call{node, key, body})
|
|
b.n++
|
|
id := fmt.Sprintf("s%d", b.n)
|
|
module, tool, _ := strings.Cut(key, ".")
|
|
args, _ := body.(map[string]any)
|
|
step := delivery.Step{ID: id, Tool: tool, Node: node, State: delivery.StepPassed}
|
|
if reason, ok := b.fails[node+"/"+tool]; ok {
|
|
step.State, step.Reason = delivery.StepFailed, reason
|
|
} else if strings.HasSuffix(tool, "_fetch") {
|
|
step.Result = json.RawMessage(`{"workspace":"w-` + node + `"}`)
|
|
} else {
|
|
d := digest
|
|
if v, ok := b.digests[node+"/"+fmt.Sprint(args["artifact"])]; ok {
|
|
d = v
|
|
}
|
|
step.Result = json.RawMessage(`{"digest":"` + d + `"}`)
|
|
}
|
|
sk, _ := delivery.StepKey(node, id)
|
|
b.steps.mu.Lock()
|
|
b.steps.steps[module+"/"+sk] = step
|
|
b.steps.mu.Unlock()
|
|
return json.RawMessage(`{"step":"` + id + `","key":"` + sk + `","state":"running"}`), nil
|
|
}
|
|
|
|
func (b *buildModules) callsOf(tool string) (out []call) {
|
|
b.mu.Lock()
|
|
defer b.mu.Unlock()
|
|
for _, c := range b.calls {
|
|
if strings.HasSuffix(c.key, "."+tool) {
|
|
out = append(out, c)
|
|
}
|
|
}
|
|
return out
|
|
}
|
|
|
|
type fakeManifests struct {
|
|
byDigest map[string]catalogue.Manifest
|
|
}
|
|
|
|
func (f *fakeManifests) Manifest(_ context.Context, d string) (catalogue.Manifest, error) {
|
|
m, ok := f.byDigest[d]
|
|
if !ok {
|
|
return catalogue.Manifest{}, fmt.Errorf("manifests.%s is not there", d)
|
|
}
|
|
return m, nil
|
|
}
|
|
|
|
func mdigest(c byte) string { return hex64(c) }
|
|
|
|
type rig struct {
|
|
b *Builder
|
|
st *memStore
|
|
bm *buildModules
|
|
tools *ToolchainSet
|
|
mf *fakeManifests
|
|
key string
|
|
}
|
|
|
|
func newRig(t *testing.T, artifacts ...catalogue.Artifact) *rig {
|
|
t.Helper()
|
|
r := &rig{st: newMem(), bm: newBuildModules(), tools: &ToolchainSet{}, mf: &fakeManifests{byDigest: map[string]catalogue.Manifest{mdigest('a'): {Module: "m", Build: &catalogue.Build{Artifacts: artifacts}}}}}
|
|
for _, n := range []string{"ace", "novox"} {
|
|
r.tools.Put(GoBuildModule, n+".go", tcValue(t, "go", "go1.27.1"), false)
|
|
r.tools.Put(NodeBuildModule, n+".typescript", tcValue(t, "typescript", "tsc5"), false)
|
|
}
|
|
r.b = &Builder{
|
|
Claimer: Claimer{Store: r.st, By: "novox.builder"}, Runner: Runner{Caller: r.bm, Steps: r.bm.steps, Bound: time.Second},
|
|
Toolchains: r.tools, Manifests: r.mf, Nodes: 2, Logf: t.Logf,
|
|
}
|
|
var err error
|
|
if r.key, err = delivery.BuilderBuildKey("m", fp1); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
return r
|
|
}
|
|
|
|
func moduleValue(t *testing.T, path string) []byte {
|
|
t.Helper()
|
|
data, err := delivery.EncodeModule(delivery.Module{Repository: "novox/mesh-catalog", Commit: "7a74cee83f88e6585a5511c3cc53bdf0f5303d20", FP: "src1:" + hex64('f'), FP2: fp1, Manifest: mdigest('a'), Path: path})
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
return data
|
|
}
|
|
|
|
func (r *rig) consider(t *testing.T, path string) delivery.BuilderBuild {
|
|
t.Helper()
|
|
r.b.Module(context.Background(), "m", moduleValue(t, path))
|
|
r.b.Wait()
|
|
if _, ok := r.st.vals[r.key]; !ok {
|
|
t.Fatal("no build key was written")
|
|
}
|
|
return valueOf(t, r.st, r.key)
|
|
}
|
|
|
|
var (
|
|
goBundle = catalogue.Artifact{Name: "tool", Kind: "bundle", Language: "go"}
|
|
tsBundle = catalogue.Artifact{Name: "web", Kind: "bundle", Language: "typescript"}
|
|
archiveA = catalogue.Artifact{Name: "files", Kind: "archive"}
|
|
imageArt = catalogue.Artifact{Name: "img", Kind: "image"}
|
|
manifestD = mdigest('a')
|
|
)
|
|
|
|
func TestABundleAndAnArchiveAreBuiltOnTwoNodesAndDoneWhenTheDigestsAgree(t *testing.T) { // 1607/AC7, ADR 0478 clauses 1, 4
|
|
r := newRig(t, goBundle, archiveA, tsBundle)
|
|
v := r.consider(t, "modules/m")
|
|
if v.State != delivery.BuildDone || v.By != "novox.builder" || v.Manifest != manifestD || len(v.Artifacts) != 3 {
|
|
t.Fatalf("build = %+v, want done by novox.builder with three artifacts", v)
|
|
}
|
|
for i, want := range []string{"files", "tool", "web"} {
|
|
if a := v.Artifacts[i]; a.Name != want || a.Digest != digest || a.Reference != "" {
|
|
t.Errorf("artifact %d = %+v, want %s with the digest and no reference (ADR 0478 clause 6)", i, a, want)
|
|
}
|
|
}
|
|
if v.Artifacts[0].Kind != "archive" || v.Artifacts[1].Kind != "bundle" {
|
|
t.Errorf("kinds = %+v", v.Artifacts)
|
|
}
|
|
if got := v.Toolchains["go"]; got.Version != "go1.27.1" || got.Recipe != "1" {
|
|
t.Errorf("toolchains = %+v, want go and typescript named", v.Toolchains)
|
|
}
|
|
if got := v.Toolchains["typescript"]; got.Version != "tsc5" {
|
|
t.Errorf("toolchains = %+v", v.Toolchains)
|
|
}
|
|
// Two fetches per build module, one on each node; every build names the module's directory.
|
|
if n := len(r.bm.callsOf("build_go_fetch")); n != 2 {
|
|
t.Errorf("%d go fetches, want one per node", n)
|
|
}
|
|
if n := len(r.bm.callsOf("build_go_build")); n != 4 { // tool and files, each on two nodes
|
|
t.Errorf("%d go builds, want 4", n)
|
|
}
|
|
if n := len(r.bm.callsOf("build_node_build")); n != 2 {
|
|
t.Errorf("%d node builds, want 2", n)
|
|
}
|
|
c := r.bm.callsOf("build_go_build")[0]
|
|
if args := c.body.(map[string]any); args["module"] != "modules/m" || !strings.HasPrefix(fmt.Sprint(args["workspace"]), "w-") {
|
|
t.Errorf("build args = %+v, want the module's path and the fetch's workspace", args)
|
|
}
|
|
f := r.bm.callsOf("build_go_fetch")[0].body.(map[string]any)
|
|
if f["repository"] != "novox/mesh-catalog" || f["commit"] != "7a74cee83f88e6585a5511c3cc53bdf0f5303d20" {
|
|
t.Errorf("fetch args = %+v", f)
|
|
}
|
|
}
|
|
|
|
func TestDifferingDigestsFailTheBuildNamingBothNodes(t *testing.T) { // 1607/AC4, ADR 0478 clause 4
|
|
r := newRig(t, goBundle)
|
|
r.bm.digests["ace/tool"] = "sha256:" + hex64('9')
|
|
v := r.consider(t, "modules/m")
|
|
if v.State != delivery.BuildFailed || !strings.Contains(v.Error, "ace") || !strings.Contains(v.Error, "novox") || !strings.Contains(v.Error, digest) || !strings.Contains(v.Error, hex64('9')) {
|
|
t.Errorf("build = %+v, want failed naming both nodes and both digests", v)
|
|
}
|
|
}
|
|
|
|
func TestOneQualifyingNodeBuildsOnceAndTheKeySaysSo(t *testing.T) { // 1607/AC4, ADR 0478 clause 4
|
|
r := newRig(t, goBundle)
|
|
r.tools.Put(GoBuildModule, "ace.go", nil, true)
|
|
v := r.consider(t, "modules/m")
|
|
if v.State != delivery.BuildDone || !strings.Contains(v.Step, "one node") || !strings.Contains(v.Step, "novox") {
|
|
t.Errorf("build = %+v, want done with its step saying one node, novox", v)
|
|
}
|
|
if n := len(r.bm.callsOf("build_go_build")); n != 1 {
|
|
t.Errorf("%d builds, want one", n)
|
|
}
|
|
}
|
|
|
|
func TestAFailingStepFailsTheBuildWithItsReasonAndNodeAndIsTriedAgainAsANewRevision(t *testing.T) { // 1607/AC7, ADR 0429 clause 18
|
|
r := newRig(t, goBundle)
|
|
r.bm.fails["ace/build_go_build"] = "go build of tool: undefined: x"
|
|
v := r.consider(t, "modules/m")
|
|
if v.State != delivery.BuildFailed || !strings.Contains(v.Error, "ace") || !strings.Contains(v.Error, "undefined: x") {
|
|
t.Fatalf("build = %+v, want failed with the node and the reason", v)
|
|
}
|
|
rev := r.st.revs[r.key]
|
|
delete(r.bm.fails, "ace/build_go_build")
|
|
v = r.consider(t, "modules/m")
|
|
if v.State != delivery.BuildDone || r.st.revs[r.key] <= rev {
|
|
t.Errorf("build = %+v at revision %d (was %d), want done as a new revision of the same key", v, r.st.revs[r.key], rev)
|
|
}
|
|
}
|
|
|
|
func TestAModuleWithAnImageGetsARefusedKeyNeverBuiltAgain(t *testing.T) { // 1607/AC2, ADR 0478 clause 2
|
|
r := newRig(t, archiveA, imageArt)
|
|
v := r.consider(t, "modules/m")
|
|
if v.State != delivery.BuildFailed || !strings.HasPrefix(v.Error, RefusedPrefix) || !strings.Contains(v.Error, "image") || !strings.Contains(v.Error, "build-agent") {
|
|
t.Fatalf("build = %+v, want failed 'refused kind: image' naming build-agent", v)
|
|
}
|
|
if len(r.bm.calls) != 0 {
|
|
t.Errorf("%d calls, want none for a refused kind", len(r.bm.calls))
|
|
}
|
|
writes := r.st.writes
|
|
r.consider(t, "modules/m")
|
|
if r.st.writes != writes {
|
|
t.Errorf("a refused key was written again (%d writes, was %d)", r.st.writes, writes)
|
|
}
|
|
}
|
|
|
|
func TestAModuleKeyWithNoPathFailsNamingThatReason(t *testing.T) { // 1607/AC8, ADR 0478 clause 8
|
|
r := newRig(t, goBundle)
|
|
v := r.consider(t, "")
|
|
if v.State != delivery.BuildFailed || !strings.Contains(v.Error, "path") || len(r.bm.calls) != 0 {
|
|
t.Errorf("build = %+v with %d calls, want failed naming the missing path and nothing called", v, len(r.bm.calls))
|
|
}
|
|
}
|
|
|
|
func TestNoQualifyingNodeFailsTheBuildNamingTheLanguage(t *testing.T) { // 1607/AC4
|
|
r := newRig(t, tsBundle)
|
|
r.tools.Put(NodeBuildModule, "ace.typescript", nil, true)
|
|
r.tools.Put(NodeBuildModule, "novox.typescript", nil, true)
|
|
v := r.consider(t, "modules/m")
|
|
if v.State != delivery.BuildFailed || !strings.Contains(v.Error, "typescript") || !strings.Contains(v.Error, delivery.ToolchainNoReport) {
|
|
t.Errorf("build = %+v, want failed naming typescript and that no toolchain is reported", v)
|
|
}
|
|
}
|
|
|
|
func TestAnUnreadableManifestFailsTheBuild(t *testing.T) {
|
|
r := newRig(t, goBundle)
|
|
delete(r.mf.byDigest, mdigest('a'))
|
|
v := r.consider(t, "modules/m")
|
|
if v.State != delivery.BuildFailed || !strings.Contains(v.Error, "manifest") {
|
|
t.Errorf("build = %+v, want failed naming the manifest", v)
|
|
}
|
|
}
|
|
|
|
func TestAModuleWithNoFingerprintOrNoArtifactIsNotBuilt(t *testing.T) { // 1607/AC2
|
|
r := newRig(t)
|
|
r.b.Module(context.Background(), "m", moduleValue(t, "modules/m"))
|
|
data, _ := delivery.EncodeModule(delivery.Module{Repository: "novox/r", Commit: "c", FP: "src1:" + hex64('f'), None2: "toolchain not yet reported"})
|
|
r.b.Module(context.Background(), "n", data)
|
|
r.b.Wait()
|
|
if len(r.st.vals) != 0 {
|
|
t.Errorf("keys %v were written: a module with no new-form fingerprint or no artifact has nothing to build", r.st.vals)
|
|
}
|
|
}
|
|
|
|
func TestAKeyTheBuilderLeftBuildingIsBuiltAtStartAndDoneKeysAreNotBuiltTwice(t *testing.T) { // 1607/AC3, AC7
|
|
r := newRig(t, goBundle)
|
|
seed(t, r.st, delivery.BuilderBuild{State: delivery.BuildBuilding, By: "novox.builder", Manifest: manifestD})
|
|
if v := r.consider(t, "modules/m"); v.State != delivery.BuildDone {
|
|
t.Fatalf("build = %+v, want the builder's own building key built", v)
|
|
}
|
|
calls := len(r.bm.calls)
|
|
r.b.Module(context.Background(), "m", moduleValue(t, "modules/m"))
|
|
r.b.Reconsider(context.Background())
|
|
r.b.Wait()
|
|
if len(r.bm.calls) != calls {
|
|
t.Errorf("a done key was built again (%d calls, was %d)", len(r.bm.calls), calls)
|
|
}
|
|
}
|
|
|
|
func TestTwoInstancesBuildOneFingerprintOnce(t *testing.T) { // 1607/AC7
|
|
r := newRig(t, goBundle)
|
|
other := &Builder{Claimer: Claimer{Store: r.st, By: "ace.builder"}, Runner: r.b.Runner, Toolchains: r.tools, Manifests: r.mf, Nodes: 2, Logf: t.Logf}
|
|
value := moduleValue(t, "modules/m")
|
|
var wg sync.WaitGroup
|
|
for _, b := range []*Builder{r.b, other} {
|
|
wg.Add(1)
|
|
go func() { defer wg.Done(); b.Module(context.Background(), "m", value); b.Wait() }()
|
|
}
|
|
wg.Wait()
|
|
if n := len(r.bm.callsOf("build_go_build")); n != 2 { // one instance, two nodes
|
|
t.Errorf("%d builds, want 2: one instance built it", n)
|
|
}
|
|
}
|