Files
mesh-catalog/modules/mesh-build-node/cmd/mesh-build-node/push_test.go
T
jschoubben 22b92b827d BM publish version: the build modules' bundle asks name the ask's version, as the SDK now refuses one without
A bundle publishes under the version its ask reserved, and the delivery SDK's
publish refuses an empty one loudly; the tests' bundle asks carry a version, a
bundle asked with none is shown refused, and an archive still publishes by its
digest alone.

Builds: design 47 H2 H-A-373, H-A-373 addendum
2026-11-22 01:50:30 +01:00

227 lines
9.4 KiB
Go

package main
import (
"archive/tar"
"bytes"
"compress/gzip"
"context"
"crypto/sha256"
"encoding/base64"
"encoding/hex"
"encoding/json"
"errors"
"os"
"path/filepath"
"reflect"
"slices"
"strings"
"sync"
"testing"
registry "git.novox.be/novox/mesh-delivery/modules/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/modules/mesh-delivery-sdk/buildstep"
"git.novox.be/novox/mesh-delivery/modules/mesh-delivery-sdk/steprun"
"git.novox.be/novox/mesh-sdk/go/forgekey"
)
// pushed are the blobs the fake holder took, by digest, for the tests that read an archive back.
var pushed sync.Map
// fakeStore is the registry seats' holder as m's publish meets it (publish.go; CAT-BN3): publish opens a transfer and
// answers the part it lacks, publish-part takes the chunks in sequence, and the last one keeps the whole part when it
// hashes to its digest. Each file is kept by name and digest; fail, when set, is the holder's refusal at the last chunk.
type fakeStore struct {
mu sync.Mutex
blobs map[string][]byte // "<name>@<digest>"
fail error
open map[string]registry.PublishInput
chunks map[string][]byte
published []registry.PublishInput
seats []string
}
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()
raw, _ := json.Marshal(body)
seat, verb, _ := strings.Cut(strings.TrimPrefix(key, "seat:"), ".")
if seat != steprun.ArtifactSeat && seat != steprun.NpmSeat {
return nil, errors.New("nats: no responders available for request")
}
switch verb {
case "publish":
var in registry.PublishInput
_ = json.Unmarshal(raw, &in)
if (seat == steprun.ArtifactSeat) != (in.Kind == "bundle" || in.Kind == "archive") || in.Transfer == "" || strings.ContainsAny(in.Transfer, "/. *>") {
t.Errorf("publish on %s of kind %q, transfer %q", seat, in.Kind, in.Transfer)
}
s.open[in.Transfer] = in
s.seats = append(s.seats, seat)
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
}
if sum := sha256.Sum256(whole); "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 published, by its digest.
func archiveOf(t *testing.T, r BuildResult) []byte {
t.Helper()
raw, ok := pushed.Load(r.Digest)
if !ok {
t.Fatalf("nothing was published under %s", r.Digest)
}
return raw.([]byte)
}
// 1587-S6 (ADR 0498 §1; CAT-BN3): a done bundle is published through oci-artifact-registry and answers the seat's
// reference of its archive, under the manifest's module. Its publish names the version the ask reserved, so the bundle
// is marked against its reservation (design 47 H, H-A-372).
func TestADoneBundleAnswersTheReferenceOfTheArchiveItPushed(t *testing.T) {
m, _, _ := builtMachine(t, ``)
store := withStore(t, m)
args := buildArgs()
out, err := m.build(context.Background(), args)
if err != nil {
t.Fatal(err)
}
r := out.(BuildResult)
if want := "oci-artifact-registry/m/tools@" + r.Digest; r.Reference != want {
t.Errorf("reference %q, want %q", r.Reference, want)
}
if store.blobs["m/tools@"+r.Digest] == nil || len(store.seats) != 1 || store.seats[0] != steprun.ArtifactSeat {
t.Errorf("the holder holds %d files through %v, not m/tools@%s through %s", len(store.blobs), store.seats, r.Digest, steprun.ArtifactSeat)
}
if len(store.published) != 1 || store.published[0].Version != "1.4.3" {
t.Errorf("the bundle was published as %+v, want the ask's version 1.4.3", store.published)
}
// Design 47 H2 §800, H-A-403, H-A-427: an install runs no script and makes no link; its tree alone, under the module's
// directory, is published as the archive <module>:node-modules under the ask's version, and the end names its reference.
t.Run("an install runs no script", func(t *testing.T) {
m := &Module{Work: t.TempDir(), Toolchain: &Toolchain{Paths: Paths{Home: t.TempDir(), Cache: t.TempDir()},
Registry: func() (Registry, error) { return Registry{}, nil }}}
store := withStore(t, m)
var ran []string
var written map[string]any
m.Run = func(_ context.Context, _ []string, name string, args ...string) forgekey.Ran {
ran = append([]string{name}, args...)
dir := args[slices.Index(args, "--prefix")+1]
raw, _ := os.ReadFile(filepath.Join(dir, "package.json"))
_ = json.Unmarshal(raw, &written)
os.MkdirAll(filepath.Join(dir, "node_modules", "left-pad"), 0o755)
os.WriteFile(filepath.Join(dir, "node_modules", "left-pad", "index.js"), []byte("module.exports = 1\n"), 0o644)
return forgekey.Ran{}
}
lock := `{"lockfileVersion": 3, "packages": {"": {"name": "@novox/module-gitea", "scripts": {"install": "touch ran"},
"dependencies": {"left-pad": "^1.3.0"}}, "node_modules/left-pad": {"version": "1.3.0", "hasInstallScript": true}}}`
install := func(lock string) (any, error) {
return m.installTreeStep(context.Background(), map[string]any{"module": "gitea", "commit": strings.Repeat("a", 40), "fingerprint": "fp",
"pipeline": "p1", "lock": lock, "artifact": sdkstep.InstallArtifact("gitea"), "version": "0.4.2"})
}
out, err := install(lock)
if err != nil {
t.Fatal(err)
}
if !slices.Contains(ran, "--ignore-scripts") || !slices.Contains(ran, "--no-bin-links") || ran[0] != "npm" || ran[1] != "ci" {
t.Errorf("the install ran %v, want npm ci --ignore-scripts --no-bin-links", ran)
}
if _, scripts := written["scripts"]; scripts || written["name"] != "@novox/module-gitea" {
t.Errorf("the package.json beside the lock is %v, want the lock's root with no script", written)
}
if len(store.published) != 1 || store.published[0].Name != "gitea/node-modules" || store.published[0].Kind != "archive" || store.published[0].Version != "0.4.2" {
t.Fatalf("published %+v, want gitea/node-modules of kind archive at 0.4.2", store.published)
}
digest := store.published[0].Digest
if want := map[string]any{"archive": "oci-artifact-registry/gitea/node-modules@" + digest}; !reflect.DeepEqual(out, want) {
t.Errorf("the install answered %v, want %v", out, want)
}
if names := archiveNames(t, store.blobs["gitea/node-modules@"+digest]); !slices.Equal(names, []string{"modules/gitea/node_modules/left-pad/index.js"}) {
t.Errorf("the archive holds %v, want the tree alone under the module's directory", names)
}
if _, err := install(`{"lockfileVersion": 1, "dependencies": {}}`); err == nil {
t.Error("a lock with no root package was installed")
}
})
}
// archiveNames are the file names a packed archive holds (steprun.Pack: tar in gzip).
func archiveNames(t *testing.T, archive []byte) []string {
t.Helper()
z, err := gzip.NewReader(bytes.NewReader(archive))
if err != nil {
t.Fatal(err)
}
var names []string
r := tar.NewReader(z)
for {
h, err := r.Next()
if err != nil {
break
}
names = append(names, h.Name)
}
return names
}
// 1587-S7 and S8 (ADR 0498 §2, the lead's decision L9): a publish the holder refuses fails the step `not stored:
// <artifact>: <the holder's words>`, a seat nothing holds too, and whatever it answered, no packed file is left.
func TestARefusedPushFailsNotStoredAndNoPackedFileStays(t *testing.T) {
for _, c := range []struct {
fail error
noSeat bool
words string
}{{nil, false, ""}, {errors.New("refused: the store did not take it: 507 Insufficient Storage"), false, "507"},
{nil, true, "no responders"}} {
m, _, work := builtMachine(t, ``)
withStore(t, m).fail = c.fail
if c.noSeat {
m.Call = func(string, any) (json.RawMessage, error) {
return nil, errors.New("nats: no responders available for request")
}
}
_, err := m.build(context.Background(), buildArgs())
if c.words != "" && (err == nil || !strings.HasPrefix(err.Error(), builds.NotStored+"tools: ") || !strings.Contains(err.Error(), c.words)) {
t.Errorf("a refused publish answered %v", err)
}
if c.noSeat && (err == nil || !sdkstep.IsPassing(err) || !strings.Contains(err.Error(), "publish: ")) {
// A publish met by no responders is a transport fault with no seat call to learn why (design 47 H2 §799).
t.Errorf("no responders answered %v, want a transport fault opening publish:", err)
}
var archives []string
filepath.WalkDir(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("publish answered %v: packed files left on the node: %v", c.fail, archives)
}
}
}