@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
package/dist/state-ops.js
CHANGED
|
@@ -34,13 +34,21 @@
|
|
|
34
34
|
* The exec is injectable (`./state/exec.ts`), and so is the gated-request
|
|
35
35
|
* ledger, so a test runs no binary and writes no ledger.
|
|
36
36
|
*/
|
|
37
|
-
import {
|
|
37
|
+
import { mkdtempSync, rmSync } from "node:fs";
|
|
38
|
+
import { tmpdir } from "node:os";
|
|
38
39
|
import { join } from "node:path";
|
|
39
40
|
import { CHANT_EXIT } from "@intentius/chant/exit-codes";
|
|
40
|
-
import {
|
|
41
|
+
import { gatedRequestDigest } from "@intentius/chant/lifecycle/gated-request";
|
|
42
|
+
import { gatedOp } from "@intentius/chant/lifecycle/gated-op";
|
|
43
|
+
import { exportStateVersion, StateExportError } from "@intentius/chant/state-export";
|
|
44
|
+
import { parseLockInfo, releaseStateLock, StateUnlockError } from "@intentius/chant/state-unlock";
|
|
41
45
|
import { plannerForBinary, terraformChangeSetPart } from "./change-set.js";
|
|
42
46
|
import { DEFAULT_LOCK_TIMEOUT, lockTimeoutArgs } from "./lock-timeout.js";
|
|
47
|
+
import { initialisedBackend, isRefused, stateObject } from "./state/backend.js";
|
|
43
48
|
import { lastLines, runBinary } from "./state/exec.js";
|
|
49
|
+
import { gitlabState, gitlabStore, shownUrl } from "./state/gitlab.js";
|
|
50
|
+
import { stateStore } from "./state/store.js";
|
|
51
|
+
import { gitlabVersionSource, stateVersionSource } from "./state/versions.js";
|
|
44
52
|
/** The ops the gated requests of this module wait at, one per kind, as terragucci named them. */
|
|
45
53
|
export const STATE_OPS = {
|
|
46
54
|
unlock: "tf-unlock",
|
|
@@ -150,89 +158,124 @@ export async function planCopy(r) {
|
|
|
150
158
|
export async function applyPlanFile(r) {
|
|
151
159
|
await ran(r, ["apply", "-input=false", "-no-color", ...lockTimeoutArgs("apply", r.lockTimeout ?? DEFAULT_LOCK_TIMEOUT, r.env ?? process.env), r.planFile], "apply");
|
|
152
160
|
}
|
|
153
|
-
|
|
161
|
+
// ── the gate around them ────────────────────────────────────────────────────
|
|
162
|
+
// The walk itself is chant core's since #4762 (`@intentius/chant/lifecycle/gated-op`), engine-neutral; these names stay for the products written against them.
|
|
163
|
+
export { attachmentDigest, requestedAt } from "@intentius/chant/lifecycle/gated-op";
|
|
164
|
+
/** Run `act` once its request is approved: chant core's {@link gatedOp}. */
|
|
165
|
+
export const gatedStateOp = gatedOp;
|
|
166
|
+
/** The digest a request binds, for a caller that prints it before it asks. */
|
|
167
|
+
export const stateOpDigest = (op, request) => gatedRequestDigest(op, request);
|
|
154
168
|
/**
|
|
155
|
-
*
|
|
156
|
-
*
|
|
157
|
-
*
|
|
158
|
-
*
|
|
159
|
-
* its `at`; with none, `now`.
|
|
169
|
+
* Where a root's state is: init (in a data dir of its own unless `init` is
|
|
170
|
+
* false), then the backend init recorded. `act` runs while the data dir is
|
|
171
|
+
* there, with the env that names it, so a binary run inside it finds the
|
|
172
|
+
* root initialised; the data dir is removed after.
|
|
160
173
|
*/
|
|
161
|
-
|
|
162
|
-
const
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
const
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
.
|
|
170
|
-
|
|
174
|
+
async function withBackend(r, act, readOnly = false) {
|
|
175
|
+
const env = r.env ?? process.env;
|
|
176
|
+
if (r.init === false)
|
|
177
|
+
return act(objectOf(r.dir, env, r.binary, readOnly), env);
|
|
178
|
+
const data = mkdtempSync(join(tmpdir(), "chant-state-data-"));
|
|
179
|
+
try {
|
|
180
|
+
const benv = { ...env, TF_DATA_DIR: data };
|
|
181
|
+
await initRoot({ ...r, env: benv });
|
|
182
|
+
return await act(objectOf(r.dir, benv, r.binary, readOnly), benv);
|
|
183
|
+
}
|
|
184
|
+
finally {
|
|
185
|
+
rmSync(data, { recursive: true, force: true });
|
|
186
|
+
}
|
|
171
187
|
}
|
|
172
|
-
|
|
173
|
-
export const attachmentDigest = (attachment) => `sha256:${createHash("sha256").update(attachment).digest("hex")}`;
|
|
188
|
+
const isGitLab = (o) => "address" in o;
|
|
174
189
|
/**
|
|
175
|
-
*
|
|
176
|
-
*
|
|
177
|
-
*
|
|
178
|
-
*
|
|
179
|
-
* cannot be written is reported as `recorded: false`; the act already ran.
|
|
190
|
+
* The root's state. A read needs no lock file, so an s3 backend that locks
|
|
191
|
+
* with DynamoDB is read as one that takes the lock file: the refusal is
|
|
192
|
+
* about holding its lock, which a read never does. An http backend whose
|
|
193
|
+
* address is GitLab's state API is that state.
|
|
180
194
|
*/
|
|
181
|
-
|
|
182
|
-
const
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
const
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
const
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
return false;
|
|
223
|
-
}
|
|
195
|
+
function objectOf(dir, env, binary, readOnly) {
|
|
196
|
+
const o = stateObject(dir, env, undefined, binary);
|
|
197
|
+
if (!isRefused(o))
|
|
198
|
+
return o;
|
|
199
|
+
const recorded = initialisedBackend(dir, env);
|
|
200
|
+
if (o.backend === "http" && recorded?.type === "http")
|
|
201
|
+
return gitlabState(recorded.config, env) ?? o;
|
|
202
|
+
if (!readOnly || o.refused !== "no-lockfile")
|
|
203
|
+
return o;
|
|
204
|
+
return stateObject(dir, env, recorded ? { ...recorded, config: { ...recorded.config, use_lockfile: true } } : undefined, binary, { backendConfig: { use_lockfile: "true" } });
|
|
205
|
+
}
|
|
206
|
+
/**
|
|
207
|
+
* chant core's state export (`exportStateVersion`) of one root: its backend
|
|
208
|
+
* read by an init of its own, its versions read from the bucket or container
|
|
209
|
+
* (`./state/versions.ts`). Throws `StateExportError` for a backend that keeps
|
|
210
|
+
* no versions chant reads.
|
|
211
|
+
*/
|
|
212
|
+
export async function terraformStateExport(input) {
|
|
213
|
+
const source = await withBackend(input, async (object) => (isGitLab(object) ? gitlabVersionSource(gitlabStore(object, input.fetch)) : stateVersionSource(object, input.fetch)), true);
|
|
214
|
+
if ("refused" in source)
|
|
215
|
+
throw new StateExportError(`${input.root}: ${source.refused}`);
|
|
216
|
+
return exportStateVersion({ ...input, subject: input.root, source });
|
|
217
|
+
}
|
|
218
|
+
/** What a state lock release reads and releases. */
|
|
219
|
+
const RELEASES = "a lock release reads the lock of an s3 backend's lock file, a gcs backend, an azurerm backend and a GitLab-managed state";
|
|
220
|
+
/**
|
|
221
|
+
* A root's lock as chant core's lock release reads it: the lock its store
|
|
222
|
+
* holds (`StateStore.heldLock`: the s3 lock file, the gcs lock object, whose
|
|
223
|
+
* generation is the ID the binary takes, the azurerm lease), released with
|
|
224
|
+
* the binary's fixed `force-unlock` of that ID. A GitLab-managed state's lock
|
|
225
|
+
* is read by asking for it and released by its ID on the unlock address,
|
|
226
|
+
* which GitLab refuses for any other lock. A local state's lock goes with
|
|
227
|
+
* the process that took it, so it is refused.
|
|
228
|
+
*/
|
|
229
|
+
export function terraformLockSource(r) {
|
|
230
|
+
const o = r.object;
|
|
231
|
+
const parsed = (text, where, id) => {
|
|
232
|
+
const info = parseLockInfo(text);
|
|
233
|
+
if (!info)
|
|
234
|
+
throw new StateOpError(r.root, "a lock read", 0, `${where} holds no lock ID the binary wrote; read it, and remove it by hand if it is not a lock`);
|
|
235
|
+
return id ? { ...info, ID: id } : info;
|
|
224
236
|
};
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
237
|
+
if (isGitLab(o)) {
|
|
238
|
+
if (!o.lockAddress || !o.unlockAddress)
|
|
239
|
+
return { refused: `its http backend names no ${o.lockAddress ? "unlock_address (TF_HTTP_UNLOCK_ADDRESS)" : "lock_address (TF_HTTP_LOCK_ADDRESS)"}, so no lock it takes can be read and released` };
|
|
240
|
+
const store = gitlabStore(o, r.fetch);
|
|
241
|
+
const location = shownUrl(o.lockAddress);
|
|
242
|
+
return {
|
|
243
|
+
location,
|
|
244
|
+
read: async () => {
|
|
245
|
+
const text = await store.probeLock();
|
|
246
|
+
return text === undefined ? undefined : parsed(text, location);
|
|
247
|
+
},
|
|
248
|
+
release: (id) => store.releaseLock(id),
|
|
249
|
+
};
|
|
232
250
|
}
|
|
251
|
+
if (isRefused(o))
|
|
252
|
+
return { refused: `${o.reason}; ${RELEASES}` };
|
|
253
|
+
if (o.backend === "local")
|
|
254
|
+
return { refused: `the state is a local file, whose lock goes with the process that took it; ${RELEASES}` };
|
|
255
|
+
const store = stateStore(o, r.dir, r.fetch);
|
|
256
|
+
return {
|
|
257
|
+
location: store.lockLocation,
|
|
258
|
+
async read() {
|
|
259
|
+
const held = await store.heldLock();
|
|
260
|
+
return held ? parsed(held.info, store.lockLocation, held.id || undefined) : undefined;
|
|
261
|
+
},
|
|
262
|
+
release: (id) => forceUnlockState({ ...r, id }),
|
|
263
|
+
};
|
|
264
|
+
}
|
|
265
|
+
/**
|
|
266
|
+
* chant core's lock release (`releaseStateLock`) of one root's state: its
|
|
267
|
+
* backend read by an init of its own, the lock read from the store, released
|
|
268
|
+
* by `force-unlock` in the same data dir. Throws `StateUnlockError` for a
|
|
269
|
+
* backend whose lock chant does not read.
|
|
270
|
+
*/
|
|
271
|
+
export async function terraformStateUnlock(input) {
|
|
272
|
+
return withBackend(input, async (object, env) => {
|
|
273
|
+
const lock = terraformLockSource({ ...input, env, object, ...(input.fetch ? { fetch: input.fetch } : {}) });
|
|
274
|
+
if ("refused" in lock)
|
|
275
|
+
throw new StateUnlockError(`${input.root}: ${lock.refused}`);
|
|
276
|
+
return releaseStateLock({ ...input, subject: input.root, lock });
|
|
277
|
+
});
|
|
233
278
|
}
|
|
234
|
-
/** The digest a request binds, for a caller that prints it before it asks. */
|
|
235
|
-
export const stateOpDigest = (op, request) => gatedRequestDigest(op, request);
|
|
236
279
|
const actorOf = (env) => env.GITHUB_ACTOR || env.GITLAB_USER_LOGIN || env.USER || env.USERNAME || "unknown";
|
|
237
280
|
const dirOf = (cwd, input) => input.dir ?? join(cwd ?? process.cwd(), input.root);
|
|
238
281
|
async function gatedRun(op, input, kind, fields, act, attachment) {
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@intentius/chant-lexicon-terraform",
|
|
3
|
-
"version": "0.124.
|
|
3
|
+
"version": "0.124.77",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"files": [
|
|
6
6
|
"src/",
|
|
@@ -93,7 +93,7 @@
|
|
|
93
93
|
"access": "public"
|
|
94
94
|
},
|
|
95
95
|
"peerDependencies": {
|
|
96
|
-
"@intentius/chant": "^0.124.
|
|
96
|
+
"@intentius/chant": "^0.124.77",
|
|
97
97
|
"zod": "^4.3.6"
|
|
98
98
|
},
|
|
99
99
|
"description": "terraform lexicon for chant, existing Terraform root modules as declarable entities",
|
|
@@ -0,0 +1,132 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* #4762: a pull request's copy of Terraform roots through chant core's
|
|
3
|
+
* prEnvUp and prEnvDown, with the binary faked (it records its arguments and
|
|
4
|
+
* answers a plan with the actions it is told) and the ports in memory. No
|
|
5
|
+
* binary, backend or ledger is reached.
|
|
6
|
+
*/
|
|
7
|
+
import { mkdirSync, mkdtempSync, writeFileSync } from "node:fs";
|
|
8
|
+
import { tmpdir } from "node:os";
|
|
9
|
+
import { join } from "node:path";
|
|
10
|
+
import { describe, expect, test } from "vitest";
|
|
11
|
+
import { prEnvDown, prEnvUp, type PrEnvApproval, type PrEnvPorts } from "@intentius/chant/components/pr-env-run";
|
|
12
|
+
import type { PrEnvRecord } from "@intentius/chant/components/pr-environment";
|
|
13
|
+
import { carriesSuffix, copyBackend, CopyRefusedError, suffixedKey, terraformPrEnvAdapter, terraformPrEnvTeardown, type CopyRoot } from "./pr-env";
|
|
14
|
+
import type { BinaryExec } from "./state/exec";
|
|
15
|
+
|
|
16
|
+
const HEAD = "b".repeat(40);
|
|
17
|
+
const tmp = (): string => mkdtempSync(join(tmpdir(), "chant-pr-env-"));
|
|
18
|
+
|
|
19
|
+
function ports(approval: PrEnvApproval, records: PrEnvRecord[] = []): PrEnvPorts & { records: PrEnvRecord[] } {
|
|
20
|
+
return { records, readRecords: async () => [...records], appendRecord: async (r) => void records.push(r), decideGate: async () => approval };
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
/** The binary: every call recorded with its root's directory; a plan of `actions`, a destroy of one delete. */
|
|
24
|
+
function fakeBinary(actions: string[] = ["create"]) {
|
|
25
|
+
const calls: string[] = [];
|
|
26
|
+
const exec: BinaryExec = async (_bin, args, dir) => {
|
|
27
|
+
calls.push(`${dir.split("/").at(-1)}: ${args.join(" ")}`);
|
|
28
|
+
if (args[0] === "show") {
|
|
29
|
+
const destroy = calls.some((c) => c.includes("-destroy") && c.includes(args.at(-1)!));
|
|
30
|
+
const acts = destroy ? ["delete"] : actions;
|
|
31
|
+
return { code: 0, stdout: JSON.stringify({ resource_changes: acts.map((a, i) => ({ address: `null_resource.r${i}`, mode: "managed", type: "null_resource", name: `r${i}`, change: { actions: [a], before: a === "create" ? null : {}, after: a === "delete" ? null : {} } })) }), out: "" };
|
|
32
|
+
}
|
|
33
|
+
return { code: 0, stdout: "", out: args[0] === "plan" ? "Plan: 1 to add, 0 to change, 0 to destroy." : "" };
|
|
34
|
+
};
|
|
35
|
+
return { exec, calls };
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
/** A root directory whose code declares `backend`. */
|
|
39
|
+
function rootWith(repo: string, name: string, backend: string): CopyRoot {
|
|
40
|
+
const dir = join(repo, name);
|
|
41
|
+
mkdirSync(dir, { recursive: true });
|
|
42
|
+
writeFileSync(join(dir, "main.tf"), `terraform {\n${backend}\n}\n`);
|
|
43
|
+
return { root: name, dir, binary: "terraform", env: {} };
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
describe("a copy's backend", () => {
|
|
47
|
+
test("suffixes a key before .tfstate, a gcs prefix at its end, and a GitLab state's name and lock", () => {
|
|
48
|
+
expect(suffixedKey("envs/dev/terraform.tfstate", "pr-7")).toBe("envs/dev/terraform-pr-7.tfstate");
|
|
49
|
+
expect(suffixedKey("envs/dev/", "pr-7")).toBe("envs/dev-pr-7");
|
|
50
|
+
expect(carriesSuffix("s3://acme/net-pr-7.tfstate", "pr-7")).toBe(true);
|
|
51
|
+
expect(carriesSuffix("s3://acme/net-pr-70.tfstate", "pr-7")).toBe(false);
|
|
52
|
+
const repo = tmp();
|
|
53
|
+
const gcs = rootWith(repo, "net", 'backend "gcs" {\n bucket = "acme"\n prefix = "envs/net"\n}');
|
|
54
|
+
expect(copyBackend(gcs.dir, "net", "pr-7")).toEqual({ type: "gcs", attribute: "prefix", key: "envs/net-pr-7", location: "gs://acme/envs/net-pr-7" });
|
|
55
|
+
const a = "https://gitlab.example.com/api/v4/projects/7/terraform/state/net";
|
|
56
|
+
const http = rootWith(repo, "gl", `backend "http" {\n address = "${a}"\n lock_address = "${a}/lock"\n}`);
|
|
57
|
+
expect(copyBackend(http.dir, "gl", "pr-7")).toEqual({ type: "http", attribute: "address", key: `${a}-pr-7`, location: `${a}-pr-7`, extra: { lock_address: `${a}-pr-7/lock` } });
|
|
58
|
+
});
|
|
59
|
+
|
|
60
|
+
test("refuses a cloud block, a backend with no key attribute, and a block that names no key", () => {
|
|
61
|
+
const repo = tmp();
|
|
62
|
+
expect(() => copyBackend(rootWith(repo, "c", 'cloud {\n organization = "acme"\n}').dir, "c", "pr-7")).toThrow(CopyRefusedError);
|
|
63
|
+
expect(() => copyBackend(rootWith(repo, "p", 'backend "pg" {\n conn_str = "x"\n}').dir, "p", "pr-7")).toThrow(/backend is pg/);
|
|
64
|
+
expect(() => copyBackend(rootWith(repo, "s", 'backend "s3" {\n bucket = "acme"\n}').dir, "s", "pr-7")).toThrow(/names no key/);
|
|
65
|
+
});
|
|
66
|
+
});
|
|
67
|
+
|
|
68
|
+
describe("terraformPrEnvAdapter and terraformPrEnvTeardown", () => {
|
|
69
|
+
const S3 = (key: string) => `backend "s3" {\n bucket = "acme"\n key = "${key}"\n use_lockfile = true\n}`;
|
|
70
|
+
|
|
71
|
+
test("each root's copy inits under its suffixed key, waits at the gate, applies its saved plan, and is destroyed in reverse order", async () => {
|
|
72
|
+
const repo = tmp();
|
|
73
|
+
const roots = [rootWith(repo, "network", S3("network.tfstate")), rootWith(repo, "app", S3("app/terraform.tfstate"))];
|
|
74
|
+
const bin = fakeBinary();
|
|
75
|
+
const work = tmp();
|
|
76
|
+
const adapter = () => terraformPrEnvAdapter({ roots: async () => roots, work, exec: bin.exec, lockTimeout: false });
|
|
77
|
+
const waiting = await prEnvUp({ pr: 7, head: HEAD, template: "copy", stack: "product", adapter: adapter(), ports: ports({ status: "pending" }) });
|
|
78
|
+
expect(waiting.exit).toBe(3);
|
|
79
|
+
expect(bin.calls.filter((c) => c.includes("apply"))).toEqual([]);
|
|
80
|
+
const p = ports({ status: "approved", approvedBy: ["bo"] });
|
|
81
|
+
const applied = await prEnvUp({ pr: 7, head: HEAD, template: "copy", stack: "product", adapter: adapter(), ports: p });
|
|
82
|
+
expect(applied.exit).toBe(0);
|
|
83
|
+
expect(bin.calls).toContain("network: init -input=false -no-color -reconfigure -backend-config=key=network-pr-7.tfstate");
|
|
84
|
+
expect(bin.calls).toContain("app: init -input=false -no-color -reconfigure -backend-config=key=app/terraform-pr-7.tfstate");
|
|
85
|
+
expect(bin.calls.filter((c) => c.includes(": apply")).map((c) => c.split(":")[0])).toEqual(["network", "app"]);
|
|
86
|
+
expect(p.records[0]!.locations).toEqual({ network: "s3://acme/network-pr-7.tfstate", app: "s3://acme/app/terraform-pr-7.tfstate" });
|
|
87
|
+
|
|
88
|
+
bin.calls.length = 0;
|
|
89
|
+
let done = 0;
|
|
90
|
+
const teardown = terraformPrEnvTeardown({ name: "product", checkout: async () => ({ roots, done: () => void done++ }), work, exec: bin.exec, lockTimeout: false });
|
|
91
|
+
const down = await prEnvDown({ pr: 7, reason: "closed", ports: p, adapters: [teardown], commit: HEAD });
|
|
92
|
+
expect(down.report.status).toBe("destroyed");
|
|
93
|
+
expect(bin.calls.filter((c) => c.includes(": plan")).every((c) => c.includes("-destroy"))).toBe(true);
|
|
94
|
+
expect(bin.calls.filter((c) => c.includes(": apply")).map((c) => c.split(":")[0])).toEqual(["app", "network"]);
|
|
95
|
+
expect(done).toBe(1);
|
|
96
|
+
});
|
|
97
|
+
|
|
98
|
+
test("a prepared root plans with no init of its own, and is refused when its recorded key is its own", async () => {
|
|
99
|
+
const repo = tmp();
|
|
100
|
+
const bin = fakeBinary();
|
|
101
|
+
const prepared = (key: string): CopyRoot => ({
|
|
102
|
+
root: "unit",
|
|
103
|
+
dir: join(repo, "unit"),
|
|
104
|
+
binary: "terraform",
|
|
105
|
+
prepare: async () => {
|
|
106
|
+
const dir = join(repo, "unit-cache");
|
|
107
|
+
mkdirSync(join(dir, ".terraform"), { recursive: true });
|
|
108
|
+
writeFileSync(join(dir, ".terraform", "terraform.tfstate"), JSON.stringify({ version: 3, backend: { type: "s3", config: { bucket: "acme", key } } }));
|
|
109
|
+
return { dir, env: {} };
|
|
110
|
+
},
|
|
111
|
+
});
|
|
112
|
+
const ok = terraformPrEnvAdapter({ roots: async () => [prepared("unit/terraform-pr-7.tfstate")], work: tmp(), exec: bin.exec, lockTimeout: false });
|
|
113
|
+
const r = await prEnvUp({ pr: 7, head: HEAD, template: "copy", stack: "product", adapter: ok, ports: ports({ status: "approved" }) });
|
|
114
|
+
expect(r.exit).toBe(0);
|
|
115
|
+
expect(bin.calls.some((c) => c.includes("init"))).toBe(false);
|
|
116
|
+
const own = terraformPrEnvAdapter({ roots: async () => [prepared("unit/terraform.tfstate")], work: tmp(), exec: bin.exec, hint: "make remote_state read the suffix" });
|
|
117
|
+
const refused = await prEnvUp({ pr: 7, head: HEAD, template: "copy", stack: "product", adapter: own, ports: ports({ status: "approved" }) });
|
|
118
|
+
expect(refused.report.status).toBe("plan-failed");
|
|
119
|
+
expect(own.refused).toBeInstanceOf(CopyRefusedError);
|
|
120
|
+
expect(own.refused!.message).toMatch(/root's own state, so its copy is refused; make remote_state read the suffix$/);
|
|
121
|
+
});
|
|
122
|
+
|
|
123
|
+
test("a root no copy can be made of refuses the environment, and nothing plans", async () => {
|
|
124
|
+
const repo = tmp();
|
|
125
|
+
const bin = fakeBinary();
|
|
126
|
+
const adapter = terraformPrEnvAdapter({ roots: async () => [rootWith(repo, "c", 'cloud {\n organization = "acme"\n}')], work: tmp(), exec: bin.exec });
|
|
127
|
+
const r = await prEnvUp({ pr: 7, head: HEAD, template: "copy", stack: "product", adapter, ports: ports({ status: "approved" }) });
|
|
128
|
+
expect(r.report.status).toBe("plan-failed");
|
|
129
|
+
expect(adapter.refused).toBeInstanceOf(CopyRefusedError);
|
|
130
|
+
expect(bin.calls).toEqual([]);
|
|
131
|
+
});
|
|
132
|
+
});
|