@devries-cameron20 / t3code Mirror · read-only
Fork 0

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

main
ts 146 lines 5.14 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 dataflow from "@distilled.cloud/gcp/dataflow_v1b3";
8 import * as datapipelines from "@distilled.cloud/gcp/datapipelines_v1";
9 import * as Effect from "effect/Effect";
10 import * as Schedule from "effect/Schedule";
11 import { expectProbe } from "../bindingHost.ts";
12 import DataPipelinesBindingsHost, { BindBatch } from "./fixtures/bindings-host.ts";
13
14 const testOptions = { providers: GCP.providers() };
15 const { test, beforeAll, afterAll } = Test.make(testOptions);
16 const sharedStack = Core.scratchStack(testOptions, "DataPipelinesBindings");
17
18 let baseUrl: string;
19 let pipelineName: string;
20 let hostAccount: string;
21
22 /** Roles the host's service account holds on the project policy. */
23 const projectRoles = Effect.gen(function* () {
24 const { project } = yield* GcpEnvironment.current;
25 const policy = yield* crm.getIamPolicyProjects({
26 resource: `projects/${project}`,
27 body: { options: { requestedPolicyVersion: 3 } },
28 });
29 return (policy.bindings ?? [])
30 .filter((binding) => (binding.members ?? []).includes(`serviceAccount:${hostAccount}`))
31 .map((binding) => ({
32 role: binding.role,
33 condition: binding.condition?.expression,
34 }))
35 .sort((a, b) => (a.role ?? "").localeCompare(b.role ?? ""));
36 });
37
38 // Data Pipelines has no resource-level IAM, so both bindings grant on the
39 // project (unconditioned).
40 const expectedProjectRoles = [
41 { role: "roles/datapipelines.admin", condition: undefined },
42 { role: "roles/datapipelines.invoker", condition: undefined },
43 ];
44
45 // RunPipeline must run before StopPipeline archives the pipeline.
46 describe(
47 "DataPipelines Bindings",
48 {
49 sequential: true,
50 tags: ["provider:gcp", "provider:gcp:datapipelines", "provider:gcp:run", "live"],
51 },
52 () => {
53 beforeAll(
54 Effect.gen(function* () {
55 yield* sharedStack.destroy();
56 const out = yield* sharedStack.deploy(
57 Effect.gen(function* () {
58 const host = yield* DataPipelinesBindingsHost;
59 const pipeline = yield* BindBatch;
60 return {
61 uri: host.uri,
62 serviceAccount: host.serviceAccount,
63 pipeline: pipeline.name,
64 };
65 }),
66 );
67 baseUrl = out.uri!;
68 pipelineName = out.pipeline;
69 hostAccount = out.serviceAccount!;
70 }),
71 { timeout: 900_000 },
72 );
73
74 afterAll(sharedStack.destroy(), { timeout: 600_000 });
75
76 describe("RunPipeline", () => {
77 test.provider(
78 "launches a pipeline job as the host's service account",
79 (_stack) =>
80 Effect.gen(function* () {
81 const ran =
82 yield* expectProbe<datapipelines.GoogleCloudDatapipelinesV1RunPipelineResponse>(
83 baseUrl,
84 "runPipeline",
85 );
86 expect(ran.job?.id).toEqual(expect.any(String));
87
88 const jobIds = yield* datapipelines
89 .listProjectsLocationsPipelinesJobs({ parent: pipelineName })
90 .pipe(
91 Effect.map((page) => (page.jobs ?? []).map((job) => job.id)),
92 Effect.repeat({
93 schedule: Schedule.spaced("3 seconds"),
94 until: (ids) => ids.includes(ran.job?.id),
95 times: 20,
96 }),
97 );
98 expect(jobIds).toContain(ran.job?.id);
99
100 // The pipeline job is a real Dataflow job (Word Count on one file:
101 // it finishes, or fails once the scratch bucket is destroyed, on
102 // its own within minutes).
103 const { project } = yield* GcpEnvironment.current;
104 const job = yield* dataflow.getProjectsLocationsJobs({
105 projectId: project,
106 location: "us-central1",
107 jobId: ran.job!.id!,
108 });
109 expect(job.id).toEqual(ran.job?.id);
110
111 expect(yield* projectRoles).toEqual(expectedProjectRoles);
112 }),
113 {
114 tags: ["provider:gcp", "provider:gcp:datapipelines", "live"],
115 timeout: 600_000,
116 },
117 );
118 });
119
120 describe("StopPipeline", () => {
121 test.provider(
122 "archives the pipeline as the host's service account",
123 (_stack) =>
124 Effect.gen(function* () {
125 const stopped = yield* expectProbe<datapipelines.GoogleCloudDatapipelinesV1Pipeline>(
126 baseUrl,
127 "stopPipeline",
128 );
129 expect(stopped.name).toEqual(pipelineName);
130 expect(stopped.state).toEqual("STATE_ARCHIVED");
131
132 const live = yield* datapipelines.getProjectsLocationsPipelines({
133 name: pipelineName,
134 });
135 expect(live.state).toEqual("STATE_ARCHIVED");
136
137 expect(yield* projectRoles).toEqual(expectedProjectRoles);
138 }),
139 {
140 tags: ["provider:gcp", "provider:gcp:datapipelines", "live"],
141 timeout: 600_000,
142 },
143 );
144 });
145 },
146 );