/** * The mesh assigns confluence — a tools-only, outbound-only external-SaaS integration * (novox/hq ADR 0039), the shape gitlab proved — and its runtime comes up and serves the module's * full tool surface, with NO valid Confluence credentials. * * confluence is the thinnest converted shape: no service container, no listener, no provisioner — * only a broker-bound runtime that serves the module's Confluence tools under a scoped account * (ADR 0047). The sharp point (the Servarr lesson) is that the runtime MUST register and serve every * tool even though the lab has no real Confluence and confluence's own token is a mesh-minted * own-secret pointing at nothing. confluence's client is built lazily and never throws at * registration, so: * - the mesh-runtime-confluence container comes up and STAYS up (no crash on a missing token); * - it logs `[mesh-tools] serving 3 tool(s)` — the install proof; * - it binds its serve queues on the broker (e.g. serve.confluence.confluence_search), so the tools * are actually reachable, not merely loaded; * - it got its own scoped broker account (anchor-confluence). * A tool would only fail if it were actually invoked without real creds — which this bed does not do, * because the point is exactly that serving does not require them. * * MESH_LAB_HOST_BINARY=.../mesh-host MESH_LAB_BUNDLE=.../examples/foundation-first-node.lock * scripts/build-module-runtime.sh confluence builds mesh-runtime-confluence:development into the * local daemon; scenarios/tools-confluence.yml stocks it. There is no service image. */ import { test, before, after } from "node:test"; import assert from "node:assert/strict"; import { existsSync, readFileSync } from "node:fs"; import { loadScenario } from "../../src/declaration/parse.ts"; import { raise } from "../../src/lifecycle/raise.ts"; import { destroy, exec } from "../../src/lifecycle/operate.ts"; import { hostBinaryPath, HOST_PATH } from "../../src/lifecycle/place.ts"; import { labIsUsable, destroyAll, foundationBundle, onTheMachine, catalogueModule, catalogueIsPresent } from "./harness.ts"; import type { HeldImage } from "../../src/pinning.ts"; const capability = await labIsUsable(); const binary = hostBinaryPath(); const bundle = process.env["MESH_LAB_BUNDLE"] ?? ""; const skip = !capability.usable ? `lab not usable: ${capability.why}` : !binary || !existsSync(binary) ? "MESH_LAB_HOST_BINARY is not set to a built mesh-host" : !bundle || !existsSync(bundle) ? "MESH_LAB_BUNDLE is not set to a foundation bundle (mesh-host examples/)" : catalogueIsPresent(); const SCENARIO = "tools-confluence"; const MACHINE = "anchor"; let instanceId = ""; /** The mesh's own images, as the machines hold them. */ let held: HeldImage[] = []; function quote(s: string): string { return `'${s.replaceAll("'", `'\\''`)}'`; } async function on(command: string, timeoutMs?: number): Promise<{ out: string; ok: boolean }> { const { stdout } = await exec(instanceId, MACHINE, [ "sh", "-c", `exec 2>&1\n${command}\necho "__exit=$?"`, ], timeoutMs); const marker = stdout.lastIndexOf("__exit="); if (marker < 0) return { out: stdout, ok: false }; return { out: stdout.slice(0, marker), ok: stdout.slice(marker + 7).trim() === "0" }; } async function must(command: string, timeoutMs?: number): Promise { const { out, ok } = await on(command, timeoutMs); if (!ok) throw new Error(`${MACHINE}: ${command}\n${out}`); return out; } /** The control plane, a container on the node. */ async function mesh(command: string, timeoutMs?: number): Promise { return must(`docker exec mesh-controller /mesh-controller ${command}`, timeoutMs); } /** The reference a manifest should carry, once this scenario has been raised. */ /** What a manifest's image reference becomes on the machine — ours by ID, everything else as written. */ function pinned(reference: string): string { return onTheMachine(reference, held); } /** The foundation bundle: ours by the ID the machine holds, everything else upstream. */ function bundleFor(images: HeldImage[]): string { return foundationBundle(bundle, images); } function tokenFrom(said: string): string { const found = said.split("\n").map((l) => l.trim()).find((l) => l.length > 100 && !l.includes(" ")); assert.ok(found, `no token in:\n${said}`); return found; } async function settled(withinMs = 600_000): Promise { const until = Date.now() + withinMs; let last = ""; while (Date.now() < until) { const asked = await on(`docker exec mesh-controller /mesh-controller status --json`); if (asked.ok) { try { const state = JSON.parse(asked.out) as { wrong: { node: string; outcome: string }[]; waiting: { node: string }[]; reported: { node: string; outcome: string; current: boolean }[]; }; const bad = state.wrong.find((w) => w.node === MACHINE); if (bad) throw new Error(`${MACHINE} did not apply what it was sent: ${bad.outcome}\n${asked.out}`); const word = state.reported.find((r) => r.node === MACHINE); if (!state.waiting.some((w) => w.node === MACHINE) && word?.outcome === "applied" && word.current) return; last = asked.out; } catch (err) { if (err instanceof Error && err.message.includes("did not apply")) throw err; last = asked.out; } } await new Promise((r) => setTimeout(r, 5000)); } throw new Error(`${MACHINE} never caught up within ${Math.round(withinMs / 1000)}s. Last:\n${last}`); } before(async () => { if (skip) return; const raised = await raise(loadScenario(`scenarios/${SCENARIO}.yml`), { onProgress: (m) => console.log(`raise: ${m}`), }); instanceId = raised.instanceId; held = raised.images; // Raise the foundation — store, broker, control — from the bundle. await must(`cat > /tmp/foundation.lock <<'MESHBUNDLE'\n${bundleFor(raised.images)}\nMESHBUNDLE`); await must(`${HOST_PATH} apply /tmp/foundation.lock`, 600_000); const up = await must(`docker ps --format '{{.Names}}'`); for (const c of ["mesh-store", "mesh-broker", "mesh-controller"]) { assert.match(up, new RegExp(c), `the foundation did not raise ${c}:\n${up}`); } // The node joins its own mesh, so it is a node the mesh can assign to, and start the host so it // applies what it is pushed. await mesh(`node add ${MACHINE}`); const token = tokenFrom(await mesh(`token issue --node ${MACHINE}`)); await must(`${HOST_PATH} enrol --token ${quote(token)}`); await must(`nohup ${HOST_PATH} run > /var/log/mesh-host.log 2>&1 & sleep 3`); }, { timeout: 1_800_000 }); after(async () => { if (instanceId) await destroy(instanceId); await destroyAll(`${SCENARIO}-`); }, { timeout: 600_000 }); test("the mesh assigns confluence: its tools-only runtime comes up and serves the full tool surface with no valid token", { skip, timeout: 1_500_000, }, async () => { // confluence is tools-only and outbound-only: no service, no listener, no provisioner — just a // broker-bound runtime serving the module's tools. Its own token is a mesh-minted own-secret; the // lab has no real Confluence, so the token points at nothing — and that is the case under test: the // runtime must serve every tool regardless. The manifest is the catalogue's own, its runtime // artifact the image this scenario stocked (novox/hq 04-ISSUES/073). const manifest = catalogueModule("confluence", held); await must(`printf %s ${quote(manifest)} > /tmp/confluence.json && docker cp /tmp/confluence.json mesh-controller:/confluence.json`); await mesh("module add /confluence.json"); // confluence serves tools, so it is issued a scoped broker account (it emits/consumes nothing else). const issued = await mesh(`module issue confluence --node ${MACHINE}`); assert.match(issued, /scoped to what it emits and consumes/, issued); await mesh(`assign ${MACHINE} confluence`); // ONE push, ONE convergence. await mesh(`push ${MACHINE}`); await settled(); // --- the runtime is up and STABLE — it did not crash on a missing/invalid token ----------------- const running = await must(`docker ps --format '{{.Names}}'`); assert.match(running, /(^|\n)mesh-runtime-confluence(\n|$)/, `mesh-runtime-confluence is not running after the push:\n${running}\n---host log---\n${(await on(`tail -40 /var/log/mesh-host.log`)).out}`); await new Promise((r) => setTimeout(r, 5000)); const state = (await must(`docker inspect -f '{{.State.Running}} {{.RestartCount}}' mesh-runtime-confluence`)).trim(); assert.equal(state, "true 0", `the confluence runtime is not up and stable — it should serve tools even with no valid token:\n${state}\n` + `${(await on(`docker logs mesh-runtime-confluence 2>&1 | tail -30`)).out}`); // --- the install proof: the runtime logs it is serving the full tool surface -------------------- let log = ""; const untilLogged = Date.now() + 60_000; while (Date.now() < untilLogged) { log = (await on(`docker logs mesh-runtime-confluence 2>&1`)).out; if (/\[mesh-tools\] serving \d+ tool\(s\)/.test(log)) break; await new Promise((r) => setTimeout(r, 3000)); } const served = log.match(/\[mesh-tools\] serving (\d+) tool\(s\)/); assert.ok(served, `the confluence runtime never logged it is serving tools:\n${log}`); assert.equal(Number(served[1]), 3, `the confluence runtime served ${served?.[1]} tools, expected the full surface of 3:\n${log}`); // --- the tools are actually reachable: the runtime bound its serve queues on the broker --------- let served2 = ""; const untilServing = Date.now() + 90_000; while (Date.now() < untilServing) { served2 = await must(`docker exec mesh-broker lavinmqctl list_queues name 2>&1 || true`); if (/serve\.confluence\.confluence_search/.test(served2)) break; await new Promise((r) => setTimeout(r, 3000)); } assert.match(served2, /serve\.confluence\.confluence_search/, `confluence's runtime never bound its serve queue:\n${(await on(`docker logs mesh-runtime-confluence 2>&1 | tail -20`)).out}\n---\n${served2}`); // --- and the control plane is the way to ask it (novox/hq ADR 0095, issue 049) ------------------ // No account in the mesh but the control plane's may create a reply queue and publish to a // module's request key. Asked through it, the tool ANSWERS — with an error, since the lab has no // Confluence and no token, which is an answer: the round trip is what is under test, and a // timeout would read differently. const asked = await on(`docker exec mesh-controller /mesh-controller ask confluence confluence_search '{"query":"mesh"}' --wait 60s`, 90_000); assert.doesNotMatch(asked.out, /did not answer/, `the tool was never reached through the control plane:\n${asked.out}`); assert.match(asked.out, /"(result|error)"/, `the control plane printed no answer:\n${asked.out}`); // --- confluence got its own scoped broker account ----------------------------------------------- const users = await must(`docker exec mesh-broker lavinmqctl list_users 2>&1`); assert.match(users, /anchor-confluence/, `the scoped account anchor-confluence is not on the broker:\n${users}`); });