Files
mesh-host/internal/selfcompose/versions_test.go
T
jschoubben 63133503a9 The node-engine composes nothing of a package artifact (H-A-466)
A package is a build input published to a registry, never placed on a
machine, and no registry seat the engine reads numbers it: asking its
digest failed the whole build, so a module declaring a package (the
indexer, the forge bridge) was wanted and held. The engine now leaves
package artifacts out of what it resolves (lookUp), holds
(partsMissing), builds (buildOf) and takes in (takein.Manifest,
takein.Built, and the manifest handed to Resolve), and so does the
candidate compose, which shares partsOf.

Builds: design 47 H2 H-A-466
2026-11-22 04:32:20 +01:00

417 lines
21 KiB
Go

package selfcompose
import (
"context"
"crypto/sha256"
"encoding/hex"
"encoding/json"
"errors"
"runtime"
"strings"
"sync"
"testing"
"time"
"github.com/novox/mesh-host/internal/agent"
"github.com/novox/mesh-host/internal/nodegrant"
"github.com/novox/mesh-host/internal/takein"
"git.novox.be/novox/mesh-sdk/go/catalogue"
"git.novox.be/novox/mesh-contracts/go/seats/meshmoduleassigner"
"git.novox.be/novox/mesh-delivery/modules/mesh-delivery-contracts/go/clients/meshcatalogclient"
"git.novox.be/novox/mesh-delivery/modules/mesh-delivery-contracts/go/seats/meshcatalog"
"git.novox.be/novox/mesh-delivery/modules/mesh-delivery-contracts/go/seats/meshwants"
"git.novox.be/novox/mesh-networking/modules/mesh-connectivity-contracts/go/seats/nodepacketfilter"
)
// Design 47 G A-430, A-430a (the lead's answer A-430b): a want naming its version is resolved to its artifacts'
// digests through the verb versions of the registry seat of each artifact's kind, under `<module>/<artifact>`; an upstream copy
// by the digest its manifest pins. The build is made from those digests. A lookup that fails is said, naming the module, the
// version and the seat, and holds the module whole.
func aWantNamingItsVersionIsResolvedThroughTheRegistrySeats(t *testing.T) {
fp := meshcatalogclient.SourcePrefix2 + strings.Repeat("a", 64)
d := func(c string) string { return "sha256:" + strings.Repeat(c, 64) }
manifest := map[string]any{"module": "mail", "version": "1", "build": map[string]any{"artifacts": []any{
map[string]any{"name": "server", "kind": "upstream", "from": "alpine@" + d("9")},
map[string]any{"name": "files", "kind": "archive", "from": "files"},
map[string]any{"name": "web", "kind": "image", "from": "Dockerfile"},
}}}
entry := meshcatalog.CatalogEntry{Module: "mail", Fp: fp, Manifest: manifest, Artifacts: []meshcatalog.Artifact{
{Ref: "mail:server", Kind: "upstream"}, {Ref: "mail:files", Kind: "archive"}, {Ref: "mail:web", Kind: "image"}}}
// H-A-466: a build with a bundle and a package resolves the bundle's digest alone; no digest is asked of the package, a build
// input never placed, and nothing is held for it.
fpPkg := meshcatalogclient.SourcePrefix2 + strings.Repeat("b", 64)
withPackage := meshcatalog.CatalogEntry{Module: "mail", Fp: fpPkg, Manifest: map[string]any{"module": "mail", "version": "1",
"build": map[string]any{"artifacts": []any{
map[string]any{"name": "tools", "kind": "bundle", "language": "go", "from": "tools"},
map[string]any{"name": "mailapi", "kind": "package", "language": "go", "from": "api"},
}}}, Artifacts: []meshcatalog.Artifact{{Ref: "mail:tools", Kind: "bundle", Language: "go"},
{Ref: "mail:mailapi", Kind: "package", Language: "go", Package: &meshcatalog.ArtifactPackage{Name: "git.example/mail/api"}}}}
// The seats hold mail/files and mail/web; versions newest first.
held := map[string]string{
"oci-artifact-registry.mail/files": `{"name":"mail/files","versions":[{"name":"mail/files","kind":"archive","digest":"` + d("2") + `","version":"1.3.0"},{"name":"mail/files","kind":"archive","digest":"` + d("1") + `","version":"1.2.3"}]}`,
"container-image-registry.mail/web": `{"name":"mail/web","versions":[{"name":"mail/web","kind":"image","digest":"` + d("3") + `","version":"1.2.3"}]}`,
"container-image-registry.mail/other": `{"name":"mail/other","versions":[]}`,
"oci-artifact-registry.mail/tools": `{"name":"mail/tools","versions":[{"name":"mail/tools","kind":"bundle","digest":"` + d("4") + `","version":"1.2.3"}]}`,
}
for name, row := range map[string]struct {
want meshwants.WantsByWantNodeModuleValue
entry *meshcatalog.CatalogEntry // the build's entry, the one above when nil
down string // a seat whose call fails
parts map[string]string
missing string // a word the reason holds
said []string
}{
"resolved": {want: meshwants.WantsByWantNodeModuleValue{Fp: fp, Version: "1.2.3", Self: true},
parts: map[string]string{"mail:server": d("9"), "mail:files": d("1"), "mail:web": d("3")}},
"a version a seat does not hold": {want: meshwants.WantsByWantNodeModuleValue{Fp: fp, Version: "2.0.0", Self: true},
missing: "2.0.0", said: []string{"mail at version 2.0.0", "oci-artifact-registry", "container-image-registry"}},
"a seat that does not answer": {want: meshwants.WantsByWantNodeModuleValue{Fp: fp, Version: "1.2.3", Self: true}, down: "container-image-registry",
missing: "mail:web", said: []string{"mail at version 1.2.3: the seat container-image-registry"}},
"a package artifact": {want: meshwants.WantsByWantNodeModuleValue{Fp: fpPkg, Version: "1.2.3", Self: true}, entry: &withPackage,
parts: map[string]string{"mail:tools": d("4")}},
} {
var mu sync.Mutex
var said []string
env := Env{Say: func(line string) { mu.Lock(); said = append(said, line); mu.Unlock() }}
calls := 0
env.Registry = EntryCall(func(_ context.Context, seat, verb string, args any) (json.RawMessage, error) {
calls++
if verb != "versions" {
t.Errorf("%s: asked %s.%s", name, seat, verb)
}
if seat == row.down {
return nil, errors.New("no responders")
}
answer, ok := held[seat+"."+args.(map[string]string)["name"]]
if !ok {
return nil, errors.New("not held")
}
return json.RawMessage(answer), nil
})
env.Entry = EntryCall(func(context.Context, string, string, any) (json.RawMessage, error) {
return nil, errors.New("not asked")
})
entry := entry
if row.entry != nil {
entry = *row.entry
}
c := New(env)
c.wants["mail"] = row.want
c.keptEnt[entry.Fp] = keptEntry{entry: entry}
parts, missing := c.partsOf("mail", entry, row.want)
b := c.buildOf("mail", entry.Fp)
if row.missing != "" {
if parts != nil || b != nil || !strings.Contains(missing, row.missing) {
t.Errorf("%s: parts %v, build %v, missing %q: want the module held whole, naming %s", name, parts, b, missing, row.missing)
}
if c.entryHolds()["mail"] == "" {
t.Errorf("%s: the module is not held", name)
}
for _, w := range row.said {
if !strings.Contains(strings.Join(said, "\n"), w) {
t.Errorf("%s: said %q, want a line naming %q", name, said, w)
}
}
continue
}
if missing != "" || len(parts) != len(row.parts) {
t.Fatalf("%s: parts %v, missing %q (said %q)", name, parts, missing, said)
}
if held := c.entryHolds()["mail"]; held != "" {
t.Errorf("%s: the module is held: %s", name, held)
}
for ref, digest := range row.parts {
if parts[ref] != digest {
t.Errorf("%s: %s at %s, want %s", name, ref, parts[ref], digest)
}
}
if b == nil || len(b.Artifacts) != len(row.parts) {
t.Fatalf("%s: build %+v", name, b)
}
// The take-in of the build's manifest passes: no package is counted as missing or waited on (H-A-466).
raw, _ := json.Marshal(entry.Manifest)
m, err := catalogue.Decode(raw)
if err != nil {
t.Fatalf("%s: %v", name, err)
}
if _, err := takein.Manifest(context.Background(), m, *b, nil); err != nil {
t.Errorf("%s: the take-in refused the build: %v", name, err)
}
for _, a := range b.Artifacts {
if a.Digest != row.parts["mail:"+a.Name] || a.Reference != referenceOf("mail", a.Name, a.Digest) {
t.Errorf("%s: artifact %+v", name, a)
}
}
// A resolution is asked once, and it names the node keeping the build its digests are of.
before := calls
c.partsOf("mail", entry, row.want)
if calls != before {
t.Errorf("%s: resolved again: %d calls, then %d", name, before, calls)
}
}
}
// Design 47 H §771: the order applies the holder of a seat first, found by the claim of the manifest of the build the want names.
func TestAModulesClaimsAreReadFromTheEntryOfItsWantsBuild(t *testing.T) {
fp := meshcatalogclient.SourcePrefix2 + strings.Repeat("a", 64)
c := New(Env{})
c.keptEnt = map[string]keptEntry{}
c.wants["bus"] = meshwants.WantsByWantNodeModuleValue{Fp: fp}
c.keptEnt[fp] = keptEntry{entry: meshcatalog.CatalogEntry{Module: "bus", Fp: fp,
Manifest: map[string]any{"module": "bus", "version": "1", "claims": []any{map[string]any{"name": "mesh-broker"}}}}}
if claims, known := c.Claims(context.Background(), "bus", "mesh-broker"); !claims || !known {
t.Errorf("the bus claims mesh-broker: claims %v, known %v", claims, known)
}
if claims, known := c.Claims(context.Background(), "bus", "node-backup"); claims || !known {
t.Errorf("the bus claims no node-backup: claims %v, known %v", claims, known)
}
if claims, known := c.Claims(context.Background(), "ghost", "mesh-broker"); claims || known {
t.Errorf("a module with no want has no manifest read: claims %v, known %v", claims, known)
}
// Design 08 control-verbs §1, §4, §5: the compose of the filter asks the holder of node-packet-filter its verb allowed
// within the compose-ask bound; an unanswered allowed is a part withheld, said once until an answer comes between.
t.Run("the filter asks allowed of its holder", func(t *testing.T) {
now := time.Date(2026, 11, 21, 20, 0, 0, 0, time.UTC)
var said []string
var asked []time.Duration
answer, failure := json.RawMessage(`{"allowances":[{"key":"allow.tcp.8000","port":8000,"protocol":"tcp","at":"a1","by":"op","why":"w"}]}`), error(nil)
env := Env{Node: "desk", Say: func(line string) { said = append(said, line) }, Now: func() time.Time { return now },
Allowed: func(_ context.Context, within time.Duration) (json.RawMessage, error) {
asked = append(asked, within)
return answer, failure
}}
c := New(env)
c.keptEnt = map[string]keptEntry{}
c.wants["nft"] = meshwants.WantsByWantNodeModuleValue{Fp: fp}
c.keptEnt[fp] = keptEntry{entry: meshcatalog.CatalogEntry{Module: "nft", Fp: fp,
Manifest: map[string]any{"module": "nft", "version": "1", "claims": []any{map[string]any{"name": "node-packet-filter"}}}}}
own := meshmoduleassigner.Assignments{Node: "desk", Modules: []meshmoduleassigner.PlacedModule{{Module: "nft"}}}
got := c.filterOf(context.Background(), own)
if len(asked) != 1 || asked[0] != DefaultComposeAskBound || got.Withheld != "" || len(got.Allowing.Allowances) != 1 ||
got.Allowing.Allowances[0].Key != "allow.tcp.8000" || !got.Allowing.At.Equal(now) {
t.Errorf("asked %v, given %+v; want allowed asked once within 10s and its allowance given at the compose's time", asked, got)
}
failure = errors.New("nats: no responders available for request")
for range 2 {
if got = c.filterOf(context.Background(), own); !strings.Contains(got.Withheld, "node-packet-filter's verb allowed on desk, which did not answer within 10s: nats: no responders") {
t.Errorf("an unanswered allowed gives %+v, want the part withheld naming the holder asked", got)
}
}
answer, failure = json.RawMessage(`{"allowances":[{"key":"allow.tcp.8000","port":8000,"protocol":"tcp","until":"tomorrow"}]}`), nil
if got = c.filterOf(context.Background(), own); !strings.Contains(got.Withheld, `answered what is not its answer: allow.tcp.8000 has the until "tomorrow"`) {
t.Errorf("an answer that is not allowed's gives %+v, want the part withheld", got)
}
// H-A-321: the composition read of the holder names the part kept, and of another module none.
c.mu.Lock()
c.filter = got
c.mu.Unlock()
if parts := c.LastWithheldParts("nft"); len(parts) != 1 || parts[AllowedPart] != got.Withheld || AllowedPart != "node-packet-filter.allowed" {
t.Errorf("the holder's withheld parts are %v, want node-packet-filter.allowed with its why", parts)
}
if parts := c.LastWithheldParts("web"); parts != nil {
t.Errorf("another module's withheld parts are %v, want none", parts)
}
if n := strings.Count(strings.Join(said, "\n"), "is withheld"); n != 1 {
t.Errorf("the withheld part is said %d times over three unanswered composes, want once:\n%s", n, strings.Join(said, "\n"))
}
answer = json.RawMessage(`{"allowances":[]}`)
c.filterOf(context.Background(), own)
failure = errors.New("nats: timeout")
c.filterOf(context.Background(), own)
if n := strings.Count(strings.Join(said, "\n"), "is withheld"); n != 2 {
t.Errorf("the withheld part is said %d times, want again after a compose with an answer between", n)
}
asked = nil
c.env.ComposeAskBound = 3 * time.Second
if c.filterOf(context.Background(), own); len(asked) != 1 || asked[0] != 3*time.Second {
t.Errorf("asked within %v, want the setting compose-ask-bound", asked)
}
asked = nil
own.Decided.Adopted = true
if got = c.filterOf(context.Background(), own); len(asked) != 0 || got.Withheld != "" || len(got.Allowing.Allowances) != 0 {
t.Errorf("an adopted node asked %d times and is given %+v, want none asked and none rendered", len(asked), got)
}
own.Decided.Adopted = false
own.Modules = []meshmoduleassigner.PlacedModule{{Module: "ghost"}}
if c.filterOf(context.Background(), own); len(asked) != 0 {
t.Errorf("a node with no holder of node-packet-filter placed asked allowed %d times", len(asked))
}
})
// Design 08 control-verbs §3, §8: the node hears allowances-changed on its own node's subject, never another's, and
// composes again on it.
t.Run("an allowances-changed of its node composes again", func(t *testing.T) {
b := newFakeBus()
env := b.env()
var mu sync.Mutex
heard := map[string]chan []byte{}
env.Hear = func(subject string) (<-chan []byte, func(), error) {
mu.Lock()
defer mu.Unlock()
heard[subject] = make(chan []byte)
return heard[subject], func() {}, nil
}
c := New(env)
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
out, err := c.Watch(ctx, "n1.>")
if err != nil {
t.Fatal(err)
}
go func() {
for range out {
}
}()
for _, in := range c.inputs() {
b.send(in.bucket, agent.Update{Caught: true})
}
passAt := func() time.Time { c.mu.Lock(); defer c.mu.Unlock(); return c.last.At }
for deadline := time.Now().Add(10 * time.Second); passAt().IsZero(); runtime.Gosched() {
if time.Now().After(deadline) {
t.Fatal("no first pass")
}
}
first := passAt()
mu.Lock()
own, other := heard[nodepacketfilter.SubjectAllowancesChanged("n1")], heard[nodepacketfilter.SubjectAllowancesChanged("n2")]
mu.Unlock()
if own == nil || other != nil {
t.Fatalf("heard %v, want the own node's allowances-changed and no other's", heard)
}
// And the node's grant lets it subscribe there, as the grants part grants the consume (every machine's, as `*`).
granted := false
for _, f := range nodegrant.For("n1").Subscribe {
granted = granted || subjectCovers(f, nodepacketfilter.SubjectAllowancesChanged("n1"))
}
if !granted {
t.Errorf("the node's grant does not let it subscribe to %s", nodepacketfilter.SubjectAllowancesChanged("n1"))
}
select {
case own <- []byte(`{"node":"n1","change":"allowed"}`):
case <-time.After(10 * time.Second):
t.Fatal("nothing hears the node's allowances-changed")
}
for deadline := time.Now().Add(10 * time.Second); passAt().Equal(first); runtime.Gosched() {
if time.Now().After(deadline) {
t.Fatal("an allowances-changed of the node composed nothing again")
}
}
})
}
// Design 47 H, the lead's answer H-A-146 (A-430a: the bytes are fetched by the digest the versions answer gives): a resolution
// whose fetch failed is forgotten and said so in the journal; the seats' verb versions is asked again at the module's next want, at
// the next compose by hand, and after a bound while the module is held, never before; a resolution whose fetch succeeded is kept.
func aResolutionWhoseFetchFailedIsForgotten(t *testing.T) {
fp := meshcatalogclient.SourcePrefix2 + strings.Repeat("a", 64)
body := []byte("the files")
sum := sha256.Sum256(body)
good := "sha256:" + hex.EncodeToString(sum[:])
wrong := "sha256:" + strings.Repeat("e", 64) // the store's answer of the manifest's digest where the blob's was due
entry := meshcatalog.CatalogEntry{Module: "mail", Fp: fp, Manifest: map[string]any{"module": "mail", "version": "1"},
Artifacts: []meshcatalog.Artifact{{Ref: "mail:files", Kind: "archive"}}}
want := meshwants.WantsByWantNodeModuleValue{Fp: fp, Version: "1.2.3", Self: true}
for name, row := range map[string]struct {
answer string // the digest versions answers
fetch error // the store's answer to the fetch
then string // what follows the fetch: "" (a pass within the bound), want, compose, bound
asked bool // versions is asked again
forgets bool // the journal says it forgot
failed bool // the fetch fails though nothing is forgotten
again time.Duration // the setting resolve-again-minutes; 0 is its default
}{
"a fetch that succeeded keeps its resolution": {answer: good, then: "bound"},
"a 404, the next pass within the bound": {answer: wrong, fetch: notFound{}, forgets: true},
"a 404, then the module's next want": {answer: wrong, fetch: notFound{}, then: "want", asked: true, forgets: true},
"a 404, then a compose by hand": {answer: wrong, fetch: notFound{}, then: "compose", asked: true, forgets: true},
"a 404, then the bound while it is held": {answer: wrong, fetch: notFound{}, then: "bound", asked: true, forgets: true},
"bytes that are not of the digest": {answer: wrong, then: "want", asked: true, forgets: true},
// Principle 11: the bound is the setting resolve-again-minutes, one minute by default.
"a 404, a minute within a setting of five": {answer: wrong, fetch: notFound{}, then: "minute", forgets: true, again: 5 * time.Minute},
"a 404, then the setting's bound of five": {answer: wrong, fetch: notFound{}, then: "bound", asked: true, forgets: true, again: 5 * time.Minute},
// H-A-150: a fetch nothing answered for keeps its resolution, and the module is not held for it.
"a bus that did not answer keeps it": {answer: good, fetch: errors.New("nats: timeout"), failed: true},
"a store away keeps it": {answer: good, fetch: storeAway{}, failed: true},
} {
now := time.Date(2026, 11, 20, 16, 0, 0, 0, time.UTC)
var said []string
env := Env{Node: "desk", Say: func(line string) { said = append(said, line) }, Now: func() time.Time { return now },
ResolveAgain: row.again}
calls := 0
env.Registry = EntryCall(func(context.Context, string, string, any) (json.RawMessage, error) {
calls++
return json.RawMessage(`{"versions":[{"digest":"` + row.answer + `","version":"1.2.3"}]}`), nil
})
env.Fetch = func(context.Context, string) ([]byte, error) { return body, row.fetch }
env.Entry = EntryCall(func(context.Context, string, string, any) (json.RawMessage, error) {
return nil, errors.New("not asked")
})
c := New(env)
c.wants["mail"] = want
c.keptEnt[fp] = keptEntry{entry: entry}
parts, _ := c.partsOf("mail", entry, want)
_, err := reader{c: c}.Archive(context.Background(), referenceOf("mail", "files", parts["mail:files"]), parts["mail:files"])
if (err != nil) != (row.forgets || row.failed) {
t.Errorf("%s: the fetch: %v", name, err)
}
switch row.then {
case "want":
raw, _ := json.Marshal(want)
c.takeWant(agent.Update{Key: meshwants.WantsByWantNodeModuleKey("desk", "mail"), Value: raw})
case "compose":
c.watching.Store(true)
ended, cancel := context.WithCancel(context.Background())
cancel()
c.ComposeNow(ended)
case "bound":
now = now.Add(max(row.again, DefaultResolveAgain))
case "minute":
now = now.Add(time.Minute)
}
before := calls
again, missing := c.partsOf("mail", entry, want)
if asked := calls > before; asked != row.asked {
t.Errorf("%s: versions asked again %v, want %v", name, asked, row.asked)
}
if row.forgets && !row.asked && (again != nil || !strings.Contains(missing, "forgotten")) {
t.Errorf("%s: parts %v, missing %q: want the module held, saying the resolution was forgotten", name, again, missing)
}
if !row.forgets && again["mail:files"] != good {
t.Errorf("%s: parts %v: want the resolution kept", name, again)
}
line := "resolution of mail 1.2.3 forgotten: "
if got := strings.Contains(strings.Join(said, "\n"), line); got != row.forgets {
t.Errorf("%s: said %q; want %q said: %v", name, said, line, row.forgets)
}
}
}
// notFound is the artifact store's answer that it holds no such blob, as the apply's fetch says it (StoreAnswered).
type notFound struct{}
func (notFound) Error() string { return "artifact-store answered 404 Not Found" }
func (notFound) StoreAnswered() bool { return true }
// storeAway is a gateway's answer that the store is away (a 503): no answer of the store's own.
type storeAway struct{}
func (storeAway) Error() string { return "artifact-store answered 503 Service Unavailable" }
func (storeAway) StoreAnswered() bool { return false }
// subjectCovers says whether the subject filter f matches the subject s, by NATS's wildcards (* one token, > the rest).
func subjectCovers(f, s string) bool {
ft, st := strings.Split(f, "."), strings.Split(s, ".")
for i, t := range ft {
if t == ">" {
return i < len(st)
}
if i >= len(st) || (t != "*" && t != st[i]) {
return false
}
}
return len(ft) == len(st)
}