package resolution import ( "encoding/json" "fmt" "path/filepath" "reflect" "strings" "testing" mcat "git.novox.be/novox/mesh-sdk/go/catalogue" "github.com/novox/mesh-host/internal/beside" ) // The catalogue's foundation modules as they are — in a merge check the catalogue cloned beside it, // elsewhere the copy captured in testdata/beside (internal/beside, novox/hq issue 432) — parsed by the // real parser (novox/hq ADR 0100): // the store and the broker say which of their ports the mesh guards on an adopted node, and the // filter module loads its table through a unit of its own whose stop deletes only that table. // moduleTree is where a module is read from: the catalogue beside, with the catalogue as the root. In // mesh-controller it first looked in the repository's own modules/, where build-agent and // route-proxy live whole (novox/hq ADR 0420 §1, issue 1019); mesh-sdk has no such directory, and the // tests that read those two modules stay in mesh-controller (issue 1379). func moduleTree(t *testing.T, module string) (root, dir string) { t.Helper() return beside.Dir(t, "mesh-catalog"), filepath.Join(beside.Catalogue(t), module) } func catalogueManifest(t *testing.T, module string) mcat.Manifest { t.Helper() root, dir := moduleTree(t, module) m, err := mcat.ReadTreeManifest(root, dir) if err != nil { t.Fatalf("%s does not parse:\n%v", module, err) } return m } func TestTheForgesPortIsGivenLikeAnyOtherProvidersPort(t *testing.T) { forge := catalogueManifest(t, "gitea") // The catalogue's number is a default and the node's setting moves it. given, err := GivenPorts(forge, []mcat.Layer{{From: "anchor", Placement: map[string]any{mcat.PortsSetting: map[string]any{"3000": float64(3100)}}}}) if err != nil { t.Fatalf("the forge's port cannot be given on a node: %v", err) } if given[3000] != 3100 { t.Fatalf("the forge was given %v", given) } // And every consumer of the package registry is told where the machine actually put it, // because that is read from what the forge serves rather than written in the consumer. if got := ServedOn(forge, "npm-package-registry", given)["port"]; got != 3100 { t.Errorf("the package registry is served on %v, not the port this node gave it", got) } if got := ServedOn(forge, "npm-package-registry", nil)["port"]; got != float64(3000) { t.Errorf("without a setting the forge serves %v, not the catalogue's port", got) } // And so is where a repository on it is cloned from (novox/hq ADR 0111), for the same reason: // a build composes the URL from what the forge serves, so a given port is a followed port. if got := ServedOn(forge, "git", given)["port"]; got != 3100 { t.Errorf("git is served on %v, not the port this node gave the forge", got) } } // The forge holds the seats it answers for (novox/hq ADR 0110, 0111), parsed by the real parser — // which refuses a delivering seat claimed by a module that does not provide what it delivers. func TestTheForgeHoldsTheNpmAndGitSeats(t *testing.T) { forge := catalogueManifest(t, "gitea") holds := map[string]bool{} for _, c := range forge.Claims { holds[c.Name] = true } for _, seat := range []string{"npm-package-registry", "git"} { if !holds[seat] { t.Errorf("gitea does not claim the %s seat: %+v", seat, forge.Claims) } } git := ServedOn(forge, "git", nil) if git["scheme"] != "http" || git["port"] != float64(3000) { t.Errorf("gitea serves nothing a clone URL can be composed from: %v", git) } npm := ServedOn(forge, "npm-package-registry", nil) if npm["npm-path"] != "/api/packages/novox/npm/" { t.Errorf("gitea no longer says where its npm registry is: %v", npm) } } // **And the forge's own address follows it**, composed from the manifest in the catalogue beside // this checkout (novox/hq 04-ISSUES/088). // // The forge is reached a third way that neither test above covers: by its own code, over the // machine's loopback, told where to go in its environment. The `2999:3000` mapping that lets the // forge go on binding 3000 does nothing for a caller dialling the machine — so a literal there is // wrong on every node whose assignment differs, and wrong for a second reason on a node given the // port (ADR 0100). Composed through the whole path, because what proves the placeholder resolves // in an `env` at all is a declaration, not a manifest. func TestTheForgesOwnAddressFollowsThePortTheNodeGaveIt(t *testing.T) { forge, err := catalogueManifest(t, "gitea").Resolve(forgeBuilt(catalogueManifest(t, "gitea"))) if err != nil { t.Fatalf("the forge's manifest does not resolve against its own build: %v", err) } r := mcat.Resolution{Node: "anchor", Account: "op", PublicDomain: "example.org", Modules: []mcat.Manifest{forge, theRuntime(t)}, Needs: []mcat.Needed{ {Name: "postgres-database", For: "gitea", From: "anchor", At: "127.0.0.1", Serves: map[string]any{"port": float64(5432)}, Sealed: "sealed-db"}, {Name: "route", For: "gitea", From: "anchor"}, {Name: "secret", For: "gitea", From: "anchor", Local: "internal-token", Sealed: "sealed-token"}, {Name: "secret", For: "gitea", From: "anchor", Local: "admin", Sealed: "sealed-admin"}, }} // The number this node was given for the forge — the one the machine it is about to run on // already publishes. out, err := Declaration(r, mcat.Rendering{ArtifactStore: "anchor.internal:5101", Needed: map[string]map[string]string{mcat.RuntimeModule: {"broker": "sealed-broker"}}, Given: map[string]map[int]int{"gitea": {3000: 2999}}, }) if err != nil { t.Fatalf("the forge does not compose: %v", err) } // What the machine publishes, and what the forge's sidecar is told to dial: one number. server := fileNamed(out, "gitea.server") if server == nil { t.Fatalf("the forge's own container is not in the declaration: %v", out) } if published := fmt.Sprint(server["ports"]); !strings.Contains(published, "2999:3000") { t.Fatalf("the forge is not published on the port this node gave it: %v", server["ports"]) } // The forge's own code runs in the node's runtime (novox/hq ADR 0198), given its words there. runtime := fileNamed(out, mcat.RuntimeModule+"."+RuntimeProcessID()) if runtime == nil { t.Fatalf("the node's runtime is not in the declaration: %v", ids(out)) } env, _ := runtime["env"].(map[string]string) var given map[string]map[string]string if err := json.Unmarshal([]byte(env[mcat.RuntimeToolEnv]), &given); err != nil { t.Fatalf("the runtime's %s is not JSON: %q", mcat.RuntimeToolEnv, env[mcat.RuntimeToolEnv]) } if given["gitea"]["MESH_GITEA_URL"] != "http://127.0.0.1:2999" { t.Fatalf("the forge's code dials %v while the machine publishes the forge on 2999 — "+ "whatever reads it dials a dead port", given["gitea"]["MESH_GITEA_URL"]) } } // gitea's own sshd is unmodified — the module's own internal port is 22, the number in // `listens`, the same convention every other module in the catalogue uses (its internal port, // not an invented identity). Composed from the manifest in the catalogue beside this checkout, // because what the mesh publishes is a fact about what the module actually writes. func declaredGiteaSsh(t *testing.T, given map[int]int) map[string]any { t.Helper() forge := catalogueManifest(t, "gitea") resolved, err := forge.Resolve(forgeBuilt(forge)) if err != nil { t.Fatalf("the forge's manifest does not resolve against its own build: %v", err) } r := mcat.Resolution{Node: "anchor", Account: "op", PublicDomain: "example.org", Modules: []mcat.Manifest{resolved, theRuntime(t)}, Needs: []mcat.Needed{ {Name: "postgres-database", For: "gitea", From: "anchor", At: "127.0.0.1", Serves: map[string]any{"port": float64(5432)}, Sealed: "sealed-db"}, {Name: "route", For: "gitea", From: "anchor"}, {Name: "secret", For: "gitea", From: "anchor", Local: "internal-token", Sealed: "sealed-token"}, {Name: "secret", For: "gitea", From: "anchor", Local: "admin", Sealed: "sealed-admin"}, }} givenPorts := map[int]int{3000: 3000} for k, v := range given { givenPorts[k] = v } out, err := Declaration(r, mcat.Rendering{ArtifactStore: "anchor.internal:5101", Needed: map[string]map[string]string{mcat.RuntimeModule: {"broker": "sealed-broker"}}, Ports: map[string]map[int]int{"gitea": givenPorts}, Given: map[string]map[int]int{"gitea": given}, }) if err != nil { t.Fatalf("the forge does not compose: %v", err) } server := fileNamed(out, "gitea.server") if server == nil { t.Fatalf("the forge's own container is not in the declaration: %v", out) } return server } // **The forge publishes ssh at the mesh's own fixed convention by default** (novox/hq ADR 0100). // // `222` is the mesh's own public convention for the forge's ssh, written directly in the // manifest's `ports` — every node the forge has run on used the same number, so it needs no // per-node setting to reach it. func TestTheForgesSshPortIsTheMeshsFixedConventionByDefault(t *testing.T) { forge := catalogueManifest(t, "gitea") // Nothing was given — no node moved this port — which is the ordinary answer: the mesh only // reports what a setting moved, and the manifest's own `222:22` needs no move to be reached. given, err := GivenPorts(forge, nil) if err != nil { t.Fatalf("the forge's ssh port cannot be given on a node: %v", err) } if len(given) != 0 { t.Fatalf("nothing moved the forge's ssh port, yet it was given %v", given) } server := declaredGiteaSsh(t, given) if published := fmt.Sprint(server["ports"]); !strings.Contains(published, "222:22") { t.Fatalf("the forge is published on %v, not its own fixed convention", server["ports"]) } } // **A node whose predecessor served git on a different number can still be told to leave it // there.** The setting names the port the module itself listens on — 22, gitea's own sshd, the // same number `listens` uses — not the mesh's own default machine-side number, so moving it does // not require guessing what the manifest happens to default to. func TestANodeMayGiveTheForgesSshPortADifferentNumber(t *testing.T) { forge := catalogueManifest(t, "gitea") given, err := GivenPorts(forge, []mcat.Layer{{From: "anchor", Placement: map[string]any{mcat.PortsSetting: map[string]any{"22": float64(9022)}}}}) if err != nil { t.Fatalf("the forge's ssh port cannot be moved on a node: %v", err) } if want := map[int]int{22: 9022}; !reflect.DeepEqual(given, want) { t.Fatalf("the forge was given %v, and the setting named %v", given, want) } server := declaredGiteaSsh(t, given) if published := fmt.Sprint(server["ports"]); !strings.Contains(published, "9022:22") || strings.Contains(published, "222:22") { t.Fatalf("the forge is published on %v, not the port this node gave it", server["ports"]) } } // forgeBuilt is a build of every artifact the forge's manifest declares: its code bundle and, since the // catalogue added it (novox/hq ADR 0251 §4), its npm registry's. Named from the manifest, so a third // artifact does not break every forge test again. func forgeBuilt(forge mcat.Manifest) []mcat.Built { var out []mcat.Built for _, a := range forge.Build.Artifacts { out = append(out, mcat.Built{Name: a.Name, Kind: mcat.ArtifactBundle, Reference: mcat.ArtifactStoreScheme + "gitea/" + a.Name + "/blobs/" + bundleDigest, Digest: bundleDigest}) } return out } // catalogueResolved is a catalogue module as the mesh holds it once built: the manifest as written, resolved against // what the build produced besides the archive its templates are packed into, which carries the text read back from the // module's files/ directory as the real build reads it (novox/hq ADR 0349 §2) — so a module whose file text moved into // templates composes with it, and one that did not is resolved as it was. func catalogueResolved(t *testing.T, module string, built ...mcat.Built) mcat.Manifest { t.Helper() root, dir := moduleTree(t, module) m, err := mcat.ReadTreeManifestWritten(root, dir) if err != nil { t.Fatalf("%s does not parse:\n%v", module, err) } if name := mcat.TemplatesArtifact(m); name != "" { read, err := mcat.ReadTemplates(m, dir) if err != nil { t.Fatalf("%s: %v", module, err) } // By the path inside the archive, as the build reads it back, not by the path in the module's directory. texts := map[string]string{} for path, text := range read { texts[strings.TrimPrefix(path, m.TemplatesDir()+"/")] = text } built = append(built, mcat.Built{Name: name, Kind: mcat.ArtifactArchive, Reference: mcat.ArtifactStoreScheme + module + "/" + name + "/blobs/" + bundleDigest, Digest: bundleDigest, Texts: texts}) } held, err := m.Resolve(built) if err != nil { t.Fatalf("%s does not resolve against its own build: %v", module, err) } return held }