The best way to control Claude Code, Codex and more from any computer or phone
Mirrored from https://github.com/Camo67/t3code.git
Synced Nov 21, 06:00 UTC
| 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-cnp-host"; |
| 25 | const HOST_ZONE = CAPACITY_ZONE; |
| 26 | |
| 27 | const waitUntilGone = (project: string, zone: string, clusterId: string, nodePoolId: string) => |
| 28 | container |
| 29 | .getProjectsZonesClustersNodePools({ |
| 30 | projectId: project, |
| 31 | zone, |
| 32 | clusterId, |
| 33 | nodePoolId, |
| 34 | }) |
| 35 | .pipe( |
| 36 | Effect.as("found" as const), |
| 37 | Effect.catchTag("NotFound", () => Effect.succeed("gone" as const)), |
| 38 | Effect.repeat({ |
| 39 | schedule: Schedule.spaced("3 seconds"), |
| 40 | until: (status) => status === "gone", |
| 41 | times: 10, |
| 42 | }), |
| 43 | ); |
| 44 | |
| 45 | const lastSegment = (value: string) => { |
| 46 | const trimmed = value.replace(/\/+$/, ""); |
| 47 | const parts = trimmed.split("/"); |
| 48 | return parts[parts.length - 1] || trimmed; |
| 49 | }; |
| 50 | |
| 51 | // GKE cluster create/delete takes 5–10 minutes. |
| 52 | const waitClusterOp = (project: string, zone: string, operation: container.Operation) => { |
| 53 | const raw = operation.name ?? ""; |
| 54 | const fromLink = operation.selfLink ?? ""; |
| 55 | const operationId = lastSegment( |
| 56 | raw.includes("/operations/") ? raw : fromLink.includes("/operations/") ? fromLink : raw, |
| 57 | ); |
| 58 | return waitForOperation( |
| 59 | { ...operation, name: operationId }, |
| 60 | (id) => |
| 61 | container.getProjectsZonesOperations({ |
| 62 | projectId: project, |
| 63 | zone, |
| 64 | operationId: id, |
| 65 | }), |
| 66 | { budget: "20 minutes", interval: "10 seconds" }, |
| 67 | ); |
| 68 | }; |
| 69 | |
| 70 | test.provider( |
| 71 | "lists zonal clusters and treats a missing node pool as NotFound", |
| 72 | (stack) => |
| 73 | Effect.gen(function* () { |
| 74 | yield* stack.destroy(); |
| 75 | |
| 76 | const { project } = yield* GcpEnvironment.current; |
| 77 | const page = yield* container.listProjectsZonesClusters({ |
| 78 | projectId: project, |
| 79 | zone: "-", |
| 80 | }); |
| 81 | expect((page.clusters ?? []).map((cluster) => cluster.name)).not.toContain( |
| 82 | "alchemy-missing-cluster", |
| 83 | ); |
| 84 | |
| 85 | const missing = yield* container |
| 86 | .getProjectsZonesClustersNodePools({ |
| 87 | projectId: project, |
| 88 | zone: HOST_ZONE, |
| 89 | clusterId: "alchemy-missing-cluster", |
| 90 | nodePoolId: "alchemy-missing-pool", |
| 91 | }) |
| 92 | .pipe( |
| 93 | Effect.as("found" as const), |
| 94 | Effect.catchTag("NotFound", () => Effect.succeed("gone" as const)), |
| 95 | ); |
| 96 | expect(missing).toEqual("gone"); |
| 97 | |
| 98 | yield* stack.destroy(); |
| 99 | }).pipe(logLevel), |
| 100 | { tags: ["provider:gcp", "provider:gcp:container", "live"], timeout: 90_000 }, |
| 101 | ); |
| 102 | |
| 103 | /** |
| 104 | * A throwaway zonal host cluster without Workload Identity (the node pool |
| 105 | * must not request GKE_METADATA on it). Owned by this test: a leftover from |
| 106 | * an interrupted run is reused if RUNNING, replaced otherwise, and always |
| 107 | * deleted when the test ends. |
| 108 | */ |
| 109 | const withHostCluster = <A, E, R>(project: string, body: Effect.Effect<A, E, R>) => { |
| 110 | const ref = { |
| 111 | projectId: project, |
| 112 | zone: HOST_ZONE, |
| 113 | clusterId: HOST_CLUSTER_ID, |
| 114 | }; |
| 115 | const deleteHost = container.deleteProjectsZonesClusters(ref).pipe( |
| 116 | Effect.flatMap((operation) => waitClusterOp(project, HOST_ZONE, operation)), |
| 117 | Effect.catchTag("NotFound", () => Effect.void), |
| 118 | ); |
| 119 | const ensureHost = Effect.gen(function* () { |
| 120 | const existing = yield* container |
| 121 | .getProjectsZonesClusters(ref) |
| 122 | .pipe(Effect.catchTag("NotFound", () => Effect.succeed(undefined))); |
| 123 | if (existing?.status === "RUNNING") return; |
| 124 | if (existing !== undefined) yield* deleteHost; |
| 125 | const created = yield* container.createProjectsZonesClusters({ |
| 126 | projectId: project, |
| 127 | zone: HOST_ZONE, |
| 128 | body: { |
| 129 | cluster: { |
| 130 | name: HOST_CLUSTER_ID, |
| 131 | ipAllocationPolicy: { useIpAliases: true }, |
| 132 | nodePools: [ |
| 133 | { |
| 134 | name: "default-pool", |
| 135 | initialNodeCount: 1, |
| 136 | config: { |
| 137 | machineType: "e2-medium", |
| 138 | diskSizeGb: 20, |
| 139 | diskType: "pd-standard", |
| 140 | spot: true, |
| 141 | }, |
| 142 | }, |
| 143 | ], |
| 144 | }, |
| 145 | }, |
| 146 | }); |
| 147 | yield* waitClusterOp(project, HOST_ZONE, created); |
| 148 | }); |
| 149 | return ensureHost.pipe(Effect.andThen(body), Effect.ensuring(Effect.ignore(deleteHost))); |
| 150 | }; |
| 151 | |
| 152 | test.provider.skipIf(!runLifecycle)( |
| 153 | "create, update, and delete a zonal node pool", |
| 154 | (stack) => |
| 155 | withGkeClusterSlot( |
| 156 | Effect.gen(function* () { |
| 157 | yield* stack.destroy(); |
| 158 | const { project } = yield* GcpEnvironment.current; |
| 159 | const hostId = HOST_CLUSTER_ID; |
| 160 | const hostZone = HOST_ZONE; |
| 161 | yield* withHostCluster( |
| 162 | project, |
| 163 | Effect.gen(function* () { |
| 164 | const created = yield* stack.deploy( |
| 165 | Effect.gen(function* () { |
| 166 | const pool = yield* GCP.Container.ClustersNodePool("Workers", { |
| 167 | cluster: hostId, |
| 168 | zone: hostZone, |
| 169 | nodeCount: POOL_NODE_COUNT, |
| 170 | machineType: "e2-medium", |
| 171 | diskSizeGb: 20, |
| 172 | spot: true, |
| 173 | management: { autoRepair: false, autoUpgrade: true }, |
| 174 | labels: { env: "test" }, |
| 175 | metadata: { "disable-legacy-endpoints": "true" }, |
| 176 | // The host has no Workload Identity, so GKE_METADATA is |
| 177 | // rejected; set the alternative explicitly. |
| 178 | workloadMetadataConfig: { mode: "GCE_METADATA" }, |
| 179 | shieldedInstanceConfig: { |
| 180 | enableIntegrityMonitoring: true, |
| 181 | enableSecureBoot: true, |
| 182 | }, |
| 183 | advancedMachineFeatures: { enableNestedVirtualization: false }, |
| 184 | }); |
| 185 | return { pool }; |
| 186 | }), |
| 187 | ); |
| 188 | |
| 189 | expect(created.pool.name).toContain("/nodePools/"); |
| 190 | expect(created.pool.nodePoolId).toEqual(expect.any(String)); |
| 191 | expect(created.pool.clusterId).toEqual(hostId); |
| 192 | expect(created.pool.zone).toEqual(hostZone); |
| 193 | expect(created.pool.labels).toMatchObject({ env: "test" }); |
| 194 | expect(created.pool.spot).toEqual(true); |
| 195 | expect(created.pool.nodeCount).toEqual(POOL_NODE_COUNT); |
| 196 | expect(created.pool.metadata["disable-legacy-endpoints"]).toEqual("true"); |
| 197 | expect(created.pool.workloadMetadataConfig?.mode).toEqual("GCE_METADATA"); |
| 198 | expect(created.pool.shieldedInstanceConfig?.enableSecureBoot).toEqual(true); |
| 199 | expect(["RUNNING", "RUNNING_WITH_ERROR"]).toContain(created.pool.status); |
| 200 | |
| 201 | const fetched = yield* container.getProjectsZonesClustersNodePools({ |
| 202 | projectId: project, |
| 203 | zone: hostZone, |
| 204 | clusterId: hostId, |
| 205 | nodePoolId: created.pool.nodePoolId, |
| 206 | }); |
| 207 | expect(fetched.name).toEqual(created.pool.nodePoolId); |
| 208 | expect(fetched.config?.resourceLabels?.env).toEqual("test"); |
| 209 | expect(fetched.config?.spot).toEqual(true); |
| 210 | expect(fetched.config?.metadata?.["disable-legacy-endpoints"]).toEqual("true"); |
| 211 | expect(fetched.config?.workloadMetadataConfig?.mode).toEqual("GCE_METADATA"); |
| 212 | expect(fetched.config?.shieldedInstanceConfig?.enableSecureBoot).toEqual(true); |
| 213 | |
| 214 | const workersProps: GCP.Container.ClustersNodePoolProps = { |
| 215 | cluster: hostId, |
| 216 | zone: hostZone, |
| 217 | nodePoolId: created.pool.nodePoolId, |
| 218 | nodeCount: POOL_NODE_COUNT, |
| 219 | machineType: "e2-medium", |
| 220 | diskSizeGb: 20, |
| 221 | spot: true, |
| 222 | management: { autoRepair: true, autoUpgrade: true }, |
| 223 | labels: { env: "prod", role: "workers" }, |
| 224 | metadata: { "disable-legacy-endpoints": "true" }, |
| 225 | // The host has no Workload Identity, so GKE_METADATA is |
| 226 | // rejected; set the alternative explicitly. |
| 227 | workloadMetadataConfig: { mode: "GCE_METADATA" }, |
| 228 | shieldedInstanceConfig: { |
| 229 | enableIntegrityMonitoring: true, |
| 230 | enableSecureBoot: true, |
| 231 | }, |
| 232 | advancedMachineFeatures: { enableNestedVirtualization: false }, |
| 233 | }; |
| 234 | const workers = (props: Partial<GCP.Container.ClustersNodePoolProps> = {}) => |
| 235 | GCP.Container.ClustersNodePool("Workers", { ...workersProps, ...props }); |
| 236 | |
| 237 | const updated = yield* stack.deploy(workers()); |
| 238 | |
| 239 | expect(updated.name).toEqual(created.pool.name); |
| 240 | expect(updated.labels).toMatchObject({ |
| 241 | env: "prod", |
| 242 | role: "workers", |
| 243 | }); |
| 244 | expect(updated.management?.autoRepair).toEqual(true); |
| 245 | |
| 246 | const refetched = yield* container.getProjectsZonesClustersNodePools({ |
| 247 | projectId: project, |
| 248 | zone: hostZone, |
| 249 | clusterId: hostId, |
| 250 | nodePoolId: created.pool.nodePoolId, |
| 251 | }); |
| 252 | expect(refetched.config?.resourceLabels?.env).toEqual("prod"); |
| 253 | expect(refetched.config?.resourceLabels?.role).toEqual("workers"); |
| 254 | expect(refetched.management?.autoRepair).toEqual(true); |
| 255 | |
| 256 | // VM settings are fixed when the pool's nodes are created. |
| 257 | expect( |
| 258 | (yield* stack.plan( |
| 259 | workers({ advancedMachineFeatures: { enableNestedVirtualization: true } }), |
| 260 | )).resources.Workers, |
| 261 | ).toMatchObject({ action: "replace", deleteFirst: true }); |
| 262 | |
| 263 | yield* stack.destroy(); |
| 264 | |
| 265 | const gone = yield* waitUntilGone(project, hostZone, hostId, created.pool.nodePoolId); |
| 266 | expect(gone).toEqual("gone"); |
| 267 | |
| 268 | // Adopting a pool created outside Alchemy: GKE injects metadata and |
| 269 | // omits false booleans, which must not count as changes. |
| 270 | const foreignId = "alch-cnp-foreign"; |
| 271 | const poolRef = { projectId: project, zone: hostZone, clusterId: hostId }; |
| 272 | const existingForeign = yield* container |
| 273 | .getProjectsZonesClustersNodePools({ ...poolRef, nodePoolId: foreignId }) |
| 274 | .pipe(Effect.catchTag("NotFound", () => Effect.succeed(undefined))); |
| 275 | if (existingForeign === undefined) { |
| 276 | const op = yield* container.createProjectsZonesClustersNodePools({ |
| 277 | ...poolRef, |
| 278 | body: { |
| 279 | nodePool: { |
| 280 | name: foreignId, |
| 281 | initialNodeCount: POOL_NODE_COUNT, |
| 282 | config: { machineType: "e2-medium", diskSizeGb: 20, spot: true }, |
| 283 | }, |
| 284 | }, |
| 285 | }); |
| 286 | yield* waitClusterOp(project, hostZone, op); |
| 287 | } |
| 288 | const adopted = (props: Partial<GCP.Container.ClustersNodePoolProps>) => |
| 289 | GCP.Container.ClustersNodePool("Adopted", { |
| 290 | cluster: hostId, |
| 291 | zone: hostZone, |
| 292 | nodePoolId: foreignId, |
| 293 | nodeCount: POOL_NODE_COUNT, |
| 294 | machineType: "e2-medium", |
| 295 | diskSizeGb: 20, |
| 296 | spot: true, |
| 297 | ...props, |
| 298 | }).pipe(adopt(true)); |
| 299 | const actionOf = (props: Partial<GCP.Container.ClustersNodePoolProps>) => |
| 300 | stack.plan(adopted(props)).pipe(Effect.map((plan) => plan.resources.Adopted)); |
| 301 | |
| 302 | expect((yield* actionOf({ metadata: {} })).action).not.toBe("replace"); |
| 303 | expect( |
| 304 | (yield* actionOf({ |
| 305 | metadata: {}, |
| 306 | shieldedInstanceConfig: { enableSecureBoot: false }, |
| 307 | })).action, |
| 308 | ).not.toBe("replace"); |
| 309 | expect( |
| 310 | (yield* actionOf({ metadata: { "disable-legacy-endpoints": "true" } })).action, |
| 311 | ).not.toBe("replace"); |
| 312 | expect(yield* actionOf({ metadata: { owner: "team-a" } })).toMatchObject({ |
| 313 | action: "replace", |
| 314 | deleteFirst: true, |
| 315 | }); |
| 316 | |
| 317 | const adoptedPool = yield* stack.deploy( |
| 318 | adopted({ metadata: { "disable-legacy-endpoints": "true" } }), |
| 319 | ); |
| 320 | expect(adoptedPool.nodePoolId).toEqual(foreignId); |
| 321 | yield* stack.destroy(); |
| 322 | expect(yield* waitUntilGone(project, hostZone, hostId, foreignId)).toEqual("gone"); |
| 323 | }), |
| 324 | ); |
| 325 | }), |
| 326 | ).pipe(logLevel), |
| 327 | // Host create ~6 min, pool create/update ~5 min, host delete ~5 min. |
| 328 | { |
| 329 | tags: ["provider:gcp", "provider:gcp:container", "live"], |
| 330 | timeout: 1_800_000, |
| 331 | retry: 0, |
| 332 | }, |
| 333 | ); |