@devries-cameron20 / t3code Mirror · read-only
Fork 0

The best way to control Claude Code, Codex and more from any computer or phone

Mirrored from https://github.com/Camo67/t3code.git

Synced Nov 21, 06:00 UTC

main
ts 646 lines 27 KB
Raw
1 import * as AWS from "@alchemy.run/aws";
2 import { amazonLinux2023, Instance, Subnet, Vpc, type InstanceProps } from "@alchemy.run/aws/EC2";
3 import { Queue } from "@alchemy.run/aws/SQS";
4 import { describe, expect } from "@alchemy.run/test";
5 import * as ec2 from "@distilled.cloud/aws/ec2";
6 import * as Drift from "alchemy/Drift";
7 import * as Output from "alchemy/Output";
8 import * as Provider from "alchemy/Provider";
9 import { State } from "alchemy/State/State";
10 import * as Effect from "effect/Effect";
11 import * as Redacted from "effect/Redacted";
12 import { MinimumLogLevel } from "effect/References";
13 import * as Schedule from "effect/Schedule";
14 import { assertInstanceTerminated, assertVpcGone } from "./Gone.ts";
15 import * as Test from "./VpcTest.ts";
16
17 // The fixed-IP fixture keeps two VPCs to verify allocation scope. Tests run
18 // sequentially so the file holds at most these two VPCs.
19 const { test } = Test.make({ providers: AWS.providers() }, 2);
20
21 const logLevel = Effect.provideService(MinimumLogLevel, process.env.DEBUG ? "Debug" : "Info");
22
23 // Terminate an instance outside the stack and wait until EC2 reports it
24 // terminated; shutting-down -> terminated can take a minute or two.
25 const terminateOutOfBand = Effect.fn(function* (instanceId: string) {
26 yield* ec2.terminateInstances({ InstanceIds: [instanceId] });
27 yield* ec2.describeInstances({ InstanceIds: [instanceId] }).pipe(
28 Effect.map((r) => r.Reservations?.[0]?.Instances?.[0]?.State?.Name),
29 Effect.repeat({
30 schedule: Schedule.spaced("5 seconds"),
31 until: (state) => state === "terminated",
32 times: 36,
33 }),
34 );
35 });
36
37 describe.sequential("Instance", { tags: ["provider:aws", "provider:aws:ec2", "live"] }, () => {
38 // `list()` enumerates every non-terminated instance in the account/region via
39 // the paginated `ec2.describeInstances` op (items nested under
40 // Reservations[].Instances[]). Deploy a real instance, resolve the provider
41 // from context with the typed `findProvider`, call `list()`, and assert the
42 // deployed instance appears in the exhaustively paginated result.
43 test.provider(
44 "list enumerates the deployed instance",
45 (stack) =>
46 Effect.gen(function* () {
47 yield* stack.destroy();
48
49 const imageId = amazonLinux2023();
50
51 // The testing account has no default VPC, so provision a VPC + subnet to
52 // launch the instance into.
53 const deployed = yield* stack.deploy(
54 Effect.gen(function* () {
55 const vpc = yield* Vpc("ListInstanceVpc", { cidrBlock: "10.0.0.0/16" });
56 const subnet = yield* Subnet("ListInstanceSubnet", {
57 vpcId: vpc.vpcId,
58 cidrBlock: "10.0.1.0/24",
59 });
60 const instance = yield* Instance("ListInstance", {
61 imageId,
62 instanceType: "t3.micro",
63 subnetId: subnet.subnetId,
64 });
65 return { vpc, instance };
66 }),
67 );
68
69 const provider = yield* Provider.findProvider(Instance);
70 const all = yield* provider.list();
71
72 expect(all.some((x) => x.instanceId === deployed.instance.instanceId)).toBe(true);
73
74 yield* stack.destroy();
75
76 // Zero-orphan proof: the instance reached a terminal state and the VPC
77 // (which cannot delete while any ENI lingers) is gone.
78 yield* assertInstanceTerminated(deployed.instance.instanceId);
79 yield* assertVpcGone(deployed.vpc.vpcId);
80 }).pipe(logLevel),
81 { timeout: 240_000 },
82 );
83
84 // Metadata options are applied by RunInstances at launch and updated in
85 // place via ModifyInstanceMetadataOptions — the instance id must not change.
86 test.provider(
87 "metadataOptions apply at launch and update in place",
88 (stack) =>
89 Effect.gen(function* () {
90 yield* stack.destroy();
91
92 const imageId = amazonLinux2023();
93
94 const program = (metadataOptions: InstanceProps["metadataOptions"]) =>
95 Effect.gen(function* () {
96 const vpc = yield* Vpc("MetadataInstanceVpc", { cidrBlock: "10.0.0.0/16" });
97 const subnet = yield* Subnet("MetadataInstanceSubnet", {
98 vpcId: vpc.vpcId,
99 cidrBlock: "10.0.1.0/24",
100 });
101 const instance = yield* Instance("MetadataInstance", {
102 imageId,
103 instanceType: "t3.micro",
104 subnetId: subnet.subnetId,
105 metadataOptions,
106 });
107 return { vpc, instance };
108 });
109
110 const observeMetadata = (instanceId: string) =>
111 ec2
112 .describeInstances({ InstanceIds: [instanceId] })
113 .pipe(Effect.map((r) => r.Reservations?.[0]?.Instances?.[0]?.MetadataOptions));
114
115 const first = yield* stack.deploy(
116 program({ httpTokens: "required", httpPutResponseHopLimit: 1 }),
117 );
118 expect(yield* observeMetadata(first.instance.instanceId)).toMatchObject({
119 HttpTokens: "required",
120 HttpPutResponseHopLimit: 1,
121 });
122
123 const next = program({
124 httpTokens: "required",
125 httpPutResponseHopLimit: 2,
126 instanceMetadataTags: "enabled",
127 });
128 const plan = yield* stack.plan(next);
129 expect(plan.resources.MetadataInstance).toMatchObject({ action: "update" });
130
131 const second = yield* stack.deploy(next);
132 expect(second.instance.instanceId).toBe(first.instance.instanceId);
133 expect(yield* observeMetadata(second.instance.instanceId)).toMatchObject({
134 HttpTokens: "required",
135 HttpPutResponseHopLimit: 2,
136 InstanceMetadataTags: "enabled",
137 });
138 expect(second.instance.metadataOptions).toMatchObject({
139 httpTokens: "required",
140 httpPutResponseHopLimit: 2,
141 instanceMetadataTags: "enabled",
142 });
143
144 yield* stack.destroy();
145
146 yield* assertInstanceTerminated(second.instance.instanceId);
147 yield* assertVpcGone(second.vpc.vpcId);
148 }).pipe(logLevel),
149 { timeout: 300_000 },
150 );
151
152 // Replacement must launch a distinct physical instance. The create phase of a
153 // replacement runs under a freshly minted generation id, so the tag-based
154 // recovery lookup (branded with `alchemy::instance`) can never re-adopt the
155 // old generation's live instance — which the cleanup phase then terminates,
156 // leaving state pointing at a terminated instance (#1026).
157 test.provider.skipIf(!!process.env.FAST)(
158 "replace launches a distinct instance and terminates only the old one",
159 (stack) =>
160 Effect.gen(function* () {
161 yield* stack.destroy();
162
163 const imageId = amazonLinux2023();
164
165 const program = (userData: string, privateIpAddress?: string) =>
166 Effect.gen(function* () {
167 const vpc = yield* Vpc("ReplaceInstanceVpc", { cidrBlock: "10.0.0.0/16" });
168 const subnet = yield* Subnet("ReplaceInstanceSubnet", {
169 vpcId: vpc.vpcId,
170 cidrBlock: "10.0.1.0/24",
171 });
172 const instance = yield* Instance("ReplaceInstance", {
173 imageId,
174 instanceType: "t3.micro",
175 subnetId: subnet.subnetId,
176 userData,
177 privateIpAddress,
178 tags: { Name: "alchemy-replace-instance-test" },
179 });
180 return { vpc, instance };
181 });
182
183 const first = yield* stack.deploy(program("#!/bin/bash\necho generation-one\n"));
184 const next = program("#!/bin/bash\necho generation-two\n");
185 const automatic = yield* stack.plan(next);
186 expect(automatic.resources.ReplaceInstance).toMatchObject({
187 action: "replace",
188 deleteFirst: false,
189 });
190 const pinned = yield* stack.plan(
191 program("#!/bin/bash\necho generation-two\n", first.instance.privateIpAddress),
192 );
193 expect(pinned.resources.ReplaceInstance).toMatchObject({
194 action: "replace",
195 deleteFirst: true,
196 });
197
198 // userData is a force-new prop, so this deploy plans a replacement.
199 const second = yield* stack.deploy(next);
200
201 // The replacement created a distinct physical instance...
202 expect(second.instance.instanceId).not.toBe(first.instance.instanceId);
203
204 // ...that is alive after cleanup (out-of-band via distilled)...
205 const live = yield* ec2.describeInstances({ InstanceIds: [second.instance.instanceId] });
206 const liveState = live.Reservations?.[0]?.Instances?.[0]?.State?.Name ?? "unknown";
207 expect(["pending", "running"]).toContain(liveState);
208
209 // ...while the old generation was the one terminated.
210 yield* assertInstanceTerminated(first.instance.instanceId);
211
212 yield* stack.destroy();
213
214 // Zero-orphan proof for the replacement generation and the VPC.
215 yield* assertInstanceTerminated(second.instance.instanceId);
216 yield* assertVpcGone(second.vpc.vpcId);
217 }).pipe(logLevel),
218 { timeout: 600_000 },
219 );
220
221 // EC2 runs userData only at launch. When it interpolates an upstream that
222 // is being replaced, its new value is unknown at plan time; the instance
223 // must still be replaced, or it keeps running the old userData.
224 test.provider.skipIf(!!process.env.FAST)(
225 "replaces the instance when userData depends on a replaced upstream",
226 (stack) =>
227 Effect.gen(function* () {
228 yield* stack.destroy();
229
230 const program = (fifo: boolean) =>
231 Effect.gen(function* () {
232 const vpc = yield* Vpc("UpstreamUserDataVpc", { cidrBlock: "10.0.0.0/16" });
233 const subnet = yield* Subnet("UpstreamUserDataSubnet", {
234 vpcId: vpc.vpcId,
235 cidrBlock: "10.0.1.0/24",
236 });
237 // Toggling `fifo` replaces the queue, so its URL is unknown at plan time.
238 const queue = yield* Queue(
239 "UpstreamUserDataQueue",
240 fifo ? { fifo: true } : { fifo: false },
241 );
242 const instance = yield* Instance("UpstreamUserDataInstance", {
243 imageId: amazonLinux2023(),
244 instanceType: "t3.micro",
245 subnetId: subnet.subnetId,
246 userData: Output.interpolate`#!/bin/bash
247 echo ${queue.queueUrl}
248 `,
249 });
250 return { vpc, queue, instance };
251 });
252
253 const first = yield* stack.deploy(program(false));
254 const plan = yield* stack.plan(program(true));
255 expect(plan.resources.UpstreamUserDataInstance?.action).toBe("replace");
256
257 const second = yield* stack.deploy(program(true));
258 expect(second.instance.instanceId).not.toBe(first.instance.instanceId);
259
260 // The new instance launched with the replaced queue's URL.
261 const userData = yield* ec2.describeInstanceAttribute({
262 InstanceId: second.instance.instanceId,
263 Attribute: "userData",
264 });
265 const script = yield* Effect.sync(() =>
266 Buffer.from(userData.UserData?.Value ?? "", "base64").toString("utf8"),
267 );
268 expect(script).toContain(second.queue.queueUrl);
269 yield* assertInstanceTerminated(first.instance.instanceId);
270
271 yield* stack.destroy();
272 yield* assertInstanceTerminated(second.instance.instanceId);
273 yield* assertVpcGone(second.vpc.vpcId);
274 }).pipe(logLevel),
275 { timeout: 600_000 },
276 );
277
278 // A launch whose reply was lost leaves no saved output, and the tag lookup
279 // can miss an instance EC2 has not indexed yet. The ClientToken alchemy
280 // derives from the resource's generation makes the retried RunInstances
281 // return the first instance instead of a second one.
282 test.provider(
283 "a launch retried after a lost reply returns the same instance",
284 (stack) =>
285 Effect.gen(function* () {
286 yield* stack.destroy();
287
288 const deployWith = (tags: Record<string, string>) =>
289 stack.deploy(
290 Effect.gen(function* () {
291 const vpc = yield* Vpc("ClientTokenVpc", { cidrBlock: "10.0.0.0/16" });
292 const subnet = yield* Subnet("ClientTokenSubnet", {
293 vpcId: vpc.vpcId,
294 cidrBlock: "10.0.1.0/24",
295 });
296 const instance = yield* Instance("ClientTokenInstance", {
297 imageId: amazonLinux2023(),
298 instanceType: "t3.micro",
299 subnetId: subnet.subnetId,
300 tags,
301 });
302 return { vpc, instance };
303 }),
304 );
305 const key = { stack: stack.name, stage: stack.stage, fqn: "ClientTokenInstance" };
306
307 // Simulate a lost RunInstances reply: drop the saved output and the
308 // generation tag the recovery lookup finds the instance by.
309 const loseReply = Effect.fn(function* (instanceId: string) {
310 const state = yield* yield* State;
311 const row = yield* state.get(key);
312 if (row?.status !== "created") {
313 return yield* Effect.die(new Error("Expected a created instance row"));
314 }
315 const { attr: _attr, ...rest } = row;
316 yield* state.set({ ...key, value: { ...rest, status: "creating" } });
317 yield* ec2.deleteTags({
318 Resources: [instanceId],
319 Tags: [{ Key: "alchemy::instance" }],
320 });
321 return row.instanceId;
322 });
323
324 const ownedInstances = ec2
325 .describeInstances({
326 Filters: [
327 { Name: "tag:alchemy::stack", Values: [stack.name] },
328 { Name: "tag:alchemy::stage", Values: [stack.stage] },
329 { Name: "tag:alchemy::id", Values: ["ClientTokenInstance"] },
330 { Name: "instance-state-name", Values: ["pending", "running"] },
331 ],
332 })
333 .pipe(
334 Effect.map((owned) => (owned.Reservations ?? []).flatMap((r) => r.Instances ?? [])),
335 );
336
337 const first = yield* deployWith({ v: "1" });
338 const generation = yield* loseReply(first.instance.instanceId);
339
340 const retried = yield* deployWith({ v: "1" });
341 expect(retried.instance.instanceId).toBe(first.instance.instanceId);
342
343 // Only one live instance carries this resource's ownership tags, and
344 // the sync put the generation tag back.
345 const instances = yield* ownedInstances;
346 expect(instances.map((i) => i.InstanceId)).toEqual([first.instance.instanceId]);
347 expect(instances[0]?.Tags?.find((tag) => tag.Key === "alchemy::instance")?.Value).toBe(
348 generation,
349 );
350
351 // Lose the reply again, then terminate the instance outside the
352 // stack. The retried token now returns a terminated instance, so the
353 // launch must start a new one rather than fail or adopt the tombstone.
354 yield* loseReply(first.instance.instanceId);
355 yield* terminateOutOfBand(first.instance.instanceId);
356
357 const relaunched = yield* deployWith({ v: "1" });
358 expect(relaunched.instance.instanceId).not.toBe(first.instance.instanceId);
359 expect((yield* ownedInstances).map((i) => i.InstanceId)).toEqual([
360 relaunched.instance.instanceId,
361 ]);
362
363 // Terminate the instance outside the stack and change a tag. The
364 // update finds the saved instance terminated and relaunches under a
365 // token keyed on it. Then lose that reply: state still holds the
366 // terminated instance, so the retry must send the same token and get
367 // the same new instance.
368 const state = yield* yield* State;
369 const beforeRecreate = yield* state.get(key);
370 yield* terminateOutOfBand(relaunched.instance.instanceId);
371 const recreated = yield* deployWith({ v: "2" });
372 expect(recreated.instance.instanceId).not.toBe(relaunched.instance.instanceId);
373
374 yield* state.set({ ...key, value: beforeRecreate! });
375 yield* ec2.deleteTags({
376 Resources: [recreated.instance.instanceId],
377 Tags: [{ Key: "alchemy::instance" }],
378 });
379 const retriedRecreate = yield* deployWith({ v: "2" });
380 expect(retriedRecreate.instance.instanceId).toBe(recreated.instance.instanceId);
381 expect((yield* ownedInstances).map((i) => i.InstanceId)).toEqual([
382 recreated.instance.instanceId,
383 ]);
384
385 yield* stack.destroy();
386 yield* assertInstanceTerminated(recreated.instance.instanceId);
387 yield* assertVpcGone(first.vpc.vpcId);
388 }).pipe(logLevel),
389 { timeout: 900_000 },
390 );
391
392 // EC2 keeps listing a terminated instance for a while. `read` must report
393 // it as missing, so drift sees the instance gone rather than changed.
394 // Repair relaunches it under a new ClientToken.
395 test.provider(
396 "drift reports an instance terminated outside the stack as missing and repair relaunches it",
397 (stack) =>
398 Effect.gen(function* () {
399 yield* stack.destroy();
400
401 const program = Effect.gen(function* () {
402 const vpc = yield* Vpc("DriftInstanceVpc", { cidrBlock: "10.0.0.0/16" });
403 const subnet = yield* Subnet("DriftInstanceSubnet", {
404 vpcId: vpc.vpcId,
405 cidrBlock: "10.0.1.0/24",
406 });
407 const instance = yield* Instance("DriftInstance", {
408 imageId: amazonLinux2023(),
409 instanceType: "t3.micro",
410 subnetId: subnet.subnetId,
411 });
412 return { vpc, instance };
413 });
414
415 const deployed = yield* stack.deploy(program);
416 const state = yield* yield* State;
417 const key = { stack: stack.name, stage: stack.stage, fqn: "DriftInstance" };
418 const beforeRepair = yield* state.get(key);
419
420 yield* terminateOutOfBand(deployed.instance.instanceId);
421
422 const drift = yield* Drift.detect({ name: stack.name, stage: stack.stage }).pipe(
423 Effect.provide(stack.state),
424 );
425 expect(drift.resources.DriftInstance?.action).toBe("missing");
426
427 const repaired = yield* Drift.repair({ name: stack.name, stage: stack.stage }).pipe(
428 Effect.provide(stack.state),
429 );
430 expect(repaired.resources.DriftInstance?.action).toBe("recreated");
431 const relaunched = repaired.resources.DriftInstance?.attr as typeof deployed.instance;
432 expect(relaunched.instanceId).not.toBe(deployed.instance.instanceId);
433 const live = yield* ec2.describeInstances({ InstanceIds: [relaunched.instanceId] });
434 expect(live.Reservations?.[0]?.Instances?.[0]?.State?.Name).toBe("running");
435
436 // Lose the repair's reply: state still holds the terminated instance
437 // and the recovery lookup misses. Repairing again must return the
438 // same instance rather than launch a second one.
439 yield* state.set({ ...key, value: beforeRepair! });
440 yield* ec2.deleteTags({
441 Resources: [relaunched.instanceId],
442 Tags: [{ Key: "alchemy::instance" }],
443 });
444 const retried = yield* Drift.repair({ name: stack.name, stage: stack.stage }).pipe(
445 Effect.provide(stack.state),
446 );
447 expect(retried.resources.DriftInstance?.action).toBe("recreated");
448 expect((retried.resources.DriftInstance?.attr as typeof deployed.instance).instanceId).toBe(
449 relaunched.instanceId,
450 );
451 const owned = yield* ec2.describeInstances({
452 Filters: [
453 { Name: "tag:alchemy::stack", Values: [stack.name] },
454 { Name: "tag:alchemy::stage", Values: [stack.stage] },
455 { Name: "tag:alchemy::id", Values: ["DriftInstance"] },
456 { Name: "instance-state-name", Values: ["pending", "running"] },
457 ],
458 });
459 expect(
460 (owned.Reservations ?? []).flatMap((r) => r.Instances ?? []).map((i) => i.InstanceId),
461 ).toEqual([relaunched.instanceId]);
462
463 yield* stack.destroy();
464 yield* assertInstanceTerminated(relaunched.instanceId);
465 yield* assertVpcGone(deployed.vpc.vpcId);
466 }).pipe(logLevel),
467 { timeout: 900_000 },
468 );
469
470 // A Redacted userData is revealed only for RunInstances and compared by
471 // value: the copy decoded from state is a new object, which must not
472 // replace the instance on every deploy.
473 test.provider(
474 "accepts Redacted userData and compares it by value",
475 (stack) =>
476 Effect.gen(function* () {
477 yield* stack.destroy();
478
479 const program = (token: string) =>
480 Effect.gen(function* () {
481 const vpc = yield* Vpc("RedactedUserDataVpc", { cidrBlock: "10.0.0.0/16" });
482 const subnet = yield* Subnet("RedactedUserDataSubnet", {
483 vpcId: vpc.vpcId,
484 cidrBlock: "10.0.1.0/24",
485 });
486 const instance = yield* Instance("RedactedUserDataInstance", {
487 imageId: amazonLinux2023(),
488 instanceType: "t3.micro",
489 subnetId: subnet.subnetId,
490 userData: Redacted.make(`#!/bin/bash\necho TOKEN=${token}\n`),
491 });
492 return { vpc, instance };
493 });
494
495 const first = yield* stack.deploy(program("one"));
496 const userData = yield* ec2.describeInstanceAttribute({
497 InstanceId: first.instance.instanceId,
498 Attribute: "userData",
499 });
500 const script = yield* Effect.sync(() =>
501 Buffer.from(userData.UserData?.Value ?? "", "base64").toString("utf8"),
502 );
503 expect(script).toContain("TOKEN=one");
504
505 const unchanged = yield* stack.plan(program("one"));
506 expect(unchanged.resources.RedactedUserDataInstance?.action).toBe("noop");
507 const changed = yield* stack.plan(program("two"));
508 expect(changed.resources.RedactedUserDataInstance?.action).toBe("replace");
509
510 yield* stack.destroy();
511 yield* assertInstanceTerminated(first.instance.instanceId);
512 yield* assertVpcGone(first.vpc.vpcId);
513 }).pipe(logLevel),
514 { timeout: 300_000 },
515 );
516
517 const fixedIpProgram = (props: Partial<InstanceProps> = {}) =>
518 Effect.gen(function* () {
519 const vpc = yield* Vpc("FixedIpVpc", { cidrBlock: "10.0.0.0/16" });
520 const subnet = yield* Subnet("FixedIpSubnet", { vpcId: vpc.vpcId, cidrBlock: "10.0.1.0/24" });
521 const otherVpc = yield* Vpc("OtherFixedIpVpc", { cidrBlock: "10.0.0.0/16" });
522 const otherSubnet = yield* Subnet("OtherFixedIpSubnet", {
523 vpcId: otherVpc.vpcId,
524 cidrBlock: "10.0.1.0/24",
525 });
526 const instance = yield* Instance("FixedIpInstance", {
527 imageId: amazonLinux2023(),
528 instanceType: "t3.micro",
529 subnetId: subnet.subnetId,
530 privateIpAddress: "10.0.1.10",
531 userData: "#!/bin/bash\necho generation-one\n",
532 ...props,
533 });
534 return { vpc, subnet, otherVpc, otherSubnet, instance };
535 });
536
537 test.provider(
538 "fixed-IP replacements delete first only when reusing the same allocation",
539 (stack) =>
540 Effect.gen(function* () {
541 yield* stack.destroy();
542 const first = yield* stack.deploy(fixedIpProgram());
543 const unchanged = yield* stack.plan(fixedIpProgram());
544 expect(unchanged.resources.FixedIpInstance?.action).toBe("noop");
545
546 const tags = { Name: "alchemy-fixed-ip-replacement" };
547 const update = yield* stack.plan(fixedIpProgram({ tags }));
548 expect(update.resources.FixedIpInstance?.action).toBe("update");
549 const updated = yield* stack.deploy(fixedIpProgram({ tags }));
550 expect(updated.instance.instanceId).toBe(first.instance.instanceId);
551
552 const differentIp = yield* stack.plan(fixedIpProgram({ privateIpAddress: "10.0.1.11" }));
553 expect(differentIp.resources.FixedIpInstance).toMatchObject({
554 action: "replace",
555 deleteFirst: false,
556 });
557 const differentSubnet = yield* stack.plan(
558 fixedIpProgram({ subnetId: first.otherSubnet.subnetId }),
559 );
560 expect(differentSubnet.resources.FixedIpInstance).toMatchObject({
561 action: "replace",
562 deleteFirst: false,
563 });
564
565 const next = fixedIpProgram({ userData: "#!/bin/bash\necho generation-two\n", tags });
566 const replacement = yield* stack.plan(next);
567 expect(replacement.resources.FixedIpInstance).toMatchObject({
568 action: "replace",
569 deleteFirst: true,
570 });
571 const second = yield* stack.deploy(next);
572 expect(second.instance.instanceId).not.toBe(first.instance.instanceId);
573 const live = yield* ec2.describeInstances({ InstanceIds: [second.instance.instanceId] });
574 expect(live.Reservations?.[0]?.Instances?.[0]).toMatchObject({
575 PrivateIpAddress: "10.0.1.10",
576 SubnetId: first.subnet.subnetId,
577 State: { Name: "running" },
578 });
579 yield* assertInstanceTerminated(first.instance.instanceId);
580 const settled = yield* stack.plan(next);
581 expect(settled.resources.FixedIpInstance?.action).toBe("noop");
582
583 yield* stack.destroy();
584 yield* assertInstanceTerminated(second.instance.instanceId);
585 yield* assertVpcGone(first.vpc.vpcId);
586 yield* assertVpcGone(first.otherVpc.vpcId);
587 }).pipe(logLevel),
588 { timeout: 120_000 },
589 );
590
591 test.provider(
592 "recovers a launched fixed-IP replacement whose output was not persisted",
593 (stack) =>
594 Effect.gen(function* () {
595 yield* stack.destroy();
596 const state = yield* yield* State;
597 const key = { stack: stack.name, stage: stack.stage, fqn: "FixedIpInstance" };
598 const first = yield* stack.deploy(fixedIpProgram());
599 const firstState = yield* state.get(key);
600 if (firstState?.status !== "created" && firstState?.status !== "updated") {
601 return yield* Effect.die(new Error("Expected a deployed original instance"));
602 }
603
604 const next = fixedIpProgram({ privateIpAddress: "10.0.1.11" });
605 const second = yield* stack.deploy(next);
606 const secondState = yield* state.get(key);
607 if (secondState?.status !== "created" && secondState?.status !== "updated") {
608 return yield* Effect.die(new Error("Expected a deployed replacement instance"));
609 }
610 // Retain the real candidate's generation and tags, but simulate the
611 // interrupted commit between its EC2 launch and persisting its output.
612 yield* state.set({
613 ...key,
614 value: {
615 ...secondState,
616 status: "replacing",
617 attr: undefined,
618 deleteFirst: false,
619 old: firstState,
620 },
621 });
622 const recovery = yield* stack.plan(next);
623 expect(recovery.resources.FixedIpInstance).toMatchObject({
624 action: "replace",
625 deleteFirst: false,
626 });
627 expect(recovery.resources.FixedIpInstance).not.toMatchObject({ restart: true });
628 const recovered = yield* stack.deploy(next);
629 expect(recovered.instance.instanceId).toBe(second.instance.instanceId);
630 const live = yield* ec2.describeInstances({ InstanceIds: [recovered.instance.instanceId] });
631 expect(live.Reservations?.[0]?.Instances?.[0]).toMatchObject({
632 PrivateIpAddress: "10.0.1.11",
633 State: { Name: "running" },
634 });
635 const settled = yield* stack.plan(next);
636 expect(settled.resources.FixedIpInstance?.action).toBe("noop");
637
638 yield* stack.destroy();
639 yield* assertInstanceTerminated(first.instance.instanceId);
640 yield* assertInstanceTerminated(second.instance.instanceId);
641 yield* assertVpcGone(first.vpc.vpcId);
642 yield* assertVpcGone(first.otherVpc.vpcId);
643 }).pipe(logLevel),
644 { timeout: 120_000 },
645 );
646 });