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

main
ts 147 lines 5.5 KB
Raw
1 import * as AWS from "@alchemy.run/aws";
2 import { ReplicationSubnetGroup } from "@alchemy.run/aws/DMS";
3 import { Subnet, Vpc } from "@alchemy.run/aws/EC2";
4 import { expect } from "@alchemy.run/test";
5 import * as Test from "@alchemy.run/test/Alchemy";
6 import * as dms from "@distilled.cloud/aws/database-migration-service";
7 import * as ec2 from "@distilled.cloud/aws/ec2";
8 import * as Effect from "effect/Effect";
9 import * as Schedule from "effect/Schedule";
10 import { DMS_SUBNET_GROUP_VPC, reapDmsOrphans } from "./reap.ts";
11
12 const { test } = Test.make({ providers: AWS.providers() });
13
14 const findGroup = (identifier: string) =>
15 dms
16 .describeReplicationSubnetGroups({
17 Filters: [{ Name: "replication-subnet-group-id", Values: [identifier] }],
18 })
19 .pipe(
20 Effect.map((r) => r.ReplicationSubnetGroups?.[0]),
21 Effect.catchTag("ResourceNotFoundFault", () => Effect.succeed(undefined)),
22 );
23
24 const assertGone = (identifier: string) =>
25 findGroup(identifier).pipe(
26 Effect.flatMap((group) =>
27 group === undefined
28 ? Effect.void
29 : Effect.fail(new Error(`subnet group '${identifier}' still exists`)),
30 ),
31 Effect.retry({
32 schedule: Schedule.max([Schedule.fixed("2 seconds"), Schedule.recurs(15)]),
33 }),
34 );
35
36 test.provider(
37 "create, update, delete a DMS replication subnet group",
38 (stack) =>
39 Effect.gen(function* () {
40 yield* stack.destroy();
41 // A previous hard-killed run (in-memory scratch state) may have
42 // orphaned the VPC fixture — reap it out-of-band before deploying.
43 yield* reapDmsOrphans(DMS_SUBNET_GROUP_VPC);
44
45 // Resolve three AZs so the update can swap the subnet set.
46 const azResult = yield* ec2.describeAvailabilityZones({});
47 const azs = (azResult.AvailabilityZones ?? [])
48 .filter((az) => az.State === "available")
49 .map((az) => az.ZoneName!);
50 const [az1, az2, az3] = azs;
51
52 const { group, subnetCId } = yield* stack.deploy(
53 Effect.gen(function* () {
54 const vpc = yield* Vpc(DMS_SUBNET_GROUP_VPC.logicalId, {
55 cidrBlock: DMS_SUBNET_GROUP_VPC.cidr,
56 });
57 const subnetA = yield* Subnet("DmsSubnetA", {
58 vpcId: vpc.vpcId,
59 cidrBlock: "10.91.1.0/24",
60 availabilityZone: az1,
61 });
62 const subnetB = yield* Subnet("DmsSubnetB", {
63 vpcId: vpc.vpcId,
64 cidrBlock: "10.91.2.0/24",
65 availabilityZone: az2,
66 });
67 const subnetC = yield* Subnet("DmsSubnetC", {
68 vpcId: vpc.vpcId,
69 cidrBlock: "10.91.3.0/24",
70 availabilityZone: az3,
71 });
72 const group = yield* ReplicationSubnetGroup("Group", {
73 description: "alchemy dms test subnets",
74 subnetIds: [subnetA.subnetId, subnetB.subnetId],
75 tags: { env: "test" },
76 });
77 return {
78 group,
79 subnetCId: subnetC.subnetId.as<string>(),
80 };
81 }),
82 );
83
84 expect(group.replicationSubnetGroupArn).toContain(":subgrp:");
85 expect(group.subnetIds.length).toBe(2);
86 expect(group.tags.env).toBe("test");
87
88 // Out-of-band verification via distilled.
89 const observed = yield* findGroup(group.replicationSubnetGroupIdentifier);
90 expect(observed?.SubnetGroupStatus).toBe("Complete");
91 expect(observed?.Subnets?.length).toBe(2);
92
93 // Update: swap one subnet + change the description.
94 const { group: updated } = yield* stack.deploy(
95 Effect.gen(function* () {
96 const vpc = yield* Vpc(DMS_SUBNET_GROUP_VPC.logicalId, {
97 cidrBlock: DMS_SUBNET_GROUP_VPC.cidr,
98 });
99 const subnetA = yield* Subnet("DmsSubnetA", {
100 vpcId: vpc.vpcId,
101 cidrBlock: "10.91.1.0/24",
102 availabilityZone: az1,
103 });
104 yield* Subnet("DmsSubnetB", {
105 vpcId: vpc.vpcId,
106 cidrBlock: "10.91.2.0/24",
107 availabilityZone: az2,
108 });
109 const subnetC = yield* Subnet("DmsSubnetC", {
110 vpcId: vpc.vpcId,
111 cidrBlock: "10.91.3.0/24",
112 availabilityZone: az3,
113 });
114 const group = yield* ReplicationSubnetGroup("Group", {
115 description: "alchemy dms test subnets (updated)",
116 subnetIds: [subnetA.subnetId, subnetC.subnetId],
117 tags: { env: "test" },
118 });
119 return { group };
120 }),
121 );
122
123 // Same subnet group (in-place modify).
124 expect(updated.replicationSubnetGroupIdentifier).toBe(group.replicationSubnetGroupIdentifier);
125 const observed2 = yield* findGroup(group.replicationSubnetGroupIdentifier);
126 const observedSubnetIds = (observed2?.Subnets ?? [])
127 .map((s) => s.SubnetIdentifier)
128 .filter((s): s is string => typeof s === "string");
129 expect(observedSubnetIds).toContain(subnetCId);
130 expect(observed2?.ReplicationSubnetGroupDescription).toBe(
131 "alchemy dms test subnets (updated)",
132 );
133
134 yield* stack.destroy();
135 yield* assertGone(group.replicationSubnetGroupIdentifier);
136 }).pipe(
137 // Converge to zero leftovers even when the body (or the engine's own
138 // scratch destroy) fails: the reaper deletes the VPC fixture out-of-band
139 // with idempotent typed calls. `orDie` — a finalizer must not swallow
140 // its own failure silently.
141 Effect.ensuring(reapDmsOrphans(DMS_SUBNET_GROUP_VPC).pipe(Effect.orDie)),
142 ),
143 {
144 tags: ["provider:aws", "provider:aws:dms", "provider:aws:ec2", "live"],
145 timeout: 300_000,
146 },
147 );