The best way to control Claude Code, Codex and more from any computer or phone
| 1 | import * as GCP from "@alchemy.run/gcp"; |
| 2 | import { expect } from "@alchemy.run/test"; |
| 3 | import * as Test from "@alchemy.run/test/Alchemy"; |
| 4 | import * as compute from "@distilled.cloud/gcp/compute_v1"; |
| 5 | import * as Effect from "effect/Effect"; |
| 6 | import { MinimumLogLevel } from "effect/References"; |
| 7 | import * as Schedule from "effect/Schedule"; |
| 8 | import { DEFAULT_NETWORK } from "../networkQuota.ts"; |
| 9 | |
| 10 | const { test } = Test.make({ providers: GCP.providers() }); |
| 11 | |
| 12 | const logLevel = Effect.provideService(MinimumLogLevel, process.env.DEBUG ? "Debug" : "Info"); |
| 13 | |
| 14 | const SHARED_SECRET = "alchemy-test-shared-secret"; |
| 15 | |
| 16 | const waitUntilGone = (project: string, region: string, vpnTunnelName: string) => |
| 17 | compute.getVpnTunnels({ project, region, vpnTunnel: vpnTunnelName }).pipe( |
| 18 | Effect.as("found" as const), |
| 19 | Effect.catchTag("NotFound", () => Effect.succeed("gone" as const)), |
| 20 | Effect.repeat({ |
| 21 | schedule: Schedule.spaced("1 second"), |
| 22 | until: (status) => status === "gone", |
| 23 | times: 15, |
| 24 | }), |
| 25 | ); |
| 26 | |
| 27 | test.provider( |
| 28 | "create, update, and delete an HA VPN tunnel", |
| 29 | (stack) => |
| 30 | Effect.gen(function* () { |
| 31 | yield* stack.destroy(); |
| 32 | |
| 33 | const created = yield* stack.deploy( |
| 34 | Effect.gen(function* () { |
| 35 | const gateway = yield* GCP.Compute.VpnGateway("Gateway", { |
| 36 | region: "us-central1", |
| 37 | network: DEFAULT_NETWORK, |
| 38 | }); |
| 39 | const router = yield* GCP.Compute.Router("Edge", { |
| 40 | region: "us-central1", |
| 41 | network: DEFAULT_NETWORK, |
| 42 | bgp: { asn: 64514 }, |
| 43 | }); |
| 44 | const peer = yield* GCP.Compute.ExternalVpnGateway("Peer", { |
| 45 | redundancyType: "SINGLE_IP_INTERNALLY_REDUNDANT", |
| 46 | interfaces: [{ id: 0, ipAddress: "15.0.0.120" }], |
| 47 | }); |
| 48 | const tunnel = yield* GCP.Compute.VpnTunnel("Tunnel", { |
| 49 | region: "us-central1", |
| 50 | vpnGateway: gateway.vpnGatewayName, |
| 51 | vpnGatewayInterface: 0, |
| 52 | peerExternalGateway: peer.externalVpnGatewayName, |
| 53 | peerExternalGatewayInterface: 0, |
| 54 | router: router.routerName, |
| 55 | sharedSecret: SHARED_SECRET, |
| 56 | description: "ha vpn tunnel", |
| 57 | labels: { env: "test" }, |
| 58 | }); |
| 59 | return { gateway, router, peer, tunnel }; |
| 60 | }), |
| 61 | ); |
| 62 | |
| 63 | expect(created.tunnel.vpnTunnelName).toEqual(expect.any(String)); |
| 64 | expect(created.tunnel.region).toEqual("us-central1"); |
| 65 | expect(created.tunnel.description).toEqual("ha vpn tunnel"); |
| 66 | expect(created.tunnel.labels).toMatchObject({ env: "test" }); |
| 67 | expect(created.tunnel.vpnGateway).toEqual( |
| 68 | expect.stringContaining(created.gateway.vpnGatewayName), |
| 69 | ); |
| 70 | expect(created.tunnel.vpnGatewayInterface).toEqual(0); |
| 71 | expect(created.tunnel.peerExternalGateway).toEqual( |
| 72 | expect.stringContaining(created.peer.externalVpnGatewayName), |
| 73 | ); |
| 74 | expect(created.tunnel.peerExternalGatewayInterface).toEqual(0); |
| 75 | expect(created.tunnel.router).toEqual(expect.stringContaining(created.router.routerName)); |
| 76 | expect(created.tunnel.ikeVersion).toEqual(2); |
| 77 | expect(created.tunnel.sharedSecretHash).toEqual(expect.any(String)); |
| 78 | |
| 79 | const fetched = yield* compute.getVpnTunnels({ |
| 80 | project: created.tunnel.project, |
| 81 | region: created.tunnel.region, |
| 82 | vpnTunnel: created.tunnel.vpnTunnelName, |
| 83 | }); |
| 84 | expect(fetched.name).toEqual(created.tunnel.vpnTunnelName); |
| 85 | expect(fetched.description).toEqual("ha vpn tunnel"); |
| 86 | expect(fetched.labels?.env).toEqual("test"); |
| 87 | expect(fetched.labels?.["alchemy-id"]).toEqual(expect.any(String)); |
| 88 | expect(fetched.vpnGateway).toEqual(expect.stringContaining(created.gateway.vpnGatewayName)); |
| 89 | expect(fetched.vpnGatewayInterface).toEqual(0); |
| 90 | expect(fetched.peerExternalGateway).toEqual( |
| 91 | expect.stringContaining(created.peer.externalVpnGatewayName), |
| 92 | ); |
| 93 | expect(fetched.router).toEqual(expect.stringContaining(created.router.routerName)); |
| 94 | expect(fetched.ikeVersion).toEqual(2); |
| 95 | |
| 96 | const updated = yield* stack.deploy( |
| 97 | Effect.gen(function* () { |
| 98 | const gateway = yield* GCP.Compute.VpnGateway("Gateway", { |
| 99 | vpnGatewayName: created.gateway.vpnGatewayName, |
| 100 | region: "us-central1", |
| 101 | network: DEFAULT_NETWORK, |
| 102 | }); |
| 103 | const router = yield* GCP.Compute.Router("Edge", { |
| 104 | routerName: created.router.routerName, |
| 105 | region: "us-central1", |
| 106 | network: DEFAULT_NETWORK, |
| 107 | bgp: { asn: 64514 }, |
| 108 | }); |
| 109 | const peer = yield* GCP.Compute.ExternalVpnGateway("Peer", { |
| 110 | externalVpnGatewayName: created.peer.externalVpnGatewayName, |
| 111 | redundancyType: "SINGLE_IP_INTERNALLY_REDUNDANT", |
| 112 | interfaces: [{ id: 0, ipAddress: "15.0.0.120" }], |
| 113 | }); |
| 114 | const tunnel = yield* GCP.Compute.VpnTunnel("Tunnel", { |
| 115 | vpnTunnelName: created.tunnel.vpnTunnelName, |
| 116 | region: "us-central1", |
| 117 | vpnGateway: gateway.vpnGatewayName, |
| 118 | vpnGatewayInterface: 0, |
| 119 | peerExternalGateway: peer.externalVpnGatewayName, |
| 120 | peerExternalGatewayInterface: 0, |
| 121 | router: router.routerName, |
| 122 | sharedSecret: SHARED_SECRET, |
| 123 | description: "ha vpn tunnel", |
| 124 | labels: { env: "prod", role: "vpn" }, |
| 125 | }); |
| 126 | return { gateway, router, peer, tunnel }; |
| 127 | }), |
| 128 | ); |
| 129 | |
| 130 | expect(updated.tunnel.vpnTunnelName).toEqual(created.tunnel.vpnTunnelName); |
| 131 | expect(updated.tunnel.vpnTunnelId).toEqual(created.tunnel.vpnTunnelId); |
| 132 | expect(updated.tunnel.labels).toMatchObject({ |
| 133 | env: "prod", |
| 134 | role: "vpn", |
| 135 | }); |
| 136 | |
| 137 | const fetchedUpdated = yield* compute.getVpnTunnels({ |
| 138 | project: updated.tunnel.project, |
| 139 | region: updated.tunnel.region, |
| 140 | vpnTunnel: updated.tunnel.vpnTunnelName, |
| 141 | }); |
| 142 | expect(fetchedUpdated.labels?.env).toEqual("prod"); |
| 143 | expect(fetchedUpdated.labels?.role).toEqual("vpn"); |
| 144 | expect(fetchedUpdated.id).toEqual(created.tunnel.vpnTunnelId); |
| 145 | |
| 146 | yield* stack.destroy(); |
| 147 | |
| 148 | const gone = yield* waitUntilGone( |
| 149 | created.tunnel.project, |
| 150 | created.tunnel.region, |
| 151 | created.tunnel.vpnTunnelName, |
| 152 | ); |
| 153 | expect(gone).toEqual("gone"); |
| 154 | }).pipe(logLevel), |
| 155 | { tags: ["provider:gcp", "provider:gcp:compute", "live"], timeout: 240_000 }, |
| 156 | ); |