// PEP-232, spec docker-platform «Mac выводится из контура только после переезда адресов»: the scripts run // by an administrator on the Mac and on the server, here — on temp dirs with mock launchctl, ssh and docker. import assert from "node:assert/strict"; import { spawnSync } from "node:child_process"; import { chmodSync, existsSync, mkdirSync, mkdtempSync, readFileSync, rmSync, statSync, writeFileSync } from "node:fs"; import { tmpdir } from "node:os"; import { join } from "node:path"; import { fileURLToPath } from "node:url"; import { after, describe, test } from "node:test"; const script = (p: string) => fileURLToPath(new URL(`../../../scripts/server-move/${p}`, import.meta.url)); const tmp = mkdtempSync(join(tmpdir(), "pep232-")); after(() => rmSync(tmp, { recursive: true, force: true })); const OLD_KEY = ["GITEA", "OLD", "TOKEN"].join("_"); function bin(dir: string, name: string, body: string) { mkdirSync(dir, { recursive: true }); const f = join(dir, name); writeFileSync(f, `#!/usr/bin/env bash\n${body}\n`); chmodSync(f, 0o755); return f; } /** A Mac: two loaded agents with plists, the server route, a deferred Bugsink route. */ function mac(name: string, serverReady: boolean) { const root = join(tmp, name); const agents = join(root, "LaunchAgents"); const dyn = join(root, "dynamic"); const state = join(root, "loaded"); mkdirSync(agents, { recursive: true }); mkdirSync(dyn, { recursive: true }); for (const s of ["web", "tunnel"]) writeFileSync(join(agents, `quest.telecom.peppa.${s}.plist`), ""); writeFileSync(join(agents, "com.other.plist"), ""); writeFileSync(join(dyn, "peppa-server.yml"), "http: {}"); writeFileSync(join(dyn, "bugsink.yml.before-server"), "http: {}"); writeFileSync(join(dyn, "gitea.yml"), "http: {}"); writeFileSync(state, "quest.telecom.peppa.web\nquest.telecom.peppa.tunnel\n"); const bins = join(root, "bin"); const launchctl = bin(bins, "launchctl", ` case "$1" in list) echo "PID Status Label"; while read -r l; do echo "1 0 $l"; done < "${state}" ;; bootout) l=\${2##*/}; grep -vx "$l" "${state}" > "${state}.n" || true; mv "${state}.n" "${state}" ;; esac`); const ssh = bin(bins, "ssh", `echo "ssh $*" >> "${root}/ssh.log"; ${serverReady ? "exit 0" : 'echo "адреса ещё не на туннеле"; exit 1'}`); const env = { ...process.env, PEPPA_LAUNCHCTL_BIN: launchctl, PEPPA_SSH_BIN: ssh, PEPPA_LAUNCH_AGENTS: agents, PEPPA_LAUNCHD_RETIRED: join(root, "retired"), PEPPA_TRAEFIK_DYNAMIC: dyn, PEPPA_PUBLIC_HEALTH_URL: "http://127.0.0.1:1/api/health", OLD_GITEA_URL: "http://127.0.0.1:1", PEPPA_DECOMMISSION_SETTLE_SEC: "0", }; const run = (...args: string[]) => spawnSync("bash", [script("mac-decommission.sh"), ...args], { env, encoding: "utf8" }); return { root, agents, dyn, state, run }; } describe("PEP-232: Mac выходит из контура", () => { test("Сценарий: скрипты разбираются оболочкой; отката на Mac больше нет", () => { for (const f of ["mac-decommission.sh", "server-finish.sh"]) { const r = spawnSync("bash", ["-n", script(f)], { encoding: "utf8" }); assert.equal(r.status, 0, `${f}: ${r.stderr}`); } assert.ok(!existsSync(script("mac-rollback.sh"))); }); test("Сценарий: адреса уже на туннеле сервера — агенты выгружены, plist и маршрут отложены", () => { const m = mac("ready", true); const plan = m.run("plan"); assert.equal(plan.status, 0, plan.stderr); assert.match(plan.stdout, /quest\.telecom\.peppa\.tunnel/); assert.ok(existsSync(join(m.agents, "quest.telecom.peppa.web.plist")), "plan changes nothing"); const r = m.run("go"); assert.equal(r.status, 0, r.stdout + r.stderr); assert.match(readFileSync(join(m.root, "ssh.log"), "utf8"), /server-finish\.sh check/); assert.equal(readFileSync(m.state, "utf8").trim(), "", "nothing of FORGE stays loaded"); for (const s of ["web", "tunnel"]) { assert.ok(!existsSync(join(m.agents, `quest.telecom.peppa.${s}.plist`))); assert.ok(existsSync(join(m.root, "retired", `quest.telecom.peppa.${s}.plist`))); } assert.ok(existsSync(join(m.agents, "com.other.plist")), "other agents of the Mac stay"); assert.ok(!existsSync(join(m.dyn, "peppa-server.yml"))); assert.ok(existsSync(join(m.dyn, "retired", "peppa-server.yml"))); assert.ok(existsSync(join(m.dyn, "retired", "bugsink.yml.before-server"))); assert.ok(existsSync(join(m.dyn, "gitea.yml")), "other routes of the Mac stay"); assert.match(r.stdout, /проверка снаружи/); }); test("Сценарий: адреса ещё идут через Mac — отказ, ничего не снято", () => { const m = mac("not-ready", false); const r = m.run("go"); assert.equal(r.status, 1); assert.match(r.stdout, /сервер не подтвердил/); assert.match(readFileSync(m.state, "utf8"), /quest\.telecom\.peppa\.tunnel/); assert.ok(existsSync(join(m.agents, "quest.telecom.peppa.tunnel.plist"))); assert.ok(existsSync(join(m.dyn, "peppa-server.yml"))); }); describe("сервер", () => { const dir = join(tmp, "server"); mkdirSync(dir, { recursive: true }); const envFile = join(dir, ".env"); const docker = (edge: string, state = "running") => bin(join(dir, "bin"), "docker", ` case "$1" in inspect) echo "${state}" ;; exec) printf '%s\\n' "${edge}" ;; esac`); const run = (mode: string, dockerBin = docker("ingress: ok")) => spawnSync("bash", [script("server-finish.sh"), mode], { env: { ...process.env, PEPPA_ENV_FILE: envFile, PEPPA_DOCKER: dockerBin }, encoding: "utf8", }); test("Сценарий: check — без туннеля сервера отказ, с переписываемым адресом отказ, иначе можно", () => { writeFileSync(envFile, "PEPPA_SECRET=x\n"); let r = run("check"); assert.equal(r.status, 1); assert.match(r.stdout, /нет CLOUDFLARE_TUNNEL_ID/); writeFileSync(envFile, "PEPPA_SECRET=x\nCLOUDFLARE_TUNNEL_ID=abc\n"); r = run("check", docker("(dry run) ingress: planned\n~ shop-prod.telecom.quest ← mac.cfargotunnel.com (будет переписано)")); assert.equal(r.status, 1); assert.match(r.stdout, /shop-prod\.telecom\.quest/); r = run("check", docker("ingress: ok", "exited")); assert.equal(r.status, 1); assert.match(r.stdout, /peppa-cloudflared: exited/); r = run("check", docker("(dry run) ingress: ok")); assert.equal(r.status, 0, r.stdout + r.stderr); assert.match(r.stdout, /Mac можно выводить/); }); test("Сценарий: ключ старой Gitea убран, копия рядом, права 0600", () => { writeFileSync(envFile, `PEPPA_SECRET=x\n${OLD_KEY}=old\nGITEA_TOKEN=new\n`, { mode: 0o600 }); const r = run("env"); assert.equal(r.status, 0, r.stdout + r.stderr); assert.equal(readFileSync(envFile, "utf8"), "PEPPA_SECRET=x\nGITEA_TOKEN=new\n"); assert.equal(statSync(envFile).mode & 0o777, 0o600); assert.match(readFileSync(`${envFile}.before-pep-232`, "utf8"), new RegExp(`${OLD_KEY}=old`)); assert.equal(statSync(`${envFile}.before-pep-232`).mode & 0o777, 0o600); const again = run("env"); assert.equal(again.status, 0); assert.match(again.stdout, /уже нет/); }); }); });