@intentius/chant-lexicon-terraform 0.124.84 → 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/dist/ci-setup.d.ts +11 -2
- package/dist/ci-setup.d.ts.map +1 -1
- package/dist/ci-setup.js +11 -6
- package/dist/components/capability-plugin.d.ts +12 -0
- package/dist/components/capability-plugin.d.ts.map +1 -1
- package/dist/components/capability-plugin.js +22 -4
- package/dist/compose.d.ts.map +1 -1
- package/dist/compose.js +1 -1
- package/dist/estate-migration.d.ts +101 -0
- package/dist/estate-migration.d.ts.map +1 -0
- package/dist/estate-migration.js +471 -0
- package/dist/integrity.json +2 -2
- package/dist/manifest.json +1 -1
- package/dist/op/activities/terraform.d.ts +65 -0
- package/dist/op/activities/terraform.d.ts.map +1 -1
- package/dist/op/activities/terraform.js +94 -1
- package/dist/state/backend.d.ts +46 -9
- package/dist/state/backend.d.ts.map +1 -1
- package/dist/state/backend.js +84 -31
- package/dist/state/gitlab.d.ts +1 -1
- package/dist/state/gitlab.d.ts.map +1 -1
- package/dist/state/gitlab.js +27 -14
- package/dist/state/index.d.ts +1 -0
- package/dist/state/index.d.ts.map +1 -1
- package/dist/state/index.js +1 -0
- package/dist/state/s3.d.ts +9 -3
- package/dist/state/s3.d.ts.map +1 -1
- package/dist/state/s3.js +4 -3
- package/dist/state/source.d.ts +92 -0
- package/dist/state/source.d.ts.map +1 -0
- package/dist/state/source.js +254 -0
- package/dist/state/store.d.ts +41 -4
- package/dist/state/store.d.ts.map +1 -1
- package/dist/state/store.js +125 -10
- package/dist/state/versions.d.ts +77 -10
- package/dist/state/versions.d.ts.map +1 -1
- package/dist/state/versions.js +118 -64
- package/dist/state-migration.d.ts +43 -1
- package/dist/state-migration.d.ts.map +1 -1
- package/dist/state-migration.js +251 -20
- package/dist/state-ops.d.ts +2 -3
- package/dist/state-ops.d.ts.map +1 -1
- package/dist/state-ops.js +4 -7
- package/package.json +2 -2
- package/src/ci-setup.test.ts +20 -0
- package/src/ci-setup.ts +11 -6
- package/src/components/capability-plugin.ts +36 -3
- package/src/components/terraform-apply-command-hook.test.ts +157 -0
- package/src/compose.ts +1 -0
- package/src/estate-migration.test.ts +186 -0
- package/src/estate-migration.ts +563 -0
- package/src/op/activities/terraform.ts +153 -3
- package/src/state/backend-verdict.test.ts +4 -3
- package/src/state/backend.test.ts +3 -1
- package/src/state/backend.ts +108 -33
- package/src/state/gitlab.ts +25 -12
- package/src/state/index.ts +1 -0
- package/src/state/s3.ts +5 -4
- package/src/state/source.ts +289 -0
- package/src/state/store.ts +145 -11
- package/src/state/versions.test.ts +365 -0
- package/src/state/versions.ts +148 -68
- package/src/state-migration-backends.test.ts +277 -0
- package/src/state-migration.ts +265 -23
- package/src/state-ops.ts +6 -8
|
@@ -0,0 +1,157 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* `terraformApplyCapabilityWith({ onCommand })` (chant#4865): every engine
|
|
3
|
+
* command the step runs for a root goes through the product's hook, with its
|
|
4
|
+
* name, args, directory and environment before it runs, and its exit code,
|
|
5
|
+
* duration and output after. Run by core's `runOpWave` with `parallelism` 2
|
|
6
|
+
* against a fake `terraform` on `PATH` that logs each command with the
|
|
7
|
+
* `$FAKE_TAG` it was run with, so the test reads which variable the hook added
|
|
8
|
+
* reached which command. Nothing reaches a cloud.
|
|
9
|
+
*/
|
|
10
|
+
|
|
11
|
+
import { chmodSync, mkdirSync, mkdtempSync, readFileSync, realpathSync, writeFileSync } from "node:fs";
|
|
12
|
+
import { tmpdir } from "node:os";
|
|
13
|
+
import { join } from "node:path";
|
|
14
|
+
import { afterEach, describe, expect, test, vi } from "vitest";
|
|
15
|
+
import { CapabilityRegistry, type Capability } from "@intentius/chant/components/capability";
|
|
16
|
+
import { memoryGateLedgerPort, runOpWave, type OpWavesSpec } from "@intentius/chant/op";
|
|
17
|
+
import { terraformApplyCapabilityWith, type TerraformCommand, type TerraformCommandResult } from "./capability-plugin";
|
|
18
|
+
|
|
19
|
+
afterEach(() => vi.unstubAllEnvs());
|
|
20
|
+
|
|
21
|
+
const FAKE = `#!/bin/sh
|
|
22
|
+
cmd="$1"
|
|
23
|
+
root="\${PWD##*/}"
|
|
24
|
+
echo "$cmd $root \${FAKE_TAG:-none}" >> "$FAKE_LOG"
|
|
25
|
+
case "$cmd" in
|
|
26
|
+
plan)
|
|
27
|
+
sleep 0.05
|
|
28
|
+
for a in "$@"; do case "$a" in -out=*) out="\${a#-out=}" ;; esac; done
|
|
29
|
+
echo plan > "$out"
|
|
30
|
+
exit 2 ;;
|
|
31
|
+
apply) sleep 0.05 ;;
|
|
32
|
+
show) echo "{\\"format_version\\":\\"1.2\\",\\"resource_changes\\":[{\\"address\\":\\"terraform_data.$root\\",\\"mode\\":\\"managed\\",\\"type\\":\\"terraform_data\\",\\"name\\":\\"$root\\",\\"change\\":{\\"actions\\":[\\"create\\"],\\"before\\":null,\\"after\\":{}}}]}" ;;
|
|
33
|
+
output) echo '{"id":{"value":"x"}}' ;;
|
|
34
|
+
esac
|
|
35
|
+
exit 0
|
|
36
|
+
`;
|
|
37
|
+
|
|
38
|
+
function project(): { dir: string; log: string } {
|
|
39
|
+
const dir = realpathSync(mkdtempSync(join(tmpdir(), "chant-tf-hook-")));
|
|
40
|
+
const bin = join(dir, "bin");
|
|
41
|
+
mkdirSync(bin);
|
|
42
|
+
writeFileSync(join(bin, "terraform"), FAKE);
|
|
43
|
+
chmodSync(join(bin, "terraform"), 0o755);
|
|
44
|
+
const log = join(dir, "fake.log");
|
|
45
|
+
writeFileSync(log, "");
|
|
46
|
+
vi.stubEnv("PATH", `${bin}:${process.env.PATH ?? ""}`);
|
|
47
|
+
vi.stubEnv("FAKE_LOG", log);
|
|
48
|
+
vi.stubEnv("TF_PLUGIN_CACHE_DIR", "");
|
|
49
|
+
for (const r of ["a", "b", "up"]) {
|
|
50
|
+
mkdirSync(join(dir, "roots", r), { recursive: true });
|
|
51
|
+
writeFileSync(join(dir, "roots", r, "main.tf"), "");
|
|
52
|
+
}
|
|
53
|
+
writeFileSync(join(dir, "chant.config.json"), JSON.stringify({ lexicons: ["terraform"], terraform: { binary: "terraform" } }));
|
|
54
|
+
return { dir, log };
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
const SPEC = {
|
|
58
|
+
name: "tf-apply",
|
|
59
|
+
op: "tf-apply",
|
|
60
|
+
gateName: "wave-{k}",
|
|
61
|
+
parallelism: 2,
|
|
62
|
+
step: { kind: "terraform-apply", root: "{target}", dir: "{target}", lockTimeout: false },
|
|
63
|
+
waves: [
|
|
64
|
+
{
|
|
65
|
+
name: "wave-1",
|
|
66
|
+
gate: "never",
|
|
67
|
+
runs: [
|
|
68
|
+
{ target: "roots/a" },
|
|
69
|
+
// b reads an output of `up`, which the step inits and reads through the same hook.
|
|
70
|
+
{ target: "roots/b", input: { reads: [{ var: "id", root: "up", dir: "roots/up", output: ["id"] }] } },
|
|
71
|
+
],
|
|
72
|
+
},
|
|
73
|
+
],
|
|
74
|
+
} as unknown as OpWavesSpec;
|
|
75
|
+
|
|
76
|
+
describe("terraform-apply's hook around each engine command (chant#4865)", () => {
|
|
77
|
+
test("the hook sees each command of a root's plan and apply, times it, and its variable reaches the command", async () => {
|
|
78
|
+
const p = project();
|
|
79
|
+
const seen: Array<{ command: TerraformCommand; result: TerraformCommandResult }> = [];
|
|
80
|
+
const cap = terraformApplyCapabilityWith({
|
|
81
|
+
onCommand: (command) => ({
|
|
82
|
+
env: { FAKE_TAG: `${command.root}:${command.name}` },
|
|
83
|
+
after: (result) => {
|
|
84
|
+
seen.push({ command, result });
|
|
85
|
+
},
|
|
86
|
+
}),
|
|
87
|
+
});
|
|
88
|
+
const result = await runOpWave({
|
|
89
|
+
spec: SPEC,
|
|
90
|
+
specFile: "waves.json",
|
|
91
|
+
wave: 1,
|
|
92
|
+
cwd: p.dir,
|
|
93
|
+
show: () => ({ sha: "c".repeat(40), text: JSON.stringify(SPEC) }),
|
|
94
|
+
gates: memoryGateLedgerPort(),
|
|
95
|
+
capabilities: new CapabilityRegistry().register(cap as unknown as Capability<never, unknown>),
|
|
96
|
+
log: () => {},
|
|
97
|
+
});
|
|
98
|
+
expect(result.exitCode).toBe(0);
|
|
99
|
+
expect(result.applied).toEqual(["roots/a", "roots/b"]);
|
|
100
|
+
|
|
101
|
+
const of = (root: string, upstream?: string) => seen.filter((s) => s.command.root === root && s.command.upstream === upstream);
|
|
102
|
+
const names = (root: string, method: string, upstream?: string) => of(root, upstream).filter((s) => s.command.method === method).map((s) => s.command.name);
|
|
103
|
+
expect(names("roots/a", "plan")).toEqual(["init", "plan", "show"]);
|
|
104
|
+
expect(names("roots/a", "run")).toEqual(["apply", "output"]);
|
|
105
|
+
expect(names("roots/b", "plan")).toEqual(["init", "plan", "show"]);
|
|
106
|
+
expect(names("roots/b", "plan", "up")).toEqual(["init", "output"]);
|
|
107
|
+
|
|
108
|
+
const plan = of("roots/a").find((s) => s.command.name === "plan")!;
|
|
109
|
+
expect(plan.command.binary).toBe("terraform");
|
|
110
|
+
expect(plan.command.cwd).toBe(join(p.dir, "roots", "a"));
|
|
111
|
+
expect(plan.command.args[0]).toBe("plan");
|
|
112
|
+
expect(plan.command.args).toContain("-detailed-exitcode");
|
|
113
|
+
expect(plan.command.env.TF_IN_AUTOMATION).toBe("1");
|
|
114
|
+
expect(plan.result.code).toBe(2);
|
|
115
|
+
expect(plan.result.seconds).toBeGreaterThanOrEqual(0.04);
|
|
116
|
+
const apply = of("roots/a").find((s) => s.command.name === "apply")!;
|
|
117
|
+
expect(apply.result.code).toBe(0);
|
|
118
|
+
expect(apply.result.seconds).toBeGreaterThanOrEqual(0.04);
|
|
119
|
+
expect(of("roots/b").find((s) => s.command.name === "plan")!.command.args).toContain("-var=id=x");
|
|
120
|
+
expect(of("roots/a").find((s) => s.command.name === "show")!.result.stdout).toContain("terraform_data.a");
|
|
121
|
+
|
|
122
|
+
// Every command the fake ran carried the variable the hook added for it.
|
|
123
|
+
const lines = readFileSync(p.log, "utf-8").split("\n").filter(Boolean);
|
|
124
|
+
expect(lines.length).toBe(seen.length);
|
|
125
|
+
for (const line of lines) {
|
|
126
|
+
const [cmd, , tag] = line.split(" ");
|
|
127
|
+
expect(tag).not.toBe("none");
|
|
128
|
+
expect(tag!.endsWith(`:${cmd}`)).toBe(true);
|
|
129
|
+
}
|
|
130
|
+
expect(lines).toContain("plan a roots/a:plan");
|
|
131
|
+
expect(lines).toContain("apply b roots/b:apply");
|
|
132
|
+
expect(lines).toContain("output up roots/b:output");
|
|
133
|
+
});
|
|
134
|
+
|
|
135
|
+
test("a failed command reaches the hook's after with its exit code before the step fails", async () => {
|
|
136
|
+
const p = project();
|
|
137
|
+
writeFileSync(join(p.dir, "bin", "terraform"), `#!/bin/sh\necho "$1 \${PWD##*/} \${FAKE_TAG:-none}" >> "$FAKE_LOG"\necho broken >&2\nexit 1\n`);
|
|
138
|
+
const results: Array<[string, TerraformCommandResult]> = [];
|
|
139
|
+
const cap = terraformApplyCapabilityWith({ onCommand: (c) => ({ after: (r) => void results.push([c.name, r]) }) });
|
|
140
|
+
const spec = { ...SPEC, waves: [{ name: "wave-1", gate: "never", runs: [{ target: "roots/a" }] }] } as unknown as OpWavesSpec;
|
|
141
|
+
await expect(
|
|
142
|
+
runOpWave({
|
|
143
|
+
spec,
|
|
144
|
+
specFile: "waves.json",
|
|
145
|
+
wave: 1,
|
|
146
|
+
cwd: p.dir,
|
|
147
|
+
show: () => ({ sha: "d".repeat(40), text: JSON.stringify(spec) }),
|
|
148
|
+
gates: memoryGateLedgerPort(),
|
|
149
|
+
capabilities: new CapabilityRegistry().register(cap as unknown as Capability<never, unknown>),
|
|
150
|
+
log: () => {},
|
|
151
|
+
}),
|
|
152
|
+
).rejects.toThrow("terraform init -input=false");
|
|
153
|
+
expect(results.map(([n]) => n)).toEqual(["init"]);
|
|
154
|
+
expect(results[0]![1].code).toBe(1);
|
|
155
|
+
expect(results[0]![1].stderr).toContain("broken");
|
|
156
|
+
});
|
|
157
|
+
});
|
package/src/compose.ts
CHANGED
|
@@ -463,6 +463,7 @@ export function compositionFor(input: CompositionInput): Composition {
|
|
|
463
463
|
...(rc.config!.backendConfig ? { backendConfig: rc.config!.backendConfig } : {}),
|
|
464
464
|
...(rc.config!.backendConfigFiles ? { backendConfigFiles: rc.config!.backendConfigFiles } : {}),
|
|
465
465
|
}),
|
|
466
|
+
input.env,
|
|
466
467
|
);
|
|
467
468
|
if (refusal) return ["backend", `\`${m}\`: ${refusal.reason}`];
|
|
468
469
|
return undefined;
|
|
@@ -0,0 +1,186 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* #4806: migrations into and between choudoufu estates, planned by the
|
|
3
|
+
* lexicon and run by chant core's gated migration. choudoufu is faked over
|
|
4
|
+
* an in-memory set of live resources, each carrying its estate marker:
|
|
5
|
+
* `plan` of a root destroys each resource its estate holds that its code
|
|
6
|
+
* (`resources.txt`) does not name and creates each one it names that no
|
|
7
|
+
* resource of its estate is; `live-mv` retags; `live-import` ratifies and
|
|
8
|
+
* stamps the resources of a state file. No binary, cloud or git is reached.
|
|
9
|
+
*/
|
|
10
|
+
import { mkdirSync, mkdtempSync, writeFileSync, readFileSync } from "node:fs";
|
|
11
|
+
import { tmpdir } from "node:os";
|
|
12
|
+
import { join } from "node:path";
|
|
13
|
+
import { describe, expect, test } from "vitest";
|
|
14
|
+
import { fakeLedger } from "@intentius/chant/__fixtures__/gated-ledger";
|
|
15
|
+
import { runStateMigration } from "@intentius/chant/state-migration";
|
|
16
|
+
import { parseRatification, planEstateMigration, PROGRESS_FLAG, stampFailures } from "./estate-migration";
|
|
17
|
+
import type { BinaryExec, StateFile } from "./state/exec";
|
|
18
|
+
import { MigrationError } from "./state-migration";
|
|
19
|
+
|
|
20
|
+
/** Live resources by address: the estate whose marker each carries, and its live id. */
|
|
21
|
+
function fakeChoudoufu(opts: { progress?: boolean } = {}) {
|
|
22
|
+
const live = new Map<string, { estate: string | undefined; id: string }>();
|
|
23
|
+
const calls: string[] = [];
|
|
24
|
+
const plans = new Map<string, string[]>();
|
|
25
|
+
const estateOf = (dir: string) => readFileSync(join(dir, "estate.txt"), "utf-8").trim();
|
|
26
|
+
const codeOf = (dir: string) => readFileSync(join(dir, "resources.txt"), "utf-8").split("\n").filter(Boolean);
|
|
27
|
+
const exec: BinaryExec = async (_bin, args, dir, _env, io) => {
|
|
28
|
+
const name = dir.split("/").at(-1)!;
|
|
29
|
+
calls.push(`${name}: ${args.join(" ")}`);
|
|
30
|
+
const estate = estateOf(dir);
|
|
31
|
+
if (args[0] === "init") return { code: 0, stdout: "", out: "" };
|
|
32
|
+
if (args[0] === "live-mv" && args[1] === "-help") return { code: 0, stdout: "", out: opts.progress ? ` ${PROGRESS_FLAG} one line per resource\n` : "" };
|
|
33
|
+
if (args[0] === "live-import" && args[1] === "-help") return { code: 0, stdout: "", out: "" };
|
|
34
|
+
if (args[0] === "plan") {
|
|
35
|
+
const code = new Set(codeOf(dir));
|
|
36
|
+
const mine = [...live].filter(([, v]) => v.estate === estate).map(([a]) => a);
|
|
37
|
+
const changes = [...mine.filter((a) => !code.has(a)).map((a) => `${a}=delete`), ...[...code].filter((a) => live.get(a)?.estate !== estate).map((a) => `${a}=create`)];
|
|
38
|
+
plans.set(args.find((a) => a.startsWith("-out="))!.slice(5), changes);
|
|
39
|
+
return { code: 0, stdout: "", out: changes.length ? `Plan: ${changes.length} to change.` : "No changes." };
|
|
40
|
+
}
|
|
41
|
+
if (args[0] === "show") {
|
|
42
|
+
const changes = plans.get(args.at(-1)!) ?? [];
|
|
43
|
+
return { code: 0, stdout: JSON.stringify({ resource_changes: changes.map((c) => ({ address: c.split("=")[0], change: { actions: [c.split("=")[1]] } })) }), out: "" };
|
|
44
|
+
}
|
|
45
|
+
if (args[0] === "live-mv") {
|
|
46
|
+
const from = args.find((a) => a.startsWith("-from-estate="))!.slice(13);
|
|
47
|
+
const address = args.at(-1)!;
|
|
48
|
+
const r = live.get(address);
|
|
49
|
+
if (!r || r.estate !== from) return { code: 0, stdout: JSON.stringify({ refusal: { summary: "not found", detail: `${address} is not in estate ${from}` } }), out: "" };
|
|
50
|
+
const dry = args.includes("-dry-run");
|
|
51
|
+
if (!dry) {
|
|
52
|
+
r.estate = estate;
|
|
53
|
+
if (args.includes(PROGRESS_FLAG)) io?.stderr?.(JSON.stringify({ address, outcome: "moved" }));
|
|
54
|
+
}
|
|
55
|
+
return { code: 0, stdout: JSON.stringify({ resource: { live_id: r.id }, dry_run: dry, written: !dry }), out: "" };
|
|
56
|
+
}
|
|
57
|
+
if (args[0] === "live-import") {
|
|
58
|
+
const st = JSON.parse(readFileSync(args.find((a) => a.startsWith("-state="))!.slice(7), "utf-8")) as StateFile;
|
|
59
|
+
const rows = st.resources.map((r) => ({ address: `${r.type}.${r.name}`, live: live.get(`${r.type}.${r.name}`) }));
|
|
60
|
+
const verified = rows.filter((r) => r.live);
|
|
61
|
+
const missing = rows.filter((r) => !r.live);
|
|
62
|
+
if (args.includes("-approve")) {
|
|
63
|
+
for (const r of verified) r.live!.estate = estate;
|
|
64
|
+
return { code: 0, stdout: `Stamped ${verified.length}: 0 failed, ${missing.length} skipped.\n`, out: "" };
|
|
65
|
+
}
|
|
66
|
+
const out = [
|
|
67
|
+
`VERIFIED (${verified.length}) - eligible for stamping`,
|
|
68
|
+
...verified.map((r) => ` ${r.address} ${r.address.split(".")[0]} live id: ${r.live!.id}`),
|
|
69
|
+
"",
|
|
70
|
+
...(missing.length ? [`MISSING (${missing.length}) - not found`, ...missing.map((r) => ` ${r.address} ${r.address.split(".")[0]} live id: -`)] : []),
|
|
71
|
+
].join("\n");
|
|
72
|
+
return { code: 0, stdout: out, out };
|
|
73
|
+
}
|
|
74
|
+
return { code: 1, stdout: "", out: `unexpected ${args.join(" ")}` };
|
|
75
|
+
};
|
|
76
|
+
return { exec, calls, live };
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
function root(repo: string, name: string, estate: string, code: string[]) {
|
|
80
|
+
const dir = join(repo, name);
|
|
81
|
+
mkdirSync(dir, { recursive: true });
|
|
82
|
+
writeFileSync(join(dir, "estate.txt"), estate);
|
|
83
|
+
writeFileSync(join(dir, "resources.txt"), code.join("\n"));
|
|
84
|
+
return { root: name, dir, binary: "/usr/local/bin/choudoufu", estate, env: {} };
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
const gated = (ledger: ReturnType<typeof fakeLedger>, planned: Awaited<ReturnType<typeof planEstateMigration>>, name = "retag-queue") => ({ name, by: "ana", roots: planned.roots, engine: planned.engine, words: { op: "tf-migrate" }, rules: { mode: "ledger" as const }, ledger });
|
|
88
|
+
|
|
89
|
+
describe("planEstateMigration: a retag", () => {
|
|
90
|
+
function setup(progress = false) {
|
|
91
|
+
const repo = mkdtempSync(join(tmpdir(), "chant-estate-"));
|
|
92
|
+
const bin = fakeChoudoufu({ progress });
|
|
93
|
+
bin.live.set("aws_sqs_queue.jobs", { estate: "platform", id: "q-123" });
|
|
94
|
+
bin.live.set("aws_s3_bucket.logs", { estate: "platform", id: "b-1" });
|
|
95
|
+
const roots = [root(repo, "platform", "platform", ["aws_s3_bucket.logs"]), root(repo, "queues", "queues", ["aws_sqs_queue.jobs"])];
|
|
96
|
+
return { repo, bin, roots, work: mkdtempSync(join(tmpdir(), "chant-estate-work-")) };
|
|
97
|
+
}
|
|
98
|
+
const moves = [{ from: "platform", to: "queues", addresses: ["aws_sqs_queue.jobs"] }];
|
|
99
|
+
|
|
100
|
+
test("proves the retag with a dry run, waits for an approval, then retags and plans both roots clean", async () => {
|
|
101
|
+
const { bin, roots, work } = setup(true);
|
|
102
|
+
const lines: string[] = [];
|
|
103
|
+
const verified: string[] = [];
|
|
104
|
+
const planned = await planEstateMigration({ name: "retag-queue", moves, roots, work, exec: bin.exec, progress: (l) => (lines.push(l), true), verified: (r) => verified.push(r) });
|
|
105
|
+
expect(planned.retags).toEqual([{ address: "aws_sqs_queue.jobs", from: "platform", to: "queues", from_estate: "platform", to_estate: "queues", live_id: "q-123", followers: [] }]);
|
|
106
|
+
expect(planned.roots.map((r) => [r.root, r.location, r.proof.changes])).toEqual([["platform", "estate platform", []], ["queues", "estate queues", []]]);
|
|
107
|
+
// A joining root is not planned before the retag.
|
|
108
|
+
expect(bin.calls.filter((c) => c.startsWith("queues: plan"))).toEqual([]);
|
|
109
|
+
const ledger = fakeLedger();
|
|
110
|
+
expect((await runStateMigration(gated(ledger, planned))).status).toBe("waiting");
|
|
111
|
+
expect(bin.live.get("aws_sqs_queue.jobs")!.estate).toBe("platform");
|
|
112
|
+
ledger.approve("bo");
|
|
113
|
+
const r = await runStateMigration(gated(ledger, planned));
|
|
114
|
+
expect(r).toMatchObject({ status: "applied", exit: 0 });
|
|
115
|
+
expect(bin.live.get("aws_sqs_queue.jobs")!.estate).toBe("queues");
|
|
116
|
+
expect(lines).toEqual([JSON.stringify({ address: "aws_sqs_queue.jobs", outcome: "moved" })]);
|
|
117
|
+
expect(verified).toEqual(["platform", "queues"]);
|
|
118
|
+
});
|
|
119
|
+
|
|
120
|
+
test("a resource the leaving root's plan does not destroy is refused at plan", async () => {
|
|
121
|
+
const { bin, roots, work } = setup();
|
|
122
|
+
await expect(planEstateMigration({ name: "m", moves: [{ from: "platform", to: "queues", addresses: ["aws_s3_bucket.logs"] }], roots, work, exec: bin.exec })).rejects.toThrow(/does not destroy it/);
|
|
123
|
+
});
|
|
124
|
+
|
|
125
|
+
test("a binary that is not choudoufu is refused", async () => {
|
|
126
|
+
const { bin, roots, work } = setup();
|
|
127
|
+
await expect(planEstateMigration({ name: "m", moves, roots: roots.map((r) => ({ ...r, binary: "tofu" })), work, exec: bin.exec })).rejects.toBeInstanceOf(MigrationError);
|
|
128
|
+
});
|
|
129
|
+
});
|
|
130
|
+
|
|
131
|
+
describe("planEstateMigration: an adoption", () => {
|
|
132
|
+
function setup() {
|
|
133
|
+
const repo = mkdtempSync(join(tmpdir(), "chant-adopt-"));
|
|
134
|
+
const bin = fakeChoudoufu();
|
|
135
|
+
bin.live.set("aws_sqs_queue.jobs", { estate: undefined, id: "q-9" });
|
|
136
|
+
const r = root(repo, "app", "app", ["aws_sqs_queue.jobs"]);
|
|
137
|
+
writeFileSync(join(r.dir, "old.tfstate"), JSON.stringify({ version: 4, serial: 3, lineage: "l", resources: [{ mode: "managed", type: "aws_sqs_queue", name: "jobs", instances: [] }] }));
|
|
138
|
+
return { bin, roots: [r], dir: r.dir, work: mkdtempSync(join(tmpdir(), "chant-adopt-work-")) };
|
|
139
|
+
}
|
|
140
|
+
const backends = [{ root: "app", from: { backend: "local", config: { path: "old.tfstate" } } }];
|
|
141
|
+
|
|
142
|
+
test("ratifies the old state, binds it as the source, stamps under approval and plans clean", async () => {
|
|
143
|
+
const { bin, roots, work } = setup();
|
|
144
|
+
const planned = await planEstateMigration({ name: "adopt-app", backends, roots, work, exec: bin.exec });
|
|
145
|
+
expect(planned.change).toBe("adopt");
|
|
146
|
+
expect(planned.stamps).toEqual([{ root: "app", estate: "app", address: "aws_sqs_queue.jobs", type: "aws_sqs_queue", status: "VERIFIED", live_id: "q-9" }]);
|
|
147
|
+
expect(planned.roots[0]).toMatchObject({ location: "estate app", source: { location: "old.tfstate" }, proof: { changes: [], summary: "live-import: 1 of 1 resource instance verified" } });
|
|
148
|
+
const ledger = fakeLedger();
|
|
149
|
+
ledger.approve("bo");
|
|
150
|
+
const r = await runStateMigration(gated(ledger, planned, "adopt-app"));
|
|
151
|
+
expect(r.status).toBe("applied");
|
|
152
|
+
expect(bin.live.get("aws_sqs_queue.jobs")!.estate).toBe("app");
|
|
153
|
+
expect(ledger.details[0]).toMatchObject({ "app.source": "old.tfstate" });
|
|
154
|
+
});
|
|
155
|
+
|
|
156
|
+
test("an old state written after the plan refuses the stamp", async () => {
|
|
157
|
+
const { bin, roots, work, dir } = setup();
|
|
158
|
+
const planned = await planEstateMigration({ name: "adopt-app", backends, roots, work, exec: bin.exec });
|
|
159
|
+
writeFileSync(join(dir, "old.tfstate"), JSON.stringify({ version: 4, serial: 4, lineage: "l", resources: [] }));
|
|
160
|
+
const ledger = fakeLedger();
|
|
161
|
+
ledger.approve("bo");
|
|
162
|
+
const r = await runStateMigration(gated(ledger, planned, "adopt-app"));
|
|
163
|
+
expect(r).toMatchObject({ status: "moved", exit: 4 });
|
|
164
|
+
expect(bin.live.get("aws_sqs_queue.jobs")!.estate).toBeUndefined();
|
|
165
|
+
});
|
|
166
|
+
|
|
167
|
+
test("a resource that does not verify fails the proof, and nothing is asked", async () => {
|
|
168
|
+
const { bin, roots, work } = setup();
|
|
169
|
+
bin.live.delete("aws_sqs_queue.jobs");
|
|
170
|
+
const planned = await planEstateMigration({ name: "adopt-app", backends, roots, work, exec: bin.exec });
|
|
171
|
+
const ledger = fakeLedger();
|
|
172
|
+
expect(await runStateMigration(gated(ledger, planned, "adopt-app"))).toMatchObject({ status: "proof-failed", failing: ["app"] });
|
|
173
|
+
expect(ledger.writes).toEqual([]);
|
|
174
|
+
});
|
|
175
|
+
});
|
|
176
|
+
|
|
177
|
+
describe("live-import's reports", () => {
|
|
178
|
+
test("parseRatification and stampFailures read the report and the count line", () => {
|
|
179
|
+
expect(parseRatification("VERIFIED (1) - eligible for stamping\n a.b a live id: x\n\nMISSING (1) - gone\n c.d c live id: -\n")).toEqual([
|
|
180
|
+
{ address: "a.b", type: "a", status: "VERIFIED", live_id: "x" },
|
|
181
|
+
{ address: "c.d", type: "c", status: "MISSING" },
|
|
182
|
+
]);
|
|
183
|
+
expect(stampFailures("Stamped 3: 1 failed, 0 skipped.")).toBe(1);
|
|
184
|
+
expect(stampFailures("nothing")).toBeUndefined();
|
|
185
|
+
});
|
|
186
|
+
});
|