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

main
ts 504 lines 19.6 KB
Raw
1 import * as AWS from "@alchemy.run/aws";
2 import { Cluster } from "@alchemy.run/aws/ECS/Cluster";
3 import { Service } from "@alchemy.run/aws/ECS/Service";
4 import { expect } from "@alchemy.run/test";
5 import * as Test from "@alchemy.run/test/Alchemy";
6 import * as ec2 from "@distilled.cloud/aws/ec2";
7 import * as ecr from "@distilled.cloud/aws/ecr";
8 import * as ecs from "@distilled.cloud/aws/ecs";
9 import * as elbv2 from "@distilled.cloud/aws/elastic-load-balancing-v2";
10 import * as iam from "@distilled.cloud/aws/iam";
11 import { isResourceState, State, type ResourceState } from "alchemy/State";
12 import * as Effect from "effect/Effect";
13 import * as Schedule from "effect/Schedule";
14 import { getDefaultVpcNetwork } from "../DefaultVpc.ts";
15
16 const { test } = Test.make({ providers: AWS.providers() });
17
18 // The default VPC's per-AZ default subnets (exactly one per AZ). Concurrent
19 // suite runs create EXTRA subnets in the default VPC, and an ALB rejects two
20 // subnets in the same AZ — so never use the full subnet list for ALBs.
21 const defaultAzSubnets = (vpcId: string) =>
22 ec2
23 .describeSubnets({
24 Filters: [
25 { Name: "vpc-id", Values: [vpcId] },
26 { Name: "default-for-az", Values: ["true"] },
27 ],
28 })
29 .pipe(Effect.map((r) => (r.Subnets ?? []).flatMap((s) => (s.SubnetId ? [s.SubnetId] : []))));
30
31 /**
32 * Delete any leftover out-of-band "legacy" fixtures carrying {@link name}.
33 *
34 * The reap test builds its fixtures under a fixed name and relies on
35 * finalizers to reclaim them, which a hard kill skips. Matching by listing
36 * (rather than describe-by-name) keeps this free of not-found handling: an
37 * absent fixture simply doesn't appear.
38 */
39 const reclaimLegacyFixtures = Effect.fn("reclaimLegacyFixtures")(function* (
40 name: string,
41 vpcId: string,
42 ) {
43 const lbs = yield* elbv2.describeLoadBalancers({});
44 const lbArn = (lbs.LoadBalancers ?? []).find(
45 (lb) => lb.LoadBalancerName === name,
46 )?.LoadBalancerArn;
47 if (lbArn) {
48 const listeners = yield* elbv2.describeListeners({
49 LoadBalancerArn: lbArn,
50 });
51 yield* Effect.forEach(
52 (listeners.Listeners ?? []).flatMap((l) => (l.ListenerArn ? [l.ListenerArn] : [])),
53 (ListenerArn) => elbv2.deleteListener({ ListenerArn }),
54 );
55 yield* elbv2.deleteLoadBalancer({ LoadBalancerArn: lbArn });
56 }
57
58 const tgs = yield* elbv2.describeTargetGroups({});
59 const tgArn = (tgs.TargetGroups ?? []).find((tg) => tg.TargetGroupName === name)?.TargetGroupArn;
60 if (tgArn) {
61 yield* elbv2.deleteTargetGroup({ TargetGroupArn: tgArn });
62 }
63
64 const sgs = yield* ec2.describeSecurityGroups({
65 Filters: [
66 { Name: "vpc-id", Values: [vpcId] },
67 { Name: "group-name", Values: [name] },
68 ],
69 });
70 const sgId = (sgs.SecurityGroups ?? [])[0]?.GroupId;
71 if (sgId) {
72 yield* ec2.deleteSecurityGroup({ GroupId: sgId });
73 }
74 });
75
76 // The image-owning `Service` platform form: `image:` (mirrored into ECR) with
77 // `loadBalancer: true` — the Service synthesizes its own task definition
78 // (roles, log group, ECR repository) and wires an ALB + target group +
79 // listener + owned ingress security group, defaulting networking to the
80 // account's default VPC. `desiredCount: 0` keeps the test inside the speed
81 // budget (no Fargate placement, no image pull on the service side — the
82 // mirror is a local docker pull/tag/push of a tiny busybox).
83 //
84 // Requires a local Docker daemon (same as the ECS Bindings fixture).
85 test.provider(
86 "image-owning service synthesizes its task definition and ALB",
87 (stack) =>
88 Effect.gen(function* () {
89 yield* stack.destroy();
90
91 const deployed = yield* stack.deploy(
92 Effect.gen(function* () {
93 const cluster = yield* Cluster("PlatformSvcCluster", {
94 clusterName: "alchemy-test-ecs-service-platform",
95 });
96 // Docker Hub busybox: the public.ecr.aws mirror aggressively
97 // rate-limits anonymous pulls during local builds.
98 return yield* Service("PlatformEdge", {
99 cluster,
100 image: "busybox:stable",
101 command: ["sh", "-c", "while true; do sleep 30; done"],
102 port: 80,
103 desiredCount: 0,
104 loadBalancer: true,
105 environmentFiles: [
106 {
107 value: "arn:aws:s3:::alchemy-test-ecs-env-files/edge.env",
108 type: "s3",
109 },
110 ],
111 });
112 }),
113 );
114
115 // Synthesized task definition attributes.
116 expect(deployed.serviceArn).toBeTruthy();
117 expect(deployed.taskDefinitionArn).toBeTruthy();
118 expect(deployed.taskFamily).toBeTruthy();
119 expect(deployed.repositoryName).toBeTruthy();
120 expect(deployed.imageUri).toContain(deployed.repositoryName!);
121 expect(deployed.code?.hash).toBeTruthy();
122 expect(deployed.taskRoleArn).toBeTruthy();
123 expect(deployed.executionRoleArn).toBeTruthy();
124 expect(deployed.logGroupName).toBeTruthy();
125
126 // Managed ingress attributes.
127 expect(deployed.url).toMatch(/^http:\/\//);
128 expect(deployed.loadBalancerArn).toBeTruthy();
129 expect(deployed.targetGroupArn).toBeTruthy();
130 expect(deployed.listenerArn).toBeTruthy();
131 expect(deployed.securityGroupId).toBeTruthy();
132
133 // Out-of-band: the registered task definition runs the mirrored image.
134 const described = yield* ecs.describeTaskDefinition({
135 taskDefinition: deployed.taskDefinitionArn,
136 });
137 const container = described.taskDefinition?.containerDefinitions?.[0];
138 expect(container?.image).toBe(deployed.imageUri);
139 expect(container?.command).toEqual(["sh", "-c", "while true; do sleep 30; done"]);
140 expect(container?.portMappings?.[0]?.containerPort).toBe(80);
141 // `environmentFiles` lands on the synthesized primary container, with
142 // the execution role granted read access to the referenced object.
143 expect(container?.environmentFiles).toEqual([
144 {
145 value: "arn:aws:s3:::alchemy-test-ecs-env-files/edge.env",
146 type: "s3",
147 },
148 ]);
149 const envFilesPolicy = yield* iam.getRolePolicy({
150 RoleName: deployed.executionRoleArn!.split(":role/")[1]!,
151 PolicyName: "alchemy-environment-files",
152 });
153 expect(decodeURIComponent(envFilesPolicy.PolicyDocument ?? "")).toContain(
154 "arn:aws:s3:::alchemy-test-ecs-env-files/edge.env",
155 );
156
157 // Out-of-band: the service is wired to the generated target group.
158 const services = yield* ecs.describeServices({
159 cluster: deployed.clusterArn,
160 services: [deployed.serviceName],
161 });
162 const svc = services.services?.[0];
163 expect(svc?.loadBalancers?.[0]?.targetGroupArn).toBe(deployed.targetGroupArn);
164 expect(svc?.loadBalancers?.[0]?.containerPort).toBe(80);
165
166 yield* stack.destroy();
167
168 // Zero-orphan proofs: every owned resource is gone (or terminally
169 // deleting) after destroy.
170 const repoGone = yield* ecr
171 .describeRepositories({
172 repositoryNames: [deployed.repositoryName!],
173 })
174 .pipe(
175 Effect.map(() => false),
176 Effect.catchTag("RepositoryNotFoundException", () => Effect.succeed(true)),
177 );
178 expect(repoGone).toBe(true);
179
180 const lbGone = yield* elbv2
181 .describeLoadBalancers({
182 LoadBalancerArns: [deployed.loadBalancerArn!],
183 })
184 .pipe(
185 Effect.map((r) => (r.LoadBalancers ?? []).length === 0),
186 Effect.catchTag("LoadBalancerNotFoundException", () => Effect.succeed(true)),
187 );
188 expect(lbGone).toBe(true);
189
190 const sgGone = yield* ec2
191 .describeSecurityGroups({ GroupIds: [deployed.securityGroupId!] })
192 .pipe(
193 Effect.map((r) => (r.SecurityGroups ?? []).length === 0),
194 Effect.catchTag("InvalidGroup.NotFound", () => Effect.succeed(true)),
195 );
196 expect(sgGone).toBe(true);
197
198 // The revision is deregistered (INACTIVE / DELETE_IN_PROGRESS) or
199 // already hard-deleted.
200 const taskDefGone = yield* ecs
201 .describeTaskDefinition({
202 taskDefinition: deployed.taskDefinitionArn,
203 })
204 .pipe(
205 Effect.map((r) => r.taskDefinition?.status !== "ACTIVE"),
206 Effect.catchTag("ClientException", () => Effect.succeed(true)),
207 );
208 expect(taskDefGone).toBe(true);
209
210 const clusters = yield* ecs.describeClusters({
211 clusters: ["alchemy-test-ecs-service-platform"],
212 });
213 expect((clusters.clusters ?? []).some((c) => c.status === "ACTIVE")).toBe(false);
214 }),
215 {
216 tags: [
217 "provider:aws",
218 "provider:aws:ec2",
219 "provider:aws:ecr",
220 "provider:aws:ecs",
221 "provider:aws:iam",
222 "live",
223 ],
224 timeout: 420_000,
225 },
226 );
227
228 // `host` / `bridge` tasks have no ENI: ECS rejects an `awsvpcConfiguration`
229 // for them, and their ALB targets must register by instance, not IP. Deploy
230 // an EC2 host-mode service with `desiredCount: 0` (no container instance
231 // needed) and check both on the live service and target group.
232 test.provider(
233 "host network mode skips awsvpc configuration and targets instances",
234 (stack) =>
235 Effect.gen(function* () {
236 yield* stack.destroy();
237
238 const deployed = yield* stack.deploy(
239 Effect.gen(function* () {
240 const cluster = yield* Cluster("HostModeCluster", {});
241 return yield* Service("HostModeEdge", {
242 cluster,
243 image: "busybox:stable",
244 command: ["sh", "-c", "while true; do sleep 30; done"],
245 port: 80,
246 networkMode: "host",
247 requiresCompatibilities: ["EC2"],
248 launchType: "EC2",
249 desiredCount: 0,
250 loadBalancer: true,
251 });
252 }),
253 );
254
255 const described = yield* ecs.describeTaskDefinition({
256 taskDefinition: deployed.taskDefinitionArn,
257 });
258 expect(described.taskDefinition?.networkMode).toBe("host");
259
260 const services = yield* ecs.describeServices({
261 cluster: deployed.clusterArn,
262 services: [deployed.serviceName],
263 });
264 const svc = services.services?.[0];
265 expect(svc?.launchType).toBe("EC2");
266 expect(svc?.networkConfiguration).toBeUndefined();
267 expect(svc?.loadBalancers?.[0]?.targetGroupArn).toBe(deployed.targetGroupArn);
268
269 const targetGroups = yield* elbv2.describeTargetGroups({
270 TargetGroupArns: [deployed.targetGroupArn!],
271 });
272 expect(targetGroups.TargetGroups?.[0]?.TargetType).toBe("instance");
273
274 yield* stack.destroy();
275
276 const clusters = yield* ecs.describeClusters({ clusters: [deployed.clusterArn] });
277 expect((clusters.clusters ?? []).some((c) => c.status === "ACTIVE")).toBe(false);
278 const tgGone = yield* elbv2
279 .describeTargetGroups({ TargetGroupArns: [deployed.targetGroupArn!] })
280 .pipe(
281 Effect.map((r) => (r.TargetGroups ?? []).length === 0),
282 Effect.catchTag("TargetGroupNotFoundException", () => Effect.succeed(true)),
283 );
284 expect(tgGone).toBe(true);
285 }),
286 {
287 tags: ["provider:aws", "provider:aws:ec2", "provider:aws:ecs", "live"],
288 timeout: 420_000,
289 },
290 );
291
292 // Migration reap: state rows written by the pre-composition provider carry
293 // the inline-created ALB/TG/listener/SG ARNs in the service's own attributes
294 // with no `ingressKind` marker. The first reconcile under the composed shape
295 // must DELETE that legacy inline infrastructure (nothing stranded) and stamp
296 // the composed attrs. We simulate the legacy row exactly the way the #736
297 // test simulates a wedged one: deploy under the new shape, create a real
298 // "legacy" ALB/TG/listener/SG out of band, then rewrite the service's state
299 // row to reference them (marker stripped, `ingress` prop removed,
300 // `loadBalancer: true`), and redeploy.
301 test.provider(
302 "legacy inline ingress is reaped on first reconcile under the composed shape",
303 (stack) =>
304 Effect.gen(function* () {
305 yield* stack.destroy();
306 const net = yield* getDefaultVpcNetwork;
307
308 const program = Effect.gen(function* () {
309 const cluster = yield* Cluster("LegacyReapCluster", {
310 clusterName: "alchemy-test-ecs-service-legacy-reap",
311 });
312 return yield* Service("LegacyReapEdge", {
313 cluster,
314 image: "busybox:stable",
315 command: ["sh", "-c", "while true; do sleep 30; done"],
316 port: 80,
317 desiredCount: 0,
318 loadBalancer: true,
319 });
320 });
321
322 const deployed = yield* stack.deploy(program);
323 expect(deployed.loadBalancerArn).toBeTruthy();
324
325 // Out-of-band "legacy" inline ingress (never carried any traffic, so
326 // reap deletes are instant). Finalizers reclaim them if the test dies
327 // before the reap runs.
328 const legacyName = "alchemy-test-ecs-svc-legacy";
329 // The scaffolding below is created out-of-band under a fixed name and
330 // reclaimed by finalizers. A hard kill (SIGKILL, machine death) skips
331 // finalizers, so the previous run's copy survives and the next run dies
332 // on DuplicateLoadBalancerName before it can test anything. Reclaim any
333 // same-named leftovers first so the suite is rerunnable after a crash.
334 //
335 // This is scaffolding hygiene, NOT adoption of a leaked stack-managed
336 // resource: these are fixtures the test itself creates, and the reap
337 // under test is unaffected by whether they pre-existed.
338 yield* reclaimLegacyFixtures(legacyName, net.vpcId);
339 const legacySubnets = yield* defaultAzSubnets(net.vpcId);
340 const legacyLb = yield* elbv2.createLoadBalancer({
341 Name: legacyName,
342 Type: "application",
343 Scheme: "internal",
344 Subnets: legacySubnets,
345 });
346 const legacyLbArn = legacyLb.LoadBalancers?.[0]?.LoadBalancerArn!;
347 yield* Effect.addFinalizer(() =>
348 elbv2.deleteLoadBalancer({ LoadBalancerArn: legacyLbArn }).pipe(Effect.ignore),
349 );
350 const legacyTg = yield* elbv2.createTargetGroup({
351 Name: legacyName,
352 VpcId: net.vpcId,
353 TargetType: "ip",
354 Protocol: "HTTP",
355 Port: 80,
356 });
357 const legacyTgArn = legacyTg.TargetGroups?.[0]?.TargetGroupArn!;
358 yield* Effect.addFinalizer(() =>
359 elbv2.deleteTargetGroup({ TargetGroupArn: legacyTgArn }).pipe(Effect.ignore),
360 );
361 const legacyListener = yield* elbv2.createListener({
362 LoadBalancerArn: legacyLbArn,
363 Port: 80,
364 Protocol: "HTTP",
365 DefaultActions: [{ Type: "forward", TargetGroupArn: legacyTgArn }],
366 });
367 const legacyListenerArn = legacyListener.Listeners?.[0]?.ListenerArn!;
368 yield* Effect.addFinalizer(() =>
369 elbv2.deleteListener({ ListenerArn: legacyListenerArn }).pipe(Effect.ignore),
370 );
371 const legacySg = yield* ec2.createSecurityGroup({
372 GroupName: legacyName,
373 Description: "legacy inline ingress SG (reap test)",
374 VpcId: net.vpcId,
375 });
376 const legacySgId = legacySg.GroupId!;
377 yield* Effect.addFinalizer(() =>
378 ec2.deleteSecurityGroup({ GroupId: legacySgId }).pipe(Effect.ignore),
379 );
380
381 // Rewrite the service's state row into the legacy inline shape.
382 const state = yield* yield* State;
383 const stage = stack.stage;
384 const fqns = yield* state.list({ stack: stack.name, stage });
385 const rows = yield* Effect.forEach(fqns, (fqn) =>
386 state.get({ stack: stack.name, stage, fqn }).pipe(Effect.map((row) => ({ fqn, row }))),
387 );
388 const serviceRow = rows.find(
389 (r): r is { fqn: string; row: ResourceState } =>
390 isResourceState(r.row) && r.row.resourceType === "AWS.ECS.Service",
391 );
392 if (!serviceRow) {
393 return yield* Effect.die(new Error("no AWS.ECS.Service state row found after deploy"));
394 }
395 yield* state.set({
396 stack: stack.name,
397 stage,
398 fqn: serviceRow.fqn,
399 value: {
400 ...serviceRow.row,
401 attr: {
402 ...(serviceRow.row.attr as object),
403 // Legacy rows have the inline ARNs and NO ingressKind marker.
404 ingressKind: undefined,
405 loadBalancerArn: legacyLbArn,
406 targetGroupArn: legacyTgArn,
407 listenerArn: legacyListenerArn,
408 securityGroupId: legacySgId,
409 url: "http://legacy.invalid",
410 },
411 props: {
412 ...serviceRow.row.props,
413 ingress: undefined,
414 loadBalancer: true,
415 },
416 },
417 });
418
419 // Redeploy: reconcile must reap the legacy inline infra and stamp the
420 // composed attrs.
421 const redeployed = yield* stack.deploy(program);
422 expect(redeployed.ingressKind).toBe("owned");
423 expect(redeployed.loadBalancerArn).toBeTruthy();
424 expect(redeployed.loadBalancerArn).not.toBe(legacyLbArn);
425 expect(redeployed.targetGroupArn).not.toBe(legacyTgArn);
426
427 // The redeploy registered a NEW task-definition revision — the
428 // superseded one must be reaped (deregistered + deleted), not left
429 // ACTIVE forever. This is the exact leak this test used to produce:
430 // one stranded ACTIVE revision per run.
431 expect(redeployed.taskDefinitionArn).not.toBe(deployed.taskDefinitionArn);
432 const supersededReaped = yield* ecs
433 .describeTaskDefinition({
434 taskDefinition: deployed.taskDefinitionArn,
435 })
436 .pipe(
437 Effect.map((r) => r.taskDefinition?.status !== "ACTIVE"),
438 Effect.catchTag("ClientException", () => Effect.succeed(true)),
439 );
440 expect(supersededReaped).toBe(true);
441
442 // The legacy inline resources are gone (nothing stranded). The ALB
443 // deletion is async — poll bounded.
444 const legacyLbGone = yield* elbv2
445 .describeLoadBalancers({ LoadBalancerArns: [legacyLbArn] })
446 .pipe(
447 Effect.map((r) => (r.LoadBalancers ?? []).length === 0),
448 Effect.catchTag("LoadBalancerNotFoundException", () => Effect.succeed(true)),
449 Effect.repeat({
450 schedule: Schedule.spaced("5 seconds"),
451 until: (gone) => gone,
452 times: 12,
453 }),
454 );
455 expect(legacyLbGone).toBe(true);
456 const legacyTgGone = yield* elbv2
457 .describeTargetGroups({ TargetGroupArns: [legacyTgArn] })
458 .pipe(
459 Effect.map((r) => (r.TargetGroups ?? []).length === 0),
460 Effect.catchTag("TargetGroupNotFoundException", () => Effect.succeed(true)),
461 );
462 expect(legacyTgGone).toBe(true);
463 const legacySgGone = yield* ec2.describeSecurityGroups({ GroupIds: [legacySgId] }).pipe(
464 Effect.map((r) => (r.SecurityGroups ?? []).length === 0),
465 Effect.catchTag("InvalidGroup.NotFound", () => Effect.succeed(true)),
466 );
467 expect(legacySgGone).toBe(true);
468
469 yield* stack.destroy();
470
471 // The composed ingress is gone after destroy too.
472 const composedLbGone = yield* elbv2
473 .describeLoadBalancers({
474 LoadBalancerArns: [redeployed.loadBalancerArn!],
475 })
476 .pipe(
477 Effect.map((r) => (r.LoadBalancers ?? []).length === 0),
478 Effect.catchTag("LoadBalancerNotFoundException", () => Effect.succeed(true)),
479 );
480 expect(composedLbGone).toBe(true);
481
482 // Zero-orphan proof for the synthesized task definition: NO revision
483 // of the family survives destroy as ACTIVE (deploy + redeploy
484 // registered two revisions; both must be gone).
485 const activeRevisions = yield* ecs
486 .listTaskDefinitions({
487 familyPrefix: redeployed.taskFamily!,
488 status: "ACTIVE",
489 })
490 .pipe(
491 Effect.map((r) =>
492 (r.taskDefinitionArns ?? []).filter((arn) =>
493 arn.includes(`/${redeployed.taskFamily!}:`),
494 ),
495 ),
496 Effect.catchTag("ClientException", () => Effect.succeed([] as string[])),
497 );
498 expect(activeRevisions).toEqual([]);
499 }),
500 {
501 tags: ["provider:aws", "provider:aws:ec2", "provider:aws:ecs", "live"],
502 timeout: 420_000,
503 },
504 );