A bed that takes the store away while a machine enrols, and holds the enrolment to completing on its own (novox/hq issue 083)
This commit is contained in:
@@ -0,0 +1,165 @@
|
||||
/**
|
||||
* **Nothing a machine sends while the store restarts is lost** (novox/hq issues 082, 083).
|
||||
*
|
||||
* The foundation's store is recreated when it is adopted, and for those seconds the control plane
|
||||
* cannot write. This bed takes the store away on the control-node, has a second machine enrol into
|
||||
* the gap, and brings the store back after the control plane has had to say "not now":
|
||||
*
|
||||
* - the enrolment completes on its own, with the keys it started with — the host asked again
|
||||
* while the mesh said it could not answer, and the token was not spent until it could;
|
||||
* - the control plane really did say "not now", so the gap was hit, not missed;
|
||||
* - the machine then applies what it is pushed and is heard from.
|
||||
*
|
||||
* It needs a host binary and the foundation bundle:
|
||||
*
|
||||
* MESH_LAB_HOST_BINARY=.../mesh-host
|
||||
* MESH_LAB_BUNDLE=.../examples/foundation-first-node.lock
|
||||
*/
|
||||
import { test, before, after } from "node:test";
|
||||
import assert from "node:assert/strict";
|
||||
import { existsSync } 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 } from "./harness.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/)"
|
||||
: false;
|
||||
|
||||
const SCENARIO = "store-window";
|
||||
let instanceId = "";
|
||||
|
||||
function quote(s: string): string {
|
||||
return `'${s.replaceAll("'", `'\\''`)}'`;
|
||||
}
|
||||
|
||||
async function on(machine: string, 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(machine: string, command: string, timeoutMs?: number): Promise<string> {
|
||||
const { out, ok } = await on(machine, command, timeoutMs);
|
||||
if (!ok) throw new Error(`${machine}: ${command}\n${out}`);
|
||||
return out;
|
||||
}
|
||||
|
||||
async function mesh(command: string, timeoutMs?: number): Promise<string> {
|
||||
return must("anchor", `docker exec mesh-controller /mesh-controller ${command}`, timeoutMs);
|
||||
}
|
||||
|
||||
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 until(what: string, within: number, check: () => Promise<boolean>, why: () => Promise<string>): Promise<void> {
|
||||
const end = Date.now() + within;
|
||||
while (Date.now() < end) {
|
||||
if (await check()) return;
|
||||
await new Promise((r) => setTimeout(r, 3000));
|
||||
}
|
||||
assert.fail(`${what} did not happen within ${Math.round(within / 1000)}s:\n${await why()}`);
|
||||
}
|
||||
|
||||
before(async () => {
|
||||
if (skip) return;
|
||||
const raised = await raise(loadScenario(`scenarios/${SCENARIO}.yml`), {
|
||||
onProgress: (m) => console.log(`raise: ${m}`),
|
||||
});
|
||||
instanceId = raised.instanceId;
|
||||
|
||||
await must("anchor", `cat > /tmp/foundation.lock <<'MESHBUNDLE'\n${foundationBundle(bundle, raised.images)}\nMESHBUNDLE`);
|
||||
await must("anchor", `${HOST_PATH} apply /tmp/foundation.lock`, 600_000);
|
||||
const up = await must("anchor", `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}`);
|
||||
}
|
||||
await mesh("node add anchor");
|
||||
const own = tokenFrom(await mesh("token issue --node anchor"));
|
||||
await must("anchor", `${HOST_PATH} enrol --token ${quote(own)}`);
|
||||
await must("anchor", `nohup ${HOST_PATH} run > /var/log/mesh-host.log 2>&1 & sleep 3`);
|
||||
}, { timeout: 1_800_000 });
|
||||
|
||||
after(async () => {
|
||||
if (process.env["MESH_LAB_KEEP"]) { console.log(`MESH_LAB_KEEP set — leaving ${instanceId} standing`); return; }
|
||||
if (instanceId) await destroy(instanceId);
|
||||
await destroyAll(`${SCENARIO}-`);
|
||||
}, { timeout: 600_000 });
|
||||
|
||||
test("a machine enrolling while the store is away joins when it comes back, with the keys it started with", {
|
||||
skip, timeout: 900_000,
|
||||
}, async () => {
|
||||
await mesh("node add laptop");
|
||||
const token = tokenFrom(await mesh("token issue --node laptop"));
|
||||
|
||||
// The store goes away, as it does when the foundation is adopted; the broker stays, so the
|
||||
// enrolment reaches the control plane and the control plane cannot write.
|
||||
await must("anchor", `docker stop mesh-store`);
|
||||
|
||||
// The machine enrols into the gap. In the background: it will be told "not now" and keep asking.
|
||||
await must("laptop", `nohup sh -c ${quote(`${HOST_PATH} enrol --token ${quote(token)} > /tmp/enrol.log 2>&1; ` +
|
||||
`echo "exit=$?" >> /tmp/enrol.log`)} > /dev/null 2>&1 & sleep 1`);
|
||||
|
||||
// The control plane said "not now" at least once — so the gap was hit, not missed.
|
||||
await until("the control plane asking the machine to enrol again", 90_000,
|
||||
async () => /asked "laptop" to enrol again/.test((await on("anchor", `docker logs mesh-controller 2>&1`)).out),
|
||||
async () => `--- controller ---\n${(await on("anchor", `docker logs --tail 30 mesh-controller 2>&1`)).out}\n` +
|
||||
`--- laptop enrol ---\n${(await on("laptop", `cat /tmp/enrol.log`)).out}`);
|
||||
|
||||
// The store comes back; the enrolment completes on its own, with nothing done by hand.
|
||||
await must("anchor", `docker start mesh-store`);
|
||||
await until("the enrolment completing", 150_000,
|
||||
async () => /exit=/.test((await on("laptop", `cat /tmp/enrol.log`)).out),
|
||||
async () => `--- laptop enrol ---\n${(await on("laptop", `cat /tmp/enrol.log`)).out}\n` +
|
||||
`--- controller ---\n${(await on("anchor", `docker logs --tail 30 mesh-controller 2>&1`)).out}`);
|
||||
const said = (await on("laptop", `cat /tmp/enrol.log`)).out;
|
||||
assert.match(said, /exit=0/, `the enrolment did not complete after the store came back:\n${said}`);
|
||||
assert.match(said, /enrolled as laptop/, `the machine was not enrolled as laptop:\n${said}`);
|
||||
|
||||
// Enrolled with the keys it generated at the start — one attempt, not a new identity — and the
|
||||
// mesh holds its sealing key, which it recorded only once the store was back.
|
||||
const generated = said.match(/generated this node's identity: (\S+)/);
|
||||
assert.ok(generated, `the enrolment did not say which identity it generated:\n${said}`);
|
||||
assert.equal((said.match(/generated this node's identity:/g) ?? []).length, 1,
|
||||
`the machine generated more than one identity:\n${said}`);
|
||||
const recorded = await must("anchor",
|
||||
`docker exec mesh-store psql -U postgres -d inventory -qAt ` +
|
||||
`-c "select name from node where sealing_key is not null order by name"`);
|
||||
assert.match(recorded, /(^|\n)laptop(\n|$)/, `the mesh holds no sealing key for laptop:\n${recorded}`);
|
||||
|
||||
// And the machine is a working member: pushed something, it applies it and is heard from.
|
||||
await must("laptop", `nohup ${HOST_PATH} run > /var/log/mesh-host.log 2>&1 & sleep 3`);
|
||||
await must("anchor", `printf %s '{"module":"marker","version":"1","resources":[` +
|
||||
`{"id":"marker","type":"file","path":"/etc/store-window","content":"joined\\\\n","mode":"0644"}]}' > /tmp/marker.json`);
|
||||
await must("anchor", `docker cp /tmp/marker.json mesh-controller:/marker.json`);
|
||||
await mesh("module add /marker.json");
|
||||
await mesh("assign laptop marker");
|
||||
await mesh("push laptop");
|
||||
await until("the machine applying what it was pushed", 120_000,
|
||||
async () => (await on("laptop", `grep -q joined /etc/store-window`)).ok,
|
||||
async () => (await on("laptop", `tail -20 /var/log/mesh-host.log`)).out);
|
||||
await until("the mesh hearing the machine's report", 120_000,
|
||||
async () => {
|
||||
const r = await on("anchor", `docker exec mesh-controller /mesh-controller status --json`);
|
||||
if (!r.ok) return false;
|
||||
const state = JSON.parse(r.out) as { reported: { node: string; outcome: string; current: boolean }[] };
|
||||
return state.reported.some((w) => w.node === "laptop" && w.outcome === "applied" && w.current);
|
||||
},
|
||||
async () => (await on("anchor", `docker exec mesh-controller /mesh-controller status 2>&1`)).out);
|
||||
});
|
||||
Reference in New Issue
Block a user