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