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 { GcpEnvironment } from "@alchemy.run/gcp/Environment"; |
| 3 | import { expect } from "@alchemy.run/test"; |
| 4 | import * as Test from "@alchemy.run/test/Alchemy"; |
| 5 | import * as networkservices from "@distilled.cloud/gcp/networkservices_v1"; |
| 6 | import * as Effect from "effect/Effect"; |
| 7 | import { MinimumLogLevel } from "effect/References"; |
| 8 | import * as Schedule from "effect/Schedule"; |
| 9 | import { defaultNetworkSelfLink } from "../networkQuota.ts"; |
| 10 | |
| 11 | const { test } = Test.make({ providers: GCP.providers() }); |
| 12 | |
| 13 | const logLevel = Effect.provideService(MinimumLogLevel, process.env.DEBUG ? "Debug" : "Info"); |
| 14 | |
| 15 | // The fixture's INTERNAL_MANAGED address currently fails with BadRequest |
| 16 | // "Invalid value for field 'resource.network': ... The field cannot be |
| 17 | // specified with subnetwork address." — it needs a subnetwork plus a |
| 18 | // proxy-only subnet. Set GCP_TEST_LB_TRAFFIC_EXTENSION=1 once reworked. |
| 19 | const runLifecycle = !process.env.FAST && !!process.env.GCP_TEST_LB_TRAFFIC_EXTENSION; |
| 20 | |
| 21 | const waitUntilGone = (name: string) => |
| 22 | networkservices.getProjectsLocationsLbTrafficExtensions({ name }).pipe( |
| 23 | Effect.as("found" as const), |
| 24 | Effect.catchTag("NotFound", () => Effect.succeed("gone" as const)), |
| 25 | Effect.repeat({ |
| 26 | schedule: Schedule.spaced("2 seconds"), |
| 27 | until: (status) => status === "gone", |
| 28 | times: 10, |
| 29 | }), |
| 30 | ); |
| 31 | |
| 32 | test.provider( |
| 33 | "getProjectsLocationsLbTrafficExtensions on a missing extension fails with a typed tag", |
| 34 | (stack) => |
| 35 | Effect.gen(function* () { |
| 36 | yield* stack.destroy(); |
| 37 | |
| 38 | const { project } = yield* GcpEnvironment.current; |
| 39 | const error = yield* Effect.flip( |
| 40 | networkservices.getProjectsLocationsLbTrafficExtensions({ |
| 41 | name: `projects/${project}/locations/us-central1/lbTrafficExtensions/alchemy-missing`, |
| 42 | }), |
| 43 | ); |
| 44 | expect(error._tag).toEqual("NotFound"); |
| 45 | |
| 46 | yield* stack.destroy(); |
| 47 | }).pipe(logLevel), |
| 48 | { |
| 49 | tags: ["provider:gcp", "provider:gcp:networkservices", "live"], |
| 50 | timeout: 90_000, |
| 51 | }, |
| 52 | ); |
| 53 | |
| 54 | test.provider.skipIf(!runLifecycle)( |
| 55 | "create, update, and delete an lb traffic extension", |
| 56 | (stack) => |
| 57 | Effect.gen(function* () { |
| 58 | const { project } = yield* GcpEnvironment.current; |
| 59 | yield* stack.destroy(); |
| 60 | |
| 61 | const created = yield* stack.deploy( |
| 62 | Effect.gen(function* () { |
| 63 | const healthCheck = yield* GCP.Compute.HealthCheck("Hc", { |
| 64 | type: "HTTP", |
| 65 | httpHealthCheck: { port: 80, requestPath: "/" }, |
| 66 | }); |
| 67 | const backend = yield* GCP.Compute.RegionBackendService("Backend", { |
| 68 | region: "us-central1", |
| 69 | loadBalancingScheme: "INTERNAL_MANAGED", |
| 70 | protocol: "HTTP", |
| 71 | healthChecks: [healthCheck.selfLink.as<string>()], |
| 72 | }); |
| 73 | const urlMap = yield* GCP.Compute.RegionUrlMap("Map", { |
| 74 | region: "us-central1", |
| 75 | defaultService: backend.selfLink.as<string>(), |
| 76 | }); |
| 77 | const proxy = yield* GCP.Compute.RegionTargetHttpProxy("Proxy", { |
| 78 | region: "us-central1", |
| 79 | urlMap: urlMap.selfLink.as<string>(), |
| 80 | }); |
| 81 | const address = yield* GCP.Compute.Address("Ip", { |
| 82 | region: "us-central1", |
| 83 | addressType: "INTERNAL", |
| 84 | purpose: "SHARED_LOADBALANCER_VIP", |
| 85 | network: defaultNetworkSelfLink(project), |
| 86 | }); |
| 87 | const rule = yield* GCP.Compute.ForwardingRule("Rule", { |
| 88 | region: "us-central1", |
| 89 | loadBalancingScheme: "INTERNAL_MANAGED", |
| 90 | ipProtocol: "TCP", |
| 91 | portRange: "80", |
| 92 | target: proxy.selfLink.as<string>(), |
| 93 | ipAddress: address.selfLink.as<string>(), |
| 94 | network: defaultNetworkSelfLink(project), |
| 95 | }); |
| 96 | const callout = yield* GCP.Compute.RegionBackendService("Callout", { |
| 97 | region: "us-central1", |
| 98 | loadBalancingScheme: "INTERNAL_MANAGED", |
| 99 | protocol: "HTTP2", |
| 100 | healthChecks: [healthCheck.selfLink.as<string>()], |
| 101 | }); |
| 102 | const extension = yield* GCP.NetworkServices.LbTrafficExtension("Inspect", { |
| 103 | location: "us-central1", |
| 104 | description: "traffic ext a", |
| 105 | labels: { env: "test" }, |
| 106 | loadBalancingScheme: "INTERNAL_MANAGED", |
| 107 | forwardingRules: [rule.selfLink.as<string>()], |
| 108 | extensionChains: [ |
| 109 | { |
| 110 | name: "all-traffic", |
| 111 | matchCondition: { celExpression: "true" }, |
| 112 | extensions: [ |
| 113 | { |
| 114 | name: "header-rewriter", |
| 115 | service: callout.selfLink.as<string>(), |
| 116 | authority: "ext.example.com", |
| 117 | timeout: "0.1s", |
| 118 | failOpen: true, |
| 119 | supportedEvents: ["REQUEST_HEADERS"], |
| 120 | }, |
| 121 | ], |
| 122 | }, |
| 123 | ], |
| 124 | }); |
| 125 | return { extension }; |
| 126 | }), |
| 127 | ); |
| 128 | |
| 129 | expect(created.extension.name).toContain("/lbTrafficExtensions/"); |
| 130 | expect(created.extension.lbTrafficExtensionId).toEqual(expect.any(String)); |
| 131 | expect(created.extension.location).toEqual("us-central1"); |
| 132 | expect(created.extension.description).toEqual("traffic ext a"); |
| 133 | expect(created.extension.loadBalancingScheme).toEqual("INTERNAL_MANAGED"); |
| 134 | expect(created.extension.labels).toMatchObject({ env: "test" }); |
| 135 | |
| 136 | const fetched = yield* networkservices.getProjectsLocationsLbTrafficExtensions({ |
| 137 | name: created.extension.name, |
| 138 | }); |
| 139 | expect(fetched.name).toEqual(created.extension.name); |
| 140 | expect(fetched.description).toEqual("traffic ext a"); |
| 141 | expect(fetched.labels?.env).toEqual("test"); |
| 142 | expect(Object.keys(fetched.labels ?? {}).some((key) => key.startsWith("alchemy-"))).toEqual( |
| 143 | true, |
| 144 | ); |
| 145 | |
| 146 | yield* stack.destroy(); |
| 147 | |
| 148 | const gone = yield* waitUntilGone(created.extension.name); |
| 149 | expect(gone).toEqual("gone"); |
| 150 | }).pipe(logLevel), |
| 151 | { |
| 152 | tags: ["provider:gcp", "provider:gcp:networkservices", "live"], |
| 153 | timeout: 900_000, |
| 154 | }, |
| 155 | ); |