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

main
ts 184 lines 6.77 KB
Raw
1 import * as AWS from "@alchemy.run/aws";
2 import type { VpcId } from "@alchemy.run/aws/EC2";
3 import { describe, expect } from "@alchemy.run/test";
4 import * as Test from "@alchemy.run/test/Alchemy";
5 import * as Core from "@alchemy.run/test/Core";
6 import * as EC2 from "@distilled.cloud/aws/ec2";
7 import * as efs from "@distilled.cloud/aws/efs";
8 import * as Effect from "effect/Effect";
9 import * as Schedule from "effect/Schedule";
10 import { getDefaultVpc } from "../DefaultVpc.ts";
11
12 const testOptions = { providers: AWS.providers() };
13 const { test, beforeAll, afterAll } = Test.make(testOptions);
14 const sharedStack = Core.scratchStack(testOptions, "EFSMountTarget");
15
16 // Mount-target churn is the slow part of EFS (1–3 minutes each way), so the
17 // suite deploys ONE mount target in a `beforeAll`, drives updates against it
18 // in tests, and tears it down once in `afterAll`. Multi-AZ coverage is gated
19 // behind AWS_TEST_EFS_MULTI_AZ=1 because it doubles the churn.
20
21 let vpcId: VpcId;
22 let subnetId: string;
23 let defaultSecurityGroupId: string;
24 let extraSecurityGroupId: string;
25 let fileSystemId: string;
26 let mountTargetId: string;
27
28 // Resolve the default VPC, a default subnet, and the default security group
29 // once. Runs inside a deploy/test effect (the `beforeAll` hook itself has no
30 // AWS context).
31 const resolveNetwork = Effect.gen(function* () {
32 if (subnetId !== undefined) return;
33 const vpc = yield* getDefaultVpc;
34 vpcId = vpc.vpcId;
35 const subnets = yield* EC2.describeSubnets({
36 Filters: [
37 { Name: "vpc-id", Values: [vpc.vpcId] },
38 { Name: "default-for-az", Values: ["true"] },
39 ],
40 });
41 subnetId = subnets.Subnets![0]!.SubnetId!;
42 const groups = yield* EC2.describeSecurityGroups({
43 Filters: [
44 { Name: "vpc-id", Values: [vpc.vpcId] },
45 { Name: "group-name", Values: ["default"] },
46 ],
47 });
48 defaultSecurityGroupId = groups.SecurityGroups![0]!.GroupId!;
49 });
50
51 const infra = (securityGroups?: () => string[]) =>
52 Effect.gen(function* () {
53 yield* resolveNetwork;
54 const files = yield* AWS.EFS.FileSystem("MtFiles");
55 const extraSg = yield* AWS.EC2.SecurityGroup("MtSg", {
56 vpcId,
57 description: "alchemy EFS mount target test",
58 });
59 const target = yield* AWS.EFS.MountTarget("MtTarget", {
60 fileSystemId: files.fileSystemId,
61 subnetId,
62 ...(securityGroups !== undefined ? { securityGroups: securityGroups() } : {}),
63 });
64 return { files, target, extraSg };
65 });
66
67 describe.sequential(
68 "EFS MountTarget",
69 { tags: ["provider:aws", "provider:aws:ec2", "provider:aws:efs", "live"] },
70 () => {
71 beforeAll(
72 Effect.gen(function* () {
73 yield* sharedStack.destroy();
74
75 // initial deploy: no explicit security groups → the VPC default group
76 const deployed = yield* sharedStack.deploy(infra());
77 fileSystemId = deployed.files.fileSystemId;
78 mountTargetId = deployed.target.mountTargetId;
79 extraSecurityGroupId = deployed.extraSg.groupId;
80 }),
81 { timeout: 240_000 },
82 );
83
84 afterAll(sharedStack.destroy(), { timeout: 240_000 });
85
86 test.provider(
87 "mount target is available in the subnet with the default security group",
88 () =>
89 Effect.gen(function* () {
90 expect(mountTargetId).toMatch(/^fsmt-/);
91
92 const observed = yield* efs
93 .describeMountTargets({ MountTargetId: mountTargetId })
94 .pipe(Effect.map((r) => r.MountTargets![0]));
95 expect(observed.LifeCycleState).toBe("available");
96 expect(observed.SubnetId).toBe(subnetId);
97 expect(observed.FileSystemId).toBe(fileSystemId);
98 expect(observed.IpAddress).toBeTruthy();
99
100 const groups = yield* efs.describeMountTargetSecurityGroups({
101 MountTargetId: mountTargetId,
102 });
103 expect(groups.SecurityGroups).toEqual([defaultSecurityGroupId]);
104 }),
105 { timeout: 60_000 },
106 );
107
108 test.provider(
109 "security groups are modified in place",
110 () =>
111 Effect.gen(function* () {
112 const updated = yield* sharedStack.deploy(
113 infra(() => [defaultSecurityGroupId, extraSecurityGroupId]),
114 );
115 // same mount target — securityGroups is mutable
116 expect(updated.target.mountTargetId).toBe(mountTargetId);
117
118 const observed = yield* efs
119 .describeMountTargetSecurityGroups({ MountTargetId: mountTargetId })
120 .pipe(Effect.map((r) => [...r.SecurityGroups].sort()));
121 expect(observed).toEqual([defaultSecurityGroupId, extraSecurityGroupId].sort());
122 }),
123 { timeout: 120_000 },
124 );
125 },
126 );
127
128 // Full teardown verification for the shared fixture is implicit: the mount
129 // target's delete waits until it is observed gone (its ENI must release
130 // before the security group and file system can delete), so a leaked mount
131 // target fails the afterAll destroy. Multi-AZ is gated — it adds another
132 // 1–3 minute create/delete cycle per AZ.
133 test.provider.skipIf(!process.env.AWS_TEST_EFS_MULTI_AZ)(
134 "creates one mount target per availability zone",
135 (stack) =>
136 Effect.gen(function* () {
137 yield* stack.destroy();
138
139 const vpc = yield* getDefaultVpc;
140 const subnets = yield* EC2.describeSubnets({
141 Filters: [
142 { Name: "vpc-id", Values: [vpc.vpcId] },
143 { Name: "default-for-az", Values: ["true"] },
144 ],
145 });
146 const [subnetA, subnetB] = subnets.Subnets!.map((s) => s.SubnetId!);
147
148 const deployed = yield* stack.deploy(
149 Effect.gen(function* () {
150 const files = yield* AWS.EFS.FileSystem("MultiAzFiles");
151 const targetA = yield* AWS.EFS.MountTarget("MtA", {
152 fileSystemId: files.fileSystemId,
153 subnetId: subnetA,
154 });
155 const targetB = yield* AWS.EFS.MountTarget("MtB", {
156 fileSystemId: files.fileSystemId,
157 subnetId: subnetB,
158 });
159 return { files, targetA, targetB };
160 }),
161 );
162
163 expect(deployed.targetA.availabilityZoneName).not.toBe(deployed.targetB.availabilityZoneName);
164
165 const observed = yield* efs.describeMountTargets({
166 FileSystemId: deployed.files.fileSystemId,
167 });
168 expect(observed.MountTargets).toHaveLength(2);
169
170 yield* stack.destroy();
171 const gone = yield* efs
172 .describeMountTargets({ FileSystemId: deployed.files.fileSystemId })
173 .pipe(
174 Effect.map((r) => (r.MountTargets ?? []).length === 0),
175 Effect.catchTag("FileSystemNotFound", () => Effect.succeed(true)),
176 Effect.repeat({ schedule: Schedule.fixed("5 seconds"), until: (g) => g, times: 36 }),
177 );
178 expect(gone).toBe(true);
179 }).pipe(
180 Effect.tap(() => stack.destroy()),
181 Effect.onError(() => stack.destroy().pipe(Effect.ignore)),
182 ),
183 { tags: ["provider:aws", "provider:aws:ec2", "provider:aws:efs", "live"], timeout: 600_000 },
184 );