diff --git a/internal/compose/catalogue_declarations_test.go b/internal/compose/catalogue_declarations_test.go new file mode 100644 index 00000000..a60fdc4b --- /dev/null +++ b/internal/compose/catalogue_declarations_test.go @@ -0,0 +1,204 @@ +package compose + +import ( + "crypto/sha256" + "fmt" + "path/filepath" + "slices" + "sort" + "strings" + "testing" + + "git.novox.be/novox/mesh-sdk/go/catalogue" + "github.com/novox/mesh-host/internal/beside" + "github.com/novox/mesh-host/internal/compose/resolution" +) + +// Design 47 H2 H-A-469: every manifest of the catalogue placed beside this repository (internal/beside: the clone beside a +// merge check, elsewhere the captured copy) is composed onto a machine and its declaration read by the node-engine's own +// validator, so a key a manifest declares that only the mesh reads (a process's bus, its serves, the next one) cannot reach +// the wire unseen again: the strict declaration read refused every process declaring serves on the running node-engine. +// +// Each manifest is composed on anchor with what it needs beside it there: a provider of each name it wants and a holder of +// each seat it depends on, the first of the catalogue's by name, the artifact store's holder (so a module that fetches from +// it is not left out), each artifact it builds stood in for by a build, and each value only the operator sets (a required +// config key) given. A manifest that does not compose so, or whose resources the composition leaves out (it needs what +// only a running mesh answers, such as a provider helper's), is named with why in the log: none of it reaches the wire, so +// nothing of it can leak there. The test fails when a declaration composed is refused for a key the node-engine does not +// know ("does not use X, and it is set"), or when fewer than half of the catalogue's manifests reached a declaration read; +// any other refusal is the catalogue's own fault, judged by its checks, and is logged (H-A-469 addendum). +func TestEveryCatalogueManifestsDeclarationIsRead(t *testing.T) { + root := beside.Dir(t, "mesh-catalog") + paths, err := filepath.Glob(filepath.Join(root, "modules", "*", "module.json")) + if err != nil || len(paths) == 0 { + t.Fatalf("no manifests under %s: %v", root, err) + } + shelf := map[string]catalogue.Manifest{} + capabilities := map[string]bool{} + for _, p := range paths { + m, err := catalogue.ReadTreeManifest(root, filepath.Dir(p)) + if err != nil { + continue // TestEveryCatalogueManifestParses (internal/compose/resolution) names it + } + var built []catalogue.Built + if m.Build != nil { + for _, a := range m.Build.Artifacts { + digest := fmt.Sprintf("sha256:%x", sha256.Sum256([]byte(m.Module+"/"+a.Name))) + built = append(built, catalogue.Built{Name: a.Name, Kind: a.Kind, Digest: digest, + Reference: catalogue.ArtifactStoreScheme + m.Module + "/" + a.Name + "@" + digest}) + } + } + if resolved, err := m.Resolve(built); err == nil { + m = resolved + } + shelf[m.Module] = m + for _, c := range m.Capabilities { + capabilities[c] = true + } + } + names := make([]string, 0, len(shelf)) + for n := range shelf { + names = append(names, n) + } + sort.Strings(names) + first := func(is func(catalogue.Manifest) bool) string { + for _, n := range names { + if is(shelf[n]) { + return n + } + } + return "" + } + providerOf := func(want string) string { + return first(func(m catalogue.Manifest) bool { + if m.Module == want { + return true + } + for _, o := range m.Provides { + if o.Name == want { + return true + } + } + return false + }) + } + holderOf := func(seat string) string { + return first(func(m catalogue.Manifest) bool { + for _, c := range m.Claims { + if c.Name == seat { + return true + } + } + return false + }) + } + withWhatItNeeds := func(module string) []string { + in := map[string]bool{} + out := []string{} + var add func(string) + add = func(n string) { + if n == "" || in[n] { + return + } + in[n] = true + out = append(out, n) + for _, w := range shelf[n].Wants() { + add(providerOf(w)) + } + for _, s := range catalogue.DependsOn(shelf, shelf[n]) { + add(holderOf(s)) + } + } + add(module) + return out + } + store := first(func(m catalogue.Manifest) bool { + for _, c := range m.Claims { + if seat, ok := catalogue.SeatNamed(c.Name); ok && c.At() == catalogue.ScopeMesh && + slices.Contains(resolution.StoreSeats, seat.Name) { + return true + } + } + return false + }) + if store == "" { + t.Fatal("no module of the catalogue holds the artifact store, so every module that fetches from it is left out") + } + operatorsValues := func(m catalogue.Manifest) map[string]any { + values := map[string]any{} + for k, d := range m.Settings { + switch { + case !d.Required: + case len(d.Enum) > 0: + values[k] = d.Enum[0] + case d.Type == "number": + values[k] = float64(1) + case d.Type == "boolean": + values[k] = true + default: + values[k] = "example" + } + } + return values + } + + read, nothing := 0, 0 + for _, module := range names { + in := aMesh(t) + for n, m := range shelf { + in.Catalogue[n] = m + } + anchor := nodeOf(t, &in, "anchor") + anchor.Assigned = append(anchor.Assigned, withWhatItNeeds(module)...) + for _, n := range withWhatItNeeds(store) { + if !slices.Contains(anchor.Assigned, n) { + anchor.Assigned = append(anchor.Assigned, n) + } + } + anchor.SealingKey, anchor.Account, anchor.AccountHome = "sealing-key", "operator", "/home/operator" + anchor.PublicDomain, anchor.OutwardLinks = "example.org", []string{"eth0"} + for c := range capabilities { + anchor.Capabilities[c] = true + } + anchor.Settings = map[string][]catalogue.Layer{} + for _, n := range anchor.Assigned { + if values := operatorsValues(shelf[n]); len(values) > 0 { + anchor.Settings[n] = []catalogue.Layer{{From: "anchor", Values: values}} + } + } + d, problems, err := Compose(in, "anchor") + if err != nil { + t.Logf("%s does not compose here, so no declaration of it is read: %s", module, + strings.ReplaceAll(err.Error(), "\n", " ")) + continue + } + if len(problems) > 0 { + // A key the node-engine does not know, and only that, is this repository's to strip (H-A-469 addendum): every + // other refusal is the catalogue's own fault, judged by its checks, and is said here only. + for _, p := range problems { + if strings.Contains(p, "does not use \"") && strings.Contains(p, "\", and it is set") { + t.Errorf("%s: a key the node-engine does not know reaches its declaration: %s", module, p) + } else { + t.Logf("%s: its declaration is refused for the catalogue's own fault, not judged here: %s", module, p) + } + } + continue + } + reached := false + for _, r := range d.Resources { + reached = reached || strings.HasPrefix(fmt.Sprint(r["id"]), module+".") + } + switch { + case reached: + read++ + case len(shelf[module].Resources) == 0: + nothing++ + default: + t.Logf("%s composes here with none of its resources, so none is read: %s", module, d.LeftOutWhy[module]) + } + } + t.Logf("%d of %d manifests reached a declaration read; %d declare no resource", read, len(names), nothing) + if read*2 < len(names) { + t.Fatalf("only %d of %d manifests reached a declaration read, so this proved little", read, len(names)) + } +}