@intentius/chant-lexicon-pulumi 0.124.85 → 0.124.86
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/README.md +9 -3
- package/dist/index.d.ts +1 -1
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +1 -1
- package/dist/integrity.json +2 -2
- package/dist/manifest.json +1 -1
- package/dist/run.d.ts +21 -0
- package/dist/run.d.ts.map +1 -1
- package/dist/run.js +36 -0
- package/dist/stack-ops.d.ts +107 -21
- package/dist/stack-ops.d.ts.map +1 -1
- package/dist/stack-ops.js +492 -221
- package/package.json +2 -2
- package/src/index.ts +1 -1
- package/src/run.ts +37 -0
- package/src/stack-ops.fake-binary.test.ts +323 -0
- package/src/stack-ops.test.ts +40 -18
- package/src/stack-ops.ts +524 -261
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@intentius/chant-lexicon-pulumi",
|
|
3
|
-
"version": "0.124.
|
|
3
|
+
"version": "0.124.86",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"files": [
|
|
6
6
|
"src/",
|
|
@@ -54,7 +54,7 @@
|
|
|
54
54
|
"access": "public"
|
|
55
55
|
},
|
|
56
56
|
"peerDependencies": {
|
|
57
|
-
"@intentius/chant": "^0.124.
|
|
57
|
+
"@intentius/chant": "^0.124.86",
|
|
58
58
|
"zod": "^4.3.6"
|
|
59
59
|
},
|
|
60
60
|
"description": "pulumi lexicon for chant: a Pulumi program as a workspace member, its preview in the change set and its refresh as drift records",
|
package/src/index.ts
CHANGED
|
@@ -111,7 +111,7 @@ export { readPulumiDrift, type PulumiDriftRunners } from "./drift-read";
|
|
|
111
111
|
export { pulumiAuditCatalog } from "./lint/audit-catalog";
|
|
112
112
|
// The Preview/Drift watch composite and its step builders (#4494).
|
|
113
113
|
export { PulumiWatchOp, type PulumiWatchOpConfig, type PulumiWatchOpResources } from "./composites/pulumi-watch-op";
|
|
114
|
-
export { gatedStackExport, gatedStackCancel, gatedStackMove, gatedStackAction, checkStackActionUrn, stackActionScope, stackActionSubject, PulumiStackActionRefused, PULUMI_ACTION_TYPE_PREFIX, type PulumiStackActionOptions, type PulumiStackActionResult, type PulumiStackActionRefusal, moveLeftovers, diyStackLockIds, pulumiStackLockIds, type PulumiStackLocks, PULUMI_STACK_OPS_OP, type PulumiStackOpOutcome, type PulumiStackOpCommon, type PulumiStackOpKind } from "./stack-ops";
|
|
114
|
+
export { gatedStackExport, gatedStackCancel, gatedStackMove, gatedStackAction, checkStackActionUrn, stackActionScope, stackActionSubject, PulumiStackActionRefused, PULUMI_ACTION_TYPE_PREFIX, type PulumiStackActionOptions, type PulumiStackActionResult, type PulumiStackActionRefusal, moveLeftovers, diyStackLockIds, diyStackLock, pulumiStackLockIds, pulumiStackVersionSource, PulumiStackOpRefused, type PulumiStackOpRefusal, type PulumiStackOpCheck, type PulumiStackExportResult, type PulumiStackCancelResult, type PulumiStackMoveResult, type PulumiStackLocks, PULUMI_STACK_OPS_OP, type PulumiStackOpOutcome, type PulumiStackOpCommon, type PulumiStackOpKind } from "./stack-ops";
|
|
115
115
|
// Review stacks (INTENTIUS/plywood#15): pr-<n> stacks only.
|
|
116
116
|
export {
|
|
117
117
|
initReviewStack,
|
package/src/run.ts
CHANGED
|
@@ -576,6 +576,43 @@ function allowedGatedPulumi(args: string[]): boolean {
|
|
|
576
576
|
return false;
|
|
577
577
|
}
|
|
578
578
|
|
|
579
|
+
/**
|
|
580
|
+
* The reads a gated stack export makes to name and fetch one version
|
|
581
|
+
* (#4808), for `./stack-ops.ts` alone. They stay off
|
|
582
|
+
* {@link runAllowedPulumi}'s list, which refuses `--version`, so no other
|
|
583
|
+
* caller reads an older update:
|
|
584
|
+
*
|
|
585
|
+
* - `pulumi stack history --json --page-size 1 --stack <stack>`: the newest
|
|
586
|
+
* update's number, on a backend that numbers its updates (Pulumi Cloud).
|
|
587
|
+
* A file backend answers `0`. Config secrets print as `[secret]`;
|
|
588
|
+
* - `pulumi stack export --stack <stack> --version <n>`: that update's
|
|
589
|
+
* export, run only after the export's request is approved and spent.
|
|
590
|
+
*/
|
|
591
|
+
export const PULUMI_STACK_OP_READS: readonly string[] = ["pulumi stack history --json --page-size 1 --stack <stack>", "pulumi stack export --stack <stack> --version <n>"];
|
|
592
|
+
|
|
593
|
+
/** `stack history --json --page-size 1 --stack <stack>`. */
|
|
594
|
+
export function pulumiStackHistoryArgs(stack: string): string[] {
|
|
595
|
+
return ["stack", "history", "--json", "--page-size", "1", "--stack", stack];
|
|
596
|
+
}
|
|
597
|
+
|
|
598
|
+
/** `stack export --stack <stack> --version <n>`. */
|
|
599
|
+
export function pulumiStackExportVersionArgs(stack: string, version: string): string[] {
|
|
600
|
+
return ["stack", "export", "--stack", stack, "--version", version];
|
|
601
|
+
}
|
|
602
|
+
|
|
603
|
+
function allowedStackOpRead(args: string[]): boolean {
|
|
604
|
+
if (args[0] !== "stack") return false;
|
|
605
|
+
if (args[1] === "history") return args.length === 7 && args[2] === "--json" && args[3] === "--page-size" && args[4] === "1" && args[5] === "--stack" && isValue(args[6]);
|
|
606
|
+
if (args[1] === "export") return args.length === 6 && args[2] === "--stack" && isValue(args[3]) && args[4] === "--version" && /^[1-9][0-9]*$/.test(args[5] ?? "");
|
|
607
|
+
return false;
|
|
608
|
+
}
|
|
609
|
+
|
|
610
|
+
/** Run one of {@link PULUMI_STACK_OP_READS}, refusing any other argv. Only `./stack-ops.ts` calls it. */
|
|
611
|
+
export async function runStackOpReadPulumi(run: PulumiRunner, args: string[], cwd: string, env?: Record<string, string>): Promise<{ stdout: string }> {
|
|
612
|
+
if (!allowedStackOpRead(args)) throw new Error(`chant reads a stack's versions only as ${PULUMI_STACK_OP_READS.join(" or ")}, not pulumi ${args.join(" ")}`);
|
|
613
|
+
return run(args, env ? { cwd, env } : { cwd });
|
|
614
|
+
}
|
|
615
|
+
|
|
579
616
|
/** Run one of {@link PULUMI_GATED_COMMANDS}, refusing any other argv. Only `./stack-ops.ts` calls it, after the request is spent. */
|
|
580
617
|
export async function runGatedPulumi(run: PulumiRunner, args: string[], cwd: string, env?: Record<string, string>): Promise<{ stdout: string }> {
|
|
581
618
|
if (!allowedGatedPulumi(args)) {
|
|
@@ -0,0 +1,323 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The gated stack operations on core's adapters (#4808), against a fake
|
|
3
|
+
* `pulumi` binary over a local file backend: the export of one version, the
|
|
4
|
+
* live-run check before a cancel, and the check that nothing moved between a
|
|
5
|
+
* move's plan and its write. The fake is a node script written per test that
|
|
6
|
+
* keeps its stacks, history and locks under a temp directory laid out as a
|
|
7
|
+
* file backend (`.pulumi/stacks`, `.pulumi/history`, `.pulumi/locks`); with
|
|
8
|
+
* `cloud` it numbers its updates and exports any of them by `--version`, as
|
|
9
|
+
* Pulumi Cloud does. The ledger is in memory, digesting with chant's own
|
|
10
|
+
* `gatedRequestDigest`.
|
|
11
|
+
*/
|
|
12
|
+
import { chmodSync, existsSync, mkdirSync, mkdtempSync, readdirSync, readFileSync, writeFileSync } from "node:fs";
|
|
13
|
+
import { tmpdir } from "node:os";
|
|
14
|
+
import { join } from "node:path";
|
|
15
|
+
import { describe, expect, test } from "vitest";
|
|
16
|
+
import { gatedRequestDigest, type GatedRequest } from "@intentius/chant/lifecycle/gated-request-digest";
|
|
17
|
+
import type { LiveRun } from "@intentius/chant/state-unlock";
|
|
18
|
+
import { engineRunnerAt, type PulumiRunner } from "./run";
|
|
19
|
+
import { gatedStackCancel, gatedStackExport, gatedStackMove, PulumiStackOpRefused, type GatedRequestApi, type PulumiStackOpCommon } from "./stack-ops";
|
|
20
|
+
|
|
21
|
+
function ledger() {
|
|
22
|
+
const pending: { digest: string; request: GatedRequest }[] = [];
|
|
23
|
+
const approvals = new Map<string, string>();
|
|
24
|
+
const spent = new Set<string>();
|
|
25
|
+
const finished: { digest: string; result: string; detail?: Record<string, unknown> }[] = [];
|
|
26
|
+
const api: GatedRequestApi = {
|
|
27
|
+
async readGatedRequests(op) {
|
|
28
|
+
return pending.map((p) => ({ op, gate: p.request.subject, digest: p.digest, request: p.request, pending: {} as never, approvals: [], ...(spent.has(p.digest) ? { applied: {} as never } : {}) }));
|
|
29
|
+
},
|
|
30
|
+
async requestGate(input) {
|
|
31
|
+
const digest = gatedRequestDigest(input.op, input.request);
|
|
32
|
+
if (spent.has(digest)) return { status: "spent", digest, request: input.request };
|
|
33
|
+
if (!pending.some((p) => p.digest === digest)) pending.push({ digest, request: input.request });
|
|
34
|
+
const by = approvals.get(digest);
|
|
35
|
+
const command = `chant approve ${input.op} ${input.request.subject} --plan ${digest}`;
|
|
36
|
+
if (!by) return { status: "waiting", digest, request: input.request, command };
|
|
37
|
+
if (by === input.request.by) return { status: "refused", digest, request: input.request, by, reason: "the requester's own approval does not count", command };
|
|
38
|
+
return { status: "approved", digest, request: input.request, approval: { version: 1, op: input.op, gate: input.request.subject, resolvedBy: by, timestamp: "2026-11-09T12:00:00Z" } as never };
|
|
39
|
+
},
|
|
40
|
+
async spendGatedRequest(input) {
|
|
41
|
+
spent.add(input.digest);
|
|
42
|
+
return { version: 1, kind: "applied", op: input.op, gate: input.gate, planDigest: input.digest, approvedAt: input.approval.timestamp, approvedBy: input.approval.resolvedBy, timestamp: "2026-11-09T12:01:00Z" };
|
|
43
|
+
},
|
|
44
|
+
async finishGatedRequest(input) {
|
|
45
|
+
finished.push({ digest: input.digest, result: input.result, ...(input.detail ? { detail: input.detail } : {}) });
|
|
46
|
+
return { record: {} as never, pushed: true };
|
|
47
|
+
},
|
|
48
|
+
};
|
|
49
|
+
return { api, pending, approve: (digest: string, by: string) => approvals.set(digest, by), finished, spent };
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
const FAKE = String.raw`
|
|
53
|
+
const fs = require("fs"), path = require("path");
|
|
54
|
+
const root = __ROOT__;
|
|
55
|
+
const cloud = __CLOUD__;
|
|
56
|
+
const a = process.argv.slice(2);
|
|
57
|
+
fs.appendFileSync(path.join(root, "calls.log"), JSON.stringify(a) + "\n");
|
|
58
|
+
const stackOf = () => a[a.indexOf("--stack") + 1];
|
|
59
|
+
const state = (s) => path.join(root, ".pulumi", "stacks", "shop", s + ".json");
|
|
60
|
+
const hist = (s) => path.join(root, ".pulumi", "history", "shop", s);
|
|
61
|
+
const versions = (s) => (fs.existsSync(hist(s)) ? fs.readdirSync(hist(s)).map((f) => Number(f.split(".")[0])).sort((x, y) => x - y) : []);
|
|
62
|
+
const locks = (s) => path.join(root, ".pulumi", "locks", "organization", "shop", s);
|
|
63
|
+
const fail = (m) => { process.stderr.write("error: " + m + "\n"); process.exit(255); };
|
|
64
|
+
const out = (t) => { process.stdout.write(t); process.exit(0); };
|
|
65
|
+
if (a[0] === "stack" && a[1] === "export") {
|
|
66
|
+
const s = stackOf();
|
|
67
|
+
const vi = a.indexOf("--version");
|
|
68
|
+
if (vi >= 0) {
|
|
69
|
+
if (!cloud) fail("the current backend (fake) does not provide the ability to export previous deployments");
|
|
70
|
+
const f = path.join(hist(s), a[vi + 1] + ".json");
|
|
71
|
+
if (!fs.existsSync(f)) fail("no version " + a[vi + 1]);
|
|
72
|
+
out(fs.readFileSync(f, "utf8"));
|
|
73
|
+
}
|
|
74
|
+
if (!fs.existsSync(state(s))) fail("no stack named " + s);
|
|
75
|
+
out(fs.readFileSync(state(s), "utf8"));
|
|
76
|
+
}
|
|
77
|
+
if (a[0] === "stack" && a[1] === "history") {
|
|
78
|
+
const v = versions(stackOf());
|
|
79
|
+
out(JSON.stringify([{ version: cloud && v.length ? v[v.length - 1] : 0, kind: "update", result: "succeeded" }]));
|
|
80
|
+
}
|
|
81
|
+
if (a[0] === "cancel") {
|
|
82
|
+
const d = locks(stackOf());
|
|
83
|
+
if (!fs.existsSync(d) || fs.readdirSync(d).length === 0) fail("no update is in progress");
|
|
84
|
+
// Released locks move aside to released/, so the fake deletes nothing.
|
|
85
|
+
const gone = path.join(root, "released");
|
|
86
|
+
fs.mkdirSync(gone, { recursive: true });
|
|
87
|
+
for (const f of fs.readdirSync(d)) fs.renameSync(path.join(d, f), path.join(gone, f));
|
|
88
|
+
out("The currently running update for '" + stackOf() + "' has been canceled!\n");
|
|
89
|
+
}
|
|
90
|
+
if (a[0] === "preview") {
|
|
91
|
+
const f = path.join(root, "preview", stackOf() + ".json");
|
|
92
|
+
out(fs.existsSync(f) ? fs.readFileSync(f, "utf8") : JSON.stringify({ steps: [], changeSummary: {} }));
|
|
93
|
+
}
|
|
94
|
+
if (a[0] === "state" && a[1] === "move") {
|
|
95
|
+
const src = a[3], dst = a[5], urns = a.slice(7);
|
|
96
|
+
const from = JSON.parse(fs.readFileSync(state(src), "utf8"));
|
|
97
|
+
const to = JSON.parse(fs.readFileSync(state(dst), "utf8"));
|
|
98
|
+
const moving = from.deployment.resources.filter((r) => urns.includes(r.urn));
|
|
99
|
+
from.deployment.resources = from.deployment.resources.filter((r) => !urns.includes(r.urn));
|
|
100
|
+
to.deployment.resources.push(...moving.map((r) => ({ ...r, urn: r.urn.replace("urn:pulumi:" + src + "::", "urn:pulumi:" + dst + "::") })));
|
|
101
|
+
fs.writeFileSync(state(src), JSON.stringify(from));
|
|
102
|
+
fs.writeFileSync(state(dst), JSON.stringify(to));
|
|
103
|
+
for (const s of [src, dst]) fs.writeFileSync(path.join(root, "preview", s + ".json"), JSON.stringify({ steps: [], changeSummary: {} }));
|
|
104
|
+
out("");
|
|
105
|
+
}
|
|
106
|
+
fail("the fake pulumi does not know " + a.join(" "));
|
|
107
|
+
`;
|
|
108
|
+
|
|
109
|
+
const exportDoc = (resources: { urn: string; type: string }[], time = "2026-11-09T10:00:00Z") => JSON.stringify({ version: 3, deployment: { manifest: { time }, resources: resources.map((r) => ({ ...r, custom: true })) } });
|
|
110
|
+
|
|
111
|
+
/** A file backend, a stack directory naming it, and a fake `pulumi` over it. */
|
|
112
|
+
function backend(opts: { cloud?: boolean } = {}) {
|
|
113
|
+
const root = mkdtempSync(join(tmpdir(), "stack-ops-backend-"));
|
|
114
|
+
mkdirSync(join(root, ".pulumi", "stacks", "shop"), { recursive: true });
|
|
115
|
+
mkdirSync(join(root, "preview"));
|
|
116
|
+
writeFileSync(join(root, "calls.log"), "");
|
|
117
|
+
const bin = join(root, "pulumi");
|
|
118
|
+
writeFileSync(bin, `#!/usr/bin/env node\n${FAKE.replace("__ROOT__", JSON.stringify(root)).replace("__CLOUD__", String(opts.cloud === true))}`);
|
|
119
|
+
chmodSync(bin, 0o755);
|
|
120
|
+
const dir = mkdtempSync(join(tmpdir(), "stack-ops-dir-"));
|
|
121
|
+
writeFileSync(join(dir, "Pulumi.yaml"), `name: shop\nruntime: nodejs\nbackend:\n url: file://${root}\n`);
|
|
122
|
+
return {
|
|
123
|
+
root,
|
|
124
|
+
dir,
|
|
125
|
+
run: engineRunnerAt(bin),
|
|
126
|
+
setState: (stack: string, text: string) => writeFileSync(join(root, ".pulumi", "stacks", "shop", `${stack}.json`), text),
|
|
127
|
+
setVersion: (stack: string, n: number, text: string) => {
|
|
128
|
+
mkdirSync(join(root, ".pulumi", "history", "shop", stack), { recursive: true });
|
|
129
|
+
writeFileSync(join(root, ".pulumi", "history", "shop", stack, `${n}.json`), text);
|
|
130
|
+
},
|
|
131
|
+
setPreview: (stack: string, steps: unknown[]) => writeFileSync(join(root, "preview", `${stack}.json`), JSON.stringify({ steps, changeSummary: {} })),
|
|
132
|
+
lock: (stack: string, id: string, timestamp: string) => {
|
|
133
|
+
const d = join(root, ".pulumi", "locks", "organization", "shop", stack);
|
|
134
|
+
mkdirSync(d, { recursive: true });
|
|
135
|
+
writeFileSync(join(d, `${id}.json`), JSON.stringify({ pid: 4242, username: "ci", hostname: "runner-7", timestamp }));
|
|
136
|
+
},
|
|
137
|
+
locks: (stack: string) => {
|
|
138
|
+
const d = join(root, ".pulumi", "locks", "organization", "shop", stack);
|
|
139
|
+
return existsSync(d) ? readdirSync(d) : [];
|
|
140
|
+
},
|
|
141
|
+
calls: (): string[][] =>
|
|
142
|
+
readFileSync(join(root, "calls.log"), "utf-8")
|
|
143
|
+
.split("\n")
|
|
144
|
+
.filter(Boolean)
|
|
145
|
+
.map((l) => JSON.parse(l) as string[]),
|
|
146
|
+
};
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
const outside = (): string => mkdtempSync(join(tmpdir(), "stack-ops-out-"));
|
|
150
|
+
const bucket = (stack: string, name: string) => ({ urn: `urn:pulumi:${stack}::shop::aws:s3/bucketV2:BucketV2::${name}`, type: "aws:s3/bucketV2:BucketV2" });
|
|
151
|
+
|
|
152
|
+
describe("export by version (#4808)", () => {
|
|
153
|
+
test("on a backend that numbers its updates, the request binds the version asked for, and the approved call exports that version", async () => {
|
|
154
|
+
const b = backend({ cloud: true });
|
|
155
|
+
const v = [1, 2, 3].map((n) => exportDoc([bucket("dev", `b${n}`)], `2026-11-0${n}T00:00:00Z`));
|
|
156
|
+
v.forEach((text, i) => b.setVersion("dev", i + 1, text));
|
|
157
|
+
b.setState("dev", v[2]!);
|
|
158
|
+
const l = ledger();
|
|
159
|
+
const out = join(outside(), "dev.v2.json");
|
|
160
|
+
const opts = { by: "alice", rules: { mode: "ledger" as const }, gated: l.api, env: {}, run: b.run, dir: b.dir, stack: "dev", engine: "pulumi" as const, out, version: "2" };
|
|
161
|
+
|
|
162
|
+
const first = await gatedStackExport(opts);
|
|
163
|
+
expect(first.status).toBe("waiting");
|
|
164
|
+
expect(first.request).toMatchObject({ kind: "stack-export", subject: "dev", fields: { version_id: "2" } });
|
|
165
|
+
expect(b.calls().some((c) => c.includes("--version"))).toBe(false);
|
|
166
|
+
expect(existsSync(out)).toBe(false);
|
|
167
|
+
|
|
168
|
+
l.approve(first.digest, "bob");
|
|
169
|
+
const done = await gatedStackExport(opts);
|
|
170
|
+
expect(done).toMatchObject({ status: "done", digest: first.digest, result: { file: out, version: "2", resources: 1 } });
|
|
171
|
+
expect(readFileSync(out, "utf-8")).toBe(v[1]);
|
|
172
|
+
expect(b.calls()).toContainEqual(["stack", "export", "--stack", "dev", "--version", "2"]);
|
|
173
|
+
expect(l.finished[0]).toMatchObject({ result: "applied", detail: { version_id: "2", approved_by: "bob" } });
|
|
174
|
+
|
|
175
|
+
// A version the stack never had is refused before anything is asked.
|
|
176
|
+
await expect(gatedStackExport({ ...opts, version: "9", out: join(outside(), "x.json") })).rejects.toThrow(/has no version 9/);
|
|
177
|
+
expect(l.pending).toHaveLength(1);
|
|
178
|
+
});
|
|
179
|
+
|
|
180
|
+
test("on a file backend the version is the export's digest, so a stack that changed after the approval asks again before writing", async () => {
|
|
181
|
+
const b = backend();
|
|
182
|
+
b.setState("dev", exportDoc([bucket("dev", "a")]));
|
|
183
|
+
const l = ledger();
|
|
184
|
+
const out = join(outside(), "dev.json");
|
|
185
|
+
const opts = { by: "alice", rules: { mode: "ledger" as const }, gated: l.api, env: {}, run: b.run, dir: b.dir, stack: "dev", engine: "pulumi" as const, out };
|
|
186
|
+
|
|
187
|
+
const first = await gatedStackExport(opts);
|
|
188
|
+
expect(first.status).toBe("waiting");
|
|
189
|
+
expect(first.request.fields.version_id).toMatch(/^sha256:[0-9a-f]{64}$/);
|
|
190
|
+
l.approve(first.digest, "bob");
|
|
191
|
+
|
|
192
|
+
// An update lands between the approval and the export: the approval was for the state before it.
|
|
193
|
+
const newer = exportDoc([bucket("dev", "a"), bucket("dev", "b")], "2026-11-09T11:00:00Z");
|
|
194
|
+
b.setState("dev", newer);
|
|
195
|
+
const again = await gatedStackExport(opts);
|
|
196
|
+
expect(again.status).toBe("waiting");
|
|
197
|
+
expect(again.digest).not.toBe(first.digest);
|
|
198
|
+
expect(existsSync(out)).toBe(false);
|
|
199
|
+
expect(l.spent.size).toBe(0);
|
|
200
|
+
|
|
201
|
+
l.approve(again.digest, "bob");
|
|
202
|
+
expect(await gatedStackExport(opts)).toMatchObject({ status: "done", result: { resources: 2 } });
|
|
203
|
+
expect(readFileSync(out, "utf-8")).toBe(newer);
|
|
204
|
+
|
|
205
|
+
// A numbered version on a backend that keeps none is refused before anything is asked.
|
|
206
|
+
await expect(gatedStackExport({ ...opts, version: "2", out: join(outside(), "y.json") })).rejects.toThrow(/has no version 2/);
|
|
207
|
+
});
|
|
208
|
+
});
|
|
209
|
+
|
|
210
|
+
describe("cancel checks the live runs (#4808)", () => {
|
|
211
|
+
const lockedAt = "2026-11-09T10:00:00Z";
|
|
212
|
+
const run = (started: string): LiveRun => ({ id: "77", status: "in_progress", started, url: "https://forge.example/acme/shop/actions/runs/77" });
|
|
213
|
+
|
|
214
|
+
test("a run that began before the lock was taken refuses the release, and nothing is asked", async () => {
|
|
215
|
+
const b = backend();
|
|
216
|
+
b.lock("dev", "lock-1", lockedAt);
|
|
217
|
+
const l = ledger();
|
|
218
|
+
const call = gatedStackCancel({ by: "alice", rules: { mode: "ledger" }, gated: l.api, env: {}, run: b.run, dir: b.dir, stack: "dev", engine: "pulumi", lockId: "lock-1", liveRuns: async () => [run("2026-11-09T09:58:00Z")] });
|
|
219
|
+
await expect(call).rejects.toBeInstanceOf(PulumiStackOpRefused);
|
|
220
|
+
await expect(call).rejects.toMatchObject({ code: "live-run" });
|
|
221
|
+
await expect(call).rejects.toThrow(/run 77 \(in_progress/);
|
|
222
|
+
expect(l.pending).toEqual([]);
|
|
223
|
+
expect(b.locks("dev")).toEqual(["lock-1.json"]);
|
|
224
|
+
});
|
|
225
|
+
|
|
226
|
+
test("with no live run it waits, and once approved runs pulumi cancel and checks the lock file is gone", async () => {
|
|
227
|
+
const b = backend();
|
|
228
|
+
b.lock("dev", "lock-1", lockedAt);
|
|
229
|
+
const l = ledger();
|
|
230
|
+
const opts = { by: "alice", rules: { mode: "ledger" as const }, gated: l.api, env: {}, run: b.run, dir: b.dir, stack: "dev", engine: "pulumi" as const, lockId: "lock-1", liveRuns: async () => [run("2026-11-09T11:00:00Z")] };
|
|
231
|
+
const asked = await gatedStackCancel(opts);
|
|
232
|
+
expect(asked.status).toBe("waiting");
|
|
233
|
+
expect(asked.request).toMatchObject({ kind: "stack-cancel", subject: "dev", fields: { id: "lock-1" } });
|
|
234
|
+
l.approve(asked.digest, "bob");
|
|
235
|
+
const done = await gatedStackCancel(opts);
|
|
236
|
+
expect(done).toMatchObject({ status: "done", result: { lockChecked: true, liveRuns: 1 } });
|
|
237
|
+
expect(b.calls()).toContainEqual(["cancel", "--yes", "--stack", "dev"]);
|
|
238
|
+
expect(b.locks("dev")).toEqual([]);
|
|
239
|
+
expect(l.finished[0]).toMatchObject({ result: "applied", detail: { lock_id: "lock-1", locked_by: "ci@runner-7", locked_at: lockedAt, approved_by: "bob" } });
|
|
240
|
+
});
|
|
241
|
+
|
|
242
|
+
test("a run that comes back after the approval holds the release, and the approval stays unspent", async () => {
|
|
243
|
+
const b = backend();
|
|
244
|
+
b.lock("dev", "lock-1", lockedAt);
|
|
245
|
+
const l = ledger();
|
|
246
|
+
let reads = 0;
|
|
247
|
+
const liveRuns = async () => (++reads >= 3 ? [run("2026-11-09T09:59:00Z")] : []);
|
|
248
|
+
const opts = { by: "alice", rules: { mode: "ledger" as const }, gated: l.api, env: {}, run: b.run, dir: b.dir, stack: "dev", engine: "pulumi" as const, lockId: "lock-1", liveRuns };
|
|
249
|
+
l.approve((await gatedStackCancel(opts)).digest, "bob");
|
|
250
|
+
await expect(gatedStackCancel(opts)).rejects.toMatchObject({ code: "held" });
|
|
251
|
+
expect(l.spent.size).toBe(0);
|
|
252
|
+
expect(b.calls().some((c) => c[0] === "cancel")).toBe(false);
|
|
253
|
+
expect(b.locks("dev")).toEqual(["lock-1.json"]);
|
|
254
|
+
});
|
|
255
|
+
|
|
256
|
+
test("with no runs reader and no forge in the environment, the release is refused: nothing says the holder is gone", async () => {
|
|
257
|
+
const b = backend();
|
|
258
|
+
b.lock("dev", "lock-1", lockedAt);
|
|
259
|
+
const l = ledger();
|
|
260
|
+
await expect(gatedStackCancel({ by: "alice", rules: { mode: "ledger" }, gated: l.api, env: {}, run: b.run, dir: b.dir, stack: "dev", engine: "pulumi", lockId: "lock-1" })).rejects.toThrow(/runs could not be read/);
|
|
261
|
+
expect(l.pending).toEqual([]);
|
|
262
|
+
});
|
|
263
|
+
});
|
|
264
|
+
|
|
265
|
+
describe("move checks nothing moved since its plan (#4808)", () => {
|
|
266
|
+
const logs = (stack: string) => bucket(stack, "logs");
|
|
267
|
+
const step = (op: string, urn: string) => ({ op, urn, type: "aws:s3/bucketV2:BucketV2", provider: "", ...(op === "create" ? { newState: { type: "aws:s3/bucketV2:BucketV2", inputs: {} } } : { oldState: { type: "aws:s3/bucketV2:BucketV2", inputs: {} } }) });
|
|
268
|
+
/** Two stacks on one file backend, net holding the bucket and app's program declaring it. */
|
|
269
|
+
function twoStacks() {
|
|
270
|
+
const b = backend();
|
|
271
|
+
b.setState("net", exportDoc([logs("net"), bucket("net", "keep")]));
|
|
272
|
+
b.setState("app", exportDoc([]));
|
|
273
|
+
b.setPreview("net", [step("delete", logs("net").urn)]);
|
|
274
|
+
b.setPreview("app", [step("create", logs("app").urn)]);
|
|
275
|
+
const appDir = mkdtempSync(join(tmpdir(), "stack-ops-dir-"));
|
|
276
|
+
writeFileSync(join(appDir, "Pulumi.yaml"), readFileSync(join(b.dir, "Pulumi.yaml"), "utf-8"));
|
|
277
|
+
return { b, appDir };
|
|
278
|
+
}
|
|
279
|
+
const common = (l: ReturnType<typeof ledger>, run: PulumiRunner, b: ReturnType<typeof backend>, appDir: string): PulumiStackOpCommon & Parameters<typeof gatedStackMove>[0] => ({
|
|
280
|
+
by: "alice",
|
|
281
|
+
rules: { mode: "ledger" },
|
|
282
|
+
gated: l.api,
|
|
283
|
+
env: {},
|
|
284
|
+
run,
|
|
285
|
+
engine: "pulumi",
|
|
286
|
+
source: { member: "shop/net", dir: b.dir, stack: "net" },
|
|
287
|
+
dest: { member: "shop/app", dir: appDir, stack: "app" },
|
|
288
|
+
urns: [logs("net").urn],
|
|
289
|
+
});
|
|
290
|
+
|
|
291
|
+
test("plans both stacks, waits, moves with pulumi state move, and both preview clean after", async () => {
|
|
292
|
+
const { b, appDir } = twoStacks();
|
|
293
|
+
const l = ledger();
|
|
294
|
+
const opts = common(l, b.run, b, appDir);
|
|
295
|
+
const asked = await gatedStackMove(opts);
|
|
296
|
+
expect(asked.status).toBe("waiting");
|
|
297
|
+
expect(asked.request).toMatchObject({ kind: "stack-move", subject: "move:net:app", fields: { urns: logs("net").urn, proof: expect.stringMatching(/^sha256:/) } });
|
|
298
|
+
expect(b.calls().some((c) => c[0] === "state")).toBe(false);
|
|
299
|
+
l.approve(asked.digest, "bob");
|
|
300
|
+
const done = await gatedStackMove(opts);
|
|
301
|
+
expect(done).toMatchObject({ status: "done", result: { engine: "pulumi", moved: [logs("net").urn] } });
|
|
302
|
+
expect(b.calls().filter((c) => c[0] === "state")).toEqual([["state", "move", "--source", "net", "--dest", "app", "--yes", logs("net").urn]]);
|
|
303
|
+
expect(l.finished[0]).toMatchObject({ result: "applied", detail: { afterNoChange: true, result: "applied", "shop/net.before_digest": expect.stringMatching(/^sha256:/) } });
|
|
304
|
+
});
|
|
305
|
+
|
|
306
|
+
test("a state that changes between the plan and the write refuses the move under the locks, and nothing moves", async () => {
|
|
307
|
+
const { b, appDir } = twoStacks();
|
|
308
|
+
const l = ledger();
|
|
309
|
+
const asked = await gatedStackMove(common(l, b.run, b, appDir));
|
|
310
|
+
l.approve(asked.digest, "bob");
|
|
311
|
+
// The approved call exports both stacks for its plan; an update lands on net before the move reads them again under the locks.
|
|
312
|
+
let exports = 0;
|
|
313
|
+
const racing: PulumiRunner = async (args, o) => {
|
|
314
|
+
if (args[0] === "stack" && args[1] === "export" && ++exports === 3) b.setState("net", exportDoc([logs("net"), bucket("net", "keep"), bucket("net", "new")]));
|
|
315
|
+
return b.run(args, o);
|
|
316
|
+
};
|
|
317
|
+
const moved = await gatedStackMove(common(l, racing, b, appDir));
|
|
318
|
+
expect(moved).toMatchObject({ status: "failed", check: "moved", error: expect.stringMatching(/shop\/net.*changed since the plan/) });
|
|
319
|
+
expect(b.calls().some((c) => c[0] === "state")).toBe(false);
|
|
320
|
+
expect(l.spent.size).toBe(1);
|
|
321
|
+
expect(l.finished[0]).toMatchObject({ result: "failed", detail: { result: "refused", moved: "shop/net" } });
|
|
322
|
+
});
|
|
323
|
+
});
|
package/src/stack-ops.test.ts
CHANGED
|
@@ -6,7 +6,7 @@
|
|
|
6
6
|
* answering with recorded documents (`testdata/pulumi-preview`), so no
|
|
7
7
|
* binary runs and no ledger or backend is written.
|
|
8
8
|
*/
|
|
9
|
-
import { mkdirSync, mkdtempSync, readFileSync, statSync, writeFileSync } from "node:fs";
|
|
9
|
+
import { existsSync, mkdirSync, mkdtempSync, readFileSync, renameSync, statSync, writeFileSync } from "node:fs";
|
|
10
10
|
import { tmpdir } from "node:os";
|
|
11
11
|
import { join } from "node:path";
|
|
12
12
|
import { describe, expect, test } from "vitest";
|
|
@@ -88,8 +88,9 @@ describe("export", () => {
|
|
|
88
88
|
const common: PulumiStackOpCommon = { by: "alice", rules: { mode: "ledger" }, gated: l.api, env: {}, run: pulumi.run };
|
|
89
89
|
const first = await gatedStackExport({ ...common, dir: stackDir(), stack: "dev", engine: "pulumi", out });
|
|
90
90
|
expect(first.status).toBe("waiting");
|
|
91
|
-
|
|
92
|
-
expect(first.request).toMatchObject({ kind: "stack-export", subject: "dev", fields: {
|
|
91
|
+
// #4808: the first call names the version (no numbered history, so the export's digest) and writes nothing.
|
|
92
|
+
expect(first.request).toMatchObject({ kind: "stack-export", subject: "dev", fields: { version_id: expect.stringMatching(/^sha256:/) } });
|
|
93
|
+
expect(existsSync(out)).toBe(false);
|
|
93
94
|
|
|
94
95
|
l.approve(first.digest, "alice");
|
|
95
96
|
expect((await gatedStackExport({ ...common, dir: stackDir(), stack: "dev", engine: "pulumi", out })).status).toBe("refused");
|
|
@@ -98,10 +99,11 @@ describe("export", () => {
|
|
|
98
99
|
const done = await gatedStackExport({ ...common, dir: stackDir(), stack: "dev", engine: "pulumi", out });
|
|
99
100
|
expect(done.status).toBe("done");
|
|
100
101
|
expect(done.digest).toBe(first.digest);
|
|
101
|
-
expect(pulumi.calls).toEqual([
|
|
102
|
+
expect(pulumi.calls.filter((c) => c[1] === "export").at(-1)).toEqual(["stack", "export", "--stack", "dev"]);
|
|
102
103
|
expect(readFileSync(out, "utf-8")).toBe(exportText);
|
|
103
104
|
expect(statSync(out).mode & 0o777).toBe(0o600);
|
|
104
|
-
expect(
|
|
105
|
+
expect(done).toMatchObject({ result: { resources: expect.any(Number), bytes: exportText.length } });
|
|
106
|
+
expect(l.finished[0]).toMatchObject({ result: "applied", detail: { content_digest: expect.stringMatching(/^sha256:/) } });
|
|
105
107
|
expect(JSON.stringify(l.finished)).not.toContain("ciphertext");
|
|
106
108
|
|
|
107
109
|
// The same request again (its `at` kept) answers spent and runs nothing.
|
|
@@ -110,7 +112,7 @@ describe("export", () => {
|
|
|
110
112
|
const again = await gatedStackExport({ ...common, now: "2000-01-01T00:00:00.000Z", dir: stackDir(), stack: "dev", engine: "pulumi", out: join(outside(), "again.json") });
|
|
111
113
|
expect(again.status).toBe("waiting");
|
|
112
114
|
expect(again.digest).not.toBe(first.digest);
|
|
113
|
-
expect(
|
|
115
|
+
expect(existsSync(join(outside(), "again.json"))).toBe(false);
|
|
114
116
|
});
|
|
115
117
|
|
|
116
118
|
test("refused in CI, inside a git working tree, and over an existing file, before anything is asked", async () => {
|
|
@@ -122,7 +124,7 @@ describe("export", () => {
|
|
|
122
124
|
await expect(gatedStackExport({ ...base, env: {}, out: join(repo, "x.json") })).rejects.toThrow(/inside the git working tree/);
|
|
123
125
|
const taken = join(outside(), "taken.json");
|
|
124
126
|
writeFileSync(taken, "{}");
|
|
125
|
-
await expect(gatedStackExport({ ...base, env: {}, out: taken })).rejects.toThrow(/never
|
|
127
|
+
await expect(gatedStackExport({ ...base, env: {}, out: taken })).rejects.toThrow(/never writes over a file/);
|
|
126
128
|
expect(l.pending).toEqual([]);
|
|
127
129
|
});
|
|
128
130
|
});
|
|
@@ -137,19 +139,22 @@ describe("cancel", () => {
|
|
|
137
139
|
|
|
138
140
|
const dir = stackDir(`file://${root}`);
|
|
139
141
|
const l = ledger();
|
|
140
|
-
|
|
141
|
-
const
|
|
142
|
+
// Like pulumi on a file backend, cancel takes the lock file away (moved aside, so the test deletes nothing).
|
|
143
|
+
const pulumi = runner((args) => {
|
|
144
|
+
if (args[0] === "cancel") renameSync(join(locks, "lock-1.json"), join(root, "released-lock-1.json"));
|
|
145
|
+
return "{}";
|
|
146
|
+
});
|
|
147
|
+
const common = { by: "alice", rules: { mode: "ledger" as const }, gated: l.api, env: {}, run: pulumi.run, dir, stack: "dev", engine: "pulumi" as const, liveRuns: async () => [] };
|
|
142
148
|
|
|
143
|
-
|
|
144
|
-
l.approve(stale.digest, "bob");
|
|
149
|
+
// #4808: a lock id that is not the one held is refused before anything is asked.
|
|
145
150
|
await expect(gatedStackCancel({ ...common, lockId: "lock-0" })).rejects.toThrow(/not the one it holds now \(lock-1\)/);
|
|
146
|
-
expect(l.
|
|
151
|
+
expect(l.pending).toEqual([]);
|
|
147
152
|
|
|
148
153
|
const asked = await gatedStackCancel({ ...common, lockId: "lock-1" });
|
|
149
154
|
expect(asked.status).toBe("waiting");
|
|
150
155
|
l.approve(asked.digest, "bob");
|
|
151
156
|
const done = await gatedStackCancel({ ...common, lockId: "lock-1" });
|
|
152
|
-
expect(done).toMatchObject({ status: "done", result: { lockChecked: true } });
|
|
157
|
+
expect(done).toMatchObject({ status: "done", result: { lockChecked: true, liveRuns: 0 } });
|
|
153
158
|
expect(pulumi.calls).toEqual([["cancel", "--yes", "--stack", "dev"]]);
|
|
154
159
|
});
|
|
155
160
|
|
|
@@ -209,13 +214,12 @@ describe("move", () => {
|
|
|
209
214
|
expect(l.finished[0]).toMatchObject({ result: "applied", detail: { afterNoChange: true } });
|
|
210
215
|
});
|
|
211
216
|
|
|
212
|
-
test("a preview that changes more than the move refuses
|
|
217
|
+
test("a preview that changes more than the move refuses before anything is asked", async () => {
|
|
213
218
|
const l = ledger();
|
|
214
219
|
const pulumi = runner((args) => (args[args.indexOf("--stack") + 1] === "net" ? previewDoc([step("delete", src("logs")), step("delete", src("other"))]) : previewDoc([step("create", dst("logs"))])));
|
|
215
220
|
const opts = { by: "alice", rules: { mode: "ledger" as const }, gated: l.api, env: {}, run: pulumi.run, engine: "pulumi" as const, source: { member: "net", dir: stackDir(), stack: "net" }, dest: { member: "app", dir: stackDir(), stack: "app" }, urns: [src("logs")] };
|
|
216
|
-
l.approve((await gatedStackMove(opts)).digest, "bob");
|
|
217
221
|
await expect(gatedStackMove(opts)).rejects.toThrow(/change more than the moved resources.*delete .*other/);
|
|
218
|
-
expect(l.
|
|
222
|
+
expect(l.pending).toEqual([]);
|
|
219
223
|
expect(pulumi.calls.some((c) => c[0] === "state")).toBe(false);
|
|
220
224
|
});
|
|
221
225
|
|
|
@@ -238,7 +242,9 @@ describe("move", () => {
|
|
|
238
242
|
l.approve((await gatedStackMove(opts)).digest, "bob");
|
|
239
243
|
const done = await gatedStackMove(opts);
|
|
240
244
|
expect(done.status).toBe("done");
|
|
245
|
+
// Each call plans with plunge's own plan; the move runs once, after the approval.
|
|
241
246
|
expect(plunge.calls.filter((c) => c[1] === "move")).toEqual([
|
|
247
|
+
["stack", "move", "--stack", "net", "--dest", "app", src("logs"), "--json"],
|
|
242
248
|
["stack", "move", "--stack", "net", "--dest", "app", src("logs"), "--json"],
|
|
243
249
|
["stack", "move", "--stack", "net", "--dest", "app", src("logs"), "--json", "--yes"],
|
|
244
250
|
]);
|
|
@@ -251,9 +257,8 @@ describe("move", () => {
|
|
|
251
257
|
return args[args.indexOf("--stack") + 1] === "net" ? previewDoc([step("delete", src("logs"))]) : previewDoc([step("create", dst("logs"))]);
|
|
252
258
|
});
|
|
253
259
|
const opts = { by: "alice", rules: { mode: "ledger" as const }, gated: l.api, env: {}, plunge: plunge.run, engine: "plunge" as const, source: { member: "net", dir: stackDir(), stack: "net" }, dest: { member: "app", dir: stackDir(), stack: "app" }, urns: [src("logs")] };
|
|
254
|
-
l.approve((await gatedStackMove(opts)).digest, "bob");
|
|
255
260
|
await expect(gatedStackMove(opts)).rejects.toThrow(/plan is refused: .*already owns/);
|
|
256
|
-
expect(l.
|
|
261
|
+
expect(l.pending).toEqual([]);
|
|
257
262
|
});
|
|
258
263
|
|
|
259
264
|
test("the requests sit under one op", () => {
|
|
@@ -396,3 +401,20 @@ describe("action (chant#4654)", () => {
|
|
|
396
401
|
expect(plunge.calls).toEqual([previewArgs, previewArgs, upArgs]);
|
|
397
402
|
});
|
|
398
403
|
});
|
|
404
|
+
|
|
405
|
+
describe("the version reads (#4808)", () => {
|
|
406
|
+
test("runStackOpReadPulumi runs exactly the history and versioned export forms", async () => {
|
|
407
|
+
const { pulumiStackExportVersionArgs, pulumiStackHistoryArgs, runStackOpReadPulumi } = await import("./run");
|
|
408
|
+
const pulumi = runner(() => "[]");
|
|
409
|
+
await runStackOpReadPulumi(pulumi.run, pulumiStackHistoryArgs("dev"), "/tmp");
|
|
410
|
+
await runStackOpReadPulumi(pulumi.run, pulumiStackExportVersionArgs("dev", "12"), "/tmp");
|
|
411
|
+
expect(pulumi.calls).toEqual([
|
|
412
|
+
["stack", "history", "--json", "--page-size", "1", "--stack", "dev"],
|
|
413
|
+
["stack", "export", "--stack", "dev", "--version", "12"],
|
|
414
|
+
]);
|
|
415
|
+
for (const bad of [["stack", "export", "--stack", "dev", "--version", "0"], ["stack", "export", "--stack", "dev", "--show-secrets", "1"], ["stack", "history", "--json", "--show-secrets", "--stack", "dev"], ["stack", "export", "--stack", "dev"]]) {
|
|
416
|
+
await expect(runStackOpReadPulumi(pulumi.run, bad, "/tmp")).rejects.toThrow(/reads a stack's versions only as/);
|
|
417
|
+
}
|
|
418
|
+
expect(pulumi.calls).toHaveLength(2);
|
|
419
|
+
});
|
|
420
|
+
});
|