The best way to control Claude Code, Codex and more from any computer or phone
Mirrored from https://github.com/Camo67/t3code.git
Synced Nov 21, 06:00 UTC
| 1 | import * as AWS from "@alchemy.run/aws"; |
| 2 | import { EIP, InternetGateway, NatGateway, Subnet, Vpc } from "@alchemy.run/aws/EC2"; |
| 3 | import { expect } from "@alchemy.run/test"; |
| 4 | import * as EC2 from "@distilled.cloud/aws/ec2"; |
| 5 | import * as Provider from "alchemy/Provider"; |
| 6 | import * as Effect from "effect/Effect"; |
| 7 | import { MinimumLogLevel } from "effect/References"; |
| 8 | import { assertEipGone, assertVpcGone } from "./Gone.ts"; |
| 9 | import * as Test from "./VpcTest.ts"; |
| 10 | |
| 11 | const { test } = Test.make({ providers: AWS.providers() }); |
| 12 | |
| 13 | const logLevel = Effect.provideService(MinimumLogLevel, process.env.DEBUG ? "Debug" : "Info"); |
| 14 | |
| 15 | // NAT gateways take ~1–2 min to become `available` and ~1–2 min to delete, so a |
| 16 | // full create + list + destroy cycle exceeds the 240s factory test budget. |
| 17 | // They also require a free VPC quota slot and an Internet Gateway attached to |
| 18 | // the VPC (a public NAT gateway fails with `Gateway.NotAttached` otherwise). |
| 19 | // Gate behind AWS_TEST_NAT_GATEWAY=1 so an account with headroom runs it |
| 20 | // unchanged; the list() implementation itself is exercised by this body. |
| 21 | test.provider.skipIf(!process.env.AWS_TEST_NAT_GATEWAY)( |
| 22 | "list enumerates the deployed NAT Gateway", |
| 23 | (stack) => |
| 24 | Effect.gen(function* () { |
| 25 | yield* stack.destroy(); |
| 26 | |
| 27 | const azResult = yield* EC2.describeAvailabilityZones({}); |
| 28 | const az1 = azResult.AvailabilityZones?.find((az) => az.State === "available")?.ZoneName!; |
| 29 | |
| 30 | // Phase 1: stand up the VPC + IGW + subnet + EIP. The IGW must be |
| 31 | // attached before the NAT gateway is created, so split the deploy. |
| 32 | yield* stack.deploy( |
| 33 | Effect.gen(function* () { |
| 34 | const vpc = yield* Vpc("ListNatVpc", { |
| 35 | cidrBlock: "10.0.0.0/16", |
| 36 | }); |
| 37 | |
| 38 | yield* InternetGateway("ListNatIgw", { |
| 39 | vpcId: vpc.vpcId, |
| 40 | }); |
| 41 | |
| 42 | yield* Subnet("ListNatSubnet", { |
| 43 | vpcId: vpc.vpcId, |
| 44 | cidrBlock: "10.0.1.0/24", |
| 45 | availabilityZone: az1, |
| 46 | mapPublicIpOnLaunch: true, |
| 47 | }); |
| 48 | |
| 49 | yield* EIP("ListNatEip", {}); |
| 50 | }), |
| 51 | ); |
| 52 | |
| 53 | // Phase 2: add the NAT gateway now that the IGW is attached. |
| 54 | const deployed = yield* stack.deploy( |
| 55 | Effect.gen(function* () { |
| 56 | const vpc = yield* Vpc("ListNatVpc", { |
| 57 | cidrBlock: "10.0.0.0/16", |
| 58 | }); |
| 59 | |
| 60 | const igw = yield* InternetGateway("ListNatIgw", { |
| 61 | vpcId: vpc.vpcId, |
| 62 | }); |
| 63 | |
| 64 | const subnet = yield* Subnet("ListNatSubnet", { |
| 65 | vpcId: vpc.vpcId, |
| 66 | cidrBlock: "10.0.1.0/24", |
| 67 | availabilityZone: az1, |
| 68 | mapPublicIpOnLaunch: true, |
| 69 | }); |
| 70 | |
| 71 | const eip = yield* EIP("ListNatEip", {}); |
| 72 | |
| 73 | const natGateway = yield* NatGateway("ListNatGateway", { |
| 74 | subnetId: subnet.subnetId, |
| 75 | allocationId: eip.allocationId, |
| 76 | }); |
| 77 | |
| 78 | return { vpc, eip, igw, natGateway }; |
| 79 | }), |
| 80 | ); |
| 81 | |
| 82 | const provider = yield* Provider.findProvider(NatGateway); |
| 83 | const all = yield* provider.list(); |
| 84 | |
| 85 | expect(all.some((x) => x.natGatewayId === deployed.natGateway.natGatewayId)).toBe(true); |
| 86 | |
| 87 | yield* stack.destroy(); |
| 88 | |
| 89 | // Zero-orphan proof: the EIP is released and the VPC (which cannot |
| 90 | // delete while the NAT gateway/subnet/IGW exist) is gone. |
| 91 | yield* assertEipGone(deployed.eip.allocationId); |
| 92 | yield* assertVpcGone(deployed.vpc.vpcId); |
| 93 | }).pipe(logLevel), |
| 94 | { tags: ["provider:aws", "provider:aws:ec2", "live"], timeout: 900_000 }, |
| 95 | ); |
| 96 | |
| 97 | test.provider.skipIf(!!process.env.FAST)( |
| 98 | "regional NAT gateways in automatic and manual mode", |
| 99 | (stack) => |
| 100 | Effect.gen(function* () { |
| 101 | yield* stack.destroy(); |
| 102 | |
| 103 | const azResult = yield* EC2.describeAvailabilityZones({}); |
| 104 | const [az1, az2] = (azResult.AvailabilityZones ?? []) |
| 105 | .filter((az) => az.State === "available" && az.ZoneType === "availability-zone") |
| 106 | .map((az) => az.ZoneName!); |
| 107 | |
| 108 | const network = Effect.gen(function* () { |
| 109 | const vpc = yield* Vpc("RegionalNatVpc", { |
| 110 | cidrBlock: "10.0.0.0/16", |
| 111 | }); |
| 112 | const igw = yield* InternetGateway("RegionalNatIgw", { |
| 113 | vpcId: vpc.vpcId, |
| 114 | }); |
| 115 | const eip1 = yield* EIP("RegionalNatEip1", {}); |
| 116 | const eip2 = yield* EIP("RegionalNatEip2", {}); |
| 117 | return { vpc, igw, eip1, eip2 }; |
| 118 | }); |
| 119 | |
| 120 | // A regional NAT gateway needs the IGW attached before it is created. |
| 121 | yield* stack.deploy(network); |
| 122 | |
| 123 | const regional = (layout: "one-zone" | "two-zones" | "moved") => |
| 124 | Effect.gen(function* () { |
| 125 | const { vpc, igw, eip1, eip2 } = yield* network; |
| 126 | const autoNat = yield* NatGateway("RegionalAutoNat", { |
| 127 | availabilityMode: "regional", |
| 128 | vpcId: vpc.vpcId, |
| 129 | }); |
| 130 | const manualNat = yield* NatGateway("RegionalManualNat", { |
| 131 | availabilityMode: "regional", |
| 132 | vpcId: vpc.vpcId, |
| 133 | availabilityZoneAddresses: |
| 134 | layout === "one-zone" |
| 135 | ? [{ availabilityZone: az1, allocationIds: [eip1.allocationId] }] |
| 136 | : layout === "two-zones" |
| 137 | ? [ |
| 138 | { availabilityZone: az1, allocationIds: [eip1.allocationId] }, |
| 139 | { availabilityZone: az2, allocationIds: [eip2.allocationId] }, |
| 140 | ] |
| 141 | : [ |
| 142 | { |
| 143 | availabilityZone: az1, |
| 144 | allocationIds: [eip1.allocationId, eip2.allocationId], |
| 145 | }, |
| 146 | ], |
| 147 | maxDrainDurationSeconds: 1, |
| 148 | }); |
| 149 | return { vpc, igw, eip1, eip2, autoNat, manualNat }; |
| 150 | }); |
| 151 | |
| 152 | const describe = (natGatewayId: string) => |
| 153 | EC2.describeNatGateways({ NatGatewayIds: [natGatewayId] }).pipe( |
| 154 | Effect.map((r) => r.NatGateways![0]!), |
| 155 | ); |
| 156 | |
| 157 | const created = yield* stack.deploy(regional("one-zone")); |
| 158 | |
| 159 | const auto = yield* describe(created.autoNat.natGatewayId); |
| 160 | expect(auto.AvailabilityMode).toBe("regional"); |
| 161 | expect(auto.VpcId).toBe(created.vpc.vpcId); |
| 162 | expect(auto.SubnetId).toBeUndefined(); |
| 163 | expect(auto.AutoProvisionZones).toBe("enabled"); |
| 164 | expect(created.autoNat.availabilityMode).toBe("regional"); |
| 165 | |
| 166 | const manual = yield* describe(created.manualNat.natGatewayId); |
| 167 | expect(manual.AvailabilityMode).toBe("regional"); |
| 168 | expect(manual.AutoProvisionZones).toBe("disabled"); |
| 169 | expect( |
| 170 | manual.NatGatewayAddresses?.find((a) => a.AllocationId === created.eip1.allocationId) |
| 171 | ?.AvailabilityZone, |
| 172 | ).toBe(az1); |
| 173 | |
| 174 | // Adding a zone associates its Elastic IP in place. |
| 175 | const updated = yield* stack.deploy(regional("two-zones")); |
| 176 | expect(updated.manualNat.natGatewayId).toBe(created.manualNat.natGatewayId); |
| 177 | const expanded = yield* describe(updated.manualNat.natGatewayId); |
| 178 | const addresses = (expanded.NatGatewayAddresses ?? []).filter( |
| 179 | (a) => a.Status !== "disassociating" && a.Status !== "failed", |
| 180 | ); |
| 181 | expect( |
| 182 | addresses.find((a) => a.AllocationId === created.eip1.allocationId)?.AvailabilityZone, |
| 183 | ).toBe(az1); |
| 184 | expect( |
| 185 | addresses.find((a) => a.AllocationId === created.eip2.allocationId)?.AvailabilityZone, |
| 186 | ).toBe(az2); |
| 187 | // The deploy waits for the association to settle. |
| 188 | expect(addresses.find((a) => a.AllocationId === created.eip2.allocationId)?.Status).toBe( |
| 189 | "succeeded", |
| 190 | ); |
| 191 | |
| 192 | // Moving an Elastic IP to another zone disassociates it, waits for the |
| 193 | // (short) drain, then associates it in the new zone — in place. |
| 194 | const moved = yield* stack.deploy(regional("moved")); |
| 195 | expect(moved.manualNat.natGatewayId).toBe(created.manualNat.natGatewayId); |
| 196 | const contracted = yield* describe(moved.manualNat.natGatewayId); |
| 197 | const live = (contracted.NatGatewayAddresses ?? []).filter( |
| 198 | (a) => a.Status !== "disassociating" && a.Status !== "failed", |
| 199 | ); |
| 200 | expect( |
| 201 | live |
| 202 | .filter((a) => a.AllocationId === created.eip2.allocationId) |
| 203 | .map((a) => a.AvailabilityZone), |
| 204 | ).toEqual([az1]); |
| 205 | |
| 206 | yield* stack.destroy(); |
| 207 | |
| 208 | yield* assertEipGone(created.eip1.allocationId); |
| 209 | yield* assertEipGone(created.eip2.allocationId); |
| 210 | yield* assertVpcGone(created.vpc.vpcId); |
| 211 | }).pipe(logLevel), |
| 212 | { tags: ["provider:aws", "provider:aws:ec2", "live"], timeout: 900_000 }, |
| 213 | ); |