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

main
ts 189 lines 6.64 KB
Raw
1 import * as GCP from "@alchemy.run/gcp";
2 import { GcpEnvironment } from "@alchemy.run/gcp/Environment";
3 import { describe, expect } from "@alchemy.run/test";
4 import * as Test from "@alchemy.run/test/Alchemy";
5 import * as Core from "@alchemy.run/test/Core";
6 import * as crm from "@distilled.cloud/gcp/cloudresourcemanager_v3";
7 import * as composer from "@distilled.cloud/gcp/composer_v1";
8 import * as Effect from "effect/Effect";
9 import * as Schedule from "effect/Schedule";
10 import { expectProbe } from "../bindingHost.ts";
11 import ComposerBindingsHost, { Airflow, TaskConfig, TaskSecret } from "./fixtures/bindings-host.ts";
12
13 const testOptions = { providers: GCP.providers() };
14 const { test, beforeAll, afterAll } = Test.make(testOptions);
15 const sharedStack = Core.scratchStack(testOptions, "ComposerBindings");
16
17 // Composer environments take 20-45 minutes to provision.
18 const runLifecycle = !!process.env.GCP_TEST_SLOW && !process.env.FAST;
19
20 let baseUrl: string;
21 let hostAccount: string;
22 let environmentName: string;
23 let configMapName: string;
24 let secretName: string;
25
26 /**
27 * Composer has no resource-level IAM, so its bindings grant on the project:
28 * `composer.viewer` (the Get* bindings) and `composer.editor`
29 * (ExecuteAirflowCommand — the narrowest role holding
30 * `composer.environments.executeAirflowCommand`).
31 */
32 const expectProjectGrants = Effect.gen(function* () {
33 const { project } = yield* GcpEnvironment.current;
34 const policy = yield* crm.getIamPolicyProjects({
35 resource: `projects/${project}`,
36 body: { options: { requestedPolicyVersion: 3 } },
37 });
38 const grants = (policy.bindings ?? [])
39 .filter((binding) => (binding.members ?? []).includes(`serviceAccount:${hostAccount}`))
40 .map((binding) => ({
41 role: binding.role,
42 condition: binding.condition?.expression,
43 }))
44 .sort((a, b) => (a.role ?? "").localeCompare(b.role ?? ""));
45 expect(grants).toEqual([
46 { role: "roles/composer.editor", condition: undefined },
47 { role: "roles/composer.viewer", condition: undefined },
48 ]);
49 });
50
51 describe.skipIf(!runLifecycle)(
52 "Composer Bindings",
53 {
54 tags: ["provider:gcp", "provider:gcp:composer", "provider:gcp:run", "live"],
55 },
56 () => {
57 beforeAll(
58 Effect.gen(function* () {
59 yield* sharedStack.destroy();
60 const out = yield* sharedStack.deploy(
61 Effect.gen(function* () {
62 const host = yield* ComposerBindingsHost;
63 const environment = yield* Airflow;
64 const config = yield* TaskConfig;
65 const secret = yield* TaskSecret;
66 return {
67 uri: host.uri,
68 serviceAccount: host.serviceAccount,
69 environment: environment.name,
70 config: config.name,
71 secret: secret.name,
72 };
73 }),
74 );
75 baseUrl = out.uri!;
76 hostAccount = out.serviceAccount!;
77 environmentName = out.environment;
78 configMapName = out.config;
79 secretName = out.secret;
80 }),
81 // Composer environment create alone can take ~45 minutes.
82 { timeout: 5_400_000 },
83 );
84
85 afterAll(sharedStack.destroy(), { timeout: 3_600_000 });
86
87 describe("GetEnvironment", () => {
88 test.provider(
89 "reads the environment as the host's service account",
90 (_stack) =>
91 Effect.gen(function* () {
92 const environment = yield* expectProbe<composer.Environment>(baseUrl, "getEnvironment");
93 const live = yield* composer.getProjectsLocationsEnvironments({
94 name: environmentName,
95 });
96 expect(environment.name).toEqual(environmentName);
97 expect(environment.uuid).toEqual(live.uuid);
98 yield* expectProjectGrants;
99 }),
100 {
101 tags: ["provider:gcp", "provider:gcp:composer", "live"],
102 timeout: 600_000,
103 },
104 );
105 });
106
107 describe("ExecuteAirflowCommand", () => {
108 test.provider(
109 "runs `airflow version` as the host's service account",
110 (_stack) =>
111 Effect.gen(function* () {
112 const started = yield* expectProbe<composer.ExecuteAirflowCommandResponse>(
113 baseUrl,
114 "executeAirflowCommand",
115 );
116 expect(started.error ?? "").toEqual("");
117 expect(started.executionId).toEqual(expect.any(String));
118
119 // The execution is visible out of band and finishes cleanly.
120 const polled = yield* composer
121 .pollAirflowCommandProjectsLocationsEnvironments({
122 environment: environmentName,
123 body: {
124 executionId: started.executionId,
125 pod: started.pod,
126 podNamespace: started.podNamespace,
127 // The API rejects a missing or zero line number.
128 nextLineNumber: 1,
129 },
130 })
131 .pipe(
132 Effect.repeat({
133 schedule: Schedule.spaced("5 seconds"),
134 until: (response) => response.outputEnd === true,
135 times: 24,
136 }),
137 );
138 expect(polled.exitInfo?.exitCode ?? 0).toEqual(0);
139 yield* expectProjectGrants;
140 }),
141 {
142 tags: ["provider:gcp", "provider:gcp:composer", "live"],
143 timeout: 600_000,
144 },
145 );
146 });
147
148 describe("GetUserWorkloadsConfigMap", () => {
149 test.provider(
150 "reads the ConfigMap as the host's service account",
151 (_stack) =>
152 Effect.gen(function* () {
153 const configMap = yield* expectProbe<composer.UserWorkloadsConfigMap>(
154 baseUrl,
155 "getUserWorkloadsConfigMap",
156 );
157 expect(configMap.name).toEqual(configMapName);
158 expect(configMap.data).toEqual({ LOG_LEVEL: "INFO" });
159 yield* expectProjectGrants;
160 }),
161 {
162 tags: ["provider:gcp", "provider:gcp:composer", "live"],
163 timeout: 600_000,
164 },
165 );
166 });
167
168 describe("GetUserWorkloadsSecret", () => {
169 test.provider(
170 "reads the Secret as the host's service account",
171 (_stack) =>
172 Effect.gen(function* () {
173 const secret = yield* expectProbe<composer.UserWorkloadsSecret>(
174 baseUrl,
175 "getUserWorkloadsSecret",
176 );
177 expect(secret.name).toEqual(secretName);
178 // Composer redacts secret values on read; the key survives.
179 expect(Object.keys(secret.data ?? {})).toEqual(["password"]);
180 yield* expectProjectGrants;
181 }),
182 {
183 tags: ["provider:gcp", "provider:gcp:composer", "live"],
184 timeout: 600_000,
185 },
186 );
187 });
188 },
189 );