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 { ClientVpnEndpoint } from "@alchemy.run/aws/EC2/ClientVpnEndpoint"; |
| 3 | import { ClientVpnRoute, type ClientVpnRouteProps } from "@alchemy.run/aws/EC2/ClientVpnRoute"; |
| 4 | import { ClientVpnTargetNetworkAssociation } from "@alchemy.run/aws/EC2/ClientVpnTargetNetworkAssociation"; |
| 5 | import { describe, expect } from "@alchemy.run/test"; |
| 6 | import * as Test from "@alchemy.run/test/Alchemy"; |
| 7 | import * as ec2 from "@distilled.cloud/aws/ec2"; |
| 8 | import * as Alchemy from "alchemy"; |
| 9 | import * as Provider from "alchemy/Provider"; |
| 10 | import * as Effect from "effect/Effect"; |
| 11 | import { |
| 12 | assertClientVpnCertificateDeleted, |
| 13 | assertClientVpnRouteDeleted, |
| 14 | clientVpnAvailabilityZones, |
| 15 | clientVpnEndpointProps, |
| 16 | clientVpnTestTimeout, |
| 17 | clientVpnNetwork, |
| 18 | importClientVpnCertificate, |
| 19 | readClientVpnRoutes, |
| 20 | readClientVpnTargetNetworks, |
| 21 | waitForClientVpn, |
| 22 | } from "./fixtures/client-vpn.ts"; |
| 23 | |
| 24 | describe( |
| 25 | "Client VPN routes", |
| 26 | { tags: ["provider:aws", "provider:aws:acm", "provider:aws:ec2", "live"] }, |
| 27 | () => { |
| 28 | const { test, beforeAll, afterAll, deploy, destroy } = Test.make({ |
| 29 | providers: AWS.providers(), |
| 30 | }); |
| 31 | const certificate = beforeAll(importClientVpnCertificate("ClientVpnRoutePrerequisites")); |
| 32 | const zones = beforeAll(clientVpnAvailabilityZones); |
| 33 | const Stack = Alchemy.Stack( |
| 34 | "ClientVpnRoutePrerequisites", |
| 35 | { providers: AWS.providers(), state: Alchemy.localState() }, |
| 36 | Effect.gen(function* () { |
| 37 | const certificateArn = yield* certificate; |
| 38 | const network = yield* clientVpnNetwork(yield* zones); |
| 39 | const endpoint = yield* ClientVpnEndpoint("Endpoint", { |
| 40 | ...clientVpnEndpointProps(certificateArn), |
| 41 | vpcId: network.vpc.vpcId, |
| 42 | splitTunnel: true, |
| 43 | }); |
| 44 | const firstAssociation = yield* ClientVpnTargetNetworkAssociation("FirstAssociation", { |
| 45 | clientVpnEndpointId: endpoint.clientVpnEndpointId, |
| 46 | subnetId: network.firstSubnet.subnetId, |
| 47 | }); |
| 48 | const secondAssociation = yield* ClientVpnTargetNetworkAssociation("SecondAssociation", { |
| 49 | clientVpnEndpointId: endpoint.clientVpnEndpointId, |
| 50 | subnetId: network.secondSubnet.subnetId, |
| 51 | }); |
| 52 | return { ...network, endpoint, firstAssociation, secondAssociation }; |
| 53 | }), |
| 54 | ); |
| 55 | const prerequisites = beforeAll(deploy(Stack), { timeout: clientVpnTestTimeout }); |
| 56 | afterAll( |
| 57 | destroy(Stack).pipe( |
| 58 | Effect.andThen(certificate.pipe(Effect.flatMap(assertClientVpnCertificateDeleted))), |
| 59 | ), |
| 60 | { timeout: clientVpnTestTimeout }, |
| 61 | ); |
| 62 | |
| 63 | test.provider.skipIf(!!process.env.FAST)( |
| 64 | "creates, lists, replaces, repairs, and deletes Client VPN routes", |
| 65 | (stack) => |
| 66 | Effect.gen(function* () { |
| 67 | yield* stack.destroy(); |
| 68 | const { endpoint, firstSubnet, secondSubnet } = yield* prerequisites; |
| 69 | const clientVpnEndpointId = endpoint.clientVpnEndpointId; |
| 70 | yield* waitForClientVpn( |
| 71 | readClientVpnTargetNetworks(clientVpnEndpointId), |
| 72 | (networks) => |
| 73 | [firstSubnet.subnetId, secondSubnet.subnetId].every((subnetId) => |
| 74 | networks.some( |
| 75 | (network) => |
| 76 | network.TargetNetworkId === subnetId && network.Status?.Code === "associated", |
| 77 | ), |
| 78 | ), |
| 79 | "route target networks", |
| 80 | ); |
| 81 | const deployRoute = ( |
| 82 | destinationCidrBlock: string, |
| 83 | targetVpcSubnetId: ClientVpnRouteProps["targetVpcSubnetId"], |
| 84 | description?: string, |
| 85 | ) => |
| 86 | stack.deploy( |
| 87 | ClientVpnRoute("Route", { |
| 88 | clientVpnEndpointId, |
| 89 | destinationCidrBlock, |
| 90 | targetVpcSubnetId, |
| 91 | description, |
| 92 | }), |
| 93 | ); |
| 94 | const created = yield* deployRoute( |
| 95 | "192.168.10.0/24", |
| 96 | firstSubnet.subnetId, |
| 97 | "Initial route", |
| 98 | ); |
| 99 | expect(created.clientVpnEndpointId).toBe(clientVpnEndpointId); |
| 100 | expect(created.destinationCidrBlock).toBe("192.168.10.0/24"); |
| 101 | expect(created.targetVpcSubnetId).toBe(firstSubnet.subnetId); |
| 102 | const routes = yield* waitForClientVpn( |
| 103 | readClientVpnRoutes(clientVpnEndpointId), |
| 104 | (items) => |
| 105 | items.some( |
| 106 | (route) => |
| 107 | route.DestinationCidr === "192.168.10.0/24" && |
| 108 | route.TargetSubnet === firstSubnet.subnetId && |
| 109 | route.Status?.Code === "active", |
| 110 | ), |
| 111 | "active custom route", |
| 112 | ); |
| 113 | expect(routes).toContainEqual( |
| 114 | expect.objectContaining({ |
| 115 | DestinationCidr: "192.168.10.0/24", |
| 116 | TargetSubnet: firstSubnet.subnetId, |
| 117 | Description: "Initial route", |
| 118 | }), |
| 119 | ); |
| 120 | const provider = yield* Provider.findProvider(ClientVpnRoute); |
| 121 | const listed = yield* waitForClientVpn( |
| 122 | provider.list(), |
| 123 | (items) => |
| 124 | items.some( |
| 125 | (route) => |
| 126 | route.clientVpnEndpointId === clientVpnEndpointId && |
| 127 | route.destinationCidrBlock === "192.168.10.0/24" && |
| 128 | route.targetVpcSubnetId === firstSubnet.subnetId, |
| 129 | ), |
| 130 | "route provider list", |
| 131 | ); |
| 132 | expect(listed).toContainEqual( |
| 133 | expect.objectContaining({ |
| 134 | clientVpnEndpointId, |
| 135 | destinationCidrBlock: "192.168.10.0/24", |
| 136 | targetVpcSubnetId: firstSubnet.subnetId, |
| 137 | }), |
| 138 | ); |
| 139 | yield* deployRoute("192.168.10.0/24", firstSubnet.subnetId, "Initial route"); |
| 140 | expect( |
| 141 | (yield* readClientVpnRoutes(clientVpnEndpointId)).filter( |
| 142 | (route) => |
| 143 | route.DestinationCidr === "192.168.10.0/24" && |
| 144 | route.TargetSubnet === firstSubnet.subnetId && |
| 145 | route.Status?.Code === "active", |
| 146 | ), |
| 147 | ).toHaveLength(1); |
| 148 | |
| 149 | // AWS has no modify-route API; description changes replace the route at the same identity. |
| 150 | yield* deployRoute("192.168.10.0/24", firstSubnet.subnetId, "Updated route"); |
| 151 | yield* waitForClientVpn( |
| 152 | readClientVpnRoutes(clientVpnEndpointId), |
| 153 | (items) => |
| 154 | items.some( |
| 155 | (route) => |
| 156 | route.DestinationCidr === "192.168.10.0/24" && |
| 157 | route.TargetSubnet === firstSubnet.subnetId && |
| 158 | route.Description === "Updated route" && |
| 159 | route.Status?.Code === "active", |
| 160 | ), |
| 161 | "replaced route description", |
| 162 | ); |
| 163 | yield* deployRoute("192.168.11.0/24", firstSubnet.subnetId); |
| 164 | yield* assertClientVpnRouteDeleted( |
| 165 | clientVpnEndpointId, |
| 166 | "192.168.10.0/24", |
| 167 | firstSubnet.subnetId, |
| 168 | ); |
| 169 | const replaced = yield* deployRoute("192.168.11.0/24", secondSubnet.subnetId); |
| 170 | expect(replaced.targetVpcSubnetId).toBe(secondSubnet.subnetId); |
| 171 | yield* assertClientVpnRouteDeleted( |
| 172 | clientVpnEndpointId, |
| 173 | "192.168.11.0/24", |
| 174 | firstSubnet.subnetId, |
| 175 | ); |
| 176 | const replacement = yield* waitForClientVpn( |
| 177 | readClientVpnRoutes(clientVpnEndpointId), |
| 178 | (items) => |
| 179 | items.some( |
| 180 | (route) => |
| 181 | route.DestinationCidr === "192.168.11.0/24" && |
| 182 | route.TargetSubnet === secondSubnet.subnetId && |
| 183 | route.Status?.Code === "active", |
| 184 | ), |
| 185 | "replacement route target", |
| 186 | ); |
| 187 | expect( |
| 188 | replacement.find( |
| 189 | (route) => |
| 190 | route.DestinationCidr === "192.168.11.0/24" && |
| 191 | route.TargetSubnet === secondSubnet.subnetId, |
| 192 | )?.Description ?? "", |
| 193 | ).toBe(""); |
| 194 | |
| 195 | yield* ec2.deleteClientVpnRoute({ |
| 196 | ClientVpnEndpointId: clientVpnEndpointId, |
| 197 | DestinationCidrBlock: "192.168.11.0/24", |
| 198 | TargetVpcSubnetId: secondSubnet.subnetId, |
| 199 | }); |
| 200 | yield* assertClientVpnRouteDeleted( |
| 201 | clientVpnEndpointId, |
| 202 | "192.168.11.0/24", |
| 203 | secondSubnet.subnetId, |
| 204 | ); |
| 205 | yield* deployRoute("192.168.11.0/24", secondSubnet.subnetId); |
| 206 | yield* waitForClientVpn( |
| 207 | readClientVpnRoutes(clientVpnEndpointId), |
| 208 | (items) => |
| 209 | items.some( |
| 210 | (route) => |
| 211 | route.DestinationCidr === "192.168.11.0/24" && |
| 212 | route.TargetSubnet === secondSubnet.subnetId && |
| 213 | route.Status?.Code === "active", |
| 214 | ), |
| 215 | "recreated out-of-band deleted route", |
| 216 | ); |
| 217 | yield* stack.destroy(); |
| 218 | yield* assertClientVpnRouteDeleted( |
| 219 | clientVpnEndpointId, |
| 220 | "192.168.11.0/24", |
| 221 | secondSubnet.subnetId, |
| 222 | ); |
| 223 | // Deleting a managed route must retain AWS's association-owned VPC routes. |
| 224 | const remaining = yield* readClientVpnRoutes(clientVpnEndpointId); |
| 225 | expect(remaining).toContainEqual( |
| 226 | expect.objectContaining({ |
| 227 | DestinationCidr: "10.171.0.0/16", |
| 228 | TargetSubnet: firstSubnet.subnetId, |
| 229 | Status: expect.objectContaining({ Code: "active" }), |
| 230 | }), |
| 231 | ); |
| 232 | expect(remaining).toContainEqual( |
| 233 | expect.objectContaining({ |
| 234 | DestinationCidr: "10.171.0.0/16", |
| 235 | TargetSubnet: secondSubnet.subnetId, |
| 236 | Status: expect.objectContaining({ Code: "active" }), |
| 237 | }), |
| 238 | ); |
| 239 | }), |
| 240 | { timeout: clientVpnTestTimeout }, |
| 241 | ); |
| 242 | }, |
| 243 | ); |