import { resolve } from "node:path"; import { mkdir, mkdtemp, writeFile } from "node:fs/promises"; const root = resolve(import.meta.dir, ".."); if (process.argv.includes("--check")) { console.log("Parsed isolated k3s reconciliation runner"); process.exit(0); } const cluster = "vp-lab4-r-" + crypto.randomUUID().replaceAll("-", "").slice(0, 8); const env = { ...process.env, VP_CLUSTER: cluster, VP_RUNTIME: "k3s", PUBLIC_PORT: "28404", BASE_URL: "http://127.0.0.1:28404", }; const parent = resolve(root, "evidence/k3s/reconciliation"); await mkdir(parent, { recursive: true }); const evidence = await mkdtemp(resolve(parent, "run-")); let stage = "preflight", created = false, primary: unknown; const cleanupErrors: string[] = []; await writeFile( resolve(evidence, "run.json"), JSON.stringify({ cluster, started_at: new Date().toISOString(), state: "started", }) + "\n", { flag: "wx" }, ); async function command(action: string[]) { const p = Bun.spawn( [process.execPath, "--no-env-file", "scripts/k3s.ts", ...action], { cwd: root, env, stdout: "pipe", stderr: "pipe", signal: AbortSignal.timeout(420000), }, ); const [out, err] = await Promise.all([ new Response(p.stdout).text(), new Response(p.stderr).text(), ]); await writeFile(resolve(evidence, stage + ".txt"), out + err); if ((await p.exited) !== 0) throw new Error(stage + " failed"); return out; } try { stage = "create_isolated_cluster"; created = true; await command(["up"]); stage = "tests"; const out = await command(["task", "integration"]); for (const name of [ "TestReconciliationAgainstPostgresAndPrivateS3", "TestS3InventoryAndStatReturnComparableVersionMetadata", "TestFixtureCleanupRejectsForeignMembership", ]) if (!out.includes("--- PASS: " + name)) throw new Error("Required integration test did not pass"); } catch (error) { primary = error; } finally { if (created) { stage = "cleanup"; try { await command(["destroy"]); } catch { cleanupErrors.push("isolated_cluster"); } } } await writeFile( resolve(evidence, "result.json"), JSON.stringify( { complete: !primary && !cleanupErrors.length, cluster, primary: primary instanceof Error ? primary.message : primary ? "unknown" : null, cleanup_errors: cleanupErrors, }, null, 2, ) + "\n", { flag: "wx" }, ); if (primary || cleanupErrors.length) throw new Error("Isolated reconciliation failed; inspect this run"); console.log("PASS isolated k3s reconciliation: " + evidence);