feat(lanes): add --qc boot flag + QC_BOOT_ENV for deterministic QC stacks (E1)

This commit is contained in:
2026-08-04 17:51:03 +07:00
parent c37933adfe
commit 31f984c750
6 changed files with 195 additions and 8 deletions
+92
View File
@@ -0,0 +1,92 @@
/**
* @file Tests for profile parsing and hook environment setup.
* @author Nguyễn Ngọc Trí Vĩ <vinnt@smartgift.vn>
*/
const os = require("node:os");
const pathMod = require("node:path");
const fsMod = require("node:fs");
const SUITE_ROOT = fsMod.mkdtempSync(pathMod.join(os.tmpdir(), "ccam-profile-"));
process.env.DASHBOARD_DB_PATH = pathMod.join(SUITE_ROOT, "dashboard.db");
process.env.LANES_ROOT = pathMod.join(SUITE_ROOT, "lanes");
process.env.CCAM_SECRETS_PATH = pathMod.join(SUITE_ROOT, "secrets.env");
const { describe, it, after } = require("node:test");
const assert = require("node:assert/strict");
const lanesLib = require("../lib/lanes");
const slots = require("../lib/lane-slots");
const profileLib = require("../lib/lane-profile");
after(() => fsMod.rmSync(SUITE_ROOT, { recursive: true, force: true }));
let laneSeq = 0;
/** A managed lane row backed by a real directory. */
function makeLane(over = {}) {
laneSeq += 1;
const cwd = pathMod.join(SUITE_ROOT, `profile-lane-cwd-${laneSeq}`);
fsMod.mkdirSync(cwd, { recursive: true });
return lanesLib.createLane({
title: `profile-lane ${laneSeq}`,
cwd,
kind: "managed",
source_repo: cwd,
...over,
});
}
/** Write a profile into a repo directory. `hooks` maps name -> script body. */
function writeProfile(root, envText, hooks = {}) {
const dir = pathMod.join(root, ".ccam", "profile");
fsMod.mkdirSync(pathMod.join(dir, "hooks"), { recursive: true });
fsMod.writeFileSync(pathMod.join(dir, "profile.env"), envText);
for (const [name, body] of Object.entries(hooks)) {
const file = pathMod.join(dir, "hooks", `${name}.sh`);
fsMod.writeFileSync(file, body);
fsMod.chmodSync(file, 0o755);
}
return dir;
}
describe("parseQcBootEnv", () => {
it("parses space-separated KEY=value pairs", () => {
assert.deepEqual(profileLib.parseQcBootEnv("MOCK_PAYMENTS=1 STUB_EMAIL=1"), {
MOCK_PAYMENTS: "1",
STUB_EMAIL: "1",
});
});
it("returns an empty object for an empty or missing declaration", () => {
assert.deepEqual(profileLib.parseQcBootEnv(""), {});
assert.deepEqual(profileLib.parseQcBootEnv(undefined), {});
});
it("ignores a malformed token with no =", () => {
assert.deepEqual(profileLib.parseQcBootEnv("GOOD=1 malformed"), { GOOD: "1" });
});
});
describe("hookEnv extraEnv", () => {
it("merges extraEnv on top of everything else, including profile.env", () => {
const lane = makeLane();
writeProfile(lane.cwd, "PORTS=api\n");
slots.allocateSlot(lane.id);
const current = lanesLib.getLane(lane.id);
const profile = profileLib.resolveProfile(current);
const env = profileLib.hookEnv(current, profile, { PORTS: "overridden" });
assert.equal(env.PORTS, "overridden");
slots.releaseSlot(lane.id);
});
it("defaults extraEnv to nothing when omitted (existing callers unaffected)", () => {
const lane = makeLane();
writeProfile(lane.cwd, "PORTS=api\n");
slots.allocateSlot(lane.id);
const current = lanesLib.getLane(lane.id);
const profile = profileLib.resolveProfile(current);
const env = profileLib.hookEnv(current, profile);
assert.equal(env.LANE_ID, String(current.id));
slots.releaseSlot(lane.id);
});
});
+64
View File
@@ -354,6 +354,70 @@ describe("lifecycle", () => {
});
});
describe("upLane qc option", () => {
it("injects QC_BOOT_ENV into the boot hook's environment when qc:true", async () => {
const lane = makeLane();
writeProfile(lane.cwd, "PORTS=web\nPORT_BASE_web=19300\nQC_BOOT_ENV=SOME_QC_VAR=from-qc\n", {
boot: [
"#!/usr/bin/env bash",
"set -euo pipefail",
'echo "QC=$SOME_QC_VAR" > "$LANE_DIR/qc-marker.txt"',
'harness_spawn web "$LANE_DIR" python3 -m http.server "$WEB_PORT" --bind 127.0.0.1',
"",
].join("\n"),
health: [
"#!/usr/bin/env bash",
"set -euo pipefail",
"for _ in $(seq 1 50); do",
' if curl -sf "http://127.0.0.1:$WEB_PORT/" >/dev/null; then exit 0; fi',
" sleep 0.2",
"done",
"exit 1",
"",
].join("\n"),
});
await runtime.upLane(lanesLib.getLane(lane.id), { qc: true });
const booted = lanesLib.getLane(lane.id);
const markerPath = pathMod.join(booted.cwd, "qc-marker.txt");
const marker = fsMod.readFileSync(markerPath, "utf8");
assert.match(marker, /QC=from-qc/);
slots.releaseSlot(lane.id);
});
it("does not touch the environment when qc is omitted (default false)", async () => {
const lane = makeLane();
writeProfile(lane.cwd, "PORTS=web\nPORT_BASE_web=19400\nQC_BOOT_ENV=SOME_QC_VAR=from-qc\n", {
boot: [
"#!/usr/bin/env bash",
"set -euo pipefail",
'echo "QC=${SOME_QC_VAR:-not-set}" > "$LANE_DIR/qc-marker-no-qc.txt"',
'harness_spawn web "$LANE_DIR" python3 -m http.server "$WEB_PORT" --bind 127.0.0.1',
"",
].join("\n"),
health: [
"#!/usr/bin/env bash",
"set -euo pipefail",
"for _ in $(seq 1 50); do",
' if curl -sf "http://127.0.0.1:$WEB_PORT/" >/dev/null; then exit 0; fi',
" sleep 0.2",
"done",
"exit 1",
"",
].join("\n"),
});
await runtime.upLane(lanesLib.getLane(lane.id));
const booted = lanesLib.getLane(lane.id);
const markerPath = pathMod.join(booted.cwd, "qc-marker-no-qc.txt");
const marker = fsMod.readFileSync(markerPath, "utf8");
assert.match(marker, /QC=not-set/);
slots.releaseSlot(lane.id);
});
});
describe("allocation is not client-patchable", () => {
it("ignores slot and ports coming through updateLane", () => {
const lane = makeLane();