The best way to control Claude Code, Codex and more from any computer or phone

main
ts 397 lines 14.5 KB
Raw
1 import * as GCP from "@alchemy.run/gcp";
2 import { GcpEnvironment } from "@alchemy.run/gcp/Environment";
3 import * as Kubernetes from "@alchemy.run/kubernetes";
4 import { connectCluster, readObject } from "@alchemy.run/kubernetes/Client";
5 import type { IdentityState, RegistryState } from "@alchemy.run/kubernetes/ClusterAdapter";
6 import type { Connection } from "@alchemy.run/kubernetes/Connection";
7 import type { KubernetesObjectRef } from "@alchemy.run/kubernetes/Objects";
8 import { describe, expect } from "@alchemy.run/test";
9 import * as Test from "@alchemy.run/test/Alchemy";
10 import * as Core from "@alchemy.run/test/Core";
11 import * as artifactregistry from "@distilled.cloud/gcp/artifactregistry_v1";
12 import * as resourcemanager from "@distilled.cloud/gcp/cloudresourcemanager_v3";
13 import * as compute from "@distilled.cloud/gcp/compute_v1";
14 import * as container from "@distilled.cloud/gcp/container_v1";
15 import * as storage from "@distilled.cloud/gcp/storage_v1";
16 import * as Data from "effect/Data";
17 import * as Effect from "effect/Effect";
18 import * as HttpClient from "effect/http/HttpClient";
19 import * as HttpClientRequest from "effect/http/HttpClientRequest";
20 import * as Layer from "effect/Layer";
21 import * as Schedule from "effect/Schedule";
22 import { SMOKE_REGION } from "../zones.ts";
23 import GkeSmokeApiLive, { GkeSmokeApi } from "./fixtures/smoke-api.ts";
24 import GkeSmokeJob from "./fixtures/smoke-job.ts";
25 import {
26 JOB_MARKER_BODY,
27 JOB_MARKER_KEY,
28 SMOKE_NAMESPACE,
29 SmokeBucket,
30 SmokeCluster,
31 SmokeNamespace,
32 } from "./fixtures/smoke-resources.ts";
33
34 /**
35 * GKE flagship smoke — the GCP counterpart of the ECS Task / EKS path:
36 *
37 * Autopilot `GCP.Container.Cluster` → an Effect-native
38 * `Kubernetes.Deployment` behind a GKE LoadBalancer, bound to a Cloud
39 * Storage bucket (`GCP.Storage.ReadWriteBucket`) through Workload
40 * Identity → an inline-effect `Kubernetes.Job` that writes a marker object
41 * through `GCP.Storage.WriteBucket`. Every side effect is verified
42 * out-of-band via distilled, and teardown is proven clean.
43 *
44 * Autopilot creation alone takes ~10 minutes, so the smoke only runs with
45 * `GCP_TEST_SLOW=1` (and never under `--fast`). The images are built
46 * locally, so it also needs a Docker daemon.
47 */
48
49 const skip = !process.env.GCP_TEST_SLOW || !!process.env.FAST;
50
51 const testOptions = {
52 providers: Layer.mergeAll(GCP.providers(), Kubernetes.providers()),
53 };
54 const { test, beforeAll, afterAll } = Test.make(testOptions);
55 // Durable (file-namespaced) state: a hard-killed run leaves resumable rows
56 // so the next run's up-front destroy reclaims the cluster.
57 const sharedStack = Core.scratchStack(
58 testOptions,
59 "GkeSmoke",
60 "test/GCP/Container/Cluster.smoke.test.ts",
61 );
62
63 interface StackOutputs {
64 clusterName: string;
65 connection: Connection;
66 bucketName: string;
67 url: string | undefined;
68 apiServiceAccount: string;
69 apiIdentity: IdentityState | undefined;
70 apiRegistry: RegistryState | undefined;
71 jobServiceAccount: string;
72 jobIdentity: IdentityState | undefined;
73 jobRegistry: RegistryState | undefined;
74 jobObjects: KubernetesObjectRef[];
75 }
76
77 let outputs: StackOutputs;
78 let baseUrl: string;
79
80 class NotReady extends Data.TaggedError("NotReady")<{
81 readonly detail: string;
82 }> {}
83
84 class StillExists extends Data.TaggedError("StillExists")<{
85 readonly what: string;
86 }> {}
87
88 const repositoryOf = (registry: RegistryState | undefined) =>
89 registry?.kind === "gcp-artifact-registry" ? registry.name : undefined;
90
91 const memberOf = (identity: IdentityState | undefined) =>
92 identity?.kind === "gcp-workload-identity" ? identity.member : undefined;
93
94 const deployProgram = Effect.gen(function* () {
95 const cluster = yield* SmokeCluster;
96 const bucket = yield* SmokeBucket;
97 yield* SmokeNamespace;
98 const api = yield* GkeSmokeApi;
99 const job = yield* GkeSmokeJob;
100 return {
101 clusterName: cluster.name,
102 connection: cluster.connection,
103 bucketName: bucket.bucketName,
104 url: api.url,
105 apiServiceAccount: api.serviceAccountName,
106 apiIdentity: api.identity,
107 apiRegistry: api.registry,
108 jobServiceAccount: job.serviceAccountName,
109 jobIdentity: job.identity,
110 jobRegistry: job.registry,
111 jobObjects: job.kubernetesObjects,
112 };
113 }).pipe(Effect.provide(GkeSmokeApiLive));
114
115 /** GET/PUT returning status + text; transport errors propagate. */
116 const call = (request: HttpClientRequest.HttpClientRequest) =>
117 HttpClient.execute(request).pipe(
118 Effect.flatMap((response) =>
119 response.text.pipe(Effect.map((text) => ({ status: response.status, text }))),
120 ),
121 );
122
123 /**
124 * Re-run `attempt` every 10s until `ready` accepts its value. Transport
125 * errors count as not ready — a fresh load balancer refuses connections
126 * until its backends pass health checks.
127 */
128 const until = <A, E, R>(
129 attempt: Effect.Effect<A, E, R>,
130 ready: (value: A) => boolean,
131 times: number,
132 ) =>
133 attempt.pipe(
134 Effect.filterOrFail(ready, (value) => new NotReady({ detail: JSON.stringify(value) })),
135 Effect.tapError((error) => Effect.logDebug(`not ready: ${String(error)}`)),
136 Effect.retry({ schedule: Schedule.spaced("10 seconds"), times }),
137 );
138
139 /** Bounded wait until an out-of-band probe reports the resource gone. */
140 const waitUntilGone = <E, R>(what: string, probe: Effect.Effect<boolean, E, R>) =>
141 probe.pipe(
142 Effect.flatMap((gone) => (gone ? Effect.void : Effect.fail(new StillExists({ what })))),
143 Effect.retry({
144 while: (e) => e instanceof StillExists,
145 schedule: Schedule.max([Schedule.fixed("5 seconds"), Schedule.recurs(24)]),
146 }),
147 );
148
149 /** The KSA Workload Identity principals, derived independently. */
150 const expectedPrincipals = Effect.gen(function* () {
151 const { project } = yield* GcpEnvironment.current;
152 const { name } = yield* resourcemanager.getProjects({
153 name: `projects/${project}`,
154 });
155 const number = (name ?? "").split("/").pop()!;
156 const principal = (ksa: string) =>
157 `principal://iam.googleapis.com/projects/${number}/locations/global/` +
158 `workloadIdentityPools/${project}.svc.id.goog/subject/ns/${SMOKE_NAMESPACE}/sa/${ksa}`;
159 return {
160 api: principal(outputs.apiServiceAccount),
161 job: principal(outputs.jobServiceAccount),
162 };
163 });
164
165 /** Load-balancer plumbing GKE created for Services in the smoke namespace. */
166 const namespaceLoadBalancers = Effect.gen(function* () {
167 const { project } = yield* GcpEnvironment.current;
168 const marker = `"kubernetes.io/service-name":"${SMOKE_NAMESPACE}/`;
169 const rules = yield* compute.listForwardingRules({
170 project,
171 region: SMOKE_REGION,
172 });
173 const backends = yield* compute.listRegionBackendServices({
174 project,
175 region: SMOKE_REGION,
176 });
177 return [
178 ...(rules.items ?? []).filter((r) => (r.description ?? "").includes(marker)),
179 ...(backends.items ?? []).filter((b) => (b.description ?? "").includes(marker)),
180 ].map((item) => item.name);
181 });
182
183 describe.skipIf(skip).sequential(
184 "GKE smoke",
185 {
186 tags: [
187 "provider:gcp",
188 "provider:gcp:container",
189 "provider:gcp:storage",
190 "provider:gcp:artifactregistry",
191 "provider:kubernetes",
192 "live",
193 ],
194 },
195 () => {
196 beforeAll(
197 Effect.gen(function* () {
198 yield* Effect.logInfo("GKE smoke: destroying previous stack");
199 yield* sharedStack.destroy();
200
201 yield* Effect.logInfo("GKE smoke: deploying stack (~10 min)");
202 outputs = (yield* sharedStack.deploy(deployProgram)) as StackOutputs;
203 expect(outputs.url).toMatch(/^http:\/\/[^/]+:3000$/);
204 baseUrl = outputs.url!.replace(/\/+$/, "");
205
206 yield* Effect.logInfo(`GKE smoke: probing ${baseUrl}/health`);
207 const health = yield* until(
208 call(HttpClientRequest.get(`${baseUrl}/health`)),
209 (r) => r.status === 200,
210 60,
211 );
212 expect(JSON.parse(health.text)).toEqual({ ok: true });
213 }),
214 { timeout: 1_800_000 },
215 );
216
217 afterAll(sharedStack.destroy(), { timeout: 1_800_000 });
218
219 test.provider(
220 "Workload Identity grants land on each KSA principal (bucket IAM)",
221 (_stack) =>
222 Effect.gen(function* () {
223 const principals = yield* expectedPrincipals;
224 // The adapter's recorded members are the same principals.
225 expect(memberOf(outputs.apiIdentity)).toBe(principals.api);
226 expect(memberOf(outputs.jobIdentity)).toBe(principals.job);
227
228 const policy = yield* storage.getIamPolicyBuckets({
229 bucket: outputs.bucketName,
230 optionsRequestedPolicyVersion: 3,
231 });
232 const rolesOf = (member: string) =>
233 (policy.bindings ?? [])
234 .filter((b) => (b.members ?? []).includes(member))
235 .map((b) => b.role);
236 // Least privilege, on the bucket only.
237 expect(rolesOf(principals.api)).toEqual(["roles/storage.objectUser"]);
238 expect(rolesOf(principals.job)).toEqual(["roles/storage.objectUser"]);
239
240 // Nothing was granted at the project level.
241 const { project } = yield* GcpEnvironment.current;
242 const projectPolicy = yield* resourcemanager.getIamPolicyProjects({
243 resource: `projects/${project}`,
244 body: { options: { requestedPolicyVersion: 3 } },
245 });
246 expect(
247 (projectPolicy.bindings ?? []).filter((b) =>
248 (b.members ?? []).some((m) => m === principals.api || m === principals.job),
249 ),
250 ).toEqual([]);
251 }),
252 {
253 tags: ["provider:gcp", "provider:gcp:container", "live"],
254 timeout: 120_000,
255 },
256 );
257
258 test.provider(
259 "the Deployment reads and writes the bucket from the pod through the LoadBalancer",
260 (_stack) =>
261 Effect.gen(function* () {
262 const key = "api/round-trip.txt";
263 const body = yield* Effect.sync(
264 () => `written through the GKE load balancer ${crypto.randomUUID()}`,
265 );
266
267 // A fresh bucket grant may take a moment to reach Storage; the
268 // pod answers 500 until then.
269 const put = yield* until(
270 call(
271 HttpClientRequest.put(`${baseUrl}/objects/${key}`).pipe(
272 HttpClientRequest.bodyText(body, "text/plain"),
273 ),
274 ),
275 (r) => r.status !== 500,
276 30,
277 );
278 expect(put.status).toBe(200);
279 expect(JSON.parse(put.text)).toEqual({ key, read: body });
280
281 // Out-of-band: the object is real.
282 const object = yield* storage.getObjects({
283 bucket: outputs.bucketName,
284 object: key,
285 });
286 expect(object.size).toBe(String(new TextEncoder().encode(body).length));
287 expect(object.contentType).toBe("text/plain");
288
289 const missing = yield* call(HttpClientRequest.get(`${baseUrl}/objects/nope/missing.txt`));
290 expect(missing.status).toBe(404);
291 }),
292 {
293 tags: ["provider:gcp", "provider:gcp:container", "live"],
294 timeout: 600_000,
295 },
296 );
297
298 test.provider(
299 "the inline-effect Job completes and its marker exists",
300 (_stack) =>
301 Effect.gen(function* () {
302 const ref = outputs.jobObjects.find((o) => o.kind === "Job");
303 expect(ref).toBeDefined();
304 const transport = yield* connectCluster(outputs.connection);
305
306 // Autopilot provisions a node for the pod on demand; the pod
307 // retries under the Job's backoff while the grant propagates.
308 const job = yield* until(
309 readObject({ transport, object: ref! }).pipe(
310 Effect.map(
311 (o) => (o as { status?: { succeeded?: number; failed?: number } }).status ?? {},
312 ),
313 ),
314 (status) => (status.succeeded ?? 0) >= 1,
315 60,
316 );
317 expect(job.succeeded).toBe(1);
318
319 // Out-of-band: the marker object the Job wrote.
320 const marker = yield* storage.getObjects({
321 bucket: outputs.bucketName,
322 object: JOB_MARKER_KEY,
323 });
324 expect(marker.size).toBe(String(new TextEncoder().encode(JOB_MARKER_BODY).length));
325
326 // …and the Deployment's read binding serves it back.
327 const read = yield* call(HttpClientRequest.get(`${baseUrl}/objects/${JOB_MARKER_KEY}`));
328 expect(read.status).toBe(200);
329 expect(read.text).toBe(JOB_MARKER_BODY);
330 }),
331 {
332 tags: ["provider:gcp", "provider:gcp:container", "live"],
333 timeout: 720_000,
334 },
335 );
336
337 test.provider(
338 "destroy leaves no cluster, load balancer, bucket, grants, or repository",
339 (_stack) =>
340 Effect.gen(function* () {
341 const principals = yield* expectedPrincipals;
342 const repositories = [outputs.apiRegistry, outputs.jobRegistry]
343 .map(repositoryOf)
344 .filter((name) => name !== undefined);
345 expect(repositories).toHaveLength(2);
346
347 yield* sharedStack.destroy();
348
349 yield* waitUntilGone(
350 "cluster",
351 container.getProjectsLocationsClusters({ name: outputs.clusterName }).pipe(
352 Effect.as(false),
353 Effect.catchTag("NotFound", () => Effect.succeed(true)),
354 ),
355 );
356 yield* waitUntilGone(
357 "bucket",
358 storage.getBuckets({ bucket: outputs.bucketName }).pipe(
359 Effect.as(false),
360 Effect.catchTag("NotFound", () => Effect.succeed(true)),
361 ),
362 );
363 for (const name of repositories) {
364 yield* waitUntilGone(
365 `repository ${name}`,
366 artifactregistry.getProjectsLocationsRepositories({ name }).pipe(
367 Effect.as(false),
368 Effect.catchTag("NotFound", () => Effect.succeed(true)),
369 ),
370 );
371 }
372 // The LoadBalancer Service was drained before the cluster went,
373 // so GKE left no forwarding rule or backend service behind.
374 yield* waitUntilGone(
375 "load balancer",
376 namespaceLoadBalancers.pipe(Effect.map((left) => left.length === 0)),
377 );
378 // No grant outlives the workloads at the project level either.
379 const { project } = yield* GcpEnvironment.current;
380 const projectPolicy = yield* resourcemanager.getIamPolicyProjects({
381 resource: `projects/${project}`,
382 body: { options: { requestedPolicyVersion: 3 } },
383 });
384 expect(
385 (projectPolicy.bindings ?? []).filter((b) =>
386 (b.members ?? []).some((m) => m === principals.api || m === principals.job),
387 ),
388 ).toEqual([]);
389 }),
390 {
391 tags: ["provider:gcp", "provider:gcp:container", "live"],
392 timeout: 1_200_000,
393 retry: 0,
394 },
395 );
396 },
397 );