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 { 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 { State } from "alchemy/State/State"; |
| 7 | import * as Data from "effect/Data"; |
| 8 | import * as Effect from "effect/Effect"; |
| 9 | import { MinimumLogLevel } from "effect/References"; |
| 10 | import * as Schedule from "effect/Schedule"; |
| 11 | import { driftActions, repairDrift, waitForDrift } from "../drift.ts"; |
| 12 | import * as Test from "./VpcTest.ts"; |
| 13 | |
| 14 | const { test } = Test.make({ providers: AWS.providers() }); |
| 15 | |
| 16 | const logLevel = Effect.provideService(MinimumLogLevel, process.env.DEBUG ? "Debug" : "Info"); |
| 17 | |
| 18 | test.provider( |
| 19 | "create, update, delete vpc", |
| 20 | (stack) => |
| 21 | Effect.gen(function* () { |
| 22 | yield* stack.destroy(); |
| 23 | |
| 24 | const vpc = yield* stack.deploy( |
| 25 | Effect.gen(function* () { |
| 26 | return yield* Vpc("TestVpc", { |
| 27 | cidrBlock: "10.0.0.0/16", |
| 28 | enableDnsSupport: true, |
| 29 | enableDnsHostnames: true, |
| 30 | }); |
| 31 | }), |
| 32 | ); |
| 33 | |
| 34 | const actualVpc = yield* EC2.describeVpcs({ |
| 35 | VpcIds: [vpc.vpcId], |
| 36 | }); |
| 37 | expect(actualVpc.Vpcs?.[0]?.VpcId).toEqual(vpc.vpcId); |
| 38 | expect(actualVpc.Vpcs?.[0]?.CidrBlock).toEqual("10.0.0.0/16"); |
| 39 | expect(actualVpc.Vpcs?.[0]?.State).toEqual("available"); |
| 40 | |
| 41 | yield* expectVpcAttribute({ |
| 42 | VpcId: vpc.vpcId, |
| 43 | Attribute: "enableDnsSupport", |
| 44 | Value: true, |
| 45 | }); |
| 46 | |
| 47 | yield* expectVpcAttribute({ |
| 48 | VpcId: vpc.vpcId, |
| 49 | Attribute: "enableDnsHostnames", |
| 50 | Value: true, |
| 51 | }); |
| 52 | |
| 53 | // Update VPC attributes |
| 54 | const updatedVpc = yield* stack.deploy( |
| 55 | Effect.gen(function* () { |
| 56 | return yield* Vpc("TestVpc", { |
| 57 | cidrBlock: "10.0.0.0/16", |
| 58 | enableDnsSupport: false, |
| 59 | enableDnsHostnames: false, |
| 60 | }); |
| 61 | }), |
| 62 | ); |
| 63 | |
| 64 | yield* expectVpcAttribute({ |
| 65 | VpcId: updatedVpc.vpcId, |
| 66 | Attribute: "enableDnsSupport", |
| 67 | Value: false, |
| 68 | }); |
| 69 | |
| 70 | yield* expectVpcAttribute({ |
| 71 | VpcId: updatedVpc.vpcId, |
| 72 | Attribute: "enableDnsHostnames", |
| 73 | Value: false, |
| 74 | }); |
| 75 | |
| 76 | yield* stack.destroy(); |
| 77 | |
| 78 | yield* assertVpcDeleted(vpc.vpcId); |
| 79 | }).pipe(logLevel), |
| 80 | { tags: ["provider:aws", "provider:aws:ec2", "live"], timeout: 15 * 60_000 }, |
| 81 | ); |
| 82 | |
| 83 | test.provider( |
| 84 | "list enumerates the deployed vpc", |
| 85 | (stack) => |
| 86 | Effect.gen(function* () { |
| 87 | yield* stack.destroy(); |
| 88 | |
| 89 | const deployed = yield* stack.deploy( |
| 90 | Effect.gen(function* () { |
| 91 | return yield* Vpc("ListVpc", { |
| 92 | cidrBlock: "10.0.0.0/16", |
| 93 | }); |
| 94 | }), |
| 95 | ); |
| 96 | |
| 97 | const provider = yield* Provider.findProvider(Vpc); |
| 98 | const all = yield* provider.list(); |
| 99 | |
| 100 | expect(all.some((v) => v.vpcId === deployed.vpcId)).toBe(true); |
| 101 | |
| 102 | yield* stack.destroy(); |
| 103 | |
| 104 | yield* assertVpcDeleted(deployed.vpcId); |
| 105 | }).pipe(logLevel), |
| 106 | { tags: ["provider:aws", "provider:aws:ec2", "live"], timeout: 15 * 60_000 }, |
| 107 | ); |
| 108 | |
| 109 | test.provider( |
| 110 | "recovers a created vpc whose output was not persisted", |
| 111 | (stack) => |
| 112 | Effect.gen(function* () { |
| 113 | yield* stack.destroy(); |
| 114 | |
| 115 | const program = Effect.gen(function* () { |
| 116 | return yield* Vpc("RecoveredVpc", { |
| 117 | cidrBlock: "10.41.0.0/16", |
| 118 | }); |
| 119 | }); |
| 120 | |
| 121 | const first = yield* stack.deploy(program); |
| 122 | |
| 123 | // Simulate a deploy killed after CreateVpc returned: the row is left in |
| 124 | // `creating` with no attributes, but the VPC (and its tags) exist. |
| 125 | const state = yield* yield* State; |
| 126 | const key = { stack: stack.name, stage: stack.stage, fqn: "RecoveredVpc" }; |
| 127 | const row = yield* state.get(key); |
| 128 | if (row?.status !== "created") { |
| 129 | return yield* Effect.die(new Error("Expected a created VPC row")); |
| 130 | } |
| 131 | const { attr: _attr, ...rest } = row; |
| 132 | yield* state.set({ ...key, value: { ...rest, status: "creating" } }); |
| 133 | |
| 134 | const recovered = yield* stack.deploy(program); |
| 135 | expect(recovered.vpcId).toBe(first.vpcId); |
| 136 | |
| 137 | const owned = yield* EC2.describeVpcs({ |
| 138 | Filters: [ |
| 139 | { Name: "tag:alchemy::stack", Values: [stack.name] }, |
| 140 | { Name: "tag:alchemy::stage", Values: [stack.stage] }, |
| 141 | { Name: "tag:alchemy::id", Values: ["RecoveredVpc"] }, |
| 142 | ], |
| 143 | }); |
| 144 | expect(owned.Vpcs?.map((vpc) => vpc.VpcId)).toEqual([first.vpcId]); |
| 145 | |
| 146 | yield* stack.destroy(); |
| 147 | |
| 148 | yield* assertVpcDeleted(first.vpcId); |
| 149 | }).pipe(logLevel), |
| 150 | { tags: ["provider:aws", "provider:aws:ec2", "live"], timeout: 15 * 60_000 }, |
| 151 | ); |
| 152 | |
| 153 | const expectVpcAttribute = Effect.fn(function* (props: { |
| 154 | VpcId: string; |
| 155 | Attribute: EC2.VpcAttributeName; |
| 156 | Value: boolean; |
| 157 | }) { |
| 158 | yield* EC2.describeVpcAttribute({ |
| 159 | VpcId: props.VpcId, |
| 160 | Attribute: props.Attribute, |
| 161 | }).pipe( |
| 162 | Effect.tap(Effect.logDebug), |
| 163 | Effect.flatMap((result: any) => |
| 164 | result[`${props.Attribute[0].toUpperCase()}${props.Attribute.slice(1)}`]?.Value === |
| 165 | props.Value |
| 166 | ? Effect.succeed(result) |
| 167 | : Effect.fail(new VpcAttributeStale()), |
| 168 | ), |
| 169 | Effect.retry({ |
| 170 | while: (e) => e._tag === "VpcAttributeStale", |
| 171 | schedule: Schedule.max([Schedule.exponential(100), Schedule.recurs(8)]), |
| 172 | }), |
| 173 | ); |
| 174 | }); |
| 175 | |
| 176 | class VpcAttributeStale extends Data.TaggedError("VpcAttributeStale") {} |
| 177 | |
| 178 | class VpcStillExists extends Data.TaggedError("VpcStillExists") {} |
| 179 | |
| 180 | export const assertVpcDeleted = Effect.fn(function* (vpcId: string) { |
| 181 | yield* EC2.describeVpcs({ |
| 182 | VpcIds: [vpcId], |
| 183 | }).pipe( |
| 184 | Effect.flatMap(() => Effect.fail(new VpcStillExists())), |
| 185 | Effect.retry({ |
| 186 | while: (e) => e._tag === "VpcStillExists", |
| 187 | schedule: Schedule.max([Schedule.exponential(100), Schedule.recurs(8)]), |
| 188 | }), |
| 189 | Effect.catchTag("InvalidVpcID.NotFound", () => Effect.void), |
| 190 | ); |
| 191 | }); |
| 192 | |
| 193 | // #2688: the VPC networking providers had no `read`, so drift skipped them. |
| 194 | // Every one must read a fresh deploy as `unchanged` and report out-of-band |
| 195 | // changes to the settings it manages. |
| 196 | test.provider( |
| 197 | "drift detects and repairs out-of-band networking changes", |
| 198 | (stack) => |
| 199 | Effect.gen(function* () { |
| 200 | yield* stack.destroy(); |
| 201 | |
| 202 | const net = yield* stack.deploy( |
| 203 | Effect.gen(function* () { |
| 204 | const vpc = yield* Vpc("DriftVpc", { |
| 205 | cidrBlock: "10.42.0.0/16", |
| 206 | enableDnsSupport: true, |
| 207 | enableDnsHostnames: true, |
| 208 | }); |
| 209 | const subnet = yield* AWS.EC2.Subnet("DriftSubnet", { |
| 210 | vpcId: vpc.vpcId, |
| 211 | cidrBlock: "10.42.1.0/24", |
| 212 | mapPublicIpOnLaunch: true, |
| 213 | }); |
| 214 | const igw = yield* AWS.EC2.InternetGateway("DriftIgw", { vpcId: vpc.vpcId }); |
| 215 | const routeTable = yield* AWS.EC2.RouteTable("DriftRouteTable", { |
| 216 | vpcId: vpc.vpcId, |
| 217 | tags: { team: "declared" }, |
| 218 | }); |
| 219 | yield* AWS.EC2.Route("DriftRoute", { |
| 220 | routeTableId: routeTable.routeTableId, |
| 221 | destinationCidrBlock: "0.0.0.0/0", |
| 222 | gatewayId: igw.internetGatewayId, |
| 223 | }); |
| 224 | const association = yield* AWS.EC2.RouteTableAssociation("DriftAssociation", { |
| 225 | routeTableId: routeTable.routeTableId, |
| 226 | subnetId: subnet.subnetId, |
| 227 | }); |
| 228 | const sg = yield* AWS.EC2.SecurityGroup("DriftSg", { |
| 229 | vpcId: vpc.vpcId, |
| 230 | description: "drift", |
| 231 | tags: { team: "declared" }, |
| 232 | }); |
| 233 | return { vpc, subnet, igw, routeTable, association, sg }; |
| 234 | }), |
| 235 | ); |
| 236 | const allUnchanged = (actions: Record<string, string | undefined>) => |
| 237 | Object.values(actions).every((action) => action === "unchanged"); |
| 238 | expect(allUnchanged(yield* driftActions(stack))).toBe(true); |
| 239 | |
| 240 | yield* EC2.modifyVpcAttribute({ |
| 241 | VpcId: net.vpc.vpcId, |
| 242 | EnableDnsHostnames: { Value: false }, |
| 243 | }); |
| 244 | yield* EC2.modifySubnetAttribute({ |
| 245 | SubnetId: net.subnet.subnetId, |
| 246 | MapPublicIpOnLaunch: { Value: false }, |
| 247 | }); |
| 248 | yield* EC2.createTags({ |
| 249 | Resources: [net.routeTable.routeTableId, net.sg.groupId], |
| 250 | Tags: [{ Key: "team", Value: "out-of-band" }], |
| 251 | }); |
| 252 | yield* EC2.deleteRoute({ |
| 253 | RouteTableId: net.routeTable.routeTableId, |
| 254 | DestinationCidrBlock: "0.0.0.0/0", |
| 255 | }); |
| 256 | |
| 257 | expect((yield* waitForDrift(stack, "DriftVpc", "drifted"))?.attr?.enableDnsHostnames).toBe( |
| 258 | false, |
| 259 | ); |
| 260 | expect( |
| 261 | (yield* waitForDrift(stack, "DriftSubnet", "drifted"))?.attr?.mapPublicIpOnLaunch, |
| 262 | ).toBe(false); |
| 263 | expect((yield* waitForDrift(stack, "DriftRouteTable", "drifted"))?.attr?.tags?.team).toBe( |
| 264 | "out-of-band", |
| 265 | ); |
| 266 | expect((yield* waitForDrift(stack, "DriftSg", "drifted"))?.attr?.tags?.team).toBe( |
| 267 | "out-of-band", |
| 268 | ); |
| 269 | expect((yield* waitForDrift(stack, "DriftRoute", "missing"))?.action).toBe("missing"); |
| 270 | |
| 271 | yield* repairDrift(stack); |
| 272 | const after = yield* driftActions(stack).pipe( |
| 273 | Effect.repeat({ until: allUnchanged, schedule: Schedule.spaced("2 seconds"), times: 15 }), |
| 274 | ); |
| 275 | expect(allUnchanged(after)).toBe(true); |
| 276 | yield* expectVpcAttribute({ |
| 277 | VpcId: net.vpc.vpcId, |
| 278 | Attribute: "enableDnsHostnames", |
| 279 | Value: true, |
| 280 | }); |
| 281 | const subnet = yield* EC2.describeSubnets({ SubnetIds: [net.subnet.subnetId] }); |
| 282 | expect(subnet.Subnets?.[0]?.MapPublicIpOnLaunch).toBe(true); |
| 283 | const routes = yield* EC2.describeRouteTables({ |
| 284 | RouteTableIds: [net.routeTable.routeTableId], |
| 285 | }); |
| 286 | expect( |
| 287 | routes.RouteTables?.[0]?.Routes?.find((r) => r.DestinationCidrBlock === "0.0.0.0/0") |
| 288 | ?.GatewayId, |
| 289 | ).toBe(net.igw.internetGatewayId); |
| 290 | |
| 291 | yield* stack.destroy(); |
| 292 | yield* assertVpcDeleted(net.vpc.vpcId); |
| 293 | }).pipe(logLevel), |
| 294 | { tags: ["provider:aws", "provider:aws:ec2", "live"], timeout: 420_000 }, |
| 295 | ); |