| 1 | import * as AWS from "@/AWS"; |
| 2 | import { CapacityProvider } from "@/AWS/ECS"; |
| 3 | import * as Test from "@/Test/Alchemy"; |
| 4 | import * as ecs from "@distilled.cloud/aws/ecs"; |
| 5 | import { expect } from "alchemy-test"; |
| 6 | import * as Effect from "effect/Effect"; |
| 7 | import { MinimumLogLevel } from "effect/References"; |
| 8 | |
| 9 | const { test } = Test.make({ providers: AWS.providers() }); |
| 10 | |
| 11 | const logLevel = Effect.provideService( |
| 12 | MinimumLogLevel, |
| 13 | process.env.DEBUG ? "Debug" : "Info", |
| 14 | ); |
| 15 | |
| 16 | // Skipped by default: requires a pre-provisioned EC2 Auto Scaling Group ARN |
| 17 | // (set via TEST_ASG_ARN) because AutoScalingGroup/LaunchTemplate currently |
| 18 | // have no test infrastructure in this repo. Run locally with: |
| 19 | // TEST_ASG_ARN=arn:aws:autoscaling:... bun vitest CapacityProvider |
| 20 | test.provider.skipIf(!process.env.TEST_ASG_ARN)( |
| 21 | "create, update, delete ECS capacity provider", |
| 22 | (stack) => |
| 23 | Effect.gen(function* () { |
| 24 | const autoScalingGroupArn = process.env.TEST_ASG_ARN!; |
| 25 | yield* stack.destroy(); |
| 26 | |
| 27 | const provider = yield* stack.deploy( |
| 28 | CapacityProvider("TestCapacityProvider", { |
| 29 | autoScalingGroupArn, |
| 30 | managedScaling: { |
| 31 | status: "ENABLED", |
| 32 | targetCapacity: 80, |
| 33 | minimumScalingStepSize: 1, |
| 34 | maximumScalingStepSize: 10, |
| 35 | }, |
| 36 | managedTerminationProtection: "DISABLED", |
| 37 | tags: { env: "test" }, |
| 38 | }), |
| 39 | ); |
| 40 | |
| 41 | expect(provider.capacityProviderArn).toMatch( |
| 42 | /^arn:aws:ecs:[^:]+:\d+:capacity-provider\//, |
| 43 | ); |
| 44 | expect(provider.status).toEqual("ACTIVE"); |
| 45 | expect(provider.managedScaling?.targetCapacity).toEqual(80); |
| 46 | expect(provider.tags.env).toEqual("test"); |
| 47 | |
| 48 | const described = yield* ecs.describeCapacityProviders({ |
| 49 | capacityProviders: [provider.name], |
| 50 | include: ["TAGS"], |
| 51 | }); |
| 52 | const found = described.capacityProviders?.[0]; |
| 53 | expect(found?.name).toEqual(provider.name); |
| 54 | expect( |
| 55 | found?.autoScalingGroupProvider?.managedScaling?.targetCapacity, |
| 56 | ).toEqual(80); |
| 57 | |
| 58 | const updated = yield* stack.deploy( |
| 59 | CapacityProvider("TestCapacityProvider", { |
| 60 | autoScalingGroupArn, |
| 61 | managedScaling: { |
| 62 | status: "ENABLED", |
| 63 | targetCapacity: 60, |
| 64 | minimumScalingStepSize: 1, |
| 65 | maximumScalingStepSize: 5, |
| 66 | }, |
| 67 | managedTerminationProtection: "DISABLED", |
| 68 | tags: { env: "test", owner: "alchemy" }, |
| 69 | }), |
| 70 | ); |
| 71 | |
| 72 | expect(updated.managedScaling?.targetCapacity).toEqual(60); |
| 73 | expect(updated.tags.owner).toEqual("alchemy"); |
| 74 | |
| 75 | yield* stack.destroy(); |
| 76 | |
| 77 | const afterDestroy = yield* ecs.describeCapacityProviders({ |
| 78 | capacityProviders: [provider.name], |
| 79 | }); |
| 80 | expect(afterDestroy.capacityProviders ?? []).toHaveLength(0); |
| 81 | }).pipe(logLevel), |
| 82 | { tags: ["provider:aws", "provider:aws:ecs", "live"], timeout: 600_000 }, |
| 83 | ); |