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

291 lines
14 KiB
Go

package main
import (
"context"
"encoding/json"
"errors"
"os"
"path/filepath"
"slices"
"strings"
"testing"
"time"
seat "git.novox.be/novox/mesh-contracts/health-and-repair/go/seats/meshconditions"
catalogclient "git.novox.be/novox/mesh-delivery/modules/mesh-delivery-contracts/go/clients/meshcatalogclient"
meshcat "git.novox.be/novox/mesh-delivery/modules/mesh-delivery-contracts/go/seats/meshcatalog"
sdkstep "git.novox.be/novox/mesh-delivery/modules/mesh-delivery-sdk/buildstep"
"git.novox.be/novox/mesh-delivery/modules/mesh-delivery-sdk/composer"
"git.novox.be/novox/mesh-sdk/go/forgekey"
)
// nodeEngineComposer is the composer command of the node-engine's repository, whose Go module is at its root (ADR 0593 §7):
// a group that changes the node-engine's composition is composed by this program built at the group's head.
const nodeEngineComposer = "github.com/novox/mesh-host/cmd/mesh-compose"
const composeCommit = "4444444444444444444444444444444444444444"
// fingerprintOf is the fingerprint the catalog graph's entry of a manifest's digest is kept under in these tests.
func fingerprintOf(digest string) string { return "fp-" + digest }
// catalogHolds is the seat mesh-catalog's entries, each body's under its fingerprint, with its manifest and the manifest's
// digest as the indexer writes them (design 47 G §506); it answers the digests.
func catalogHolds(t *testing.T, bodies ...string) (func(string) (meshcat.CatalogEntry, error), []string) {
t.Helper()
kept, digests := map[string]meshcat.CatalogEntry{}, []string{}
for _, b := range bodies {
d := catalogclient.ManifestDigest([]byte(b))
var whole map[string]any
if err := json.Unmarshal([]byte(b), &whole); err != nil {
t.Fatal(err)
}
kept[fingerprintOf(d)] = meshcat.CatalogEntry{Module: "demo", Fp: fingerprintOf(d), Manifest: whole, ManifestDigest: "sha256:" + d}
digests = append(digests, d)
}
return func(fp string) (meshcat.CatalogEntry, error) {
if e, ok := kept[fp]; ok {
return e, nil
}
return meshcat.CatalogEntry{}, errors.New("the seat keeps no entry of " + fp)
}, digests
}
func composeAskOf(t *testing.T, digest string) sdkstep.StepAsk {
t.Helper()
req, _ := json.Marshal(composer.Request{V: composer.Version, Cases: []composer.Case{{Name: "change",
Manifests: map[string]composer.Manifest{"demo": {Digest: digest, Fingerprint: fingerprintOf(digest)}}}}})
return sdkstep.StepAsk{ID: sdkstep.NewID(), Kind: sdkstep.AskCheck, Language: sdkstep.AskGo, Asker: "mesh-delivery-assigner",
At: time.Now(), Compose: &sdkstep.ComposeJob{Fetch: sdkstep.Fetch{Repository: "novox/mesh-sdk", Commit: composeCommit},
Dir: "go", Package: composer.Package, Request: req}}
}
// composeModule is a module with work and cache directories of its own, running its commands as they are (Inside: a
// unit test has no bubblewrap to shut them off in).
func composeModule(t *testing.T) *Module {
t.Helper()
m := moduleWith(newStates(), "go1.26.0")
m.Work, m.Cache, m.Inside, m.Run = t.TempDir(), t.TempDir(), true, StepRunner(t.TempDir())
return m
}
// 1622/AC3: a composer kept under the ask's key is run again, nothing fetched or built: the answer says it was not
// built, names the key, and carries the composer's verdict whole; the request it read had its manifests filled from the
// indexer's state.
func TestAKeptComposerIsRunWithTheManifestsFilled(t *testing.T) {
m := composeModule(t)
read, digests := catalogHolds(t, `{"module":"demo","version":"1"}`)
m.ReadEntry = read
a := composeAskOf(t, digests[0])
key := sdkstep.ComposerKey("novox/mesh-sdk", composeCommit, "go", composer.Package, sdkstep.ToolchainUsed{Version: "go1.26.0", Recipe: m.Recipe.Version()}, nil)
exe := m.composerPath(key)
if err := os.MkdirAll(filepath.Dir(exe), 0o700); err != nil {
t.Fatal(err)
}
// A composer that answers which manifests its request held, filled or not, as a verdict.
script := "#!/bin/sh\n" +
`if grep -q '"digest"' "$2"; then echo '{"v":1,"sdk":"stand-in","cases":[{"name":"unfilled","manifests":{}}]}'; ` +
`else echo '{"v":1,"sdk":"stand-in","cases":[{"name":"change","manifests":{"demo":{"accepted":true}}}]}'; fi` + "\n"
if err := os.WriteFile(exe, []byte(script), 0o700); err != nil {
t.Fatal(err)
}
fetched := 0
fetch := func(context.Context, map[string]any) (any, error) { fetched++; return nil, nil }
// Issue 1966: the request arrives gzip-packed, as the checker sends one over the bus's message limit.
if err := a.Compose.PackRequest(a.Compose.Request); err != nil {
t.Fatal(err)
}
out, err := m.compose(context.Background(), fetch, a)
if err != nil {
t.Fatal(err)
}
ans := out.(sdkstep.ComposeAnswer)
var v composer.Verdict
if err := json.Unmarshal(ans.Verdict, &v); err != nil {
t.Fatal(err)
}
if ans.Built || ans.Key != key || fetched != 0 || len(v.Cases) != 1 || v.Cases[0].Name != "change" || !v.Cases[0].Manifests["demo"].Accepted {
t.Fatalf("answer = %+v (fetched %d), verdict %+v", ans, fetched, v)
}
if entries, _ := os.ReadDir(m.Work); len(entries) != 0 {
t.Errorf("the run's workspace was left: %v", entries)
}
}
// 1921: the versions an ask names are an input of the composer it builds, so a composer kept for one set is not run for
// another; an ask naming none keeps the key it always had.
func TestAComposerIsKeptPerAskedVersions(t *testing.T) {
m := composeModule(t)
m.ReadEntry, _ = catalogHolds(t)
a := composeAskOf(t, "feedfeedfeedfeedfeedfeedfeedfeedfeedfeedfeedfeedfeedfeedfeedfeed")
tc := sdkstep.ToolchainUsed{Version: "go1.26.0", Recipe: m.Recipe.Version()}
plain := sdkstep.ComposerKey("novox/mesh-sdk", composeCommit, "go", composer.Package, tc, nil)
a.Compose.Fetch.Packages = sdkstep.PackageVersions{"git.novox.be/novox/mesh-sdk/go": strings.Repeat("3", 40)}
named := sdkstep.ComposerKey("novox/mesh-sdk", composeCommit, "go", composer.Package, tc, a.Compose.Fetch.Packages)
if plain == named {
t.Fatal("the key ignores the versions")
}
// A composer kept under the key without versions is not used for the ask that names some: it is fetched.
os.MkdirAll(filepath.Dir(m.composerPath(plain)), 0o700)
os.WriteFile(m.composerPath(plain), []byte("#!/bin/sh\nexit 0\n"), 0o700)
fetched := false
fetch := func(context.Context, map[string]any) (any, error) {
fetched = true
return nil, errors.New("stop")
}
m.compose(context.Background(), fetch, a)
if !fetched {
t.Error("a composer built for no versions was run for an ask naming some")
}
}
// 1622: a manifest the indexer does not keep fails the ask, naming its digest; a fetched commit with no go.mod where
// the ask builds fails it naming the directory, and its workspace is released.
func TestAComposeAskFailsLoudly(t *testing.T) {
m := composeModule(t)
m.ReadEntry, _ = catalogHolds(t)
a := composeAskOf(t, "feedfeedfeedfeedfeedfeedfeedfeedfeedfeedfeedfeedfeedfeedfeedfeed")
key := sdkstep.ComposerKey("novox/mesh-sdk", composeCommit, "go", composer.Package, sdkstep.ToolchainUsed{Version: "go1.26.0", Recipe: m.Recipe.Version()}, nil)
os.MkdirAll(filepath.Dir(m.composerPath(key)), 0o700)
os.WriteFile(m.composerPath(key), []byte("#!/bin/sh\nexit 0\n"), 0o700)
if _, err := m.compose(context.Background(), nil, a); err == nil || !strings.Contains(err.Error(), "feedfeed") {
t.Errorf("an unkept manifest: %v", err)
}
m2 := composeModule(t)
fetch := func(context.Context, map[string]any) (any, error) {
if err := os.MkdirAll(filepath.Join(m2.Work, "ws0000000000a", "tree", "go"), 0o700); err != nil {
return nil, err
}
return map[string]any{"workspace": "ws0000000000a"}, nil
}
if _, err := m2.compose(context.Background(), fetch, composeAskOf(t, "x")); err == nil || !strings.Contains(err.Error(), "no go.mod in go") {
t.Errorf("a commit with no go.mod: %v", err)
}
if entries, _ := os.ReadDir(m2.Work); len(entries) != 0 {
t.Errorf("the fetched workspace was left: %v", entries)
}
}
// Design 47 G §615: the module reads the manifests a compose ask names from the catalog graph's entry, by the seat's verb
// entry, and invokes no tool of the delivery indexer. 1921 (B0): a run's input is asked of the checker seat's verb input, as
// the manifest invokes it, and no state of the checker's inputs is read (ADR 0596 §2).
func TestTheModuleReadsTheCatalogEntries(t *testing.T) {
m := readManifest(t)
if !slices.Contains(m.Invokes, catalogclient.EntryVerb) || slices.ContainsFunc(m.Invokes, func(i string) bool { return strings.HasPrefix(i, "mesh-delivery-indexer") }) {
t.Errorf("invokes = %v: want %s and no tool of the indexer", m.Invokes, catalogclient.EntryVerb)
}
if !slices.Contains(m.Invokes, runInputVerb) || slices.Contains(m.Reads, "mesh-delivery-checker.inputs") {
t.Errorf("invokes = %v, reads = %v: want %s invoked and no inputs state read", m.Invokes, m.Reads, runInputVerb)
}
}
// Design 47 G §615, A-310: a manifest named by a digest is taken whole from the entry of the fingerprint beside it; a digest
// with no fingerprint, and an entry holding another digest, refuse the request, naming the manifest's module.
func TestAManifestComesWholeFromItsEntryAndAnyMismatchRefuses(t *testing.T) {
read, digests := catalogHolds(t, `{"module":"demo","version":"1"}`, `{"module":"demo","version":"2"}`)
m := composeModule(t)
m.ReadEntry = read
for _, c := range []struct {
name string
man composer.Manifest
want string // empty: the manifest whole
}{
{"the entry's manifest", composer.Manifest{Digest: digests[0], Fingerprint: fingerprintOf(digests[0])}, ""},
{"a digest with no fingerprint", composer.Manifest{Digest: digests[0]}, "no fingerprint"},
{"an entry holding another digest", composer.Manifest{Digest: digests[0], Fingerprint: fingerprintOf(digests[1])}, "not the digest"},
{"a fingerprint no entry has", composer.Manifest{Digest: digests[0], Fingerprint: "fp-none"}, "keeps no entry"},
} {
req := composer.Request{V: composer.Version, Cases: []composer.Case{{Name: "change", Manifests: map[string]composer.Manifest{"demo": c.man}}}}
err := composer.Fill(&req, m.manifestOf)
switch {
case c.want == "" && (err != nil || string(req.Cases[0].Manifests["demo"].Raw) != `{"module":"demo","version":"1"}`):
t.Errorf("%s: %v, %s", c.name, err, req.Cases[0].Manifests["demo"].Raw)
case c.want != "" && (err == nil || !strings.Contains(err.Error(), c.want) || !strings.Contains(err.Error(), "demo")):
t.Errorf("%s: %v", c.name, err)
}
}
}
// ADR 0615 §6: a seat that does not answer is a finding of the module's own, and its next answer clears it.
func TestACatalogThatDoesNotAnswerIsAFinding(t *testing.T) {
// Design 47 G §7 (BG12, BLD16): the finding names the module as the runtime does, never a fixed name.
t.Setenv("MESH_MODULE", "mesh-build-go-renamed")
if f := unansweredEntry("no answer", time.Unix(0, 0), time.Unix(60, 0)); f.Subject.ID != "mesh-build-go-renamed" || f.Headline != "mesh-build-go-renamed waits for the seat mesh-catalog" {
t.Fatalf("the finding names %q, %q", f.Subject.ID, f.Headline)
}
held := map[string][]byte{}
f := mapFindings(held)
at := func() time.Time { return time.Date(2026, 11, 4, 12, 0, 0, 0, time.UTC) }
if _, err := askEntryVia(func(string, any) (json.RawMessage, error) { return nil, errors.New("no responders") }, f, at, "fp"); err == nil || len(held) != 1 {
t.Fatalf("err %v, findings %v", err, held)
}
e, err := askEntryVia(func(string, any) (json.RawMessage, error) {
return json.RawMessage(`{"entry":{"module":"demo","repository":"r","commit":"c","manifest":{"x":1},"manifest-digest":"sha256:ab"}}`), nil
}, f, at, "fp")
if err != nil || e.Module != "demo" || e.ManifestDigest != "sha256:ab" || len(held) != 0 {
t.Errorf("entry %+v, err %v, findings %v", e, err, held)
}
}
type mapFindings map[string][]byte
func (m mapFindings) Write(_ context.Context, f seat.Finding) error {
m[f.ID+"."+f.Kind] = []byte(f.Summary)
return nil
}
func (m mapFindings) Clear(_ context.Context, _, id, kind, _ string) error {
delete(m, id+"."+kind)
return nil
}
// ADR 0593 §7 (C9): a compose ask naming the node-engine's repository builds its composer command in the Go module at the
// repository's root, under a key of that repository, commit and package, and runs the program over the request with its
// manifests filled; the module's answer is the program's verdict whole.
func TestAComposeAskBuildsTheNodeEnginesComposerAtItsRoot(t *testing.T) {
m := composeModule(t)
read, digests := catalogHolds(t, `{"module":"demo","version":"1"}`)
m.ReadEntry = read
a := composeAskOf(t, digests[0])
a.Compose.Fetch.Repository, a.Compose.Dir, a.Compose.Package = "novox/mesh-host", catalogclient.RootPath, nodeEngineComposer
tree := ""
var built []string
real := m.Run
m.Run = func(ctx context.Context, env []string, name string, args ...string) forgekey.Ran {
if name != "go" {
return real(ctx, env, name, args...)
}
built = args
for i, arg := range args {
if arg == "-o" {
script := "#!/bin/sh\necho '{\"v\":1,\"sdk\":\"node-engine\",\"cases\":[{\"name\":\"change\",\"manifests\":{\"demo\":{\"accepted\":true}}}]}'\n"
if err := os.WriteFile(args[i+1], []byte(script), 0o700); err != nil {
return forgekey.Ran{Status: 1, Stderr: err.Error()}
}
}
}
return forgekey.Ran{}
}
fetch := func(context.Context, map[string]any) (any, error) {
tree = filepath.Join(m.Work, "ws0000000000b", "tree")
if err := os.MkdirAll(tree, 0o700); err != nil {
return nil, err
}
return map[string]any{"workspace": "ws0000000000b"}, os.WriteFile(filepath.Join(tree, "go.mod"), []byte("module github.com/novox/mesh-host\n"), 0o600)
}
out, err := m.compose(context.Background(), fetch, a)
if err != nil {
t.Fatal(err)
}
ans := out.(sdkstep.ComposeAnswer)
key := sdkstep.ComposerKey("novox/mesh-host", composeCommit, catalogclient.RootPath, nodeEngineComposer, sdkstep.ToolchainUsed{Version: "go1.26.0", Recipe: m.Recipe.Version()}, nil)
var v composer.Verdict
if err := json.Unmarshal(ans.Verdict, &v); err != nil {
t.Fatal(err)
}
if len(built) < 3 || built[0] != "-C" || built[1] != tree || built[len(built)-1] != nodeEngineComposer {
t.Errorf("go ran as %v, want it to build %s in the tree's root %s", built, nodeEngineComposer, tree)
}
if !ans.Built || ans.Key != key || v.SDK != "node-engine" || !v.Cases[0].Manifests["demo"].Accepted {
t.Errorf("answer %+v, verdict %+v, want built under %s and demo accepted", ans, v, key)
}
}