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 { waitForOperation } from "@alchemy.run/gcp/Operation"; |
| 4 | import { expect } from "@alchemy.run/test"; |
| 5 | import * as Test from "@alchemy.run/test/Alchemy"; |
| 6 | import * as container from "@distilled.cloud/gcp/container_v1"; |
| 7 | import { adopt } from "alchemy/AdoptPolicy"; |
| 8 | import * as Effect from "effect/Effect"; |
| 9 | import { MinimumLogLevel } from "effect/References"; |
| 10 | import * as Schedule from "effect/Schedule"; |
| 11 | import { CAPACITY_ZONE, withGkeClusterSlot } from "../zones.ts"; |
| 12 | |
| 13 | const { test } = Test.make({ providers: GCP.providers() }); |
| 14 | |
| 15 | const logLevel = Effect.provideService(MinimumLogLevel, process.env.DEBUG ? "Debug" : "Info"); |
| 16 | |
| 17 | // GKE cluster create and delete each take 5-10 minutes. |
| 18 | const runLifecycle = !!process.env.GCP_TEST_SLOW && !process.env.FAST; |
| 19 | |
| 20 | // Pools stay empty: every GKE node holds an external IP and the project |
| 21 | // allows only 8 IN_USE_ADDRESSES per region, shared by every capacity test. |
| 22 | const POOL_NODE_COUNT = 0; |
| 23 | |
| 24 | const HOST_CLUSTER_ID = "alch-np-host"; |
| 25 | const HOST_LOCATION = CAPACITY_ZONE; |
| 26 | |
| 27 | const waitUntilGone = (name: string) => |
| 28 | container.getProjectsLocationsClustersNodePools({ name }).pipe( |
| 29 | Effect.as("found" as const), |
| 30 | Effect.catchTag("NotFound", () => Effect.succeed("gone" as const)), |
| 31 | Effect.repeat({ |
| 32 | schedule: Schedule.spaced("3 seconds"), |
| 33 | until: (status) => status === "gone", |
| 34 | times: 10, |
| 35 | }), |
| 36 | ); |
| 37 | |
| 38 | // GKE cluster create/delete takes 5–10 minutes. |
| 39 | const waitClusterOp = (project: string, operation: container.Operation) => { |
| 40 | const raw = operation.name ?? ""; |
| 41 | const fromLink = operation.selfLink ?? ""; |
| 42 | const name = raw.includes("/operations/") |
| 43 | ? raw.slice(Math.max(0, raw.indexOf("projects/"))) |
| 44 | : fromLink.includes("/operations/") |
| 45 | ? fromLink.slice(Math.max(0, fromLink.indexOf("projects/"))) |
| 46 | : `projects/${project}/locations/${HOST_LOCATION}/operations/${raw}`; |
| 47 | return waitForOperation( |
| 48 | { ...operation, name }, |
| 49 | (operationName) => container.getProjectsLocationsOperations({ name: operationName }), |
| 50 | { budget: "20 minutes", interval: "10 seconds" }, |
| 51 | ); |
| 52 | }; |
| 53 | |
| 54 | test.provider( |
| 55 | "lists clusters and treats a missing node pool as NotFound", |
| 56 | (stack) => |
| 57 | Effect.gen(function* () { |
| 58 | yield* stack.destroy(); |
| 59 | |
| 60 | const { project } = yield* GcpEnvironment.current; |
| 61 | const page = yield* container.listProjectsLocationsClusters({ |
| 62 | parent: `projects/${project}/locations/-`, |
| 63 | }); |
| 64 | expect((page.clusters ?? []).map((cluster) => cluster.name)).not.toContain( |
| 65 | "alchemy-missing-cluster", |
| 66 | ); |
| 67 | |
| 68 | const missing = yield* container |
| 69 | .getProjectsLocationsClustersNodePools({ |
| 70 | name: `projects/${project}/locations/${HOST_LOCATION}/clusters/alchemy-missing-cluster/nodePools/alchemy-missing-pool`, |
| 71 | }) |
| 72 | .pipe( |
| 73 | Effect.as("found" as const), |
| 74 | Effect.catchTag("NotFound", () => Effect.succeed("gone" as const)), |
| 75 | ); |
| 76 | expect(missing).toEqual("gone"); |
| 77 | |
| 78 | yield* stack.destroy(); |
| 79 | }).pipe(logLevel), |
| 80 | { tags: ["provider:gcp", "provider:gcp:container", "live"], timeout: 90_000 }, |
| 81 | ); |
| 82 | |
| 83 | /** |
| 84 | * A throwaway host cluster without Workload Identity (the node pool must |
| 85 | * not request GKE_METADATA on it). Owned by this test: a leftover from an |
| 86 | * interrupted run is reused if RUNNING, replaced otherwise, and always |
| 87 | * deleted when the test ends. |
| 88 | */ |
| 89 | const withHostCluster = <A, E, R>( |
| 90 | project: string, |
| 91 | body: (host: { clusterId: string; location: string }) => Effect.Effect<A, E, R>, |
| 92 | ) => { |
| 93 | const clusterName = `projects/${project}/locations/${HOST_LOCATION}/clusters/${HOST_CLUSTER_ID}`; |
| 94 | const deleteHost = container.deleteProjectsLocationsClusters({ name: clusterName }).pipe( |
| 95 | Effect.flatMap((operation) => waitClusterOp(project, operation)), |
| 96 | Effect.catchTag("NotFound", () => Effect.void), |
| 97 | ); |
| 98 | const ensureHost = Effect.gen(function* () { |
| 99 | const existing = yield* container |
| 100 | .getProjectsLocationsClusters({ name: clusterName }) |
| 101 | .pipe(Effect.catchTag("NotFound", () => Effect.succeed(undefined))); |
| 102 | if (existing?.status === "RUNNING") return; |
| 103 | if (existing !== undefined) yield* deleteHost; |
| 104 | const created = yield* container.createProjectsLocationsClusters({ |
| 105 | parent: `projects/${project}/locations/${HOST_LOCATION}`, |
| 106 | body: { |
| 107 | cluster: { |
| 108 | name: HOST_CLUSTER_ID, |
| 109 | ipAllocationPolicy: { useIpAliases: true }, |
| 110 | nodePools: [ |
| 111 | { |
| 112 | name: "default-pool", |
| 113 | initialNodeCount: 1, |
| 114 | config: { |
| 115 | machineType: "e2-medium", |
| 116 | diskSizeGb: 20, |
| 117 | diskType: "pd-standard", |
| 118 | spot: true, |
| 119 | }, |
| 120 | }, |
| 121 | ], |
| 122 | }, |
| 123 | }, |
| 124 | }); |
| 125 | yield* waitClusterOp(project, created); |
| 126 | }); |
| 127 | return ensureHost.pipe( |
| 128 | Effect.andThen(body({ clusterId: HOST_CLUSTER_ID, location: HOST_LOCATION })), |
| 129 | Effect.ensuring(Effect.ignore(deleteHost)), |
| 130 | ); |
| 131 | }; |
| 132 | |
| 133 | test.provider.skipIf(!runLifecycle)( |
| 134 | "create, update, and delete a node pool", |
| 135 | (stack) => |
| 136 | withGkeClusterSlot( |
| 137 | Effect.gen(function* () { |
| 138 | yield* stack.destroy(); |
| 139 | const { project } = yield* GcpEnvironment.current; |
| 140 | yield* withHostCluster(project, ({ clusterId: hostId, location: hostLocation }) => |
| 141 | Effect.gen(function* () { |
| 142 | const created = yield* stack.deploy( |
| 143 | Effect.gen(function* () { |
| 144 | return yield* GCP.Container.NodePool("Workers", { |
| 145 | cluster: hostId, |
| 146 | location: hostLocation, |
| 147 | nodeCount: POOL_NODE_COUNT, |
| 148 | machineType: "e2-medium", |
| 149 | diskSizeGb: 20, |
| 150 | spot: true, |
| 151 | management: { autoRepair: false, autoUpgrade: true }, |
| 152 | labels: { env: "test" }, |
| 153 | metadata: { "disable-legacy-endpoints": "true" }, |
| 154 | // The host has no Workload Identity, so GKE_METADATA is |
| 155 | // rejected; set the alternative explicitly. |
| 156 | workloadMetadataConfig: { mode: "GCE_METADATA" }, |
| 157 | shieldedInstanceConfig: { |
| 158 | enableIntegrityMonitoring: true, |
| 159 | enableSecureBoot: true, |
| 160 | }, |
| 161 | advancedMachineFeatures: { enableNestedVirtualization: false }, |
| 162 | }); |
| 163 | }), |
| 164 | ); |
| 165 | |
| 166 | expect(created.name).toContain("/nodePools/"); |
| 167 | expect(created.nodePoolId).toEqual(expect.any(String)); |
| 168 | expect(created.clusterId).toEqual(hostId); |
| 169 | expect(created.location).toEqual(hostLocation); |
| 170 | expect(created.labels).toMatchObject({ env: "test" }); |
| 171 | expect(created.spot).toEqual(true); |
| 172 | expect(created.nodeCount).toEqual(POOL_NODE_COUNT); |
| 173 | expect(created.metadata["disable-legacy-endpoints"]).toEqual("true"); |
| 174 | expect(created.workloadMetadataConfig?.mode).toEqual("GCE_METADATA"); |
| 175 | expect(created.shieldedInstanceConfig?.enableSecureBoot).toEqual(true); |
| 176 | expect(["RUNNING", "RUNNING_WITH_ERROR"]).toContain(created.status); |
| 177 | |
| 178 | const fetched = yield* container.getProjectsLocationsClustersNodePools({ |
| 179 | name: created.name, |
| 180 | }); |
| 181 | expect(fetched.name).toEqual(created.nodePoolId); |
| 182 | expect(fetched.config?.resourceLabels?.env).toEqual("test"); |
| 183 | expect(fetched.config?.spot).toEqual(true); |
| 184 | expect(fetched.config?.metadata?.["disable-legacy-endpoints"]).toEqual("true"); |
| 185 | expect(fetched.config?.workloadMetadataConfig?.mode).toEqual("GCE_METADATA"); |
| 186 | expect(fetched.config?.shieldedInstanceConfig?.enableSecureBoot).toEqual(true); |
| 187 | |
| 188 | const workersProps: GCP.Container.NodePoolProps = { |
| 189 | cluster: hostId, |
| 190 | location: hostLocation, |
| 191 | nodePoolId: created.nodePoolId, |
| 192 | nodeCount: POOL_NODE_COUNT, |
| 193 | machineType: "e2-medium", |
| 194 | diskSizeGb: 20, |
| 195 | spot: true, |
| 196 | management: { autoRepair: true, autoUpgrade: true }, |
| 197 | labels: { env: "prod", role: "workers" }, |
| 198 | metadata: { "disable-legacy-endpoints": "true" }, |
| 199 | // The host has no Workload Identity, so GKE_METADATA is |
| 200 | // rejected; set the alternative explicitly. |
| 201 | workloadMetadataConfig: { mode: "GCE_METADATA" }, |
| 202 | shieldedInstanceConfig: { |
| 203 | enableIntegrityMonitoring: true, |
| 204 | enableSecureBoot: true, |
| 205 | }, |
| 206 | advancedMachineFeatures: { enableNestedVirtualization: false }, |
| 207 | }; |
| 208 | const workers = (props: Partial<GCP.Container.NodePoolProps> = {}) => |
| 209 | GCP.Container.NodePool("Workers", { ...workersProps, ...props }); |
| 210 | |
| 211 | const updated = yield* stack.deploy(workers()); |
| 212 | |
| 213 | expect(updated.name).toEqual(created.name); |
| 214 | expect(updated.labels).toMatchObject({ |
| 215 | env: "prod", |
| 216 | role: "workers", |
| 217 | }); |
| 218 | expect(updated.management?.autoRepair).toEqual(true); |
| 219 | |
| 220 | const refetched = yield* container.getProjectsLocationsClustersNodePools({ |
| 221 | name: created.name, |
| 222 | }); |
| 223 | expect(refetched.config?.resourceLabels?.env).toEqual("prod"); |
| 224 | expect(refetched.config?.resourceLabels?.role).toEqual("workers"); |
| 225 | expect(refetched.management?.autoRepair).toEqual(true); |
| 226 | |
| 227 | // VM settings are fixed when the pool's nodes are created. |
| 228 | expect( |
| 229 | (yield* stack.plan( |
| 230 | workers({ advancedMachineFeatures: { enableNestedVirtualization: true } }), |
| 231 | )).resources.Workers, |
| 232 | ).toMatchObject({ action: "replace", deleteFirst: true }); |
| 233 | expect( |
| 234 | (yield* stack.plan(workers({ metadata: { "disable-legacy-endpoints": "false" } }))) |
| 235 | .resources.Workers, |
| 236 | ).toMatchObject({ action: "replace", deleteFirst: true }); |
| 237 | |
| 238 | yield* stack.destroy(); |
| 239 | |
| 240 | const gone = yield* waitUntilGone(created.name); |
| 241 | expect(gone).toEqual("gone"); |
| 242 | |
| 243 | // Adopting a pool created outside Alchemy: GKE injects metadata and |
| 244 | // omits false booleans, which must not count as changes. |
| 245 | const foreignId = "alch-np-foreign"; |
| 246 | const foreignName = `projects/${project}/locations/${hostLocation}/clusters/${hostId}/nodePools/${foreignId}`; |
| 247 | const existingForeign = yield* container |
| 248 | .getProjectsLocationsClustersNodePools({ name: foreignName }) |
| 249 | .pipe(Effect.catchTag("NotFound", () => Effect.succeed(undefined))); |
| 250 | if (existingForeign === undefined) { |
| 251 | const op = yield* container.createProjectsLocationsClustersNodePools({ |
| 252 | parent: `projects/${project}/locations/${hostLocation}/clusters/${hostId}`, |
| 253 | body: { |
| 254 | nodePool: { |
| 255 | name: foreignId, |
| 256 | initialNodeCount: POOL_NODE_COUNT, |
| 257 | config: { machineType: "e2-medium", diskSizeGb: 20, spot: true }, |
| 258 | }, |
| 259 | }, |
| 260 | }); |
| 261 | yield* waitClusterOp(project, op); |
| 262 | } |
| 263 | const adopted = (props: Partial<GCP.Container.NodePoolProps>) => |
| 264 | GCP.Container.NodePool("Adopted", { |
| 265 | cluster: hostId, |
| 266 | location: hostLocation, |
| 267 | nodePoolId: foreignId, |
| 268 | nodeCount: POOL_NODE_COUNT, |
| 269 | machineType: "e2-medium", |
| 270 | diskSizeGb: 20, |
| 271 | spot: true, |
| 272 | ...props, |
| 273 | }).pipe(adopt(true)); |
| 274 | const actionOf = (props: Partial<GCP.Container.NodePoolProps>) => |
| 275 | stack.plan(adopted(props)).pipe(Effect.map((plan) => plan.resources.Adopted)); |
| 276 | |
| 277 | expect((yield* actionOf({ metadata: {} })).action).not.toBe("replace"); |
| 278 | expect( |
| 279 | (yield* actionOf({ |
| 280 | metadata: {}, |
| 281 | shieldedInstanceConfig: { enableSecureBoot: false }, |
| 282 | })).action, |
| 283 | ).not.toBe("replace"); |
| 284 | expect( |
| 285 | (yield* actionOf({ metadata: { "disable-legacy-endpoints": "true" } })).action, |
| 286 | ).not.toBe("replace"); |
| 287 | expect(yield* actionOf({ metadata: { owner: "team-a" } })).toMatchObject({ |
| 288 | action: "replace", |
| 289 | deleteFirst: true, |
| 290 | }); |
| 291 | |
| 292 | const adoptedPool = yield* stack.deploy( |
| 293 | adopted({ metadata: { "disable-legacy-endpoints": "true" } }), |
| 294 | ); |
| 295 | expect(adoptedPool.nodePoolId).toEqual(foreignId); |
| 296 | yield* stack.destroy(); |
| 297 | expect(yield* waitUntilGone(foreignName)).toEqual("gone"); |
| 298 | }), |
| 299 | ); |
| 300 | }), |
| 301 | ).pipe(logLevel), |
| 302 | // Host create ~6 min, pool create/update ~5 min, host delete ~5 min. |
| 303 | { |
| 304 | tags: ["provider:gcp", "provider:gcp:container", "live"], |
| 305 | timeout: 1_800_000, |
| 306 | retry: 0, |
| 307 | }, |
| 308 | ); |