@intentius/chant-lexicon-terraform 0.124.75 → 0.124.77
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/dist/integrity.json +2 -2
- package/dist/manifest.json +1 -1
- package/dist/pr-env.d.ts +138 -0
- package/dist/pr-env.d.ts.map +1 -0
- package/dist/pr-env.js +248 -0
- package/dist/state/gitlab.d.ts +74 -0
- package/dist/state/gitlab.d.ts.map +1 -0
- package/dist/state/gitlab.js +275 -0
- package/dist/state/index.d.ts +2 -0
- package/dist/state/index.d.ts.map +1 -1
- package/dist/state/index.js +2 -0
- package/dist/state/store.d.ts +2 -1
- package/dist/state/store.d.ts.map +1 -1
- package/dist/state/versions.d.ts +39 -0
- package/dist/state/versions.d.ts.map +1 -0
- package/dist/state/versions.js +85 -0
- package/dist/state-migration.d.ts +85 -0
- package/dist/state-migration.d.ts.map +1 -0
- package/dist/state-migration.js +296 -0
- package/dist/state-ops.d.ts +54 -87
- package/dist/state-ops.d.ts.map +1 -1
- package/dist/state-ops.js +120 -77
- package/package.json +2 -2
- package/src/pr-env.test.ts +132 -0
- package/src/pr-env.ts +312 -0
- package/src/state/gitlab.test.ts +226 -0
- package/src/state/gitlab.ts +318 -0
- package/src/state/index.ts +2 -0
- package/src/state/store.ts +2 -1
- package/src/state/versions.ts +103 -0
- package/src/state-export-unlock.test.ts +230 -0
- package/src/state-migration.test.ts +236 -0
- package/src/state-migration.ts +353 -0
- package/src/state-ops.ts +132 -130
|
@@ -0,0 +1,230 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* #4762: a root's state export and lock release through chant core's gated
|
|
3
|
+
* operations, with the binary faked (its `init` writes what init records
|
|
4
|
+
* into the data dir it is given, its `force-unlock` removes the lock file),
|
|
5
|
+
* S3 in memory behind the store's fetch, and the ledger in memory. No
|
|
6
|
+
* binary, bucket or git is reached.
|
|
7
|
+
*/
|
|
8
|
+
import { existsSync, mkdirSync, mkdtempSync, readFileSync, writeFileSync } from "node:fs";
|
|
9
|
+
import { tmpdir } from "node:os";
|
|
10
|
+
import { join } from "node:path";
|
|
11
|
+
import { describe, expect, test } from "vitest";
|
|
12
|
+
import { fakeLedger } from "@intentius/chant/__fixtures__/gated-ledger";
|
|
13
|
+
import type { BinaryExec } from "./state/exec";
|
|
14
|
+
import type { StoreFetch } from "./state/object-store";
|
|
15
|
+
import { terraformStateExport, terraformStateUnlock } from "./state-ops";
|
|
16
|
+
|
|
17
|
+
type Answer = Awaited<ReturnType<StoreFetch>>;
|
|
18
|
+
const answer = (status = 200, body = "", headers: Record<string, string> = {}): Answer => {
|
|
19
|
+
const h = Object.fromEntries(Object.entries(headers).map(([k, v]) => [k.toLowerCase(), v]));
|
|
20
|
+
return { ok: status < 300, status, text: async () => body, headers: { get: (n: string) => h[n.toLowerCase()] ?? null, forEach: (fn: (v: string, k: string) => void) => Object.entries(h).forEach(([k, v]) => fn(v, k)) } as Answer["headers"] };
|
|
21
|
+
};
|
|
22
|
+
|
|
23
|
+
/** A versioned S3 bucket in memory, path-style at http://s3.test/<bucket>/<key>. */
|
|
24
|
+
function fakeS3() {
|
|
25
|
+
const versions = new Map<string, Array<{ id: string; body: string }>>();
|
|
26
|
+
const seen: string[] = [];
|
|
27
|
+
let n = 0;
|
|
28
|
+
const put = (key: string, body: string) => versions.set(key, [...(versions.get(key) ?? []), { id: `v${++n}`, body }]);
|
|
29
|
+
const fetch: StoreFetch = async (url, init) => {
|
|
30
|
+
const u = new URL(url);
|
|
31
|
+
const key = decodeURIComponent(u.pathname.slice(1));
|
|
32
|
+
const vid = u.searchParams.get("versionId");
|
|
33
|
+
seen.push(`${init.method} ${key}${vid ? `?${vid}` : ""}`);
|
|
34
|
+
const all = versions.get(key) ?? [];
|
|
35
|
+
if (init.method === "PUT") {
|
|
36
|
+
if (init.headers["if-none-match"] === "*" && all.length) return answer(412);
|
|
37
|
+
put(key, String(init.body));
|
|
38
|
+
return answer(200);
|
|
39
|
+
}
|
|
40
|
+
if (init.method === "DELETE") {
|
|
41
|
+
versions.delete(key);
|
|
42
|
+
return answer(204);
|
|
43
|
+
}
|
|
44
|
+
const v = vid ? all.find((x) => x.id === vid) : all.at(-1);
|
|
45
|
+
if (!v) return answer(404);
|
|
46
|
+
return answer(200, init.method === "HEAD" ? "" : v.body, { "x-amz-version-id": v.id });
|
|
47
|
+
};
|
|
48
|
+
return { versions, seen, fetch, put };
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
/** GitLab's Terraform state API in memory, for project 7 at http://gitlab.test: versions by serial, the lock by LOCK and UNLOCK. */
|
|
52
|
+
function fakeGitLab() {
|
|
53
|
+
const versions: Array<{ serial: number; body: string }> = [];
|
|
54
|
+
let lock: Record<string, unknown> | undefined;
|
|
55
|
+
const seen: string[] = [];
|
|
56
|
+
const fetch: StoreFetch = async (url, init) => {
|
|
57
|
+
const u = new URL(url);
|
|
58
|
+
seen.push(`${init.method} ${u.pathname}`);
|
|
59
|
+
const m = u.pathname.match(/^\/api\/v4\/projects\/7\/terraform\/state\/net(\/lock|\/versions\/(\d+))?$/);
|
|
60
|
+
if (!m) return answer(404);
|
|
61
|
+
if (m[1] === "/lock") {
|
|
62
|
+
if (init.method === "LOCK") {
|
|
63
|
+
if (lock) return answer(409, JSON.stringify(lock));
|
|
64
|
+
lock = JSON.parse(String(init.body)) as Record<string, unknown>;
|
|
65
|
+
return answer(200);
|
|
66
|
+
}
|
|
67
|
+
const id = (JSON.parse(String(init.body ?? "{}")) as { ID?: string }).ID;
|
|
68
|
+
if (lock && lock.ID !== id) return answer(409, JSON.stringify(lock));
|
|
69
|
+
lock = undefined;
|
|
70
|
+
return answer(200);
|
|
71
|
+
}
|
|
72
|
+
const v = m[2] !== undefined ? versions.find((x) => String(x.serial) === m[2]) : versions.at(-1);
|
|
73
|
+
if (!v) return answer(404);
|
|
74
|
+
return answer(200, init.method === "HEAD" ? "" : v.body);
|
|
75
|
+
};
|
|
76
|
+
return { versions, seen, fetch, hold: (l: Record<string, unknown> | undefined) => void (lock = l), held: () => lock };
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
const GITLAB_ADDRESS = "http://gitlab.test/api/v4/projects/7/terraform/state/net";
|
|
80
|
+
const GITLAB_BACKEND = { address: GITLAB_ADDRESS, lock_address: `${GITLAB_ADDRESS}/lock`, unlock_address: `${GITLAB_ADDRESS}/lock`, username: "gitlab-ci-token", password: "glcbt" };
|
|
81
|
+
|
|
82
|
+
const S3_ENV = { AWS_ACCESS_KEY_ID: "AK", AWS_SECRET_ACCESS_KEY: "SK", AWS_REGION: "us-east-1" };
|
|
83
|
+
const S3_BACKEND = { bucket: "acme", key: "net.tfstate", use_lockfile: true, endpoints: { s3: "http://s3.test" } };
|
|
84
|
+
|
|
85
|
+
/** The binary: `init` records `backend` in the data dir TF_DATA_DIR names; `force-unlock` runs `onUnlock`. */
|
|
86
|
+
function fakeBinary(backend: { type: string; config: Record<string, unknown> }, onUnlock: (id: string) => void = () => {}) {
|
|
87
|
+
const calls: Array<{ args: string[]; dataDir?: string }> = [];
|
|
88
|
+
const exec: BinaryExec = async (_bin, args, _dir, env) => {
|
|
89
|
+
calls.push({ args, ...(env.TF_DATA_DIR ? { dataDir: env.TF_DATA_DIR } : {}) });
|
|
90
|
+
if (args[0] === "init") {
|
|
91
|
+
mkdirSync(env.TF_DATA_DIR!, { recursive: true });
|
|
92
|
+
writeFileSync(join(env.TF_DATA_DIR!, "terraform.tfstate"), JSON.stringify({ version: 3, backend }));
|
|
93
|
+
return { code: 0, stdout: "", out: "" };
|
|
94
|
+
}
|
|
95
|
+
if (args[0] === "force-unlock") {
|
|
96
|
+
onUnlock(args.at(-1)!);
|
|
97
|
+
return { code: 0, stdout: "", out: "" };
|
|
98
|
+
}
|
|
99
|
+
return { code: 1, stdout: "", out: `unexpected ${args.join(" ")}` };
|
|
100
|
+
};
|
|
101
|
+
return { exec, calls };
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
const tmp = (): string => mkdtempSync(join(tmpdir(), "chant-tf-export-"));
|
|
105
|
+
|
|
106
|
+
function rootDir(): string {
|
|
107
|
+
const dir = join(tmp(), "net");
|
|
108
|
+
mkdirSync(dir, { recursive: true });
|
|
109
|
+
return dir;
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
const common = (ledger: ReturnType<typeof fakeLedger>, dir: string, exec: BinaryExec, fetch: StoreFetch) => ({
|
|
113
|
+
root: "net",
|
|
114
|
+
dir,
|
|
115
|
+
binary: "terraform",
|
|
116
|
+
env: S3_ENV,
|
|
117
|
+
exec,
|
|
118
|
+
fetch,
|
|
119
|
+
by: "ana",
|
|
120
|
+
rules: { mode: "ledger" as const },
|
|
121
|
+
ledger,
|
|
122
|
+
now: "2026-11-08T09:00:00Z",
|
|
123
|
+
});
|
|
124
|
+
|
|
125
|
+
describe("terraformStateExport", () => {
|
|
126
|
+
test("inits in a data dir of its own, asks for the version the bucket holds now, and writes it once approved", async () => {
|
|
127
|
+
const s3 = fakeS3();
|
|
128
|
+
s3.put("acme/net.tfstate", '{"serial":1}');
|
|
129
|
+
s3.put("acme/net.tfstate", '{"serial":2}');
|
|
130
|
+
const dir = rootDir();
|
|
131
|
+
const bin = fakeBinary({ type: "s3", config: S3_BACKEND });
|
|
132
|
+
const ledger = fakeLedger();
|
|
133
|
+
const out = join(tmp(), "net.tfstate");
|
|
134
|
+
const words = { op: "tf-state-export" };
|
|
135
|
+
const first = await terraformStateExport({ ...common(ledger, dir, bin.exec, s3.fetch), out, words });
|
|
136
|
+
expect(first).toMatchObject({ status: "waiting", version: "v2", location: "s3://acme/net.tfstate" });
|
|
137
|
+
expect(bin.calls[0]!.args).toEqual(["init", "-input=false", "-no-color"]);
|
|
138
|
+
expect(bin.calls[0]!.dataDir).toMatch(/chant-state-data-/);
|
|
139
|
+
expect(existsSync(bin.calls[0]!.dataDir!)).toBe(false);
|
|
140
|
+
expect(existsSync(join(dir, ".terraform"))).toBe(false);
|
|
141
|
+
expect(s3.seen.filter((s) => s.startsWith("GET"))).toEqual([]);
|
|
142
|
+
|
|
143
|
+
ledger.approve("bo");
|
|
144
|
+
const second = await terraformStateExport({ ...common(ledger, dir, bin.exec, s3.fetch), out, words, version: "v2" });
|
|
145
|
+
expect(second).toMatchObject({ status: "written", file: out });
|
|
146
|
+
expect(readFileSync(out, "utf-8")).toBe('{"serial":2}');
|
|
147
|
+
expect(s3.seen.filter((s) => s.startsWith("GET"))).toEqual(["GET acme/net.tfstate?v2"]);
|
|
148
|
+
});
|
|
149
|
+
|
|
150
|
+
test("an older version by its id, and an s3 backend that locks with DynamoDB is read all the same", async () => {
|
|
151
|
+
const s3 = fakeS3();
|
|
152
|
+
s3.put("acme/net.tfstate", '{"serial":1}');
|
|
153
|
+
s3.put("acme/net.tfstate", '{"serial":2}');
|
|
154
|
+
const bin = fakeBinary({ type: "s3", config: { ...S3_BACKEND, use_lockfile: false, dynamodb_table: "locks" } });
|
|
155
|
+
const ledger = fakeLedger();
|
|
156
|
+
ledger.approve("bo");
|
|
157
|
+
const out = join(tmp(), "old.tfstate");
|
|
158
|
+
const r = await terraformStateExport({ ...common(ledger, rootDir(), bin.exec, s3.fetch), out, words: { op: "tf-state-export" }, version: "v1" });
|
|
159
|
+
expect(r).toMatchObject({ status: "written", version: "v1" });
|
|
160
|
+
expect(readFileSync(out, "utf-8")).toBe('{"serial":1}');
|
|
161
|
+
});
|
|
162
|
+
|
|
163
|
+
test("a GitLab-managed state's version id is its serial", async () => {
|
|
164
|
+
const gl = fakeGitLab();
|
|
165
|
+
gl.versions.push({ serial: 3, body: '{"serial":3}' }, { serial: 4, body: '{"serial":4}' });
|
|
166
|
+
const bin = fakeBinary({ type: "http", config: GITLAB_BACKEND });
|
|
167
|
+
const ledger = fakeLedger();
|
|
168
|
+
const out = join(tmp(), "gl.tfstate");
|
|
169
|
+
const words = { op: "tf-state-export" };
|
|
170
|
+
const first = await terraformStateExport({ ...common(ledger, rootDir(), bin.exec, gl.fetch), out, words });
|
|
171
|
+
expect(first).toMatchObject({ status: "waiting", version: "4", location: GITLAB_ADDRESS });
|
|
172
|
+
await expect(terraformStateExport({ ...common(ledger, rootDir(), bin.exec, gl.fetch), out, words, version: "v3" })).rejects.toThrow(/has no version v3/);
|
|
173
|
+
ledger.approve("bo");
|
|
174
|
+
const second = await terraformStateExport({ ...common(ledger, rootDir(), bin.exec, gl.fetch), out, words, version: "4" });
|
|
175
|
+
expect(second).toMatchObject({ status: "written" });
|
|
176
|
+
expect(readFileSync(out, "utf-8")).toBe('{"serial":4}');
|
|
177
|
+
});
|
|
178
|
+
|
|
179
|
+
test("a local state keeps no versions, and is refused before anything is asked", async () => {
|
|
180
|
+
const ledger = fakeLedger();
|
|
181
|
+
const bin = fakeBinary({ type: "local", config: {} });
|
|
182
|
+
await expect(terraformStateExport({ ...common(ledger, rootDir(), bin.exec, fakeS3().fetch), words: { op: "tf-state-export" } })).rejects.toThrow(/^net: the state is a local file.*keeps no versions/);
|
|
183
|
+
expect(ledger.writes).toEqual([]);
|
|
184
|
+
});
|
|
185
|
+
});
|
|
186
|
+
|
|
187
|
+
describe("terraformStateUnlock", () => {
|
|
188
|
+
test("reads the lock file, waits for an approval of its ID, and releases it with force-unlock in the same data dir", async () => {
|
|
189
|
+
const s3 = fakeS3();
|
|
190
|
+
s3.put("acme/net.tfstate.tflock", JSON.stringify({ ID: "a1b2", Operation: "OperationTypeApply", Who: "runner@ci", Created: "2026-11-08T08:00:00Z" }));
|
|
191
|
+
const bin = fakeBinary({ type: "s3", config: S3_BACKEND }, (id) => {
|
|
192
|
+
if (id === "a1b2") s3.versions.delete("acme/net.tfstate.tflock");
|
|
193
|
+
});
|
|
194
|
+
const ledger = fakeLedger();
|
|
195
|
+
const words = { op: "tf-unlock" };
|
|
196
|
+
const first = await terraformStateUnlock({ ...common(ledger, rootDir(), bin.exec, s3.fetch), words, liveRuns: async () => [] });
|
|
197
|
+
expect(first).toMatchObject({ status: "waiting", location: "s3://acme/net.tfstate.tflock", lock: { ID: "a1b2", Who: "runner@ci" } });
|
|
198
|
+
ledger.approve("bo");
|
|
199
|
+
const second = await terraformStateUnlock({ ...common(ledger, rootDir(), bin.exec, s3.fetch), words, liveRuns: async () => [] });
|
|
200
|
+
expect(second).toMatchObject({ status: "released", approvedBy: "bo" });
|
|
201
|
+
const unlock = bin.calls.find((c) => c.args[0] === "force-unlock")!;
|
|
202
|
+
expect(unlock.args).toEqual(["force-unlock", "-force", "-no-color", "a1b2"]);
|
|
203
|
+
const init = bin.calls.filter((c) => c.args[0] === "init").at(-1)!;
|
|
204
|
+
expect(unlock.dataDir).toBe(init.dataDir);
|
|
205
|
+
expect(ledger.details[0]).toMatchObject({ lock_id: "a1b2", approved_by: "bo" });
|
|
206
|
+
});
|
|
207
|
+
|
|
208
|
+
test("a GitLab-managed state's lock is read by asking for it and released by its ID on the unlock address", async () => {
|
|
209
|
+
const gl = fakeGitLab();
|
|
210
|
+
gl.hold({ ID: "glock", Who: "runner", Created: "2026-11-08T08:00:00Z" });
|
|
211
|
+
const bin = fakeBinary({ type: "http", config: GITLAB_BACKEND });
|
|
212
|
+
const ledger = fakeLedger();
|
|
213
|
+
const words = { op: "tf-unlock" };
|
|
214
|
+
const first = await terraformStateUnlock({ ...common(ledger, rootDir(), bin.exec, gl.fetch), words, liveRuns: async () => [] });
|
|
215
|
+
expect(first).toMatchObject({ status: "waiting", location: `${GITLAB_ADDRESS}/lock`, lock: { ID: "glock" } });
|
|
216
|
+
ledger.approve("bo");
|
|
217
|
+
const second = await terraformStateUnlock({ ...common(ledger, rootDir(), bin.exec, gl.fetch), words, liveRuns: async () => [] });
|
|
218
|
+
expect(second).toMatchObject({ status: "released" });
|
|
219
|
+
expect(gl.held()).toBeUndefined();
|
|
220
|
+
expect(bin.calls.some((c) => c.args[0] === "force-unlock")).toBe(false);
|
|
221
|
+
});
|
|
222
|
+
|
|
223
|
+
test("refuses a local state and an s3 backend with no lock file", async () => {
|
|
224
|
+
const ledger = fakeLedger();
|
|
225
|
+
const opts = { words: { op: "tf-unlock" }, liveRuns: async () => [] };
|
|
226
|
+
await expect(terraformStateUnlock({ ...common(ledger, rootDir(), fakeBinary({ type: "local", config: {} }).exec, fakeS3().fetch), ...opts })).rejects.toThrow(/lock goes with the process/);
|
|
227
|
+
await expect(terraformStateUnlock({ ...common(ledger, rootDir(), fakeBinary({ type: "s3", config: { ...S3_BACKEND, use_lockfile: false } }).exec, fakeS3().fetch), ...opts })).rejects.toThrow(/use_lockfile/);
|
|
228
|
+
expect(ledger.writes).toEqual([]);
|
|
229
|
+
});
|
|
230
|
+
});
|
|
@@ -0,0 +1,236 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* #4762: a migration of resources between Terraform roots, planned by the
|
|
3
|
+
* lexicon and run by chant core's gated migration. The binary is faked: its
|
|
4
|
+
* `init` records the backend a root's `backend.json` (or the proof's
|
|
5
|
+
* override file) names into the data dir it is given, `state pull` and
|
|
6
|
+
* `state push` read and write that backend, and `plan` compares the root's
|
|
7
|
+
* `resources.txt` (its code) with its state. S3 is in memory behind the
|
|
8
|
+
* store's fetch, and the ledger is in memory. No binary, bucket or git is
|
|
9
|
+
* reached.
|
|
10
|
+
*/
|
|
11
|
+
import { existsSync, mkdirSync, mkdtempSync, readFileSync, readdirSync, writeFileSync } from "node:fs";
|
|
12
|
+
import { tmpdir } from "node:os";
|
|
13
|
+
import { join } from "node:path";
|
|
14
|
+
import { describe, expect, test } from "vitest";
|
|
15
|
+
import { fakeLedger } from "@intentius/chant/__fixtures__/gated-ledger";
|
|
16
|
+
import { runStateMigration } from "@intentius/chant/state-migration";
|
|
17
|
+
import type { BinaryExec, StateFile } from "./state/exec";
|
|
18
|
+
import type { StoreFetch } from "./state/object-store";
|
|
19
|
+
import { DEFAULT_OVERRIDE_FILE, MigrationError, moveResources, planTerraformMigration, stateDigest } from "./state-migration";
|
|
20
|
+
|
|
21
|
+
type Answer = Awaited<ReturnType<StoreFetch>>;
|
|
22
|
+
const answer = (status = 200, body = "", headers: Record<string, string> = {}): Answer => {
|
|
23
|
+
const h = Object.fromEntries(Object.entries(headers).map(([k, v]) => [k.toLowerCase(), v]));
|
|
24
|
+
return { ok: status < 300, status, text: async () => body, headers: { get: (n: string) => h[n.toLowerCase()] ?? null, forEach: (fn: (v: string, k: string) => void) => Object.entries(h).forEach(([k, v]) => fn(v, k)) } as Answer["headers"] };
|
|
25
|
+
};
|
|
26
|
+
|
|
27
|
+
/** A versioned bucket in memory, path-style at http://s3.test/<bucket>/<key>, with a PUT only when absent. */
|
|
28
|
+
function fakeS3() {
|
|
29
|
+
const versions = new Map<string, Array<{ id: string; body: string }>>();
|
|
30
|
+
let n = 0;
|
|
31
|
+
const put = (key: string, body: string) => versions.set(key, [...(versions.get(key) ?? []), { id: `v${++n}`, body }]);
|
|
32
|
+
const latest = (key: string) => versions.get(key)?.at(-1);
|
|
33
|
+
const fetch: StoreFetch = async (url, init) => {
|
|
34
|
+
const u = new URL(url);
|
|
35
|
+
const key = decodeURIComponent(u.pathname.slice(1));
|
|
36
|
+
const vid = u.searchParams.get("versionId");
|
|
37
|
+
const all = versions.get(key) ?? [];
|
|
38
|
+
if (init.method === "PUT") {
|
|
39
|
+
if (init.headers["if-none-match"] === "*" && all.length) return answer(412);
|
|
40
|
+
put(key, String(init.body));
|
|
41
|
+
return answer(200);
|
|
42
|
+
}
|
|
43
|
+
if (init.method === "DELETE") {
|
|
44
|
+
versions.delete(key);
|
|
45
|
+
return answer(204);
|
|
46
|
+
}
|
|
47
|
+
const v = vid ? all.find((x) => x.id === vid) : all.at(-1);
|
|
48
|
+
if (!v) return answer(404);
|
|
49
|
+
return answer(200, init.method === "HEAD" ? "" : v.body, { "x-amz-version-id": v.id });
|
|
50
|
+
};
|
|
51
|
+
return { versions, fetch, put, latest };
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
const S3_ENV = { AWS_ACCESS_KEY_ID: "AK", AWS_SECRET_ACCESS_KEY: "SK", AWS_REGION: "us-east-1" };
|
|
55
|
+
const s3Backend = (key: string) => ({ type: "s3", config: { bucket: "acme", key, use_lockfile: true, endpoints: { s3: "http://s3.test" } } });
|
|
56
|
+
|
|
57
|
+
/** The binary, over the fake bucket and local files. */
|
|
58
|
+
function fakeBinary(s3: ReturnType<typeof fakeS3>) {
|
|
59
|
+
const calls: string[] = [];
|
|
60
|
+
const plans = new Map<string, string[]>();
|
|
61
|
+
const backendOf = (env: NodeJS.ProcessEnv) => (JSON.parse(readFileSync(join(env.TF_DATA_DIR!, "terraform.tfstate"), "utf-8")) as { backend: { type: string; config: Record<string, unknown> } }).backend;
|
|
62
|
+
const read = (dir: string, env: NodeJS.ProcessEnv): string => {
|
|
63
|
+
const b = backendOf(env);
|
|
64
|
+
if (b.type === "local") return existsSync(String(b.config.path)) ? readFileSync(String(b.config.path), "utf-8") : "";
|
|
65
|
+
return s3.latest(`acme/${b.config.key}`)?.body ?? "";
|
|
66
|
+
};
|
|
67
|
+
const exec: BinaryExec = async (_bin, args, dir, env) => {
|
|
68
|
+
const name = dir.split("/").at(-1)!;
|
|
69
|
+
calls.push(`${name}: ${args.join(" ")}`);
|
|
70
|
+
if (args[0] === "init") {
|
|
71
|
+
const override = join(dir, DEFAULT_OVERRIDE_FILE);
|
|
72
|
+
const backend = existsSync(override) ? { type: "local", config: { path: /path = "(.*)"/.exec(readFileSync(override, "utf-8"))![1] } } : JSON.parse(readFileSync(join(dir, "backend.json"), "utf-8"));
|
|
73
|
+
mkdirSync(env.TF_DATA_DIR!, { recursive: true });
|
|
74
|
+
writeFileSync(join(env.TF_DATA_DIR!, "terraform.tfstate"), JSON.stringify({ version: 3, backend }));
|
|
75
|
+
return { code: 0, stdout: "", out: "" };
|
|
76
|
+
}
|
|
77
|
+
if (args[0] === "state" && args[1] === "pull") return { code: 0, stdout: read(dir, env), out: "" };
|
|
78
|
+
if (args[0] === "state" && args[1] === "push") {
|
|
79
|
+
const b = backendOf(env);
|
|
80
|
+
s3.put(`acme/${b.config.key}`, readFileSync(args.at(-1)!, "utf-8"));
|
|
81
|
+
return { code: 0, stdout: "", out: "" };
|
|
82
|
+
}
|
|
83
|
+
if (args[0] === "plan") {
|
|
84
|
+
const text = read(dir, env);
|
|
85
|
+
const have = new Set(text ? (JSON.parse(text) as StateFile).resources.map((r) => `${r.type}.${r.name}`) : []);
|
|
86
|
+
const code = new Set(readFileSync(join(dir, "resources.txt"), "utf-8").split("\n").filter(Boolean));
|
|
87
|
+
const changes = [...[...code].filter((a) => !have.has(a)).map((a) => `${a}=create`), ...[...have].filter((a) => !code.has(a)).map((a) => `${a}=delete`)];
|
|
88
|
+
plans.set(args.find((a) => a.startsWith("-out="))!.slice(5), changes);
|
|
89
|
+
return { code: 0, stdout: "", out: changes.length ? `Plan: ${changes.length} to change.` : "No changes. Your infrastructure matches the configuration." };
|
|
90
|
+
}
|
|
91
|
+
if (args[0] === "show") {
|
|
92
|
+
const changes = plans.get(args.at(-1)!) ?? [];
|
|
93
|
+
return { code: 0, stdout: JSON.stringify({ resource_changes: changes.map((c) => ({ address: c.split("=")[0], change: { actions: [c.split("=")[1]] } })) }), out: "" };
|
|
94
|
+
}
|
|
95
|
+
return { code: 1, stdout: "", out: `unexpected ${args.join(" ")}` };
|
|
96
|
+
};
|
|
97
|
+
return { exec, calls };
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
const resource = (type: string, name: string) => ({ mode: "managed", type, name, provider: 'provider["registry.terraform.io/hashicorp/aws"]', instances: [{ attributes: { id: name } }] });
|
|
101
|
+
const state = (serial: number, resources: ReturnType<typeof resource>[], lineage = "l-platform"): StateFile => ({ version: 4, terraform_version: "1.10.0", serial, lineage, outputs: {}, resources, check_results: null });
|
|
102
|
+
|
|
103
|
+
/** Two roots: platform holds the queue and the bucket, queues holds nothing yet; their code already has the queue in queues. */
|
|
104
|
+
function setup(codeMoved = true) {
|
|
105
|
+
const repo = mkdtempSync(join(tmpdir(), "chant-migrate-"));
|
|
106
|
+
const s3 = fakeS3();
|
|
107
|
+
s3.put("acme/platform.tfstate", JSON.stringify(state(4, [resource("aws_sqs_queue", "jobs"), resource("aws_s3_bucket", "logs")])));
|
|
108
|
+
const root = (name: string, resources: string[]) => {
|
|
109
|
+
const dir = join(repo, name);
|
|
110
|
+
mkdirSync(dir, { recursive: true });
|
|
111
|
+
writeFileSync(join(dir, "backend.json"), JSON.stringify(s3Backend(`${name}.tfstate`)));
|
|
112
|
+
writeFileSync(join(dir, "resources.txt"), resources.join("\n"));
|
|
113
|
+
return { root: name, dir, binary: "terraform", env: S3_ENV };
|
|
114
|
+
};
|
|
115
|
+
const roots = [root("platform", codeMoved ? ["aws_s3_bucket.logs"] : ["aws_s3_bucket.logs", "aws_sqs_queue.jobs"]), root("queues", codeMoved ? ["aws_sqs_queue.jobs"] : [])];
|
|
116
|
+
const bin = fakeBinary(s3);
|
|
117
|
+
const work = mkdtempSync(join(tmpdir(), "chant-migrate-work-"));
|
|
118
|
+
return { repo, s3, roots, bin, work };
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
const moves = [{ from: "platform", to: "queues", addresses: ["aws_sqs_queue.jobs"] }];
|
|
122
|
+
|
|
123
|
+
const gated = (ledger: ReturnType<typeof fakeLedger>, planned: Awaited<ReturnType<typeof planTerraformMigration>>) => ({
|
|
124
|
+
name: "move-queues",
|
|
125
|
+
by: "ana",
|
|
126
|
+
roots: planned.roots,
|
|
127
|
+
engine: planned.engine,
|
|
128
|
+
words: { op: "tf-migrate" },
|
|
129
|
+
rules: { mode: "ledger" as const },
|
|
130
|
+
ledger,
|
|
131
|
+
});
|
|
132
|
+
|
|
133
|
+
describe("moveResources", () => {
|
|
134
|
+
test("moves a resource and gives a root with no state a lineage of its own, and names every address that does not fit", () => {
|
|
135
|
+
const before = new Map<string, StateFile | null>([
|
|
136
|
+
["platform", state(4, [resource("aws_sqs_queue", "jobs")])],
|
|
137
|
+
["queues", null],
|
|
138
|
+
]);
|
|
139
|
+
const after = moveResources("m", moves, before);
|
|
140
|
+
expect(after.get("platform")).toMatchObject({ serial: 5, lineage: "l-platform", resources: [] });
|
|
141
|
+
expect(after.get("queues")).toMatchObject({ serial: 1, resources: [{ type: "aws_sqs_queue", name: "jobs" }] });
|
|
142
|
+
expect(moveResources("m", moves, before).get("queues")!.lineage).toBe(after.get("queues")!.lineage);
|
|
143
|
+
expect(() => moveResources("m", [{ from: "platform", to: "queues", addresses: ["aws_sqs_queue.other", "module.gone"] }], before)).toThrow(/aws_sqs_queue.other is not in the state of platform\n {2}module.gone is not in the state of platform/);
|
|
144
|
+
});
|
|
145
|
+
});
|
|
146
|
+
|
|
147
|
+
describe("planTerraformMigration with runStateMigration", () => {
|
|
148
|
+
test("proves both roots clean, waits for an approval, then writes the gaining root first under the locks and checks both", async () => {
|
|
149
|
+
const { s3, roots, bin, work, repo } = setup();
|
|
150
|
+
const ledger = fakeLedger();
|
|
151
|
+
const planned = await planTerraformMigration({ name: "move-queues", moves, roots, work, exec: bin.exec, fetch: s3.fetch, lock: { operation: "product migrate", info: "a state migration is writing this state" } });
|
|
152
|
+
expect(planned.roots.map((r) => [r.root, r.proof.changes])).toEqual([["platform", []], ["queues", []]]);
|
|
153
|
+
expect(planned.roots[0]).toMatchObject({ location: "s3://acme/platform.tfstate", before: { version_id: "v1" } });
|
|
154
|
+
expect(planned.roots[1]!.before).toEqual({ digest: null });
|
|
155
|
+
expect(readdirSync(join(repo, "platform")).sort()).toEqual(["backend.json", "resources.txt"]);
|
|
156
|
+
|
|
157
|
+
const first = await runStateMigration(gated(ledger, planned));
|
|
158
|
+
expect(first.status).toBe("waiting");
|
|
159
|
+
expect(s3.versions.get("acme/queues.tfstate")).toBeUndefined();
|
|
160
|
+
ledger.approve("bo");
|
|
161
|
+
const pushesBefore = bin.calls.filter((c) => c.includes("state push")).length;
|
|
162
|
+
const second = await runStateMigration(gated(ledger, planned));
|
|
163
|
+
expect(second).toMatchObject({ status: "applied", exit: 0 });
|
|
164
|
+
const pushes = bin.calls.filter((c) => c.includes("state push")).slice(pushesBefore);
|
|
165
|
+
expect(pushes.map((c) => c.split(":")[0])).toEqual(["queues", "platform"]);
|
|
166
|
+
expect(pushes.every((c) => c.includes("-lock=false"))).toBe(true);
|
|
167
|
+
expect((JSON.parse(s3.latest("acme/queues.tfstate")!.body) as StateFile).resources.map((r) => r.name)).toEqual(["jobs"]);
|
|
168
|
+
expect((JSON.parse(s3.latest("acme/platform.tfstate")!.body) as StateFile).resources.map((r) => r.name)).toEqual(["logs"]);
|
|
169
|
+
expect(s3.versions.has("acme/platform.tfstate.tflock")).toBe(false);
|
|
170
|
+
const [p, q] = [s3.latest("acme/platform.tfstate")!.id, s3.latest("acme/queues.tfstate")!.id];
|
|
171
|
+
expect(second.status === "applied" && second.result.roots.map((r) => [r.root, r.before.version_id, r.after.version_id])).toEqual([
|
|
172
|
+
["platform", "v1", p],
|
|
173
|
+
["queues", undefined, q],
|
|
174
|
+
]);
|
|
175
|
+
expect(ledger.details[0]).toMatchObject({ result: "applied", "platform.before": "v1", "platform.after": p, "queues.after": q });
|
|
176
|
+
});
|
|
177
|
+
|
|
178
|
+
test("a root whose code was not moved fails the proof, and nothing is asked", async () => {
|
|
179
|
+
const { s3, roots, bin, work } = setup(false);
|
|
180
|
+
const ledger = fakeLedger();
|
|
181
|
+
const planned = await planTerraformMigration({ name: "move-queues", moves, roots, work, exec: bin.exec, fetch: s3.fetch });
|
|
182
|
+
const r = await runStateMigration(gated(ledger, planned));
|
|
183
|
+
expect(r).toMatchObject({ status: "proof-failed", failing: ["platform", "queues"] });
|
|
184
|
+
expect(ledger.writes).toEqual([]);
|
|
185
|
+
});
|
|
186
|
+
|
|
187
|
+
test("a state written after the plan refuses the write under the lock", async () => {
|
|
188
|
+
const { s3, roots, bin, work } = setup();
|
|
189
|
+
const ledger = fakeLedger();
|
|
190
|
+
ledger.approve("bo");
|
|
191
|
+
const planned = await planTerraformMigration({ name: "move-queues", moves, roots, work, exec: bin.exec, fetch: s3.fetch });
|
|
192
|
+
s3.put("acme/platform.tfstate", JSON.stringify(state(5, [resource("aws_sqs_queue", "jobs"), resource("aws_s3_bucket", "logs"), resource("aws_s3_bucket", "more")])));
|
|
193
|
+
const r = await runStateMigration(gated(ledger, planned));
|
|
194
|
+
expect(r).toMatchObject({ status: "moved", exit: 4 });
|
|
195
|
+
expect(bin.calls.some((c) => c.includes("state push"))).toBe(false);
|
|
196
|
+
expect(s3.versions.has("acme/platform.tfstate.tflock")).toBe(false);
|
|
197
|
+
});
|
|
198
|
+
|
|
199
|
+
test("a state another run holds the lock of fails the migration with nothing written", async () => {
|
|
200
|
+
const { s3, roots, bin, work } = setup();
|
|
201
|
+
const ledger = fakeLedger();
|
|
202
|
+
ledger.approve("bo");
|
|
203
|
+
const planned = await planTerraformMigration({ name: "move-queues", moves, roots, work, exec: bin.exec, fetch: s3.fetch });
|
|
204
|
+
s3.put("acme/queues.tfstate.tflock", JSON.stringify({ ID: "other", Who: "runner@ci" }));
|
|
205
|
+
const r = await runStateMigration(gated(ledger, planned));
|
|
206
|
+
expect(r).toMatchObject({ status: "failed" });
|
|
207
|
+
expect(r.status === "failed" && r.error).toMatch(/queues: its state is locked .* by runner@ci/);
|
|
208
|
+
expect(bin.calls.some((c) => c.includes("state push"))).toBe(false);
|
|
209
|
+
expect(s3.versions.has("acme/platform.tfstate.tflock")).toBe(false);
|
|
210
|
+
});
|
|
211
|
+
|
|
212
|
+
test("a revert puts back the version a migration recorded, and is refused once the state moved past it", async () => {
|
|
213
|
+
const { s3, roots, bin, work } = setup();
|
|
214
|
+
const was = s3.latest("acme/platform.tfstate")!.body;
|
|
215
|
+
s3.put("acme/platform.tfstate", JSON.stringify(state(5, [resource("aws_s3_bucket", "logs")])));
|
|
216
|
+
const restores = [{ root: "platform", location: "s3://acme/platform.tfstate", version_id: "v1", from_version_id: "v2" }];
|
|
217
|
+
const ledger = fakeLedger();
|
|
218
|
+
ledger.approve("bo");
|
|
219
|
+
const back = roots.map((r) => (r.root === "platform" ? { ...r } : r));
|
|
220
|
+
writeFileSync(join(back[0]!.dir, "resources.txt"), "aws_s3_bucket.logs\naws_sqs_queue.jobs");
|
|
221
|
+
const planned = await planTerraformMigration({ name: "move-queues-revert", restores, roots: back, work, exec: bin.exec, fetch: s3.fetch });
|
|
222
|
+
expect(planned.roots).toHaveLength(1);
|
|
223
|
+
const r = await runStateMigration({ ...gated(ledger, planned), name: "move-queues-revert" });
|
|
224
|
+
expect(r.status).toBe("applied");
|
|
225
|
+
const now = JSON.parse(s3.latest("acme/platform.tfstate")!.body) as StateFile;
|
|
226
|
+
expect(now.resources.map((x) => x.name).sort()).toEqual(["jobs", "logs"]);
|
|
227
|
+
expect(now.serial).toBe(6);
|
|
228
|
+
expect(stateDigest({ ...now, serial: 4 })).toBe(stateDigest(JSON.parse(was) as StateFile));
|
|
229
|
+
await expect(planTerraformMigration({ name: "again", restores, roots: back, work: mkdtempSync(join(tmpdir(), "w-")), exec: bin.exec, fetch: s3.fetch })).rejects.toThrow(/moved since/);
|
|
230
|
+
});
|
|
231
|
+
|
|
232
|
+
test("an address in no state is refused at plan", async () => {
|
|
233
|
+
const { s3, roots, bin, work } = setup();
|
|
234
|
+
await expect(planTerraformMigration({ name: "m", moves: [{ from: "queues", to: "platform", addresses: ["aws_sqs_queue.jobs"] }], roots, work, exec: bin.exec, fetch: s3.fetch })).rejects.toBeInstanceOf(MigrationError);
|
|
235
|
+
});
|
|
236
|
+
});
|