forgedock 1.60.1 → 1.61.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/bin/config-schema.mjs +44 -1
- package/bin/engine/runlog.mjs +52 -0
- package/bin/engine-cli.mjs +9 -4
- package/bin/env/bench.mjs +38 -0
- package/bin/env/cli.mjs +134 -0
- package/bin/env/env.mjs +339 -0
- package/bin/forgedock.mjs +32 -3
- package/bin/help-text.mjs +2 -0
- package/bin/lean/ci-budget.mjs +3 -1
- package/bin/lean/ci-classify.mjs +74 -0
- package/bin/lean/ci-classify.test.mjs +147 -0
- package/bin/lean/ci-emit.mjs +175 -0
- package/bin/lean/ci-emit.test.mjs +177 -0
- package/bin/lean/ci-preflight.mjs +183 -0
- package/bin/lean/ci-preflight.test.mjs +122 -0
- package/bin/lean/ci-runner-health.mjs +213 -0
- package/bin/lean/ci-runner-health.test.mjs +113 -0
- package/bin/lean/ci-watch.mjs +79 -0
- package/bin/lean/ci-watch.test.mjs +80 -0
- package/bin/lean/depcache.mjs +38 -8
- package/bin/lean/deploy-stages.mjs +514 -0
- package/bin/lean/deploy-stages.test.mjs +580 -0
- package/bin/lean/effects.mjs +50 -0
- package/bin/lean/execution.mjs +8 -1
- package/bin/lean/lean.mjs +38 -6
- package/bin/lean/lean.test.mjs +61 -0
- package/bin/lean/release-deploy.mjs +12 -6
- package/bin/lean/release-flakes.mjs +16 -0
- package/bin/lean/release-flakes.test.mjs +15 -0
- package/bin/lean/release-gates.mjs +24 -1
- package/bin/lean/release-ledger.mjs +21 -0
- package/bin/lean/release-minutes-timing.test.mjs +57 -0
- package/bin/lean/release-minutes.mjs +98 -0
- package/bin/lean/release-promote.mjs +444 -0
- package/bin/lean/release-promote.test.mjs +470 -0
- package/bin/lean/release-rollback.mjs +14 -6
- package/bin/lean/release.mjs +31 -2
- package/bin/lean/sandbox-remote.mjs +11 -3
- package/bin/lean/testengine-recall.mjs +82 -0
- package/bin/lean/testengine-recall.test.mjs +103 -0
- package/bin/lean/testengine-shard.mjs +150 -0
- package/bin/lean/testengine-shard.test.mjs +111 -0
- package/bin/lean/testengine.mjs +486 -0
- package/bin/lean/testengine.test.mjs +303 -0
- package/bin/lean/verify-settle.test.mjs +156 -0
- package/bin/lean/verify.mjs +98 -3
- package/bin/pool/agent.mjs +57 -6
- package/bin/pool/ci-events.mjs +183 -0
- package/bin/pool/ci-events.test.mjs +143 -0
- package/bin/pool/docker.mjs +7 -0
- package/bin/pool/lane-tools.mjs +1 -0
- package/bin/pool/router.mjs +18 -0
- package/bin/pool/warm.mjs +1 -1
- package/bin/stats/ci-awareness.mjs +66 -0
- package/bin/stats/index.mjs +44 -3
- package/bin/stats/release-ledger.test.mjs +226 -0
- package/bin/stats/release.mjs +365 -1
- package/bin/stats/stats.test.mjs +36 -0
- package/docs/CI.md +31 -0
- package/docs/CONFIG.md +235 -2
- package/docs/ENV-LIBRARY.md +172 -0
- package/docs/STATS.md +43 -0
- package/docs/ci-events.md +88 -0
- package/docs/test-engine.md +98 -0
- package/package.json +1 -1
- package/pool/README.md +1 -0
- package/pool/image/entrypoint.sh +15 -3
- package/pool/services/docker-compose.yml +53 -0
- package/pool/services/git-mirror.sh +22 -0
- package/templates/github-actions/forge-env/action.yml +65 -0
|
@@ -0,0 +1,580 @@
|
|
|
1
|
+
// SPDX-FileCopyrightText: Copyright (c) RapierCraft Studios
|
|
2
|
+
// SPDX-License-Identifier: AGPL-3.0-or-later
|
|
3
|
+
import { describe, it } from "node:test";
|
|
4
|
+
import assert from "node:assert/strict";
|
|
5
|
+
import { readFileSync, writeFileSync, mkdirSync } from "node:fs";
|
|
6
|
+
import { join } from "node:path";
|
|
7
|
+
import { tmpDir } from "../tests/_tmp.mjs";
|
|
8
|
+
import { releaseFromCli, openRelease, RELEASE_STEPS } from "./release.mjs";
|
|
9
|
+
import { deployStep } from "./release-deploy.mjs";
|
|
10
|
+
import { runRollback } from "./release-rollback.mjs";
|
|
11
|
+
import { KINDS, CANARY, STAGES_KEY, engineOn, readEngine, validateEngine, normalise, canaryPlan, deployRisk, diffServices, stageParts, partsKey, decide, planStages, renderPlan, deployFromCli, clearStages, withEnv } from "./deploy-stages.mjs";
|
|
12
|
+
import { validateForgeYaml } from "../config-schema.mjs";
|
|
13
|
+
|
|
14
|
+
const OLD = "b".repeat(40), SHA = "a".repeat(40), SHA2 = "c".repeat(40), ID = "r20260101-000000-1";
|
|
15
|
+
const ORDER = ["build", "expand", "deploy-new", "smoke-new", "shift", "verify", "retire-old", "contract"];
|
|
16
|
+
const ENGINE = () => ({
|
|
17
|
+
enabled: true, live_release_command: "live", colour: { active_command: "active" },
|
|
18
|
+
rollback: { shift_back_command: "shift-back" },
|
|
19
|
+
stages: [
|
|
20
|
+
{ id: "build", kind: "build", run: "build", inputs: ["services/**"] },
|
|
21
|
+
{ id: "expand", kind: "migrate", run: "expand", inputs: ["db/expand/**"] },
|
|
22
|
+
{ id: "deploy-new", kind: "deploy", run: "deploy-new", inputs: ["ops/**"] },
|
|
23
|
+
{ id: "smoke-new", kind: "smoke", run: "smoke-new", inputs: ["tests/smoke/**"] },
|
|
24
|
+
{ id: "shift", kind: "shift", run: "shift", inputs: ["ops/router/**"] },
|
|
25
|
+
{ id: "verify", kind: "verify", run: "verify", inputs: ["tests/verify/**"] },
|
|
26
|
+
{ id: "retire-old", kind: "retire", run: "retire-old", inputs: ["ops/**"] },
|
|
27
|
+
{ id: "contract", kind: "contract", run: "contract", inputs: ["db/contract/**"] },
|
|
28
|
+
],
|
|
29
|
+
});
|
|
30
|
+
const TREE = { "services/api/main.py": "o1", "db/expand/001.sql": "o2", "ops/compose.yml": "o3", "tests/smoke/home.sh": "o4", "ops/router/weights": "o5", "tests/verify/v.sh": "o6", "db/contract/001.sql": "o7", "README.md": "o8" };
|
|
31
|
+
|
|
32
|
+
/**
|
|
33
|
+
* World: `live` is the sha the live record reports (the shift command at 100% moves it, shift-back restores it); `fail` maps a
|
|
34
|
+
* command to how many times it still fails; `trees` the files (path -> blob id) of each commit; `numstat` the diff to the target.
|
|
35
|
+
*/
|
|
36
|
+
function world(o = {}) {
|
|
37
|
+
const w = { live: OLD, active: "blue", fail: {}, ran: [], trees: { [SHA]: TREE }, numstat: "3\t1\tREADME.md", digests: { api: "sha256:1" }, now: 1_000_000, slept: [], gh: [], freeze: "", git: [], ...o };
|
|
38
|
+
const io = {
|
|
39
|
+
print: () => {}, now: () => w.now, sleep: async (ms) => { w.slept.push(ms); w.now += ms; },
|
|
40
|
+
rehearse: async (full) => {
|
|
41
|
+
const m = /^export (.*?); ([^;]*)$/.exec(full), cmd = m ? m[2] : full;
|
|
42
|
+
const env = Object.fromEntries([...(m?.[1] ?? "").matchAll(/(\w+)='([^']*)'/g)].map((x) => [x[1], x[2]]));
|
|
43
|
+
w.ran.push({ cmd, env });
|
|
44
|
+
w.now += w.cost?.[cmd] ?? 10;
|
|
45
|
+
if (w.fail[cmd] > 0) { w.fail[cmd] -= 1; return { ok: false, output: `${cmd} boom` }; }
|
|
46
|
+
if (cmd === "live") return w.live ? { ok: true, output: w.live } : { ok: false, output: "" };
|
|
47
|
+
if (cmd === "active") return { ok: true, output: w.active };
|
|
48
|
+
if (cmd === "expected") return { ok: true, output: "{}" };
|
|
49
|
+
if (cmd === "desired") return { ok: true, output: JSON.stringify(w.desired) };
|
|
50
|
+
if (cmd === "livesvc") return { ok: true, output: JSON.stringify(w.liveSvc) };
|
|
51
|
+
if (cmd === "build") return { ok: true, output: `built\n${JSON.stringify({ digests: w.digests })}` };
|
|
52
|
+
if (cmd === "shift" && env.FORGE_TRAFFIC_PCT === "100") w.live = env.FORGE_SHA;
|
|
53
|
+
if (cmd === "shift-back") w.live = OLD;
|
|
54
|
+
return { ok: true, output: "" };
|
|
55
|
+
},
|
|
56
|
+
git: (a) => {
|
|
57
|
+
w.git.push(a.join(" "));
|
|
58
|
+
if (a[0] === "rev-parse") { const s = a.at(-1).replace(/\^\{.*$/, ""); if (!w.trees[s] && !/^origin\//.test(s)) throw new Error("bad object"); return `${/^origin\//.test(s) ? SHA : a.at(-1).endsWith("^{tree}") ? `tree-${s}` : s}\n`; }
|
|
59
|
+
if (a[0] === "ls-tree") { const t = w.trees[a.at(-1)]; if (!t) throw new Error("bad object"); return Object.entries(t).map(([p, oid]) => `100644 blob ${oid}\t${p}`).join("\0") + "\0"; }
|
|
60
|
+
if (a[0] === "diff") { if (w.numstat == null) throw new Error("bad revision"); return w.numstat; }
|
|
61
|
+
if (a[0] === "merge-base") { if (w.notAncestor) throw new Error("exit 1"); return ""; }
|
|
62
|
+
if (a[0] === "show") { if (w.defaultYaml == null) throw new Error("not tracked"); return w.defaultYaml; }
|
|
63
|
+
throw new Error(`unexpected git ${a.join(" ")}`);
|
|
64
|
+
},
|
|
65
|
+
gh: async (a) => {
|
|
66
|
+
w.gh.push(a.join(" "));
|
|
67
|
+
if (a[0] === "variable") { if (!w.freeze) throw new Error("HTTP 404"); return w.freeze; }
|
|
68
|
+
if (a[0] === "run" && a[1] === "list") return JSON.stringify(w.runs ?? []);
|
|
69
|
+
if (a[0] === "workflow") { w.runs = [{ databaseId: (w.runs?.[0]?.databaseId ?? 0) + 1, status: "completed", conclusion: w.wfConclusion ?? "success" }, ...(w.runs ?? [])]; return ""; }
|
|
70
|
+
throw new Error(`unexpected gh ${a.join(" ")}`);
|
|
71
|
+
},
|
|
72
|
+
};
|
|
73
|
+
return { w, io };
|
|
74
|
+
}
|
|
75
|
+
const mk = (io, engine = ENGINE(), over = {}) => ({ io, repo: "o/r", def: "main", config: { deployEngine: engine, deployBounds: { pollMs: 1, runPollMs: 1 }, ...(over.config ?? {}) }, state: over.state ?? { id: ID, steps: { "post-merge": { result: { merge: SHA } } } }, save() {}, ...(over.ctx ?? {}) });
|
|
76
|
+
const cmds = (w, from = 0) => w.ran.slice(from).map((r) => r.cmd).filter((c) => ORDER.includes(c) || ["shift-back", "abort"].includes(c));
|
|
77
|
+
|
|
78
|
+
describe("deploy engine: off is today's behaviour (#5712)", () => {
|
|
79
|
+
it("no engine block, or enabled: false: the legacy step runs, the engine leaves no trace and no stage command runs", async () => {
|
|
80
|
+
for (const engine of [undefined, null, { ...ENGINE(), enabled: false }, { ...ENGINE(), enabled: "true" }]) {
|
|
81
|
+
const { w, io } = world(); const c = mk(io, null); c.config.deployEngine = engine;
|
|
82
|
+
assert.deepEqual(await deployStep(c), { skipped: true }); assert.equal(c.state.deploy, undefined); assert.equal(w.ran.length, 0); assert.equal(w.git.length, 0);
|
|
83
|
+
assert.equal(engineOn(engine), false);
|
|
84
|
+
}
|
|
85
|
+
});
|
|
86
|
+
it("readEngine: no git call and null unless the working copy turns it on; then the default branch's forge.yaml wins", () => {
|
|
87
|
+
const on = "deploy:\n engine:\n enabled: true\n stages:\n - id: a\n run: x\n", off = "deploy:\n engine:\n enabled: false\n";
|
|
88
|
+
const { w, io } = world();
|
|
89
|
+
assert.equal(readEngine({ raw: "release:\n managed: true\n", io, def: "main" }), null);
|
|
90
|
+
assert.equal(readEngine({ raw: off, io, def: "main" }), null);
|
|
91
|
+
assert.equal(w.git.length, 0);
|
|
92
|
+
assert.equal(readEngine({ raw: on, io, def: "main" }).stages[0].id, "a"); // not tracked on the default branch: the working copy
|
|
93
|
+
w.defaultYaml = off;
|
|
94
|
+
assert.equal(readEngine({ raw: on, io, def: "main" }), null, "a checkout cannot turn the engine on over the default branch");
|
|
95
|
+
w.defaultYaml = on.replace("id: a", "id: b");
|
|
96
|
+
assert.equal(readEngine({ raw: on, io, def: "main" }).stages[0].id, "b");
|
|
97
|
+
assert.match(validateEngine(readEngine({ raw: "deploy:\n engine:\n enabled: true\n stages: [a\n" }))[0], /not valid YAML/);
|
|
98
|
+
});
|
|
99
|
+
it("a release the engine started is never handed to the workflow pipeline on a host without the engine", async () => {
|
|
100
|
+
const { w, io } = world(); const c = mk(io, null, { config: { deploy: { live_release_command: "live", pipelines: { full: { workflow: "deploy.yml", trigger: "dispatch" } } } } });
|
|
101
|
+
c.state.deploy = { phase: "DEPLOYING", target: { sha: OLD }, engine: { started_at: "t" }, attempts: 0, reruns: 0 };
|
|
102
|
+
const out = await deployStep(c);
|
|
103
|
+
assert.equal(out.blocked, true); assert.equal(out.hold.label, "needs-agent"); assert.equal(w.gh.length, 0);
|
|
104
|
+
});
|
|
105
|
+
});
|
|
106
|
+
|
|
107
|
+
describe("deploy engine: stage machine (#5712)", () => {
|
|
108
|
+
it("runs every stage in order, smokes the new colour before any traffic, and ends LIVE with a checkpoint per stage", async () => {
|
|
109
|
+
const { w, io } = world(); const c = mk(io);
|
|
110
|
+
const out = await deployStep(c);
|
|
111
|
+
assert.equal(out.live, true); assert.equal(out.merge, SHA);
|
|
112
|
+
assert.deepEqual(cmds(w), ORDER);
|
|
113
|
+
assert.equal(c.state.deploy.phase, "LIVE"); assert.equal(c.state.deploy.target.sha, OLD);
|
|
114
|
+
assert.deepEqual(Object.keys(c.state.deploy.stages), ORDER);
|
|
115
|
+
for (const cp of Object.values(c.state.deploy.stages)) { assert.equal(cp.status, "done"); assert.equal(cp.sha, SHA); assert.ok(cp.key && cp.ms >= 0); }
|
|
116
|
+
assert.deepEqual(c.state.deploy.stages.build.outputs, { digests: { api: "sha256:1" } });
|
|
117
|
+
const smoke = w.ran.find((r) => r.cmd === "smoke-new");
|
|
118
|
+
assert.deepEqual([smoke.env.FORGE_COLOUR, smoke.env.FORGE_OLD_COLOUR, smoke.env.FORGE_SHA, smoke.env.FORGE_STAGE, smoke.env.FORGE_TARGET_SHA], ["green", "blue", SHA, "smoke-new", OLD]);
|
|
119
|
+
assert.ok(c.state.deploy.engine.traffic_at);
|
|
120
|
+
});
|
|
121
|
+
it("I9: an unknown rollback target blocks before any stage", async () => {
|
|
122
|
+
const { w, io } = world({ live: null });
|
|
123
|
+
const out = await deployStep(mk(io));
|
|
124
|
+
assert.match(out.reason, /rollback target unknown/); assert.deepEqual(cmds(w), []);
|
|
125
|
+
});
|
|
126
|
+
it("I26: a set freeze variable blocks before any stage", async () => {
|
|
127
|
+
const { w, io } = world({ freeze: "1" });
|
|
128
|
+
const out = await deployStep(mk(io, ENGINE(), { config: { deploy: { freeze_variable: "DEPLOY_FREEZE" } } }));
|
|
129
|
+
assert.match(out.reason, /I26/); assert.deepEqual(cmds(w), []);
|
|
130
|
+
});
|
|
131
|
+
it("an invalid stage list blocks and deploys nothing", async () => {
|
|
132
|
+
const { w, io } = world(); const e = ENGINE(); e.stages = e.stages.filter((s) => s.kind !== "smoke");
|
|
133
|
+
const out = await deployStep(mk(io, e));
|
|
134
|
+
assert.match(out.reason, /smoke stage must come before/); assert.deepEqual(cmds(w), []);
|
|
135
|
+
});
|
|
136
|
+
for (const id of ORDER) {
|
|
137
|
+
it(`resume after a failure at ${id}: earlier stages are not run again`, async () => {
|
|
138
|
+
const { w, io } = world({ fail: { [id]: 1 } }); const c = mk(io);
|
|
139
|
+
const first = await deployStep(c), i = ORDER.indexOf(id);
|
|
140
|
+
assert.equal(first.blocked, true); assert.match(first.reason, new RegExp(`stage ${id} \\(`));
|
|
141
|
+
const kind = ENGINE().stages[i].kind, pre = i < ORDER.indexOf("shift"), shifted = !pre && i < ORDER.indexOf("retire-old");
|
|
142
|
+
assert.equal(!!first.preTraffic, pre); assert.equal(!!first.rolledBack, shifted); assert.equal(!!first.hold, !pre && !shifted, kind);
|
|
143
|
+
assert.deepEqual(cmds(w), [...ORDER.slice(0, i + 1), ...(shifted ? ["shift-back"] : [])]);
|
|
144
|
+
if (pre) assert.equal(w.live, OLD, "the old colour keeps serving");
|
|
145
|
+
const mark = w.ran.length;
|
|
146
|
+
const second = await deployStep(c);
|
|
147
|
+
assert.equal(second.live, true);
|
|
148
|
+
assert.deepEqual(cmds(w, mark), ORDER.slice(shifted ? ORDER.indexOf("shift") : i), "only the failed stage and what follows it (after a shift-back: from the shift)");
|
|
149
|
+
assert.equal(c.state.deploy.stages[id].attempt, 2);
|
|
150
|
+
});
|
|
151
|
+
}
|
|
152
|
+
it("a stage that throws is a failed stage, not a crash", async () => {
|
|
153
|
+
const { io } = world(); const c = mk(io); const orig = io.rehearse;
|
|
154
|
+
io.rehearse = async (cmd, o) => { if (/; expand$/.test(cmd)) throw new Error("ssh: connection reset"); return orig(cmd, o); };
|
|
155
|
+
const out = await deployStep(c);
|
|
156
|
+
assert.equal(out.preTraffic, true); assert.match(out.reason, /connection reset/); assert.equal(c.state.deploy.stages.expand.status, "failed");
|
|
157
|
+
});
|
|
158
|
+
});
|
|
159
|
+
|
|
160
|
+
describe("deploy engine: input-change invalidation (#5712)", () => {
|
|
161
|
+
const failedSmoke = async (o = {}) => { const { w, io } = world({ fail: { "smoke-new": 1 }, ...o }); const c = mk(io); await deployStep(c); return { w, io, c, mark: w.ran.length }; };
|
|
162
|
+
it("a smoke-fix commit re-runs the smoke stage and nothing before it", async () => {
|
|
163
|
+
const { w, c, mark } = await failedSmoke();
|
|
164
|
+
w.trees[SHA2] = { ...TREE, "tests/smoke/home.sh": "o4-fixed" };
|
|
165
|
+
c.config.deploySha = SHA2;
|
|
166
|
+
const out = await deployStep(c);
|
|
167
|
+
assert.equal(out.live, true); assert.equal(out.merge, SHA2); assert.equal(w.live, SHA2);
|
|
168
|
+
assert.deepEqual(cmds(w, mark), ORDER.slice(ORDER.indexOf("smoke-new")));
|
|
169
|
+
assert.equal(c.state.deploy.stages.build.sha, SHA, "the build checkpoint is the first attempt's");
|
|
170
|
+
assert.equal(c.state.deploy.stages["smoke-new"].sha, SHA2);
|
|
171
|
+
assert.equal(c.state.deploy.target.sha, OLD, "the rollback target stays the pre-release live sha");
|
|
172
|
+
});
|
|
173
|
+
it("a commit outside every stage's inputs re-runs nothing that is done", async () => {
|
|
174
|
+
const { w, c, mark } = await failedSmoke();
|
|
175
|
+
w.trees[SHA2] = { ...TREE, "README.md": "o8-new" };
|
|
176
|
+
c.config.deploySha = SHA2; await deployStep(c);
|
|
177
|
+
assert.deepEqual(cmds(w, mark), ORDER.slice(ORDER.indexOf("smoke-new")));
|
|
178
|
+
});
|
|
179
|
+
it("a service change re-runs build; later stages re-run only when its outputs (digests) changed", async () => {
|
|
180
|
+
const same = await failedSmoke();
|
|
181
|
+
same.w.trees[SHA2] = { ...TREE, "services/api/main.py": "o1-comment" };
|
|
182
|
+
same.c.config.deploySha = SHA2; await deployStep(same.c);
|
|
183
|
+
assert.deepEqual(cmds(same.w, same.mark), ["build", ...ORDER.slice(ORDER.indexOf("smoke-new"))], "same digests: the idle colour is not redeployed");
|
|
184
|
+
const diff = await failedSmoke();
|
|
185
|
+
diff.w.trees[SHA2] = { ...TREE, "services/api/main.py": "o1-fix" }; diff.w.digests = { api: "sha256:2" };
|
|
186
|
+
diff.c.config.deploySha = SHA2; await deployStep(diff.c);
|
|
187
|
+
assert.deepEqual(cmds(diff.w, diff.mark), ORDER, "new digests: everything that depends on the build runs again");
|
|
188
|
+
});
|
|
189
|
+
it("a stage without inputs depends on the whole tree; an unreadable commit never matches a checkpoint", async () => {
|
|
190
|
+
const e = ENGINE(); delete e.stages[0].inputs;
|
|
191
|
+
const { w, io } = world({ fail: { "smoke-new": 1 } }); const c = mk(io, e); await deployStep(c);
|
|
192
|
+
w.trees[SHA2] = { ...TREE, "README.md": "o8-new" }; c.config.deploySha = SHA2;
|
|
193
|
+
const mark = w.ran.length; await deployStep(c);
|
|
194
|
+
assert.equal(cmds(w, mark)[0], "build");
|
|
195
|
+
assert.equal(decide({ status: "done", parts: stageParts({ stage: normalise(e).stages[1], tree: `unreadable:${SHA}`, services: null, needs: {}, base: {} }) }, stageParts({ stage: normalise(e).stages[1], tree: `unreadable:${SHA2}`, services: null, needs: {}, base: {} })).run, true);
|
|
196
|
+
});
|
|
197
|
+
it("a changed stage definition, a moved live baseline and a failed check each re-run the stage", async () => {
|
|
198
|
+
const { w, io } = world({ fail: { "smoke-new": 1 } }); const e = ENGINE(); const c = mk(io, e); await deployStep(c);
|
|
199
|
+
e.stages[1].run = "expand"; e.stages[1].check = "expand-check"; // definition changed (a check was added)
|
|
200
|
+
w.fail["smoke-new"] = 1; let mark = w.ran.length; await deployStep(c);
|
|
201
|
+
assert.deepEqual(cmds(w, mark), ["expand", "deploy-new", "smoke-new"], "expand declares no outputs, so its changed key reaches the stages that need it");
|
|
202
|
+
w.fail["expand-check"] = 1; w.fail["smoke-new"] = 1; mark = w.ran.length; await deployStep(c);
|
|
203
|
+
assert.deepEqual(cmds(w, mark), ["expand", "deploy-new", "smoke-new"], "a failed check means the environment drifted: that stage and all after it");
|
|
204
|
+
const p = stageParts({ stage: e.stages[0], tree: "t", services: null, needs: {}, base: { target: OLD } });
|
|
205
|
+
assert.match(decide({ status: "done", parts: p, sha: SHA }, { ...p, base: "x" }).why, /live baseline/);
|
|
206
|
+
assert.deepEqual(decide({ status: "done", parts: p, sha: SHA }, p), { run: false, why: "done at aaaaaaaa, inputs unchanged" });
|
|
207
|
+
assert.equal(decide(null, p).why, "no checkpoint");
|
|
208
|
+
assert.notEqual(partsKey(p), partsKey({ ...p, paths: "u" }));
|
|
209
|
+
});
|
|
210
|
+
});
|
|
211
|
+
|
|
212
|
+
describe("deploy engine: smoke before traffic, rollback (#5712)", () => {
|
|
213
|
+
it("a smoke failure moves no traffic: no shift, no hold, no rollback trigger, no freeze", async () => {
|
|
214
|
+
const { w, io } = world({ fail: { "smoke-new": 1 } }); const triggers = [];
|
|
215
|
+
const c = mk(io, ENGINE(), { ctx: { rollbackTrigger: (t) => triggers.push(t) } });
|
|
216
|
+
const out = await deployStep(c);
|
|
217
|
+
assert.equal(out.preTraffic, true); assert.equal(out.hold, undefined); assert.equal(out.rollback, undefined); assert.equal(out.rolledBack, undefined);
|
|
218
|
+
assert.match(out.reason, /no traffic moved, blue keeps serving \(no freeze, no hold\)/);
|
|
219
|
+
assert.ok(!cmds(w).includes("shift") && !cmds(w).includes("shift-back"));
|
|
220
|
+
assert.deepEqual(triggers, []); assert.equal(w.live, OLD);
|
|
221
|
+
assert.ok(!w.gh.some((g) => /variable set|workflow run/.test(g)));
|
|
222
|
+
assert.deepEqual(c.state.deploy.engine.last, { stage: "smoke-new", pre_traffic: true, rollback: { kind: "none", ok: true, ms: 0 } });
|
|
223
|
+
assert.equal(c.state.deploy.engine.traffic_at, undefined);
|
|
224
|
+
});
|
|
225
|
+
it("abort_command tears the idle colour down inside the budget and the next run deploys it again", async () => {
|
|
226
|
+
const e = ENGINE(); e.rollback.abort_command = "abort";
|
|
227
|
+
const { w, io } = world({ fail: { "smoke-new": 1 } }); const c = mk(io, e);
|
|
228
|
+
const out = await deployStep(c);
|
|
229
|
+
assert.equal(out.engine.rollback.kind, "abort"); assert.ok(out.engine.rollback.ms <= 60_000);
|
|
230
|
+
assert.equal(c.state.deploy.stages["deploy-new"].status, "aborted");
|
|
231
|
+
const mark = w.ran.length; await deployStep(c);
|
|
232
|
+
assert.deepEqual(cmds(w, mark), ORDER.slice(ORDER.indexOf("deploy-new")));
|
|
233
|
+
});
|
|
234
|
+
it("a failure after the shift moves traffic back inside rollback.budget_s and emits no ladder trigger", async () => {
|
|
235
|
+
const { w, io } = world({ fail: { verify: 1 } }); const triggers = [], timeouts = [];
|
|
236
|
+
const c = mk(io, ENGINE(), { ctx: { rollbackTrigger: (t) => triggers.push(t) } }); const orig = io.rehearse;
|
|
237
|
+
io.rehearse = async (cmd, o) => { if (/; shift-back$/.test(cmd)) timeouts.push(o.timeout); return orig(cmd, o); };
|
|
238
|
+
const out = await deployStep(c);
|
|
239
|
+
assert.equal(out.rolledBack, true); assert.equal(w.live, OLD); assert.deepEqual(triggers, []); assert.deepEqual(timeouts, [60_000]);
|
|
240
|
+
assert.equal(w.ran.find((r) => r.cmd === "shift-back").env.FORGE_TRAFFIC_PCT, "0");
|
|
241
|
+
assert.equal(c.state.deploy.engine.last.rollback.kind, "shift-back"); assert.ok(c.state.deploy.engine.last.rollback.ms <= 60_000);
|
|
242
|
+
assert.deepEqual(Object.values(c.state.deploy.stages).map((cp) => cp.status), ["done", "done", "done", "done", "rolled-back", "rolled-back"], "the shift and what followed it must run again");
|
|
243
|
+
});
|
|
244
|
+
it("a live record that is not the deployed sha after the shift fails verify (I28) and shifts back", async () => {
|
|
245
|
+
const { w, io } = world(); const c = mk(io); const orig = io.rehearse;
|
|
246
|
+
io.rehearse = async (cmd, o) => { const r = await orig(cmd, o); if (/; shift$/.test(cmd)) w.live = OLD; return r; }; // the router did not move
|
|
247
|
+
const out = await deployStep(c);
|
|
248
|
+
assert.match(out.reason, /LIVE_CHECK failed: live is bbbbbbbb/); assert.equal(out.rolledBack, true); assert.ok(!cmds(w).includes("verify"));
|
|
249
|
+
});
|
|
250
|
+
it("a failed or missing shift-back hands over to the rollback ladder", async () => {
|
|
251
|
+
for (const setup of [(w) => { w.fail["shift-back"] = 1; }, (w, e) => { delete e.rollback.shift_back_command; }]) {
|
|
252
|
+
const { w, io } = world({ fail: { verify: 1 } }); const e = ENGINE(); setup(w, e); const triggers = [];
|
|
253
|
+
const out = await deployStep(mk(io, e, { ctx: { rollbackTrigger: (t) => triggers.push(t) } }));
|
|
254
|
+
assert.equal(out.rolledBack, undefined); assert.match(out.rollback.reason, /stage verify/); assert.equal(triggers.length, 1); assert.equal(triggers[0].sha, SHA);
|
|
255
|
+
}
|
|
256
|
+
});
|
|
257
|
+
it("after the old colour is retired there is no shift-back: hold for a roll-forward", async () => {
|
|
258
|
+
const { w, io } = world({ fail: { contract: 1 } });
|
|
259
|
+
const out = await deployStep(mk(io));
|
|
260
|
+
assert.equal(out.hold.label, "needs-agent"); assert.ok(!cmds(w).includes("shift-back")); assert.equal(w.live, SHA);
|
|
261
|
+
});
|
|
262
|
+
it("a verification layer that asks to wait or hold is not a failure: no shift-back, the stage is retried on resume", async () => {
|
|
263
|
+
const { w, io } = world(); const c = mk(io, ENGINE(), { config: { verify: { results: [{ workflow: "smoke.yml", kind: "smoke" }] } } });
|
|
264
|
+
const out = await deployStep(c);
|
|
265
|
+
assert.match(out.reason, /stage verify: smoke.yml \(smoke\) has no completed run/); assert.equal(out.rolledBack, undefined); assert.ok(!cmds(w).includes("shift-back"));
|
|
266
|
+
assert.equal(c.state.deploy.stages.verify, undefined);
|
|
267
|
+
w.runs = [{ databaseId: 5, status: "completed", conclusion: "success" }];
|
|
268
|
+
const mark = w.ran.length, again = await deployStep(c);
|
|
269
|
+
assert.equal(again.live, true); assert.equal(again.verify.verified, true); assert.deepEqual(cmds(w, mark), ["verify", "retire-old", "contract"]);
|
|
270
|
+
});
|
|
271
|
+
it("a code rollback forgets every stage checkpoint", async () => {
|
|
272
|
+
const { io } = world({ fail: { verify: 1 } }); const c = mk(io); await deployStep(c);
|
|
273
|
+
assert.ok(Object.keys(c.state.deploy.stages).length > 0);
|
|
274
|
+
c.policy = { rollback: { ladder: ["stop"] } };
|
|
275
|
+
await clearStages(c);
|
|
276
|
+
assert.deepEqual(c.state.deploy.stages, {});
|
|
277
|
+
assert.equal(typeof runRollback, "function");
|
|
278
|
+
});
|
|
279
|
+
});
|
|
280
|
+
|
|
281
|
+
describe("deploy engine: changed services only (#5712)", () => {
|
|
282
|
+
const D = (digest, config) => ({ digest, config });
|
|
283
|
+
it("diffServices: unchanged needs the same digest and the same config hash on both sides", () => {
|
|
284
|
+
const desired = { api: D("d1", "c1"), web: D("d2", "c2"), cron: D("d3", "c3"), mail: D("d4", "c4"), old: "d5", bare: D("d6"), gone: D(null, "c7") };
|
|
285
|
+
const live = { api: D("d1", "c1"), web: D("d9", "c2"), cron: D("d3", "c9"), old: "d5", bare: D("d6"), gone: D("d7", "c7") };
|
|
286
|
+
const r = diffServices(desired, live);
|
|
287
|
+
assert.deepEqual(r.unchanged, ["api"]);
|
|
288
|
+
assert.deepEqual(Object.fromEntries(r.changed.map((c) => [c.service, c.why])), { web: "image digest changed", cron: "config changed", mail: "not running", old: "no config hash reported", bare: "no config hash reported", gone: "no desired digest reported" });
|
|
289
|
+
assert.deepEqual(diffServices({ api: D("d1", "c1") }, null).changed, [{ service: "api", why: "not running" }]);
|
|
290
|
+
});
|
|
291
|
+
const svcEngine = (mode) => { const e = ENGINE(); e.services = { mode, desired_command: "desired", live_command: "livesvc" }; for (const s of e.stages) if (["build", "deploy", "smoke"].includes(s.kind)) s.services = true; return e; };
|
|
292
|
+
const SVC = { desired: { api: D("d1", "c1"), web: D("d2", "c2"), cron: D("d3", "c3") }, liveSvc: { api: D("d0", "c1"), web: D("d2", "c2"), cron: D("d3", "c3") } };
|
|
293
|
+
it("enforce: only the changed service reaches the stages; the skipped ones are recorded", async () => {
|
|
294
|
+
const { w, io } = world(SVC); const c = mk(io, svcEngine("enforce"));
|
|
295
|
+
const out = await deployStep(c);
|
|
296
|
+
assert.equal(out.live, true);
|
|
297
|
+
const dep = w.ran.find((r) => r.cmd === "deploy-new");
|
|
298
|
+
assert.equal(dep.env.FORGE_SERVICES, "api"); assert.equal(dep.env.FORGE_SERVICES_SKIPPED, "cron,web");
|
|
299
|
+
assert.deepEqual(c.state.deploy.services, { mode: "enforce", changed: [{ service: "api", why: "image digest changed" }], skipped: ["cron", "web"] });
|
|
300
|
+
});
|
|
301
|
+
it("enforce with nothing changed: service stages are skipped, the rest still run", async () => {
|
|
302
|
+
const { w, io } = world({ ...SVC, liveSvc: SVC.desired }); const c = mk(io, svcEngine("enforce"));
|
|
303
|
+
const out = await deployStep(c);
|
|
304
|
+
assert.equal(out.live, true); assert.deepEqual(cmds(w), ["expand", "shift", "verify", "retire-old", "contract"]);
|
|
305
|
+
assert.deepEqual(out.engine.stages.filter((s) => !s.run).map((s) => s.why), ["no changed services", "no changed services", "no changed services"]);
|
|
306
|
+
});
|
|
307
|
+
it("shadow (the default with a desired_command): everything is deployed, the would-be skip and its agreement are recorded", async () => {
|
|
308
|
+
const e = svcEngine("shadow"); delete e.services.mode;
|
|
309
|
+
const { w, io } = world(SVC); const c = mk(io, e);
|
|
310
|
+
await deployStep(c);
|
|
311
|
+
const dep = w.ran.find((r) => r.cmd === "deploy-new");
|
|
312
|
+
assert.equal(dep.env.FORGE_SERVICES, "api,cron,web"); assert.equal(dep.env.FORGE_SERVICES_SKIPPED, "");
|
|
313
|
+
assert.deepEqual(c.state.deploy.services.would_skip, ["cron", "web"]);
|
|
314
|
+
assert.deepEqual(c.state.deploy.services.agreement, { agree: 2, disagree: [] });
|
|
315
|
+
const dis = world(SVC); const c2 = mk(dis.io, svcEngine("shadow")); const orig = dis.io.rehearse;
|
|
316
|
+
dis.io.rehearse = async (cmd, o) => { const r = await orig(cmd, o); if (/; deploy-new$/.test(cmd)) dis.w.liveSvc = { ...dis.w.liveSvc, web: D("d2", "c-drifted") }; return r; };
|
|
317
|
+
await deployStep(c2);
|
|
318
|
+
assert.deepEqual(c2.state.deploy.services.agreement, { agree: 1, disagree: [{ service: "web", why: "config changed" }] });
|
|
319
|
+
});
|
|
320
|
+
it("a changed digest changes the key of service stages only", async () => {
|
|
321
|
+
const { w, io } = world({ ...SVC, fail: { "smoke-new": 1 } }); const c = mk(io, svcEngine("enforce")); await deployStep(c);
|
|
322
|
+
w.desired = { ...SVC.desired, api: D("d1b", "c1") };
|
|
323
|
+
const mark = w.ran.length; await deployStep(c);
|
|
324
|
+
assert.deepEqual(cmds(w, mark), ["build", "deploy-new", "smoke-new", "shift", "verify", "retire-old", "contract"], "expand has no service slice and is not run again");
|
|
325
|
+
});
|
|
326
|
+
it("unreadable desired digests, or a refused promotion, block before anything is deployed", async () => {
|
|
327
|
+
const a = world({ desired: null, liveSvc: {} });
|
|
328
|
+
assert.match((await deployStep(mk(a.io, svcEngine("enforce")))).reason, /desired digests are unreadable/); assert.deepEqual(cmds(a.w), []);
|
|
329
|
+
const b = world(SVC), asked = [];
|
|
330
|
+
const promote = { serviceDigests: async () => SVC.desired, liveDigests: async () => SVC.liveSvc, canPromote: async (q) => { asked.push(q); return { ok: false, why: "no CI evidence for this digest" }; } };
|
|
331
|
+
const out = await deployStep(mk(b.io, svcEngine("enforce"), { ctx: { promote } }));
|
|
332
|
+
assert.match(out.reason, /service api cannot be promoted: no CI evidence/); assert.deepEqual(cmds(b.w), []);
|
|
333
|
+
assert.deepEqual(asked, [{ service: "api", digest: "d1", sha: SHA }]);
|
|
334
|
+
assert.ok(!b.w.ran.some((r) => r.cmd === "desired"), "the promote interface replaces the commands");
|
|
335
|
+
});
|
|
336
|
+
});
|
|
337
|
+
|
|
338
|
+
describe("deploy engine: canary by risk class (#5712)", () => {
|
|
339
|
+
it("canaryPlan: defaults per class, per-repo override, unknown class is the most careful", () => {
|
|
340
|
+
assert.deepEqual(canaryPlan({ canary: {} }, "low"), CANARY.low);
|
|
341
|
+
assert.deepEqual(canaryPlan({ canary: { medium: { wait_s: 30 } } }, "medium"), { steps: [10, 100], wait_s: 30 });
|
|
342
|
+
assert.deepEqual(canaryPlan({ canary: {} }, "p9"), CANARY.high);
|
|
343
|
+
});
|
|
344
|
+
it("deployRisk reuses riskOf: a small diff is low, a migration is high, an unreadable diff is high", () => {
|
|
345
|
+
const ctx = (numstat, config = {}) => ({ io: world({ numstat }).io, config });
|
|
346
|
+
assert.equal(deployRisk(ctx("3\t1\tREADME.md"), SHA, OLD).risk, "low");
|
|
347
|
+
assert.equal(deployRisk(ctx("3\t1\tinfra/migrations/001.sql"), SHA, OLD).risk, "high");
|
|
348
|
+
assert.equal(deployRisk(ctx("300\t200\tsrc/a.js"), SHA, OLD).risk, "medium");
|
|
349
|
+
assert.deepEqual(deployRisk(ctx(null), SHA, OLD), { risk: "high", rule: "unreadable diff" });
|
|
350
|
+
assert.equal(deployRisk(ctx("1\t1\tsrc/pay/x.js", { policy: { risk: { rules: [{ when: { paths: ["src/pay/**"] }, risk: "high" }] } } }), SHA, OLD).risk, "high");
|
|
351
|
+
assert.equal(deployRisk(ctx(null, { deployRisk: "low" }), SHA, OLD).risk, "low");
|
|
352
|
+
});
|
|
353
|
+
it("low risk shifts straight to 100 with no wait; high risk steps 10, 50, 100 and waits between steps", async () => {
|
|
354
|
+
const low = world(); await deployStep(mk(low.io));
|
|
355
|
+
assert.deepEqual(low.w.ran.filter((r) => r.cmd === "shift").map((r) => r.env.FORGE_TRAFFIC_PCT), ["100"]); assert.deepEqual(low.w.slept, []);
|
|
356
|
+
const high = world({ numstat: "3\t1\tinfra/migrations/001.sql" }); const e = ENGINE(); e.canary = { health_command: "health" };
|
|
357
|
+
const c = mk(high.io, e); await deployStep(c);
|
|
358
|
+
assert.deepEqual(high.w.ran.filter((r) => r.cmd === "shift").map((r) => r.env.FORGE_TRAFFIC_PCT), ["10", "50", "100"]);
|
|
359
|
+
assert.equal(high.w.slept.reduce((a, b) => a + b, 0), 600_000); assert.ok(high.w.slept.every((ms) => ms <= 60_000), "waits are sliced for the lease heartbeat");
|
|
360
|
+
assert.equal(high.w.ran.filter((r) => r.cmd === "health").length, 3); assert.equal(high.w.ran.find((r) => r.cmd === "shift").env.FORGE_RISK, "high");
|
|
361
|
+
assert.equal(c.state.deploy.engine.risk.risk, "high");
|
|
362
|
+
});
|
|
363
|
+
it("a failed canary health check shifts back before the next step", async () => {
|
|
364
|
+
const { w, io } = world({ numstat: "3\t1\tinfra/migrations/001.sql", fail: { health: 1 } }); const e = ENGINE(); e.canary = { health_command: "health" };
|
|
365
|
+
const out = await deployStep(mk(io, e));
|
|
366
|
+
assert.match(out.reason, /canary health failed at 10%/); assert.equal(out.rolledBack, true);
|
|
367
|
+
assert.deepEqual(w.ran.filter((r) => r.cmd === "shift").map((r) => r.env.FORGE_TRAFFIC_PCT), ["10"]);
|
|
368
|
+
});
|
|
369
|
+
it("a resume in the middle of a canary wait does not set the share again and waits only the remainder", async () => {
|
|
370
|
+
const HIGH = { numstat: "3\t1\tinfra/migrations/001.sql" };
|
|
371
|
+
const a = world(HIGH), ca = mk(a.io); await deployStep(ca);
|
|
372
|
+
const key = ca.state.deploy.stages.shift.key; // the same inputs give the same key in any world
|
|
373
|
+
const { w, io } = world({ ...HIGH, fail: { "smoke-new": 1 } }); const c = mk(io); await deployStep(c);
|
|
374
|
+
c.state.deploy.engine.shift = { key, step: 0, since: w.now - 100_000 }; // the host died 100 s into the wait after the 10% step
|
|
375
|
+
const mark = w.ran.length, out = await deployStep(c);
|
|
376
|
+
assert.equal(out.live, true);
|
|
377
|
+
assert.deepEqual(w.ran.slice(mark).filter((r) => r.cmd === "shift").map((r) => r.env.FORGE_TRAFFIC_PCT), ["50", "100"]);
|
|
378
|
+
const total = w.slept.reduce((a, b) => a + b, 0);
|
|
379
|
+
assert.ok(total > 499_000 && total <= 500_000, `200 s left of the first wait plus 300 s, got ${total}`);
|
|
380
|
+
});
|
|
381
|
+
});
|
|
382
|
+
|
|
383
|
+
describe("deploy engine: config (#5712)", () => {
|
|
384
|
+
it("validateEngine accepts the documented shape and names each defect", () => {
|
|
385
|
+
assert.deepEqual(validateEngine(ENGINE()), []);
|
|
386
|
+
const bad = (mut) => { const e = ENGINE(); mut(e); return validateEngine(e).join(" | "); };
|
|
387
|
+
assert.match(bad((e) => { e.stages = []; }), /non-empty list/);
|
|
388
|
+
assert.match(bad((e) => { e.stages[1].id = "build"; }), /duplicate id/);
|
|
389
|
+
assert.match(bad((e) => { e.stages[0].kind = "compile"; }), /kind "compile" is not one of/);
|
|
390
|
+
assert.match(bad((e) => { e.stages[0].workflow = "b.yml"; }), /exactly one of run or workflow/);
|
|
391
|
+
assert.match(bad((e) => { delete e.stages[0].run; }), /exactly one of run or workflow/);
|
|
392
|
+
assert.match(bad((e) => { e.stages[0].needs = ["smoke-new"]; }), /not an earlier stage/);
|
|
393
|
+
assert.match(bad((e) => { e.stages[0].inputs = "services/**"; }), /inputs: expected a list/);
|
|
394
|
+
assert.match(bad((e) => { e.stages[0].retries = 3; }), /retries: unknown key/);
|
|
395
|
+
assert.match(bad((e) => { e.stages = e.stages.filter((s) => s.kind !== "verify"); }), /verify stage is required/);
|
|
396
|
+
assert.match(bad((e) => { const [sm] = e.stages.splice(3, 1); e.stages.splice(4, 0, sm); }), /smoke stage must come before the first shift/);
|
|
397
|
+
assert.match(bad((e) => { const [r] = e.stages.splice(6, 1); e.stages.splice(4, 0, r); }), /retire stages come after verify/);
|
|
398
|
+
assert.match(bad((e) => { e.services = { mode: "sometimes" }; }), /services.mode/);
|
|
399
|
+
assert.match(bad((e) => { e.canary = { high: { steps: [50, 10, 100] } }; }), /ascending percentages ending in 100/);
|
|
400
|
+
assert.match(bad((e) => { e.canary = { severe: {} }; }), /unknown risk class/);
|
|
401
|
+
assert.match(bad((e) => { e.rollback.budget_s = 0; }), /budget_s/);
|
|
402
|
+
assert.match(bad((e) => { e.pipelines = {}; }), /pipelines: unknown key/);
|
|
403
|
+
const noRun = ENGINE(); delete noRun.stages[5].run;
|
|
404
|
+
assert.deepEqual(validateEngine(noRun), [], "a verify stage may rely on the built-in verification alone");
|
|
405
|
+
});
|
|
406
|
+
it("every stage kind documents its idempotency contract; withEnv quotes values", () => {
|
|
407
|
+
for (const k of ["build", "migrate", "deploy", "smoke", "shift", "verify", "retire", "contract", "run"]) assert.ok(KINDS[k].length > 30, k);
|
|
408
|
+
assert.equal(withEnv("x", { A: "it's", B: "1" }), `export A='it'\\''s' B='1'; x`);
|
|
409
|
+
});
|
|
410
|
+
it("forge.yaml schema check: deploy.engine keys are known, a typo is flagged, keys of stage items are not mistaken for engine keys", () => {
|
|
411
|
+
const base = 'project:\n owner: "o"\n repo: "r"\npaths:\n root: "/x"\nbranches:\n default: "main"\n';
|
|
412
|
+
const ok = base + "deploy:\n engine:\n enabled: true\n live_release_command: \"x\"\n stage_timeout_s: 600\n colour:\n active_command: \"y\"\n services:\n mode: shadow\n desired_command: \"d\"\n canary:\n high:\n wait_s: 60\n rollback:\n budget_s: 60\n shift_back_command: \"z\"\n stages:\n - id: build\n kind: build\n run: \"make images\"\n inputs:\n - \"services/**\"\n - id: verify\n kind: verify\n";
|
|
413
|
+
assert.deepEqual(validateForgeYaml(ok), []);
|
|
414
|
+
const codes = (t) => validateForgeYaml(t).map((f) => `${f.code}:${f.key}`).sort();
|
|
415
|
+
assert.deepEqual(codes(base + "deploy:\n engine:\n enable: true\n services:\n mode: maybe\n rollback:\n budget: 60\n"), ["unknown-key:deploy.engine.enable", "unknown-key:deploy.engine.rollback.budget", "wrong-type:deploy.engine.services.mode"]);
|
|
416
|
+
assert.deepEqual(codes(base + "deploy:\n engine:\n enabled: yes please\n"), ["wrong-type:deploy.engine.enabled"]);
|
|
417
|
+
});
|
|
418
|
+
});
|
|
419
|
+
|
|
420
|
+
describe("deploy engine: plan and CLI (#5712)", () => {
|
|
421
|
+
const failed = async () => { const { w, io } = world({ fail: { "smoke-new": 1 } }); const c = mk(io); await deployStep(c); return { w, io, c }; };
|
|
422
|
+
it("planStages says which stages would run and why, and runs nothing", async () => {
|
|
423
|
+
const { w, io, c } = await failed(); const mark = w.ran.length;
|
|
424
|
+
const p = await planStages({ ...c, state: structuredClone(c.state) }, SHA);
|
|
425
|
+
assert.deepEqual(p.stages.map((s) => [s.stage, s.run]), ORDER.map((id, i) => [id, i >= 3]));
|
|
426
|
+
assert.equal(p.stages[0].why, "done at aaaaaaaa, inputs unchanged"); assert.match(p.stages[3].why, /failed last time: smoke-new boom/); assert.equal(p.stages[4].why, "no checkpoint");
|
|
427
|
+
assert.ok(p.stages.every((s) => s.contract === KINDS[s.kind]));
|
|
428
|
+
assert.deepEqual(cmds(w, mark), [], "no stage command");
|
|
429
|
+
w.trees[SHA2] = { ...TREE, "services/api/main.py": "o1b" };
|
|
430
|
+
const p2 = await planStages({ ...c, state: structuredClone(c.state) }, SHA2);
|
|
431
|
+
assert.match(p2.stages[0].why, /inputs changed: files matching inputs/); assert.match(p2.stages[2].why, /re-runs if the outputs of build change/);
|
|
432
|
+
const text = renderPlan(p);
|
|
433
|
+
assert.match(text, /^deploy plan for aaaaaaaa \(live bbbbbbbb; risk low, rule default; canary 100%; new colour green\)/); assert.match(text, /\n {2}skip {2}build \(build\): done at/); assert.match(text, /\n {2}run {3}smoke-new \(smoke\): failed last time/);
|
|
434
|
+
assert.equal(io, c.io);
|
|
435
|
+
});
|
|
436
|
+
it("release deploy --plan prints the plan (text or JSON) for a release or for the default branch head", async () => {
|
|
437
|
+
const { io, c } = await failed(); const out = []; io.print = (l) => out.push(l);
|
|
438
|
+
const deps = { io, load: async (id) => (id === ID ? c.state : null), engine: c.config.deployEngine, def: "main", repo: "o/r" };
|
|
439
|
+
assert.equal(await deployFromCli(["--plan", ID], deps), 0); assert.match(out[0], /skip {2}build/);
|
|
440
|
+
assert.equal(await deployFromCli(["--plan", ID, "--json"], deps), 0); assert.equal(JSON.parse(out[1]).stages[3].run, true);
|
|
441
|
+
assert.equal(await deployFromCli(["--plan"], deps), 0); assert.match(out[2], /run {3}build \(build\): no checkpoint/);
|
|
442
|
+
assert.equal(c.state.deploy.stages["smoke-new"].status, "failed", "the plan never changes the release state");
|
|
443
|
+
});
|
|
444
|
+
it("release deploy --resume hands a merged release back to the release job; --sha must be on the default branch and contain the merge", async () => {
|
|
445
|
+
const { w, io, c } = await failed(); const out = []; io.print = (l) => out.push(l); w.trees[SHA2] = TREE;
|
|
446
|
+
const deps = { io, load: async (id) => (id === ID ? c.state : id === "r20260101-000000-2" ? { id, steps: {} } : null), engine: c.config.deployEngine, def: "main", repo: "o/r" };
|
|
447
|
+
assert.deepEqual(await deployFromCli(["--resume", ID], deps), { resume: ID, sha: null });
|
|
448
|
+
assert.deepEqual(await deployFromCli(["--resume", ID, "--sha", SHA2], deps), { resume: ID, sha: SHA2 });
|
|
449
|
+
w.notAncestor = true;
|
|
450
|
+
assert.equal(await deployFromCli(["--resume", ID, "--sha", SHA2], deps), 1); assert.match(out.at(-1), /--sha must be a commit on origin\/main that contains the release merge aaaaaaaa/);
|
|
451
|
+
assert.equal(await deployFromCli(["--resume", "r20260101-000000-2"], deps), 1); assert.match(out.at(-1), /not merged yet/);
|
|
452
|
+
assert.equal(await deployFromCli(["--resume", "r20260101-000000-9"], deps), 1); assert.match(out.at(-1), /no release/);
|
|
453
|
+
assert.equal(await deployFromCli([ID], deps), 1); assert.match(out.at(-1), /exactly one of --plan or --resume/);
|
|
454
|
+
assert.equal(await deployFromCli(["--plan", ID], { ...deps, engine: null }), 1); assert.match(out.at(-1), /deploy.engine.enabled is not on/);
|
|
455
|
+
});
|
|
456
|
+
});
|
|
457
|
+
|
|
458
|
+
describe("deploy engine: durable checkpoints and workflow stages (#5712)", () => {
|
|
459
|
+
/** A ledger `_active` branch behind `io.api` (the contents and git-data calls updateActive uses). */
|
|
460
|
+
const ledger = (io) => {
|
|
461
|
+
const l = { active: { epoch: 1, lease: { holder: "h", epoch: 1, release: ID } }, commits: 0 };
|
|
462
|
+
io.api = async (m, path, body) => {
|
|
463
|
+
if (/contents\/active\.json/.test(path)) return { content: Buffer.from(JSON.stringify(l.active)).toString("base64") };
|
|
464
|
+
if (/git\/ref\/heads\//.test(path)) return { object: { sha: "h0" } };
|
|
465
|
+
if (/git\/commits\/h0$/.test(path)) return { tree: { sha: "t0" } };
|
|
466
|
+
if (m === "POST" && /git\/trees$/.test(path)) { l.active = JSON.parse(body.tree.find((f) => f.path === "active.json").content); return { sha: "t1" }; }
|
|
467
|
+
if (m === "POST" && /git\/commits$/.test(path)) { l.commits += 1; return { sha: "h0" }; }
|
|
468
|
+
if (m === "PATCH" && /git\/refs/.test(path)) return {};
|
|
469
|
+
if (m === "PATCH" && /issues\/comments\//.test(path)) { l.beats = (l.beats ?? 0) + 1; return {}; }
|
|
470
|
+
throw new Error(`unrouted ${m} ${path}`);
|
|
471
|
+
};
|
|
472
|
+
const lctx = { io, repo: "o/r", credential: "driver" };
|
|
473
|
+
return { l, fence: (release) => ({ lctx, lease: { holder: "h", epoch: 1, release }, now: () => "t", record: async () => {} }) };
|
|
474
|
+
};
|
|
475
|
+
it("a managed release keeps checkpoints on the ledger: the next release starts from what the failed one finished", async () => {
|
|
476
|
+
const { w, io } = world({ fail: { "smoke-new": 1 } }); const { l, fence } = ledger(io);
|
|
477
|
+
const first = mk(io, ENGINE(), { ctx: { fence: fence(ID) } });
|
|
478
|
+
assert.equal((await deployStep(first)).preTraffic, true);
|
|
479
|
+
assert.deepEqual(Object.keys(l.active[STAGES_KEY]), ORDER.slice(0, 4)); assert.equal(l.active[STAGES_KEY]["smoke-new"].status, "failed");
|
|
480
|
+
assert.equal(l.active.lease.holder, "h", "only the deploy_stages key of _active is written");
|
|
481
|
+
w.trees[SHA2] = { ...TREE, "tests/smoke/home.sh": "o4-fixed" };
|
|
482
|
+
const ID2 = "r20260102-000000-1", mark = w.ran.length;
|
|
483
|
+
l.active.lease.release = ID2;
|
|
484
|
+
const next = mk(io, ENGINE(), { state: { id: ID2, steps: { "post-merge": { result: { merge: SHA2 } } } }, ctx: { fence: fence(ID2) } });
|
|
485
|
+
const out = await deployStep(next);
|
|
486
|
+
assert.equal(out.live, true); assert.deepEqual(cmds(w, mark), ORDER.slice(3), "build, expand and deploy-new of the failed release are reused");
|
|
487
|
+
assert.equal(l.active[STAGES_KEY].build.release, ID); assert.equal(l.active[STAGES_KEY].contract.release, ID2);
|
|
488
|
+
await clearStages(next);
|
|
489
|
+
assert.deepEqual(l.active[STAGES_KEY], {}); assert.deepEqual(next.state.deploy.stages, {});
|
|
490
|
+
});
|
|
491
|
+
it("a release after one that went live shares nothing with it: the live baseline moved", async () => {
|
|
492
|
+
const { w, io } = world(); const { fence } = ledger(io);
|
|
493
|
+
await deployStep(mk(io, ENGINE(), { ctx: { fence: fence(ID) } }));
|
|
494
|
+
w.trees[SHA2] = TREE; w.active = "green";
|
|
495
|
+
const ID2 = "r20260102-000000-1", mark = w.ran.length;
|
|
496
|
+
const out = await deployStep(mk(io, ENGINE(), { state: { id: ID2, steps: { "post-merge": { result: { merge: SHA2 } } } }, ctx: { fence: fence(ID2) } }));
|
|
497
|
+
assert.equal(out.live, true); assert.deepEqual(cmds(w, mark), ORDER);
|
|
498
|
+
assert.equal(w.ran.slice(mark).find((r) => r.cmd === "deploy-new").env.FORGE_COLOUR, "blue");
|
|
499
|
+
});
|
|
500
|
+
it("the lease heartbeat is kept fresh per stage; a lease taken over by another host stops the run before the next stage", async () => {
|
|
501
|
+
const { w, io } = world(); const { l, fence } = ledger(io); const c = mk(io, ENGINE(), { ctx: { fence: fence(ID) } }); const orig = io.rehearse;
|
|
502
|
+
io.rehearse = async (cmd, o) => { const r = await orig(cmd, o); if (/; expand$/.test(cmd)) l.active.lease = { holder: "conductor-gh", epoch: 2, release: ID }; return r; };
|
|
503
|
+
const out = await deployStep(c);
|
|
504
|
+
assert.match(out.reason, /stage deploy-new: fenced: another host took over/); assert.deepEqual(cmds(w), ["build", "expand"]); assert.equal(l.beats, 2);
|
|
505
|
+
assert.equal(out.preTraffic, undefined);
|
|
506
|
+
});
|
|
507
|
+
it("--sha refreshes the expected digests for the new commit", async () => {
|
|
508
|
+
const { w, io } = world({ fail: { "smoke-new": 1 } }); const c = mk(io, ENGINE(), { config: { deploy: { expected_digests_command: "expected" } } });
|
|
509
|
+
await deployStep(c); w.trees[SHA2] = TREE; c.config.deploySha = SHA2; w.fail.expected = 1;
|
|
510
|
+
assert.match((await deployStep(c)).reason, /expected_digests_command returned no digests for the new sha/);
|
|
511
|
+
assert.equal(w.ran.at(-1).env.FORGE_SHA, SHA2);
|
|
512
|
+
});
|
|
513
|
+
it("an unreadable checkpoint store blocks (I5) before any stage", async () => {
|
|
514
|
+
const { w, io } = world(); const { fence } = ledger(io); io.api = async () => { throw new Error("HTTP 502"); };
|
|
515
|
+
const out = await deployStep(mk(io, ENGINE(), { ctx: { fence: { ...fence(ID) } } }));
|
|
516
|
+
assert.match(out.reason, /cannot read the stage checkpoints \(I5\)/); assert.deepEqual(cmds(w), []);
|
|
517
|
+
});
|
|
518
|
+
it("a workflow stage is dispatched once with its inputs; a resume observes the run; a failed run is dispatched again as the next attempt", async () => {
|
|
519
|
+
const e = ENGINE(); delete e.stages[2].run; e.stages[2].workflow = "stage.yml";
|
|
520
|
+
const { w, io } = world({ wfConclusion: "failure" }); const c = mk(io, e);
|
|
521
|
+
const first = await deployStep(c);
|
|
522
|
+
assert.match(first.reason, /stage deploy-new \(deploy\) failed: stage.yml run 1 concluded failure/); assert.equal(first.preTraffic, true);
|
|
523
|
+
const disp = () => w.gh.filter((g) => g.startsWith("workflow run"));
|
|
524
|
+
assert.equal(disp().length, 1);
|
|
525
|
+
assert.match(disp()[0], /^workflow run stage.yml --repo o\/r --ref main -f stage=deploy-new -f sha=a{40} -f release=r20260101-000000-1 -f colour=green -f old_colour=blue$/);
|
|
526
|
+
w.wfConclusion = "success";
|
|
527
|
+
const second = await deployStep(c);
|
|
528
|
+
assert.equal(second.live, true); assert.equal(disp().length, 2); assert.equal(c.state.deploy.stages["deploy-new"].attempt, 2);
|
|
529
|
+
const again = world(); const c2 = mk(again.io, e); // a crash after the dispatch: the record says dispatched, so the run is observed, never dispatched twice
|
|
530
|
+
c2.state.deploy = undefined; await deployStep(c2);
|
|
531
|
+
const n = again.w.gh.filter((g) => g.startsWith("workflow run")).length;
|
|
532
|
+
delete c2.state.deploy.stages["deploy-new"]; again.w.fail["smoke-new"] = 1;
|
|
533
|
+
await deployStep(c2);
|
|
534
|
+
assert.equal(again.w.gh.filter((g) => g.startsWith("workflow run")).length, n, "same key, same attempt: observed, not redispatched");
|
|
535
|
+
});
|
|
536
|
+
});
|
|
537
|
+
|
|
538
|
+
describe("deploy engine: release CLI wiring and docs (#5712)", () => {
|
|
539
|
+
const DISK = [{ mount: "/", type: "ext4" }]; // CI /tmp can be tmpfs, which assertDiskPath refuses
|
|
540
|
+
const YAML = `deploy:\n engine:\n enabled: true\n live_release_command: "live"\n colour:\n active_command: "active"\n rollback:\n shift_back_command: "shift-back"\n stages:\n${ENGINE().stages.map((s) => ` - {id: ${s.id}, kind: ${s.kind}, run: "${s.run}", inputs: ["${s.inputs[0]}"]}`).join("\n")}\n`;
|
|
541
|
+
const setup = (yaml = YAML) => {
|
|
542
|
+
const cwd = tmpDir("deploy-stages-"), root = join(cwd, "releases"); mkdirSync(root);
|
|
543
|
+
if (yaml != null) writeFileSync(join(cwd, "forge.yaml"), yaml);
|
|
544
|
+
const { w, io } = world(); const out = []; io.print = (l) => out.push(String(l));
|
|
545
|
+
Object.assign(io, { runningLanes: async () => [], batches: async () => [], setParallel: async () => {} });
|
|
546
|
+
const state = { v: 1, id: ID, repo: "o/r", startedAt: "2026-01-01T00:00:00Z", held: [], steps: Object.fromEntries(RELEASE_STEPS.filter((s) => s !== "deploy").map((s) => [s, { done: true, result: s === "post-merge" ? { merge: SHA } : {} }])) };
|
|
547
|
+
openRelease(root, ID, DISK).save(state);
|
|
548
|
+
return { w, io, out, cwd, root, deps: { io, cwd, env: { FORGEDOCK_RELEASE_DIR: root }, mounts: DISK } };
|
|
549
|
+
};
|
|
550
|
+
it("release deploy --resume continues a merged release from the failed stage; --sha deploys the fix under the same release", async () => {
|
|
551
|
+
const { w, out, root, deps } = setup(); w.fail["smoke-new"] = 1;
|
|
552
|
+
assert.equal(await releaseFromCli(["deploy", "--resume", ID, "--repo", "o/r"], deps), 3);
|
|
553
|
+
assert.match(out.join("\n"), /blocked at deploy: stage smoke-new \(smoke\) failed: smoke-new boom; no traffic moved/);
|
|
554
|
+
assert.deepEqual(cmds(w), ORDER.slice(0, 4));
|
|
555
|
+
assert.equal(await releaseFromCli(["deploy", "--plan", ID, "--repo", "o/r"], deps), 0);
|
|
556
|
+
assert.match(out.at(-1), /skip {2}deploy-new \(deploy\): done at aaaaaaaa, inputs unchanged\n {2}run {3}smoke-new \(smoke\): failed last time/);
|
|
557
|
+
w.trees[SHA2] = { ...TREE, "tests/smoke/home.sh": "o4-fixed" };
|
|
558
|
+
const mark = w.ran.length;
|
|
559
|
+
assert.equal(await releaseFromCli(["deploy", "--resume", ID, "--sha", SHA2, "--repo", "o/r"], deps), 0);
|
|
560
|
+
assert.deepEqual(cmds(w, mark), ORDER.slice(3));
|
|
561
|
+
const st = openRelease(root, ID).load();
|
|
562
|
+
assert.equal(st.steps.deploy.done, true); assert.equal(st.deploy.phase, "LIVE"); assert.equal(st.deploy.merge, SHA2); assert.equal(st.deploy.stages.build.sha, SHA);
|
|
563
|
+
});
|
|
564
|
+
it("without the engine block the verb refuses and the release job never sees an engine", async () => {
|
|
565
|
+
const { w, out, deps } = setup("release:\n auto_merge: false\n");
|
|
566
|
+
assert.equal(await releaseFromCli(["deploy", "--plan", ID, "--repo", "o/r"], deps), 1);
|
|
567
|
+
assert.match(out.at(-1), /deploy.engine.enabled is not on/);
|
|
568
|
+
w.git.length = 0;
|
|
569
|
+
assert.equal(await releaseFromCli(["--resume", ID, "--repo", "o/r"], deps), 0); // the deploy step is skipped as before
|
|
570
|
+
assert.deepEqual(cmds(w), []); assert.equal(w.git.length, 0);
|
|
571
|
+
});
|
|
572
|
+
it("the docs/CONFIG.md example is a valid engine block and passes the forge.yaml schema check", () => {
|
|
573
|
+
const doc = readFileSync(new URL("../../docs/CONFIG.md", import.meta.url), "utf8");
|
|
574
|
+
const sec = doc.slice(doc.indexOf("## `deploy` (OPTIONAL)")), yaml = /```yaml\n([\s\S]*?)```/.exec(sec)[1];
|
|
575
|
+
const e = readEngine({ raw: yaml });
|
|
576
|
+
assert.deepEqual(validateEngine(e), []); assert.equal(e.stages.length, 8);
|
|
577
|
+
assert.deepEqual(validateForgeYaml(yaml).filter((f) => f.key.startsWith("deploy")), []);
|
|
578
|
+
for (const k of ["id", "kind", "inputs", "needs", "services", "check", "outputs_command", "timeout_s"]) assert.ok(sec.includes(`| \`${k}\` |`), k);
|
|
579
|
+
});
|
|
580
|
+
});
|