The best way to control Claude Code, Codex and more from any computer or phone
| 1 | import * as AWS from "@alchemy.run/aws"; |
| 2 | import { Cluster } from "@alchemy.run/aws/ECS/Cluster"; |
| 3 | import { expect } from "@alchemy.run/test"; |
| 4 | import * as Test from "@alchemy.run/test/Alchemy"; |
| 5 | import * as ecs from "@distilled.cloud/aws/ecs"; |
| 6 | import * as iam from "@distilled.cloud/aws/iam"; |
| 7 | import * as Drift from "alchemy/Drift"; |
| 8 | import * as Provider from "alchemy/Provider"; |
| 9 | import * as Effect from "effect/Effect"; |
| 10 | import * as Schedule from "effect/Schedule"; |
| 11 | |
| 12 | const { test } = Test.make({ providers: AWS.providers() }); |
| 13 | |
| 14 | // Canonical `list()` test (AWS account/region-scoped collection): deploy a real |
| 15 | // cluster, resolve the provider from context via the typed `findProvider`, call |
| 16 | // `list()`, and assert the deployed cluster appears in the exhaustively- |
| 17 | // paginated result (listClusters -> describeClusters hydration). |
| 18 | test.provider( |
| 19 | "list enumerates the deployed cluster", |
| 20 | (stack) => |
| 21 | Effect.gen(function* () { |
| 22 | yield* stack.destroy(); |
| 23 | |
| 24 | const cluster = yield* stack.deploy( |
| 25 | Effect.gen(function* () { |
| 26 | return yield* Cluster("ListCluster", { clusterName: "alchemy-test-ecs-cluster-list" }); |
| 27 | }), |
| 28 | ); |
| 29 | |
| 30 | const provider = yield* Provider.findProvider(Cluster); |
| 31 | const all = yield* provider.list(); |
| 32 | |
| 33 | expect(all.some((c) => c.clusterArn === cluster.clusterArn)).toBe(true); |
| 34 | |
| 35 | yield* stack.destroy(); |
| 36 | |
| 37 | // Out-of-band gone-proof: a deleted cluster is INACTIVE (or absent). |
| 38 | const after = yield* ecs.describeClusters({ clusters: ["alchemy-test-ecs-cluster-list"] }); |
| 39 | expect((after.clusters ?? []).some((c) => c.status === "ACTIVE")).toBe(false); |
| 40 | |
| 41 | // ECS can keep the terminal INACTIVE record discoverable for a while. |
| 42 | // Provider inventory (and therefore nuke) must treat it as deleted. |
| 43 | const afterList = yield* provider.list(); |
| 44 | expect(afterList.some((c) => c.clusterArn === cluster.clusterArn)).toBe(false); |
| 45 | }), |
| 46 | { tags: ["provider:aws", "provider:aws:ecs", "live"] }, |
| 47 | ); |
| 48 | |
| 49 | // #1968: ECS fills in account-default settings (containerInsights) the props |
| 50 | // never declared; an untouched cluster must not read as drifted. |
| 51 | test.provider( |
| 52 | "an untouched cluster reports no drift", |
| 53 | (stack) => |
| 54 | Effect.gen(function* () { |
| 55 | yield* stack.destroy(); |
| 56 | |
| 57 | yield* stack.deploy( |
| 58 | Effect.gen(function* () { |
| 59 | return yield* Cluster("DriftCluster", {}); |
| 60 | }), |
| 61 | ); |
| 62 | |
| 63 | const result = yield* Drift.detect({ name: stack.name, stage: stack.stage }).pipe( |
| 64 | Effect.provide(stack.state), |
| 65 | ); |
| 66 | expect(result.resources.DriftCluster?.action).toBe("unchanged"); |
| 67 | expect(result.resources.DriftCluster?.attr.settings).not.toEqual([]); |
| 68 | |
| 69 | yield* stack.destroy(); |
| 70 | }), |
| 71 | { tags: ["provider:aws", "provider:aws:ecs", "live"] }, |
| 72 | ); |
| 73 | |
| 74 | // Regression for #1558: in an account that has never run ECS, CreateCluster |
| 75 | // creates the AWSServiceRoleForECS service-linked role asynchronously, and the |
| 76 | // immediate PutClusterCapacityProviders failed with "Unable to assume the |
| 77 | // service linked role". Delete the role out of band to recreate the fresh- |
| 78 | // account state, then deploy a cluster with capacity providers. |
| 79 | test.provider( |
| 80 | "attaches capacity providers when the ECS service-linked role does not exist yet", |
| 81 | (stack) => |
| 82 | Effect.gen(function* () { |
| 83 | yield* stack.destroy(); |
| 84 | |
| 85 | const deletion = yield* iam |
| 86 | .deleteServiceLinkedRole({ RoleName: "AWSServiceRoleForECS" }) |
| 87 | .pipe(Effect.catchTag("NoSuchEntityException", () => Effect.succeed(undefined))); |
| 88 | if (deletion) { |
| 89 | const { Status, Reason } = yield* iam |
| 90 | .getServiceLinkedRoleDeletionStatus({ DeletionTaskId: deletion.DeletionTaskId }) |
| 91 | .pipe( |
| 92 | // The deletion task is not readable immediately after it starts. |
| 93 | Effect.retry({ |
| 94 | while: (e) => e._tag === "NoSuchEntityException", |
| 95 | schedule: Schedule.spaced("2 seconds"), |
| 96 | times: 10, |
| 97 | }), |
| 98 | Effect.repeat({ |
| 99 | schedule: Schedule.spaced("2 seconds"), |
| 100 | until: ({ Status }) => Status === "SUCCEEDED" || Status === "FAILED", |
| 101 | times: 30, |
| 102 | }), |
| 103 | ); |
| 104 | // IAM refuses to delete the role while any ECS resource in the |
| 105 | // account still uses it; the deploy below still has to converge. |
| 106 | if (Status !== "SUCCEEDED") { |
| 107 | yield* Effect.logWarning( |
| 108 | `AWSServiceRoleForECS deletion ${Status}; fresh-account state not reproduced`, |
| 109 | Reason, |
| 110 | ); |
| 111 | } |
| 112 | } |
| 113 | |
| 114 | const cluster = yield* stack.deploy( |
| 115 | Effect.gen(function* () { |
| 116 | return yield* Cluster("FreshAccountCluster", { |
| 117 | capacityProviders: ["FARGATE", "FARGATE_SPOT"], |
| 118 | defaultCapacityProviderStrategy: [{ capacityProvider: "FARGATE_SPOT", weight: 1 }], |
| 119 | }); |
| 120 | }), |
| 121 | ); |
| 122 | |
| 123 | const observed = (yield* ecs.describeClusters({ clusters: [cluster.clusterArn] })) |
| 124 | .clusters?.[0]; |
| 125 | expect(observed?.status).toBe("ACTIVE"); |
| 126 | expect([...(observed?.capacityProviders ?? [])].sort()).toEqual(["FARGATE", "FARGATE_SPOT"]); |
| 127 | expect(observed?.defaultCapacityProviderStrategy?.[0]?.capacityProvider).toBe("FARGATE_SPOT"); |
| 128 | |
| 129 | yield* stack.destroy(); |
| 130 | |
| 131 | const after = yield* ecs.describeClusters({ clusters: [cluster.clusterArn] }); |
| 132 | expect((after.clusters ?? []).some((c) => c.status === "ACTIVE")).toBe(false); |
| 133 | }), |
| 134 | { |
| 135 | tags: ["provider:aws", "provider:aws:ecs", "live"], |
| 136 | exclusive: "aws:ecs-service-linked-role", |
| 137 | timeout: 240_000, |
| 138 | }, |
| 139 | ); |