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

main
ts 124 lines 4.13 KB
Raw
1 import * as GCP from "@alchemy.run/gcp";
2 import { describe, expect } from "@alchemy.run/test";
3 import * as Test from "@alchemy.run/test/Alchemy";
4 import * as Core from "@alchemy.run/test/Core";
5 import * as resourcemanager from "@distilled.cloud/gcp/cloudresourcemanager_v3";
6 import * as tpu from "@distilled.cloud/gcp/tpu_v2";
7 import * as Effect from "effect/Effect";
8 import { expectProbe } from "../bindingHost.ts";
9 import TpuBindingsHost, { QueuedTrainer, Trainer } from "./fixtures/bindings-host.ts";
10
11 const testOptions = { providers: GCP.providers() };
12 const { test, beforeAll, afterAll } = Test.make(testOptions);
13 const sharedStack = Core.scratchStack(testOptions, "TpuBindings");
14
15 // Cloud TPU is disabled on the testing project (ServiceDisabled "Cloud TPU API
16 // has not been used in project ...") and TPU VMs bill by the hour (dollars
17 // per run) and need TPU quota; set GCP_TEST_TPU=1 to opt in.
18 const runLifecycle = !!process.env.GCP_TEST_TPU && !process.env.FAST;
19
20 let baseUrl: string;
21 let hostAccount: string;
22 let project: string;
23 let nodeName: string;
24 let queuedResourceName: string;
25
26 /** Project-level roles held by the host's service account. */
27 const projectRoles = () =>
28 resourcemanager
29 .getIamPolicyProjects({
30 resource: `projects/${project}`,
31 body: { options: { requestedPolicyVersion: 3 } },
32 })
33 .pipe(
34 Effect.map((policy) =>
35 (policy.bindings ?? [])
36 .filter((binding) => (binding.members ?? []).includes(`serviceAccount:${hostAccount}`))
37 .map((binding) => ({
38 role: binding.role,
39 condition: binding.condition?.expression,
40 })),
41 ),
42 );
43
44 describe.skipIf(!runLifecycle)(
45 "TPU Bindings",
46 { tags: ["provider:gcp", "provider:gcp:tpu", "provider:gcp:run", "live"] },
47 () => {
48 beforeAll(
49 Effect.gen(function* () {
50 yield* sharedStack.destroy();
51 const out = yield* sharedStack.deploy(
52 Effect.gen(function* () {
53 const host = yield* TpuBindingsHost;
54 const node = yield* Trainer;
55 const queued = yield* QueuedTrainer;
56 return {
57 uri: host.uri,
58 serviceAccount: host.serviceAccount,
59 project: host.project,
60 node: node.name,
61 queued: queued.name,
62 };
63 }),
64 );
65 baseUrl = out.uri!;
66 hostAccount = out.serviceAccount!;
67 project = out.project;
68 nodeName = out.node;
69 queuedResourceName = out.queued;
70 }),
71 { timeout: 1_800_000 },
72 );
73
74 afterAll(sharedStack.destroy(), { timeout: 1_200_000 });
75
76 // Cloud TPU has no resource-level IAM: both bindings grant tpu.viewer
77 // on the project.
78 const expectTpuViewerOnly = Effect.gen(function* () {
79 expect(yield* projectRoles()).toEqual([{ role: "roles/tpu.viewer", condition: undefined }]);
80 });
81
82 describe("GetNode", () => {
83 test.provider(
84 "reads the bound node as the host",
85 (_stack) =>
86 Effect.gen(function* () {
87 const out = yield* expectProbe<{
88 name: string;
89 acceleratorType: string;
90 }>(baseUrl, "getNode");
91 const live = yield* tpu.getProjectsLocationsNodes({
92 name: nodeName,
93 });
94 expect(out.name).toEqual(live.name);
95 expect(out.acceleratorType).toEqual("v2-8");
96 yield* expectTpuViewerOnly;
97 }),
98 {
99 tags: ["provider:gcp", "provider:gcp:tpu", "live"],
100 timeout: 600_000,
101 },
102 );
103 });
104
105 describe("GetQueuedResource", () => {
106 test.provider(
107 "reads the bound queued resource as the host",
108 (_stack) =>
109 Effect.gen(function* () {
110 const out = yield* expectProbe<{ name: string }>(baseUrl, "getQueuedResource");
111 const live = yield* tpu.getProjectsLocationsQueuedResources({
112 name: queuedResourceName,
113 });
114 expect(out.name).toEqual(live.name);
115 yield* expectTpuViewerOnly;
116 }),
117 {
118 tags: ["provider:gcp", "provider:gcp:tpu", "live"],
119 timeout: 600_000,
120 },
121 );
122 });
123 },
124 );