Files
mtusi2026lab7/scripts/test-session.ts

112 lines
3.0 KiB
TypeScript

import { randomBytes } from "node:crypto";
import { mkdir, copyFile, writeFile } from "node:fs/promises";
import { resolve } from "node:path";
import { bootstrap } from "./bootstrap";
import { createCA, issue } from "./pki";
import { local, root, compose, requireThat, safeFailure } from "./runtime";
import { provisionDB } from "./db-provision";
import { postgresTransport } from "../student-tests/postgres-transport";
import { run } from "./runtime";
import { images } from "./topology";
try {
const password = randomBytes(32).toString("hex"); // Deliberately process-only: disposable test stand, manual mode uses operator's hidden prompt.
await bootstrap(password);
await createCA(resolve(local, "operator", "foreign-ca"), password);
await issue(
resolve(local, "operator", "foreign-ca"),
resolve(local, "pki", "foreign-lb"),
"lb",
password,
);
await compose(["config", "--quiet"]);
// Validate every generated gateway with the installed native parser before any service starts.
for (const service of [
"lb",
"api1-gateway",
"api2-gateway",
"worker-gateway",
"broker-gateway",
"s3-gateway",
"collector-gateway",
"openobserve-gateway",
"grafana-gateway",
])
await compose([
"run",
"--rm",
"--no-deps",
service,
"haproxy",
"-c",
"-f",
"/usr/local/etc/haproxy/haproxy.cfg",
]);
await compose([
"up",
"-d",
"postgres",
"broker-gateway",
"broker",
"worker-gateway",
]);
for (let attempt = 0; attempt < 30; attempt++) {
const ready = await compose(
[
"exec",
"-T",
"postgres",
"pg_isready",
"-h",
"127.0.0.1",
"-U",
"operator",
"-d",
"messenger",
],
{ allowFailure: true },
);
if (ready.code === 0) break;
requireThat(attempt < 29, "POSTGRES_STARTUP_TIMEOUT");
await Bun.sleep(1000);
}
await provisionDB(password);
await postgresTransport();
console.log("PASS_BASE_TRANSPORT_TESTS_NO_NEW_IMAGES");
await writeFile(
resolve(local, "test-session-ready.json"),
JSON.stringify({
state: "WAITING_EXISTING_IMAGES_ONLY",
pid: process.pid,
password_persisted: false,
}),
);
// No pulls occur here. Root owns download approval and source handover; continue only when both arrive.
for (let attempt = 0; attempt < 3600; attempt++) {
const available = await run(
[
"docker",
"image",
"inspect",
images.bao,
images.collector,
images.openobserve,
images.grafana,
],
{ allowFailure: true },
);
if (
available.code === 0 &&
(await Bun.file(resolve(root, "BACKEND-READY.json")).exists())
) {
const ready = await import("./acceptance-session");
await ready.acceptanceSession(password);
console.log("PASS_SECURITY_SESSION_COMPLETE");
process.exit(0);
}
await Bun.sleep(10_000);
}
throw new Error("AWAITING_ROOT_IMAGES_AND_CHECKED_BACKEND");
} catch (e) {
safeFailure(e);
}