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 compute from "@distilled.cloud/gcp/compute_v1"; |
| 6 | import * as Effect from "effect/Effect"; |
| 7 | import { MinimumLogLevel } from "effect/References"; |
| 8 | import * as Schedule from "effect/Schedule"; |
| 9 | import { DEFAULT_NETWORK, 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 | const region = "us-central1"; |
| 16 | |
| 17 | const waitUntilGone = (compositeHealthCheck: string) => |
| 18 | GcpEnvironment.current.pipe( |
| 19 | Effect.flatMap(({ project }) => |
| 20 | compute |
| 21 | .getRegionCompositeHealthChecks({ |
| 22 | project, |
| 23 | region, |
| 24 | compositeHealthCheck, |
| 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 | ); |
| 37 | |
| 38 | test.provider( |
| 39 | "getRegionCompositeHealthChecks on a missing check fails with NotFound", |
| 40 | (stack) => |
| 41 | Effect.gen(function* () { |
| 42 | const { project } = yield* GcpEnvironment.current; |
| 43 | yield* stack.destroy(); |
| 44 | |
| 45 | const error = yield* Effect.flip( |
| 46 | compute.getRegionCompositeHealthChecks({ |
| 47 | project, |
| 48 | region, |
| 49 | compositeHealthCheck: "alchemy-missing-chc", |
| 50 | }), |
| 51 | ); |
| 52 | expect(error._tag).toBe("NotFound"); |
| 53 | |
| 54 | yield* stack.destroy(); |
| 55 | }).pipe(logLevel), |
| 56 | { tags: ["provider:gcp", "provider:gcp:compute", "live"], timeout: 90_000 }, |
| 57 | ); |
| 58 | |
| 59 | test.provider( |
| 60 | "insertRegionCompositeHealthChecks with a missing health source fails with NotFound", |
| 61 | () => |
| 62 | Effect.gen(function* () { |
| 63 | const { project } = yield* GcpEnvironment.current; |
| 64 | const error = yield* Effect.flip( |
| 65 | compute.insertRegionCompositeHealthChecks({ |
| 66 | project, |
| 67 | region, |
| 68 | body: { |
| 69 | name: "alchemy-chc-probe", |
| 70 | description: "alchemy entitlement probe", |
| 71 | healthDestination: `projects/${project}/regions/${region}/forwardingRules/does-not-exist`, |
| 72 | healthSources: [`projects/${project}/regions/${region}/healthSources/does-not-exist`], |
| 73 | }, |
| 74 | }), |
| 75 | ); |
| 76 | expect(error._tag).toEqual("NotFound"); |
| 77 | }).pipe(logLevel), |
| 78 | { tags: ["provider:gcp", "provider:gcp:compute", "live"], timeout: 60_000 }, |
| 79 | ); |
| 80 | |
| 81 | test.provider( |
| 82 | "create, update, and delete a regional composite health check", |
| 83 | (stack) => |
| 84 | Effect.gen(function* () { |
| 85 | const { project } = yield* GcpEnvironment.current; |
| 86 | yield* stack.destroy(); |
| 87 | |
| 88 | const created = yield* stack.deploy( |
| 89 | Effect.gen(function* () { |
| 90 | const subnet = yield* GCP.Compute.Subnetwork("IlbSubnet", { |
| 91 | network: DEFAULT_NETWORK, |
| 92 | region, |
| 93 | ipCidrRange: "172.20.1.0/24", |
| 94 | }); |
| 95 | const check = yield* GCP.Compute.RegionHealthCheck("Probe", { |
| 96 | region, |
| 97 | description: "tcp probe", |
| 98 | type: "TCP", |
| 99 | tcpHealthCheck: { port: 80 }, |
| 100 | }); |
| 101 | const backend = yield* GCP.Compute.RegionBackendService("IlbBackend", { |
| 102 | region, |
| 103 | protocol: "TCP", |
| 104 | loadBalancingScheme: "INTERNAL", |
| 105 | network: defaultNetworkSelfLink(project), |
| 106 | healthChecks: [check.selfLink.as<string>()], |
| 107 | description: "ilb backend", |
| 108 | }); |
| 109 | const rule = yield* GCP.Compute.ForwardingRule("IlbRule", { |
| 110 | region, |
| 111 | loadBalancingScheme: "INTERNAL", |
| 112 | backendService: backend.selfLink.as<string>(), |
| 113 | network: defaultNetworkSelfLink(project), |
| 114 | subnetwork: subnet.selfLink.as<string>(), |
| 115 | ipProtocol: "TCP", |
| 116 | allPorts: true, |
| 117 | }); |
| 118 | const policy = yield* GCP.Compute.RegionHealthAggregationPolicy("Agg", { |
| 119 | region, |
| 120 | description: "backend rollup", |
| 121 | }); |
| 122 | const source = yield* GCP.Compute.RegionHealthSource("Src", { |
| 123 | region, |
| 124 | sources: [backend.selfLink.as<string>()], |
| 125 | healthAggregationPolicy: policy.selfLink.as<string>(), |
| 126 | description: "ilb source", |
| 127 | }); |
| 128 | const composite = yield* GCP.Compute.RegionCompositeHealthCheck("Comp", { |
| 129 | region, |
| 130 | healthDestination: rule.selfLink.as<string>(), |
| 131 | healthSources: [source.selfLink.as<string>()], |
| 132 | description: "and backends", |
| 133 | }); |
| 134 | return { |
| 135 | subnet, |
| 136 | check, |
| 137 | backend, |
| 138 | rule, |
| 139 | policy, |
| 140 | source, |
| 141 | composite, |
| 142 | }; |
| 143 | }), |
| 144 | ); |
| 145 | |
| 146 | expect(created.composite.healthCheckName).toEqual(expect.any(String)); |
| 147 | expect(created.composite.region).toEqual(region); |
| 148 | expect(created.composite.description).toEqual("and backends"); |
| 149 | expect(created.composite.healthSources.length).toBeGreaterThan(0); |
| 150 | |
| 151 | const fetched = yield* compute.getRegionCompositeHealthChecks({ |
| 152 | project: created.composite.project, |
| 153 | region, |
| 154 | compositeHealthCheck: created.composite.healthCheckName, |
| 155 | }); |
| 156 | expect(fetched.name).toEqual(created.composite.healthCheckName); |
| 157 | expect(fetched.description).toContain("[alchemy "); |
| 158 | expect(fetched.description).toContain("and backends"); |
| 159 | |
| 160 | const updated = yield* stack.deploy( |
| 161 | Effect.gen(function* () { |
| 162 | const subnet = yield* GCP.Compute.Subnetwork("IlbSubnet", { |
| 163 | subnetworkName: created.subnet.subnetworkName, |
| 164 | network: DEFAULT_NETWORK, |
| 165 | region, |
| 166 | ipCidrRange: "172.20.1.0/24", |
| 167 | }); |
| 168 | const check = yield* GCP.Compute.RegionHealthCheck("Probe", { |
| 169 | healthCheckName: created.check.healthCheckName, |
| 170 | region, |
| 171 | description: "tcp probe", |
| 172 | type: "TCP", |
| 173 | tcpHealthCheck: { port: 80 }, |
| 174 | }); |
| 175 | const backend = yield* GCP.Compute.RegionBackendService("IlbBackend", { |
| 176 | name: created.backend.name, |
| 177 | region, |
| 178 | protocol: "TCP", |
| 179 | loadBalancingScheme: "INTERNAL", |
| 180 | network: defaultNetworkSelfLink(project), |
| 181 | healthChecks: [check.selfLink.as<string>()], |
| 182 | description: "ilb backend", |
| 183 | }); |
| 184 | const rule = yield* GCP.Compute.ForwardingRule("IlbRule", { |
| 185 | forwardingRuleName: created.rule.forwardingRuleName, |
| 186 | region, |
| 187 | loadBalancingScheme: "INTERNAL", |
| 188 | backendService: backend.selfLink.as<string>(), |
| 189 | network: defaultNetworkSelfLink(project), |
| 190 | subnetwork: subnet.selfLink.as<string>(), |
| 191 | ipProtocol: "TCP", |
| 192 | allPorts: true, |
| 193 | }); |
| 194 | const policy = yield* GCP.Compute.RegionHealthAggregationPolicy("Agg", { |
| 195 | policyName: created.policy.policyName, |
| 196 | region, |
| 197 | description: "backend rollup", |
| 198 | }); |
| 199 | const source = yield* GCP.Compute.RegionHealthSource("Src", { |
| 200 | sourceName: created.source.sourceName, |
| 201 | region, |
| 202 | sources: [backend.selfLink.as<string>()], |
| 203 | healthAggregationPolicy: policy.selfLink.as<string>(), |
| 204 | description: "ilb source", |
| 205 | }); |
| 206 | return yield* GCP.Compute.RegionCompositeHealthCheck("Comp", { |
| 207 | healthCheckName: created.composite.healthCheckName, |
| 208 | region, |
| 209 | healthDestination: rule.selfLink.as<string>(), |
| 210 | healthSources: [source.selfLink.as<string>()], |
| 211 | description: "updated composite", |
| 212 | }); |
| 213 | }), |
| 214 | ); |
| 215 | |
| 216 | expect(updated.healthCheckName).toEqual(created.composite.healthCheckName); |
| 217 | expect(updated.description).toEqual("updated composite"); |
| 218 | |
| 219 | yield* stack.destroy(); |
| 220 | |
| 221 | const gone = yield* waitUntilGone(created.composite.healthCheckName); |
| 222 | expect(gone).toEqual("gone"); |
| 223 | }).pipe(logLevel), |
| 224 | { tags: ["provider:gcp", "provider:gcp:compute", "live"], timeout: 240_000 }, |
| 225 | ); |