| 1 | import * as GCP from "@/GCP"; |
| 2 | import * as Test from "@/Test/Alchemy"; |
| 3 | import * as compute from "@distilled.cloud/gcp/compute_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 | |
| 10 | const { test } = Test.make({ providers: GCP.providers() }); |
| 11 | |
| 12 | const logLevel = Effect.provideService( |
| 13 | MinimumLogLevel, |
| 14 | process.env.DEBUG ? "Debug" : "Info", |
| 15 | ); |
| 16 | |
| 17 | const region = "us-central1"; |
| 18 | |
| 19 | const waitUntilGone = (project: string, targetTcpProxyName: string) => |
| 20 | compute |
| 21 | .getRegionTargetTcpProxies({ |
| 22 | project, |
| 23 | region, |
| 24 | targetTcpProxy: targetTcpProxyName, |
| 25 | }) |
| 26 | .pipe( |
| 27 | Effect.as("found" as const), |
| 28 | Effect.catchTag("NotFound", () => Effect.succeed("gone" as const)), |
| 29 | Effect.repeat({ |
| 30 | schedule: Schedule.spaced("1 second"), |
| 31 | until: (status) => status === "gone", |
| 32 | times: 10, |
| 33 | }), |
| 34 | ); |
| 35 | |
| 36 | const resourceTail = (value: string | undefined): string => { |
| 37 | if (value === undefined || value.length === 0) return ""; |
| 38 | const parts = value.split("/").filter((part) => part.length > 0); |
| 39 | return parts[parts.length - 1] ?? ""; |
| 40 | }; |
| 41 | |
| 42 | test.provider( |
| 43 | "getRegionTargetTcpProxies on a missing proxy fails with NotFound", |
| 44 | (stack) => |
| 45 | Effect.gen(function* () { |
| 46 | yield* stack.destroy(); |
| 47 | const { project } = yield* GcpEnvironment.current; |
| 48 | const error = yield* Effect.flip( |
| 49 | compute.getRegionTargetTcpProxies({ |
| 50 | project, |
| 51 | region, |
| 52 | targetTcpProxy: "alchemy-missing-rtcp", |
| 53 | }), |
| 54 | ); |
| 55 | expect(error._tag).toBe("NotFound"); |
| 56 | yield* stack.destroy(); |
| 57 | }).pipe(logLevel), |
| 58 | { tags: ["provider:gcp", "provider:gcp:compute", "live"], timeout: 90_000 }, |
| 59 | ); |
| 60 | |
| 61 | test.provider( |
| 62 | "create, update, and delete a regional target tcp proxy", |
| 63 | (stack) => |
| 64 | Effect.gen(function* () { |
| 65 | yield* stack.destroy(); |
| 66 | |
| 67 | const created = yield* stack.deploy( |
| 68 | Effect.gen(function* () { |
| 69 | const check = yield* GCP.Compute.RegionHealthCheck("Probe", { |
| 70 | region, |
| 71 | description: "tcp probe", |
| 72 | type: "TCP", |
| 73 | tcpHealthCheck: { port: 80 }, |
| 74 | }); |
| 75 | const backend = yield* GCP.Compute.RegionBackendService("TcpA", { |
| 76 | region, |
| 77 | protocol: "TCP", |
| 78 | loadBalancingScheme: "INTERNAL_MANAGED", |
| 79 | description: "tcp backend a", |
| 80 | healthChecks: [check.selfLink.as<string>()], |
| 81 | }); |
| 82 | const proxy = yield* GCP.Compute.RegionTargetTcpProxy("Proxy", { |
| 83 | region, |
| 84 | description: "tcp frontend", |
| 85 | service: backend.name, |
| 86 | proxyHeader: "NONE", |
| 87 | }); |
| 88 | return { check, backend, proxy }; |
| 89 | }), |
| 90 | ); |
| 91 | |
| 92 | expect(created.proxy.targetTcpProxyName).toEqual(expect.any(String)); |
| 93 | expect(created.proxy.region).toEqual(region); |
| 94 | expect(created.proxy.description).toEqual("tcp frontend"); |
| 95 | expect( |
| 96 | created.proxy.proxyHeader === "NONE" || |
| 97 | created.proxy.proxyHeader === undefined, |
| 98 | ).toEqual(true); |
| 99 | expect(resourceTail(created.proxy.service)).toEqual(created.backend.name); |
| 100 | |
| 101 | const fetched = yield* compute.getRegionTargetTcpProxies({ |
| 102 | project: created.proxy.project, |
| 103 | region, |
| 104 | targetTcpProxy: created.proxy.targetTcpProxyName, |
| 105 | }); |
| 106 | expect(fetched.name).toEqual(created.proxy.targetTcpProxyName); |
| 107 | expect(resourceTail(fetched.service)).toEqual( |
| 108 | resourceTail(created.proxy.service), |
| 109 | ); |
| 110 | expect(fetched.proxyHeader ?? "NONE").toEqual("NONE"); |
| 111 | expect(fetched.description).toContain("[alchemy "); |
| 112 | expect(fetched.description).toContain("tcp frontend"); |
| 113 | |
| 114 | const updated = yield* stack.deploy( |
| 115 | Effect.gen(function* () { |
| 116 | const check = yield* GCP.Compute.RegionHealthCheck("Probe", { |
| 117 | healthCheckName: created.check.healthCheckName, |
| 118 | region, |
| 119 | description: "tcp probe", |
| 120 | type: "TCP", |
| 121 | tcpHealthCheck: { port: 80 }, |
| 122 | }); |
| 123 | yield* GCP.Compute.RegionBackendService("TcpA", { |
| 124 | name: created.backend.name, |
| 125 | region, |
| 126 | protocol: "TCP", |
| 127 | loadBalancingScheme: "INTERNAL_MANAGED", |
| 128 | description: "tcp backend a", |
| 129 | healthChecks: [check.selfLink.as<string>()], |
| 130 | }); |
| 131 | const other = yield* GCP.Compute.RegionBackendService("TcpB", { |
| 132 | region, |
| 133 | protocol: "TCP", |
| 134 | loadBalancingScheme: "INTERNAL_MANAGED", |
| 135 | description: "tcp backend b", |
| 136 | healthChecks: [check.selfLink.as<string>()], |
| 137 | }); |
| 138 | const proxy = yield* GCP.Compute.RegionTargetTcpProxy("Proxy", { |
| 139 | targetTcpProxyName: created.proxy.targetTcpProxyName, |
| 140 | region, |
| 141 | description: "tcp frontend", |
| 142 | service: other.name, |
| 143 | proxyHeader: "PROXY_V1", |
| 144 | }); |
| 145 | return { other, proxy }; |
| 146 | }), |
| 147 | ); |
| 148 | |
| 149 | expect(updated.proxy.targetTcpProxyName).toEqual( |
| 150 | created.proxy.targetTcpProxyName, |
| 151 | ); |
| 152 | expect(updated.proxy.description).toEqual("tcp frontend"); |
| 153 | expect(updated.proxy.proxyHeader).toEqual("PROXY_V1"); |
| 154 | expect(resourceTail(updated.proxy.service)).toEqual(updated.other.name); |
| 155 | |
| 156 | const refetched = yield* compute.getRegionTargetTcpProxies({ |
| 157 | project: updated.proxy.project, |
| 158 | region, |
| 159 | targetTcpProxy: updated.proxy.targetTcpProxyName, |
| 160 | }); |
| 161 | expect(refetched.proxyHeader).toEqual("PROXY_V1"); |
| 162 | expect(resourceTail(refetched.service)).toEqual( |
| 163 | resourceTail(updated.proxy.service), |
| 164 | ); |
| 165 | expect(resourceTail(refetched.service)).not.toEqual( |
| 166 | resourceTail(created.proxy.service), |
| 167 | ); |
| 168 | |
| 169 | yield* stack.destroy(); |
| 170 | |
| 171 | const gone = yield* waitUntilGone( |
| 172 | created.proxy.project, |
| 173 | created.proxy.targetTcpProxyName, |
| 174 | ); |
| 175 | expect(gone).toEqual("gone"); |
| 176 | }).pipe(logLevel), |
| 177 | { tags: ["provider:gcp", "provider:gcp:compute", "live"], timeout: 120_000 }, |
| 178 | ); |