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

main
ts 121 lines 4.24 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 crm from "@distilled.cloud/gcp/cloudresourcemanager_v3";
6 import * as dataproc from "@distilled.cloud/gcp/dataproc_v1";
7 import * as Effect from "effect/Effect";
8 import { expectProbe } from "../bindingHost.ts";
9 import { CAPACITY_REGION } from "../zones.ts";
10 import DataprocBindingsHost, { Jobs } from "./fixtures/bindings-host.ts";
11
12 const testOptions = { providers: GCP.providers() };
13 const { test, beforeAll, afterAll } = Test.make(testOptions);
14 const sharedStack = Core.scratchStack(testOptions, "DataprocBindings");
15
16 // Cluster create + delete takes ~8 minutes (observed 475s).
17 const runLifecycle = !!process.env.GCP_TEST_SLOW && !process.env.FAST;
18
19 let baseUrl: string;
20 let hostAccount: string;
21 let clusterName: string;
22 let clusterResource: string;
23 let project: string;
24
25 const rolesOf = (bindings: ReadonlyArray<{ role?: string; members?: ReadonlyArray<string> }>) =>
26 bindings
27 .filter((binding) => (binding.members ?? []).includes(`serviceAccount:${hostAccount}`))
28 .map((binding) => binding.role)
29 .sort();
30
31 const projectRoles = Effect.gen(function* () {
32 const policy = yield* crm.getIamPolicyProjects({
33 resource: `projects/${project}`,
34 body: { options: { requestedPolicyVersion: 3 } },
35 });
36 return rolesOf(policy.bindings ?? []);
37 });
38
39 describe.skipIf(!runLifecycle)(
40 "Dataproc Bindings",
41 {
42 tags: ["provider:gcp", "provider:gcp:dataproc", "provider:gcp:run", "live"],
43 },
44 () => {
45 beforeAll(
46 Effect.gen(function* () {
47 yield* sharedStack.destroy();
48 const out = yield* sharedStack.deploy(
49 Effect.gen(function* () {
50 const host = yield* DataprocBindingsHost;
51 const cluster = yield* Jobs;
52 return {
53 uri: host.uri,
54 serviceAccount: host.serviceAccount,
55 clusterName: cluster.clusterName,
56 name: cluster.name,
57 project: cluster.project,
58 };
59 }),
60 );
61 baseUrl = out.uri!;
62 hostAccount = out.serviceAccount!;
63 clusterName = out.clusterName;
64 clusterResource = out.name;
65 project = out.project;
66 }),
67 { timeout: 1_800_000 },
68 );
69
70 afterAll(sharedStack.destroy(), { timeout: 1_200_000 });
71
72 describe("GetCluster", () => {
73 test.provider(
74 "reads the cluster as the host's service account, granted on the cluster only",
75 (_stack) =>
76 Effect.gen(function* () {
77 const cluster = yield* expectProbe<dataproc.Cluster>(baseUrl, "getCluster");
78 expect(cluster.clusterName).toEqual(clusterName);
79 expect(cluster.status?.state).toEqual("RUNNING");
80
81 const policy = yield* dataproc.getIamPolicyProjectsRegionsClusters({
82 resource: clusterResource,
83 });
84 expect(rolesOf(policy.bindings ?? [])).toEqual(["roles/dataproc.viewer"]);
85 expect(yield* projectRoles).not.toContain("roles/dataproc.viewer");
86 }),
87 {
88 tags: ["provider:gcp", "provider:gcp:dataproc", "live"],
89 timeout: 600_000,
90 },
91 );
92 });
93
94 describe("SubmitJob", () => {
95 test.provider(
96 "submits a job to the cluster as the host's service account",
97 (_stack) =>
98 Effect.gen(function* () {
99 const job = yield* expectProbe<dataproc.Job>(baseUrl, "submitJob");
100 expect(job.placement?.clusterName).toEqual(clusterName);
101 expect(job.reference?.jobId).toEqual(expect.any(String));
102
103 const live = yield* dataproc.getProjectsRegionsJobs({
104 projectId: project,
105 region: CAPACITY_REGION,
106 jobId: job.reference!.jobId!,
107 });
108 expect(live.placement?.clusterName).toEqual(clusterName);
109 expect(live.pigJob?.queryList?.queries).toEqual(["DUMP;"]);
110
111 // dataproc.jobs.create is checked on the project.
112 expect(yield* projectRoles).toEqual(["roles/dataproc.editor"]);
113 }),
114 {
115 tags: ["provider:gcp", "provider:gcp:dataproc", "live"],
116 timeout: 600_000,
117 },
118 );
119 });
120 },
121 );