Files
mesh-host/internal/compose/composed_test.go
T
jschoubben cd7acb52d1 H-A-303: a pass that holds a module for a part withheld composes the tool runner as it runs
The node-engine restarted the tool runner at every self-compose on three machines:
a module held for a part that did not answer (the catalog half's entry, the
registry seats' versions) had its slice of the runtime process rebuilt from what
runs, with its fingerprint set to its want's and its files words dropped, so
MESH_TOOL_MODULES and MESH_TOOL_ENV differed pass by pass. A held module's
entries and words are now copied verbatim from the running process, after the
fingerprints are laid, so a holding pass digests as what runs and writes no new
revision; a module whose parts answered and changed is still composed from its
build. The composition read's grants ask names the holder it asked and why, and
says it once in the journal until the grants part answers again.

Builds: design 47 H H-A-303 (1)-(3), H-A-306 (1)-(3); H-A-296
2026-11-21 20:39:06 +01:00

295 lines
15 KiB
Go

package compose
import (
"encoding/json"
"git.novox.be/novox/mesh-delivery/modules/mesh-delivery-contracts/go/clients/meshcatalogclient"
engcomp "github.com/novox/mesh-host/internal/compositions"
"slices"
"sort"
"strconv"
"strings"
"testing"
"git.novox.be/novox/mesh-networking/modules/mesh-wireguard/go/overlay"
"git.novox.be/novox/mesh-sdk/go/catalogue"
"github.com/novox/mesh-host/internal/compose/resolution"
)
func fpFor(c byte) string { return meshcatalogclient.SourcePrefix2 + strings.Repeat(string(c), 64) }
// 1943/AC5: a node's compositions are its declaration as Compose gives it, split by the module each resource belongs
// to: every resource once, each in its module's composition at the fingerprint given, the module's ownership said on
// it; a module given no fingerprint is named as skipped and composed nowhere.
func TestCompositionsAreTheDeclarationSplitByModule(t *testing.T) {
t.Run("a held tool runner keeps its program for the modules of their own account", aHeldToolRunnerKeepsItsProgramForTheModulesOfTheirOwnAccount)
in := aSmallMesh(t)
d, _, err := Compose(in, "laptop")
if err != nil {
t.Fatal(err)
}
var want []string
for _, r := range d.Resources {
want = append(want, r["type"].(string)+":"+r["id"].(string))
}
sort.Strings(want)
fps := map[string]string{"mesh-wireguard": fpFor('a'), "files": fpFor('b')}
// A-481a: the composition names the digest of the templates archive its module was rendered from.
templated := in.Catalogue["files"]
templated.TemplatesDigest = "sha256:" + strings.Repeat("e", 64)
in.Catalogue["files"] = templated
out, skipped, err := Compositions(in, "laptop", fps, Foreseeing)
if err != nil || len(skipped) != 0 {
t.Fatalf("Compositions = %v, skipped %v", err, skipped)
}
for _, c := range out {
if want := map[string][]string{"files": {templated.TemplatesDigest}}[c.Module]; !slices.Equal(c.Templates, want) {
t.Errorf("%s names the templates %v, want %v", c.Module, c.Templates, want)
}
}
var got []string
for key, c := range out {
node, module, fp, isNode, err := engcomp.ParseKey(key)
if err != nil || node != "laptop" || module != c.Module || fp != c.FP || (isNode != (module == "")) {
t.Errorf("key %s holds %s at %s (%v)", key, c.Module, c.FP, err)
}
if !isNode && fp != fps[module] {
t.Errorf("%s is composed at %s, given %s", module, fp, fps[module])
}
if _, err := engcomp.Encode(c); err != nil {
t.Errorf("%s: %v", key, err)
}
for _, raw := range c.Resources {
var r struct{ ID, Type, Module string }
_ = json.Unmarshal(raw, &r)
if r.Module != module {
t.Errorf("%s holds %s of %q", key, r.ID, r.Module)
}
got = append(got, r.Type+":"+r.ID)
}
}
sort.Strings(got)
if strings.Join(got, " ") != strings.Join(want, " ") {
t.Errorf("the compositions hold %v, the declaration %v", got, want)
}
one, skipped, err := Compositions(in, "laptop", map[string]string{"mesh-wireguard": fpFor('a')}, Foreseeing)
if err != nil || strings.Join(skipped, ",") != "files" {
t.Fatalf("one module: %v, skipped %v", err, skipped)
}
for _, c := range one {
if c.Module == "files" {
t.Errorf("a module given no fingerprint was composed")
}
}
// Issue 1126: a module the node runs inside its own composition (Node.InOwn), and the private network's always,
// given no fingerprint of the new form, is folded into the node's own as the controller's composed writer folds it:
// every resource in its place, its module taken off, its id still naming it, nothing skipped.
folding := in
folding.Nodes = append([]Node(nil), in.Nodes...)
for i := range folding.Nodes {
if folding.Nodes[i].Name == "laptop" {
folding.Nodes[i].InOwn = map[string]bool{"files": true}
}
}
all, skipped, err := Compositions(folding, "laptop", nil, Foreseeing)
nodeKey, _ := engcomp.NodeKey("laptop")
if err != nil || len(skipped) != 0 || len(all) != 1 || len(all[nodeKey].Resources) != len(want) {
t.Fatalf("folded: %v, skipped %v, %d compositions, %d of %d resources in the node's own", err, skipped, len(all), len(all[nodeKey].Resources), len(want))
}
for i, raw := range all[nodeKey].Resources {
var r map[string]any
_ = json.Unmarshal(raw, &r)
if _, says := r["module"]; says || r["id"] != d.Resources[i]["id"] {
t.Errorf("folded resource %d is %v, the declaration's %v", i, r["id"], d.Resources[i]["id"])
}
}
if _, err := engcomp.Encode(all[nodeKey]); err != nil {
t.Errorf("the node's own with the folded modules: %v", err)
}
if _, _, err := Compositions(in, "nowhere", fps, Foreseeing); err == nil {
t.Errorf("a node the mesh does not have was composed")
}
// Issue 1126, 2026-10-29: a module composed as what runs of it (folded at a build of the old form, or held) still
// contributes to the runtime what the runtime serves of it as it runs (Node.RunsTools): its manifest at the
// indexer's commit has no bundles (shanks's runtime lost the folded systemd-resolved, novox's the folded nats), and a
// held module's new build's bundles are not delivered.
dg := func(c byte) string { return "sha256:" + strings.Repeat(string(c), 64) }
runtime := catalogue.Manifest{Module: "node-tools", Version: "1", ToolRunner: true,
OwnSecrets: catalogue.OwnSecrets{"broker": {Path: "/var/lib/mesh/node-tools/broker"}},
Bundles: []catalogue.Bundle{{Name: "runtime", Source: catalogue.ArtifactStoreScheme + "node-tools/runtime/blobs/" + dg('a'),
Digest: dg('a'), Language: "go", Binary: "node-tools"}}}
store := catalogue.Manifest{Module: "artifacts", Version: "1",
Claims: []catalogue.Claim{{Name: "container-image-registry", Scope: catalogue.ScopeMesh}},
Listens: []catalogue.Listening{{Name: "registry", Port: 5100, From: catalogue.FromMesh}}}
lamp := catalogue.Manifest{Module: "lamp", Version: "1", Tools: []string{"on"},
Resources: []map[string]any{{"id": "state", "type": "directory", "path": "/var/lib/lamp", "mode": "0700"}},
Build: &catalogue.Build{Artifacts: []catalogue.Artifact{{Name: "tools", Kind: catalogue.ArtifactBundle,
Language: "go", System: "arch", From: "cmd/lamp-tools", Env: map[string]string{"LAMP_CONF": "/var/lib/lamp/conf"}}}}}
newLamp, err := lamp.Resolve([]catalogue.Built{{Name: "tools", Kind: catalogue.ArtifactBundle,
Reference: catalogue.ArtifactStoreScheme + "lamp/tools/blobs/" + dg('c'), Digest: dg('c')}})
if err != nil {
t.Fatal(err)
}
served := "lamp=" + resolution.BundlePath("lamp", "tools") + "/lamp-tools"
runtimeRuns := func(dropped bool, ran string) engcomp.Composed {
env := `"MESH_TOOL_MODULES":"` + served + `","MESH_TOOL_ENV":"{\"lamp\":{\"LAMP_CONF\":\"/var/lib/lamp/conf\"}}"`
if dropped {
env = `"MESH_TOOL_MODULES":""` // the drop already applied (shanks, 2026-10-29)
}
if ran != "" {
env = ran
}
return engcomp.Composed{Node: "anchor", Module: "node-tools", Resources: []json.RawMessage{json.RawMessage(
`{"id":"node-tools.runtime","module":"node-tools","type":"process","name":"node-tools","env":{` + env + `}}`)}}
}
lampRuns := `{"id":"lamp.bundle-tools","type":"archive","source":"` + catalogue.ArtifactStoreScheme + `lamp/tools/blobs/` + dg('b') +
`","digest":"` + dg('b') + `","path":"` + resolution.BundlePath("lamp", "tools") + `"}`
// H-A-303, H-A-306: what runs of lamp in the runtime, its fingerprint and the paths it owns among its words, at a load
// its new build no longer names.
ranServed := "lamp=" + resolution.BundlePath("lamp", "tools") + "/lamp-tools-old"
ranWords := `{"lamp":{"LAMP_CONF":"/var/lib/lamp/conf","MESH_FINGERPRINT":"` + fpFor('0') + `","MESH_OWNED_PATHS":"/var/lib/lamp"}}`
ranEnv := `"MESH_TOOL_MODULES":"` + ranServed + `","MESH_TOOL_ENV":` + strconv.Quote(ranWords)
held := func(n *Node) engcomp.Composed {
n.Held = map[string]bool{"lamp": true}
lr := engcomp.Composed{Node: "anchor", Module: "lamp", Resources: []json.RawMessage{json.RawMessage(
strings.Replace(lampRuns, `"type"`, `"module":"lamp","type"`, 1))}}
n.Kept = map[string]catalogue.Manifest{"lamp": Carried(newLamp, "lamp", &lr)}
return lr
}
for _, row := range []struct {
name string
set func(n *Node) engcomp.Composed // and what runs of lamp
fps map[string]string
dropped bool
ran string // the runtime's env as it runs, when not served's
// What the runtime is composed to serve of lamp, and its words, when not served and its manifest's words.
wantServed, wantWords string
}{
{"folded at a build of the old form", func(n *Node) engcomp.Composed {
n.InOwn = map[string]bool{"lamp": true}
n.RunsOwn = &engcomp.Composed{Node: "anchor", Resources: []json.RawMessage{json.RawMessage(lampRuns)}}
return *n.RunsOwn
}, map[string]string{}, false, "", "", ""},
{"folded, the runtime already dropped it", func(n *Node) engcomp.Composed {
n.InOwn = map[string]bool{"lamp": true}
n.RunsOwn = &engcomp.Composed{Node: "anchor", Resources: []json.RawMessage{json.RawMessage(lampRuns)}}
return *n.RunsOwn
}, map[string]string{}, true, "", "", ""},
// H-A-306: a held module's slice is what runs of it, verbatim: no fingerprint of its want where none runs.
{"held, its new build not delivered", held, map[string]string{"lamp": fpFor('1')}, false, "",
served, `{"lamp":{"LAMP_CONF":"/var/lib/lamp/conf"}}`},
// H-A-303, H-A-306: held for a part withheld, its load, its fingerprint and its files words are what runs.
{"held for a part withheld, its slice as it runs", held, map[string]string{"lamp": fpFor('1')}, false, ranEnv,
ranServed, ranWords},
// H-A-306 (3): a module whose parts answered and changed is composed from its build, not from what runs.
{"read, its build changed", func(n *Node) engcomp.Composed {
n.Kept = map[string]catalogue.Manifest{"lamp": newLamp}
return engcomp.Composed{Node: "anchor", Module: "lamp", Resources: []json.RawMessage{json.RawMessage(
strings.Replace(lampRuns, `"type"`, `"module":"lamp","type"`, 1))}}
}, map[string]string{"lamp": fpFor('1')}, false, ranEnv, "", ""},
} {
tools := aMesh(t)
tools.Catalogue["node-tools"], tools.Catalogue["artifacts"], tools.Catalogue["lamp"] = runtime, store, lamp
anchor := nodeOf(t, &tools, "anchor")
anchor.Assigned = append(anchor.Assigned, "artifacts", "node-tools", "lamp")
tools.BusMinted = []string{"anchor." + "node-tools"}
anchor.SealingKey = keyOf(t, "anchor")
ran := []engcomp.Composed{runtimeRuns(row.dropped, row.ran), row.set(anchor)}
anchor.RunsTools, anchor.RunsRuntime = ToolsRunning(ran), RuntimeRunning(ran)
row.fps[overlay.Name], row.fps["artifacts"], row.fps["node-tools"] = fpFor('a'), fpFor('5'), fpFor('e')
var digests []string
var env map[string]any
for range 2 { // two passes over the same input compose the same runtime (H-A-303)
out, _, _, err := CompositionsIn(tools, "anchor", row.fps, Foreseeing)
if err != nil {
t.Fatalf("%s: %v", row.name, err)
}
for _, c := range out {
for _, raw := range c.Resources {
var r struct {
ID string `json:"id"`
Env map[string]any `json:"env"`
}
if json.Unmarshal(raw, &r) == nil && r.ID == "node-tools.runtime" {
env = r.Env
dig, err := engcomp.DigestOf(c)
if err != nil {
t.Fatal(err)
}
digests = append(digests, dig)
}
}
}
}
if len(digests) != 2 || digests[0] != digests[1] {
t.Errorf("%s: two passes composed the runtime at %v", row.name, digests)
}
// §519: the words each bundle's module is given hold the fingerprint of the want it applies, where there is one.
wantServed, words := served, `{"lamp":{"LAMP_CONF":"/var/lib/lamp/conf"}}`
if fp := row.fps["lamp"]; fp != "" {
words = `{"lamp":{"LAMP_CONF":"/var/lib/lamp/conf","MESH_FINGERPRINT":"` + fp + `"}}`
}
if row.wantServed != "" {
wantServed, words = row.wantServed, row.wantWords
}
if env[catalogue.RuntimeToolModules] != wantServed || env[catalogue.RuntimeToolEnv] != words {
t.Errorf("%s: the runtime serves %v with %v, want %s with %s", row.name,
env[catalogue.RuntimeToolModules], env[catalogue.RuntimeToolEnv], wantServed, words)
}
}
}
// lead-47 2026-11-16: a held tool runner keeps the program it runs (Carried, runningProgram) after what the runtime serves
// is laid on the stubs (Node.RunsTools), so a module of its own account on its node is run by that program, not held
// "the tool runner is not here to run it from" (novox, the new indexer).
func aHeldToolRunnerKeepsItsProgramForTheModulesOfTheirOwnAccount(t *testing.T) {
dg := "sha256:" + strings.Repeat("d", 64)
source := catalogue.ArtifactStoreScheme + "node-tools/runtime/blobs/" + dg
runtime := catalogue.Manifest{Module: "node-tools", Version: "1", ToolRunner: true,
OwnSecrets: catalogue.OwnSecrets{"broker": {Path: "/var/lib/mesh/node-tools/broker"}}}
own := catalogue.Manifest{Module: "mesh-self-check", Version: "1", RunsAs: "mesh-self-check",
OwnSecrets: catalogue.OwnSecrets{"broker": {Path: "/var/lib/mesh/mesh-self-check/broker", Taken: catalogue.TakenAtStart}},
Bundles: []catalogue.Bundle{{Name: "tools", Source: catalogue.ArtifactStoreScheme + "mesh-self-check/tools/blobs/" + dg,
Digest: dg, Language: "go", Loads: []string{"mesh-self-check-tools"}}}}
store := catalogue.Manifest{Module: "artifacts", Version: "1",
Claims: []catalogue.Claim{{Name: "container-image-registry", Scope: catalogue.ScopeMesh}},
Listens: []catalogue.Listening{{Name: "registry", Port: 5100, From: catalogue.FromMesh}}}
runs := engcomp.Composed{Node: "anchor", Module: "node-tools", Resources: []json.RawMessage{json.RawMessage(
`{"id":"node-tools.runtime","module":"node-tools","type":"process","name":"node-tools","source":"` + source +
`","digest":"` + dg + `","run":["./node-tools"],"env":{"MESH_TOOL_MODULES":""}}`)}}
in := aMesh(t)
in.Catalogue["node-tools"], in.Catalogue["artifacts"], in.Catalogue["mesh-self-check"] = runtime, store, own
anchor := nodeOf(t, &in, "anchor")
anchor.Assigned = append(anchor.Assigned, "artifacts", "node-tools", "mesh-self-check")
in.BusMinted = []string{"anchor.node-tools", "anchor.mesh-self-check"}
anchor.SealingKey = keyOf(t, "anchor")
anchor.Held = map[string]bool{"node-tools": true}
anchor.Kept = map[string]catalogue.Manifest{"node-tools": Carried(runtime, "node-tools", &runs)}
anchor.RunsTools = ToolsRunning([]engcomp.Composed{runs})
fps := map[string]string{overlay.Name: fpFor('a'), "artifacts": fpFor('5'), "node-tools": fpFor('e'), "mesh-self-check": fpFor('6')}
out, _, _, err := CompositionsIn(in, "anchor", fps, Foreseeing)
if err != nil {
t.Fatalf("a module of its own account beside a held tool runner: %v", err)
}
ran := false
for _, c := range out {
for _, raw := range c.Resources {
var r struct {
Type string `json:"type"`
Digest string `json:"digest"`
Run []string `json:"run"`
User string `json:"user"`
}
if json.Unmarshal(raw, &r) == nil && r.Type == "process" && r.User == "mesh-self-check" {
ran = r.Digest == dg && len(r.Run) == 1 && r.Run[0] == "./node-tools"
}
}
}
if !ran {
t.Errorf("mesh-self-check is not run by the program node-tools runs: %v", out)
}
}