Files
mesh-catalog/modules/mesh-build-image/cmd/mesh-build-image/copy_test.go
T
jschoubben e4ef67adcd MV1: move contracts and sdk under modules/, importers follow
The delivery repository's contracts and SDK live under modules/ as
mesh-delivery-contracts and mesh-delivery-sdk, their Go module paths
renamed to match the folders (the TypeScript names already did), and
every importer in mesh-delivery, mesh-catalog, mesh-host, mesh-tools
and mesh-sdk follows in the same group: a design as it should be, one
path per module, checked by the group's check building every importer.

Builds: design 47 G §427, §88

Claude-Session: https://claude.ai/code/session_01NcbgfGKaxw984XyAPfLm4h
2026-11-12 05:09:56 +01:00

98 lines
4.4 KiB
Go

package main
import (
"encoding/json"
"strings"
"testing"
"time"
reg "git.novox.be/novox/mesh-delivery/modules/mesh-delivery-contracts/go/seats/containerimageregistry"
sdkstep "git.novox.be/novox/mesh-delivery/modules/mesh-delivery-sdk/buildstep"
)
func runCopy(t *testing.T, m *Module, states *keptStates, args map[string]any) (CopyResult, sdkstep.Step) {
t.Helper()
if _, err := tool(t, m, CopyTool)(args); err != nil {
t.Fatal(err)
}
st := states.waitEnd(t)
var res CopyResult
if st.State == sdkstep.StepPassed {
if err := json.Unmarshal(st.Result, &res); err != nil {
t.Fatal(err)
}
}
return res, st
}
// ADR 0498 §4, design 51 (CAT-BI1): an upstream copy is the image seat's mirror by the pinned digest; the step ends on
// the holder's outcome on the seat's events, answering its reference, and runs no command. A refusal, no outcome in the
// bound and no pin each fail the step, the last before the seat is asked.
func TestTheCopyIsTheImageSeatsMirrorAndEndsOnItsOutcome(t *testing.T) {
d := "sha256:" + strings.Repeat("a", 64)
from := "nats:2.11.17-alpine@" + d
for name, c := range map[string]struct {
from string
mirrored func(reg.MirrorInput) (string, reg.Outcome)
reason string
// elsewhere, watched, atOnce: who hears the outcome and when (the fake's fields of the same names).
elsewhere, watched, atOnce bool
}{
"published": {from: from},
"heard by another instance, handed over by the watch of mirror-waits": {from: from, elsewhere: true, watched: true},
"heard by another instance before this copy read its wait": {from: from, elsewhere: true, atOnce: true},
"heard by another instance that refused it": {from: from, elsewhere: true, atOnce: true, mirrored: func(in reg.MirrorInput) (string, reg.Outcome) {
return reg.EventRefused, reg.Outcome{Kind: "upstream", Digest: in.Digest, Reason: "the upstream answered 404"}
}, reason: "refused the copy: the upstream answered 404"},
"refused": {from: from, mirrored: func(in reg.MirrorInput) (string, reg.Outcome) {
return reg.EventRefused, reg.Outcome{Kind: "upstream", Digest: in.Digest, Reason: "the upstream answered 404"}
}, reason: "not stored: server: container-image-registry refused the copy: the upstream answered 404"},
"another digest's outcome is not this copy's": {from: from, mirrored: func(in reg.MirrorInput) (string, reg.Outcome) {
return reg.EventPublished, reg.Outcome{Kind: "upstream", Digest: "sha256:" + strings.Repeat("b", 64)}
}, reason: "said no outcome of the copy"},
"a published image is no copy's outcome": {from: from, mirrored: func(in reg.MirrorInput) (string, reg.Outcome) {
return reg.EventPublished, reg.Outcome{Kind: "image", Digest: in.Digest}
}, reason: "said no outcome of the copy"},
"no pin": {from: "nats:2.11.17-alpine", reason: "not pinned: server"},
} {
t.Run(name, func(t *testing.T) {
f := newFake(t)
f.mirrored, f.elsewhere, f.watched, f.atOnce = c.mirrored, c.elsewhere, c.watched, c.atOnce
states := newStates()
m := moduleWith(t, f, states)
m.MirrorWait = 200 * time.Millisecond
waits := m.Outcomes.Waits.(*memWaits)
m.ReadWaits = StateRead{Keys: waits.Keys, Get: waits.Get}
var asked []CopyWait
f.atAsk = func() { asked, _ = m.Waiting() }
res, st := runCopy(t, m, states, map[string]any{"module": "nats", "artifact": "server", "from": c.from})
if c.reason != "" {
if st.State != sdkstep.StepFailed || !strings.Contains(st.Reason, c.reason) {
t.Fatalf("step = %+v, want failed with %q", st, c.reason)
}
if c.reason == "not pinned: server" && len(f.mirrors) != 0 {
t.Errorf("an unpinned copy asked the seat: %v", f.mirrors)
}
return
}
if st.State != sdkstep.StepPassed || res.Digest != d || !res.Pushed ||
res.Reference != reg.Seat+"/upstream/nats/server@"+d {
t.Fatalf("step = %+v result = %+v", st, res)
}
if len(f.mirrors) != 1 || f.mirrors[0] != (reg.MirrorInput{Name: "nats/server", Source: from, Digest: d}) {
t.Errorf("mirrors = %+v", f.mirrors)
}
if len(f.lines()) != 0 {
t.Errorf("the copy ran commands: %v", f.lines())
}
// The read tool of mirror-waits answers the copy's wait while it waits.
if len(asked) != 1 || asked[0].Digest != d || asked[0].From != from || asked[0].Node != "ace" || asked[0].Outcome != nil {
t.Errorf("%s at the ask = %+v", MirrorWaitsTool, asked)
}
if keys, _ := waits.Keys(); len(keys) != 0 {
t.Errorf("an ended copy left its wait in mirror-waits: %v", keys)
}
})
}
}