Files
mesh-catalog/modules/mesh-build-go/cmd/mesh-build-go/publish_test.go
T
jschoubben 357967308f BLD11: the three build modules take the version from the ask as MESH_VERSION; version.numbered is no longer heard
The Go compile, the Node tsc/esbuild/npm and the image's buildah bud get the
build argument MESH_VERSION from the version the build ask carries. The late
publish on version.numbered goes (code, tests, the consumes of the Go and
image manifests). The image's label and tag and a program's stamp are open
(question to the lead): the design names no key or symbol.

Builds: design 47 G §349, §370, §413

Claude-Session: https://claude.ai/code/session_01JG73KEq4zoDfM9WtYpcQgZ
2026-11-11 11:00:52 +01:00

316 lines
14 KiB
Go

package main
import (
"archive/zip"
"bytes"
"crypto/sha256"
"encoding/base64"
"encoding/hex"
"encoding/json"
"errors"
"os"
"path/filepath"
"strings"
"sync"
"testing"
"git.novox.be/novox/mesh-catalog/modules/mesh-build-go/buildstep"
registry "git.novox.be/novox/mesh-delivery/contracts/go/seats/containerimageregistry"
builds "git.novox.be/novox/mesh-delivery/modules/mesh-delivery-builder/builds"
sdkstep "git.novox.be/novox/mesh-delivery/sdk/buildstep"
)
// pushed are the blobs the fake artifact store took, by digest, for the tests that read an archive back.
var pushed sync.Map
// fakeStore is the seat oci-artifact-registry's holder as m's publish meets it (publish.go): publish opens a transfer
// and answers the part it lacks, publish-part takes the chunks in sequence, and the last one stores the whole part
// when it hashes to its digest. Each blob is kept by name and digest; fail, when set, is the holder's refusal at the
// last chunk; asked are the addresses m called.
type fakeStore struct {
mu sync.Mutex
blobs map[string][]byte // "<name>@<digest>"
fail error
open map[string]registry.PublishInput
chunks map[string][]byte
asked []string
// published are the publishes whose last chunk was stored, in order.
published []registry.PublishInput
}
func withStore(t *testing.T, m *Module) *fakeStore {
t.Helper()
s := &fakeStore{blobs: map[string][]byte{}, open: map[string]registry.PublishInput{}, chunks: map[string][]byte{}}
m.Call = func(key string, body any) (json.RawMessage, error) {
s.mu.Lock()
defer s.mu.Unlock()
s.asked = append(s.asked, key)
raw, _ := json.Marshal(body)
seat, verb, _ := strings.Cut(strings.TrimPrefix(key, "seat:"), ".")
if seat != ArtifactSeat && seat != GoPackageSeat {
return nil, errors.New("nats: no responders available for request")
}
switch verb {
case "publish":
var in registry.PublishInput
_ = json.Unmarshal(raw, &in)
if (seat == ArtifactSeat) != (in.Kind == "bundle" || in.Kind == "archive") || in.Transfer == "" || strings.ContainsAny(in.Transfer, "/. *>") {
t.Errorf("publish of kind %q, transfer %q", in.Kind, in.Transfer)
}
s.open[in.Transfer] = in
// A digest held under the name already travels no more (the late publish, CAT-BG2).
if _, held := s.blobs[in.Name+"@"+in.Digest]; held {
s.published = append(s.published, in)
return json.Marshal(registry.PublishAnswer{Transfer: in.Transfer, Held: true})
}
return json.Marshal(registry.PublishAnswer{Transfer: in.Transfer, Lacks: []string{in.Digest}})
case "publish-part":
var c registry.PublishPartInput
_ = json.Unmarshal(raw, &c)
data, _ := base64.StdEncoding.DecodeString(c.Data)
s.chunks[c.Transfer] = append(s.chunks[c.Transfer], data...)
if !c.Last {
return json.Marshal(registry.PublishPartAnswer{Transfer: c.Transfer, Part: c.Part})
}
in, whole := s.open[c.Transfer], s.chunks[c.Transfer]
delete(s.chunks, c.Transfer)
if s.fail != nil {
return nil, s.fail
}
sum := sha256.Sum256(whole)
if "sha256:"+hex.EncodeToString(sum[:]) != in.Digest {
t.Errorf("published %s with bytes of another digest", in.Digest)
}
s.blobs[in.Name+"@"+in.Digest] = whole
s.published = append(s.published, in)
pushed.Store(in.Digest, whole)
return json.Marshal(registry.PublishPartAnswer{Transfer: c.Transfer, Part: c.Part, Complete: true})
}
return nil, errors.New("nats: no responders available for request")
}
return s
}
// archiveOf is the archive a build pushed, by its digest.
func archiveOf(t *testing.T, res BuildResult) []byte {
t.Helper()
raw, ok := pushed.Load(res.Digest)
if !ok {
t.Fatalf("nothing was pushed under %s", res.Digest)
}
return raw.([]byte)
}
// 1587-S2 (ADR 0498 §1): a done build answers a reference of the stated form whose tail is the build's digest, and the
// store holds the archive there; a bundle and an archive alike.
func TestADoneBuildAnswersTheReferenceOfTheArchiveItPushed(t *testing.T) {
m, states, _ := buildModule(t, manifestWith(goBundle), "the program")
store := withStore(t, m)
res, st := runBuild(t, m, states, "tools")
if st.State != sdkstep.StepPassed {
t.Fatalf("the step ended %s: %s", st.State, st.Reason)
}
if want := "oci-artifact-registry/demo/tools@" + res.Digest; res.Reference != want {
t.Errorf("reference %q, want %q", res.Reference, want)
}
if store.blobs["demo/tools@"+res.Digest] == nil || store.asked[0] != "seat:oci-artifact-registry.publish" {
t.Errorf("the seat was asked %v and holds %d blobs, not demo/tools@%s", store.asked, len(store.blobs), res.Digest)
}
a, states2, _ := archiveModule(t, "")
withStore(t, a)
arch, st := runBuild(t, a, states2, "files")
if st.State != sdkstep.StepPassed || arch.Reference != "oci-artifact-registry/demo/files@"+arch.Digest {
t.Errorf("the archive artifact: %s %q, reference %q", st.State, st.Reason, arch.Reference)
}
}
// 1587-S3 (ADR 0498 §2), CAT-BG1: a publish the seat's holder refuses fails the step `not stored: <artifact>: <the
// holder's words>`, and so does a seat nothing holds.
func TestARefusedPushFailsTheStepNotStored(t *testing.T) {
m, states, _ := buildModule(t, manifestWith(goBundle), "the program")
store := withStore(t, m)
store.fail = errors.New("refused: the forge did not take it: disk full")
if _, st := runBuild(t, m, states, "tools"); st.State != sdkstep.StepFailed || !strings.HasPrefix(st.Reason, builds.NotStored+"tools: ") || !strings.Contains(st.Reason, "disk full") {
t.Errorf("the step ended %s: %q", st.State, st.Reason)
}
m.Call = func(string, any) (json.RawMessage, error) {
return nil, errors.New("nats: no responders available for request")
}
if _, st := runBuild(t, m, states, "tools"); st.State != sdkstep.StepFailed || !strings.HasPrefix(st.Reason, builds.NotStored+"tools: ") ||
!strings.Contains(st.Reason, "oci-artifact-registry") {
t.Errorf("no holder: the step ended %s: %q", st.State, st.Reason)
}
// ADR 0664 §1 (issue 1748): "no responders" is the same words for a seat nothing holds and for a holder whose tool runner is
// being replaced; the assigner says which. A named holder makes it a transport fault (the bus gives the ask again), none
// fails at once naming it.
for _, c := range []struct {
name, holders string
passing bool
words string
}{
{"a holder named", `{"seats":[{"seat":"oci-artifact-registry","holders":[{"module":"mesh-registry","node":"novox"}]}],"holds":1}`, true, "the holder is silent: seat oci-artifact-registry, held by mesh-registry on novox"},
{"nothing holds it", `{"seats":[{"seat":"go-package-registry","holders":[{"module":"mesh-registry","node":"novox"}]}],"holds":1}`, false, "nothing holds oci-artifact-registry"},
} {
m.Call = func(key string, _ any) (json.RawMessage, error) {
if key == buildstep.AssignerHolders {
return json.RawMessage(c.holders), nil
}
return nil, errors.New("nats: no responders available for request")
}
if _, st := runBuild(t, m, states, "tools"); st.State != sdkstep.StepFailed || st.Passing != c.passing || !strings.Contains(st.Reason, c.words) {
t.Errorf("%s: the step ended %s: %q (passing %v)", c.name, st.State, st.Reason, st.Passing)
}
}
}
// 1587-S4 (the lead's decision L9): the packed file is not left on the node once its push answered, a candidate's
// included: the workspace holds no archive, pushed or refused.
func TestThePackedFileLeavesTheNodeOnceItsPushAnswered(t *testing.T) {
for _, fail := range []error{nil, errors.New("refused")} {
m, states, _ := buildModule(t, manifestWith(goBundle), "the program")
withStore(t, m).fail = fail
runBuild(t, m, states, "tools")
var archives []string
filepath.WalkDir(m.Work, func(p string, d os.DirEntry, err error) error {
if err == nil && strings.HasSuffix(p, ".tar.gz") {
archives = append(archives, p)
}
return nil
})
if len(archives) != 0 {
t.Errorf("push answered %v: archives left on the node: %v", fail, archives)
}
}
}
// CAT-BG2 (REG S23, ADR 0605 §3, §9; the lead's ruling 3 on A3k): a Go package is built as Go's module zip of the
// directory its artifact names, every file under <module path>@<version>/, its digest the zip's.
// A trunk build given a version publishes it through go-package-registry under its Go module path and that version,
// and answers <module path>@<version>; a candidate's (no version) publishes nothing and answers its digest alone. A
// directory with no go.mod fails the step naming it.
func TestAGoPackageIsBuiltAsAModuleZipAndPublishedUnderItsModulePath(t *testing.T) {
const pkg = `{"name":"go","kind":"package","language":"go","paths":["go"],"series":"0.1"}`
m, states, _ := buildModule(t, manifestWith(pkg), "unused")
src := filepath.Join(m.Work, "ws1", "tree", "modules", "demo", "go")
for name, body := range map[string]string{"go.mod": "module git.example/demo/go\n\ngo 1.26\n", "a.go": "package demo\n",
"vendor/x/x.go": "package x\n"} {
if err := os.MkdirAll(filepath.Dir(filepath.Join(src, name)), 0o755); err != nil {
t.Fatal(err)
}
if err := os.WriteFile(filepath.Join(src, name), []byte(body), 0o644); err != nil {
t.Fatal(err)
}
}
run := func(args map[string]any) (BuildResult, sdkstep.Step) {
t.Helper()
for _, tool := range m.Tools(m.StepTools()) {
if tool.Name == "build_go_build" {
if _, err := tool.Run(args); err != nil {
t.Fatal(err)
}
}
}
st := states.waitEnd(t)
var res BuildResult
if st.State == sdkstep.StepPassed {
_ = json.Unmarshal(st.Result, &res)
}
return res, st
}
store := withStore(t, m)
args := map[string]any{"workspace": "ws1", "module": "modules/demo", "artifact": "go"}
cand, st := run(args)
if st.State != sdkstep.StepPassed || !strings.HasPrefix(cand.Digest, "sha256:") || cand.Reference != "" || len(store.asked) != 0 {
t.Fatalf("a candidate's Go package: %s %q, %+v, asked %v", st.State, st.Reason, cand, store.asked)
}
args["version"], args["seat"] = "v0.1.15", GoPackageSeat
res, st := run(args)
if st.State != sdkstep.StepPassed || res.Reference != "git.example/demo/go@v0.1.15" {
t.Fatalf("a trunk Go package: %s %q, %+v", st.State, st.Reason, res)
}
if len(store.published) != 1 {
t.Fatalf("published %+v", store.published)
}
in := store.published[0]
if in.Name != "git.example/demo/go" || in.Version != "v0.1.15" || in.Kind != "package" || in.Digest != res.Digest {
t.Errorf("the publish: %+v", in)
}
z, err := zip.NewReader(bytes.NewReader(store.blobs[in.Name+"@"+in.Digest]), int64(len(store.blobs[in.Name+"@"+in.Digest])))
if err != nil {
t.Fatal(err)
}
var names []string
for _, f := range z.File {
names = append(names, f.Name)
}
if strings.Join(names, " ") != "git.example/demo/go@v0.1.15/a.go git.example/demo/go@v0.1.15/go.mod" {
t.Errorf("the zip holds %v", names)
}
if err := os.Remove(filepath.Join(src, "go.mod")); err != nil {
t.Fatal(err)
}
if _, st := run(args); st.State != sdkstep.StepFailed || !strings.Contains(st.Reason, "go.mod") {
t.Errorf("no go.mod: %s %q", st.State, st.Reason)
}
}
// K-2 (ADR 0634 §8): a module in a folder of the repository, the folder holding more than one Go package, builds and
// publishes each as a root module's would: each artifact its own zip of its own directory under its own module path,
// the sibling package's directory (a Go module of its own) left out of the zip of the folder's.
func TestEachGoPackageOfAModuleInAFolderIsPublishedUnderItsOwnModulePath(t *testing.T) {
manifest := manifestWith(`{"name":"go-aone","kind":"package","language":"go","paths":["go/seats/aone"],"series":"0.1"},
{"name":"go-btwo","kind":"package","language":"go","paths":["go/seats/btwo"],"series":"0.1"},
{"name":"go-whole","kind":"package","language":"go","paths":["go"],"series":"0.1"}`)
m, states, _ := buildModule(t, manifest, "unused")
src := filepath.Join(m.Work, "ws1", "tree", "domain-a", "contracts")
for name, body := range map[string]string{
"module.json": manifest,
"go/seats/aone/go.mod": "module git.example/c/domain-a/go/seats/aone\n\ngo 1.26\n", "go/seats/aone/a.go": "package aone\n",
"go/seats/btwo/go.mod": "module git.example/c/domain-a/go/seats/btwo\n\ngo 1.26\n", "go/seats/btwo/b.go": "package btwo\n",
"go/go.mod": "module git.example/c/domain-a/go\n\ngo 1.26\n", "go/w.go": "package w\n"} {
if err := os.MkdirAll(filepath.Dir(filepath.Join(src, name)), 0o755); err != nil {
t.Fatal(err)
}
if err := os.WriteFile(filepath.Join(src, name), []byte(body), 0o644); err != nil {
t.Fatal(err)
}
}
store := withStore(t, m)
for _, c := range []struct{ artifact, ref string }{
{"go-aone", "git.example/c/domain-a/go/seats/aone@v0.1.3"},
{"go-btwo", "git.example/c/domain-a/go/seats/btwo@v0.1.3"},
{"go-whole", "git.example/c/domain-a/go@v0.1.3"},
} {
for _, tool := range m.Tools(m.StepTools()) {
if tool.Name == "build_go_build" {
args := map[string]any{"workspace": "ws1", "module": "domain-a/contracts", "artifact": c.artifact, "version": "v0.1.3", "seat": GoPackageSeat}
if _, err := tool.Run(args); err != nil {
t.Fatal(err)
}
}
}
st := states.waitEnd(t)
var res BuildResult
if st.State != sdkstep.StepPassed {
t.Fatalf("%s: %s %q", c.artifact, st.State, st.Reason)
}
_ = json.Unmarshal(st.Result, &res)
if res.Reference != c.ref {
t.Errorf("%s published as %q, want %q", c.artifact, res.Reference, c.ref)
}
}
if len(store.published) != 3 {
t.Fatalf("published %+v", store.published)
}
in := store.published[2]
z, err := zip.NewReader(bytes.NewReader(store.blobs[in.Name+"@"+in.Digest]), int64(len(store.blobs[in.Name+"@"+in.Digest])))
if err != nil {
t.Fatal(err)
}
for _, f := range z.File {
if strings.Contains(f.Name, "/seats/") {
t.Errorf("the zip of the folder's own package holds %s, a package of its own", f.Name)
}
}
}