| 1 | import * as AWS from "@/AWS"; |
| 2 | import { Subnet } from "@/AWS/EC2"; |
| 3 | import { LoadBalancer } from "@/AWS/ELBv2"; |
| 4 | import * as Provider from "@/Provider"; |
| 5 | import * as Test from "@/Test/Alchemy"; |
| 6 | import * as EC2 from "@distilled.cloud/aws/ec2"; |
| 7 | import * as elbv2 from "@distilled.cloud/aws/elastic-load-balancing-v2"; |
| 8 | import { expect } from "alchemy-test"; |
| 9 | import * as Effect from "effect/Effect"; |
| 10 | import { MinimumLogLevel } from "effect/References"; |
| 11 | import { getDefaultVpc } from "../DefaultVpc.ts"; |
| 12 | |
| 13 | const { test } = Test.make({ providers: AWS.providers() }); |
| 14 | |
| 15 | const logLevel = Effect.provideService( |
| 16 | MinimumLogLevel, |
| 17 | process.env.DEBUG ? "Debug" : "Info", |
| 18 | ); |
| 19 | |
| 20 | // Reuse the account/region default VPC and carve stack-owned subnets (subnets |
| 21 | // don't count against the VPC limit). An ALB needs subnets in at least two AZs. |
| 22 | test.provider( |
| 23 | "list enumerates the deployed load balancer", |
| 24 | (stack) => |
| 25 | Effect.gen(function* () { |
| 26 | yield* stack.destroy(); |
| 27 | |
| 28 | const azResult = yield* EC2.describeAvailabilityZones({}); |
| 29 | const azs = |
| 30 | azResult.AvailabilityZones?.filter( |
| 31 | (az) => az.State === "available", |
| 32 | ).flatMap((az) => (az.ZoneName ? [az.ZoneName] : [])) ?? []; |
| 33 | const [az1, az2] = azs; |
| 34 | expect(az1).toBeTruthy(); |
| 35 | expect(az2).toBeTruthy(); |
| 36 | |
| 37 | const defaultVpc = yield* getDefaultVpc; |
| 38 | |
| 39 | const deployed = yield* stack.deploy( |
| 40 | Effect.gen(function* () { |
| 41 | const subnet1 = yield* Subnet("ListLbSubnet1", { |
| 42 | vpcId: defaultVpc.vpcId, |
| 43 | cidrBlock: defaultVpc.subnetCidrBlock(222), |
| 44 | availabilityZone: az1, |
| 45 | }); |
| 46 | |
| 47 | const subnet2 = yield* Subnet("ListLbSubnet2", { |
| 48 | vpcId: defaultVpc.vpcId, |
| 49 | cidrBlock: defaultVpc.subnetCidrBlock(223), |
| 50 | availabilityZone: az2, |
| 51 | }); |
| 52 | |
| 53 | const loadBalancer = yield* LoadBalancer("ListLb", { |
| 54 | subnets: [subnet1.subnetId, subnet2.subnetId], |
| 55 | scheme: "internal", |
| 56 | type: "application", |
| 57 | }); |
| 58 | |
| 59 | return { loadBalancer }; |
| 60 | }), |
| 61 | ); |
| 62 | |
| 63 | const provider = yield* Provider.findProvider(LoadBalancer); |
| 64 | const all = yield* provider.list(); |
| 65 | |
| 66 | expect( |
| 67 | all.some( |
| 68 | (x) => x.loadBalancerArn === deployed.loadBalancer.loadBalancerArn, |
| 69 | ), |
| 70 | ).toBe(true); |
| 71 | |
| 72 | yield* stack.destroy(); |
| 73 | |
| 74 | // Out-of-band: the load balancer is gone after destroy. |
| 75 | const after = yield* elbv2 |
| 76 | .describeLoadBalancers({ |
| 77 | LoadBalancerArns: [deployed.loadBalancer.loadBalancerArn], |
| 78 | }) |
| 79 | .pipe( |
| 80 | Effect.map((r) => r.LoadBalancers?.length ?? 0), |
| 81 | Effect.catchTag("LoadBalancerNotFoundException", () => |
| 82 | Effect.succeed(0), |
| 83 | ), |
| 84 | ); |
| 85 | expect(after).toBe(0); |
| 86 | }).pipe(logLevel), |
| 87 | { |
| 88 | tags: ["provider:aws", "provider:aws:ec2", "provider:aws:elbv2", "live"], |
| 89 | timeout: 600_000, |
| 90 | }, |
| 91 | ); |