1import * as GCP from "@/GCP";
2import * as Test from "@/Test/Alchemy";
3import * as cloudrun from "@distilled.cloud/gcp/run_v2";
4import { expect } from "alchemy-test";
5import * as Effect from "effect/Effect";
6import { MinimumLogLevel } from "effect/References";
7import * as Schedule from "effect/Schedule";
8
9const { test } = Test.make({ providers: GCP.providers() });
10
11const logLevel = Effect.provideService(
12 MinimumLogLevel,
13 process.env.DEBUG ? "Debug" : "Info",
14);
15
16const WORKER_IMAGE = "us-docker.pkg.dev/cloudrun/container/worker-pool";
17
18const waitUntilGone = (name: string) =>
19 cloudrun.getProjectsLocationsWorkerPools({ name }).pipe(
20 Effect.as("found" as const),
21 Effect.catchTag("NotFound", () => Effect.succeed("gone" as const)),
22 Effect.repeat({
23 schedule: Schedule.spaced("2 seconds"),
24 until: (status) => status === "gone",
25 times: 10,
26 }),
27 );
28
29test.provider(
30 "create, update, and delete a Cloud Run worker pool",
31 (stack) =>
32 Effect.gen(function* () {
33 yield* stack.destroy();
34
35 const created = yield* stack.deploy(
36 Effect.gen(function* () {
37 return yield* GCP.Run.WorkerPool("Workers", {
38 location: "us-central1",
39 description: "test run worker pool",
40 labels: { env: "test" },
41 scaling: { manualInstanceCount: 0 },
42 template: {
43 containers: [{ image: WORKER_IMAGE }],
44 },
45 });
46 }),
47 );
48
49 expect(created.name).toContain("/workerPools/");
50 expect(created.workerPoolId).toEqual(expect.any(String));
51 expect(created.location).toEqual("us-central1");
52 expect(created.description).toEqual("test run worker pool");
53 expect(created.labels).toMatchObject({ env: "test" });
54 expect(created.image).toEqual(WORKER_IMAGE);
55 expect(created.terminalConditionState).toEqual("CONDITION_SUCCEEDED");
56
57 const fetched = yield* cloudrun.getProjectsLocationsWorkerPools({
58 name: created.name,
59 });
60 expect(fetched.name).toEqual(created.name);
61 expect(fetched.labels?.env).toEqual("test");
62 expect(fetched.description).toEqual("test run worker pool");
63 expect(fetched.template?.containers?.[0]?.image).toEqual(WORKER_IMAGE);
64 expect(fetched.scaling?.manualInstanceCount).toEqual(0);
65
66 const updated = yield* stack.deploy(
67 Effect.gen(function* () {
68 return yield* GCP.Run.WorkerPool("Workers", {
69 workerPoolId: created.workerPoolId,
70 location: "us-central1",
71 description: "prod run worker pool",
72 labels: { env: "prod", role: "worker" },
73 scaling: { manualInstanceCount: 0 },
74 template: {
75 containers: [
76 {
77 image: WORKER_IMAGE,
78 env: [{ name: "ENV", value: "prod" }],
79 },
80 ],
81 },
82 });
83 }),
84 );
85
86 expect(updated.name).toEqual(created.name);
87 expect(updated.description).toEqual("prod run worker pool");
88 expect(updated.labels).toMatchObject({ env: "prod", role: "worker" });
89 expect(updated.image).toEqual(WORKER_IMAGE);
90
91 const refetched = yield* cloudrun.getProjectsLocationsWorkerPools({
92 name: created.name,
93 });
94 expect(refetched.description).toEqual("prod run worker pool");
95 expect(refetched.labels?.env).toEqual("prod");
96 expect(refetched.labels?.role).toEqual("worker");
97 expect(
98 refetched.template?.containers?.[0]?.env?.some(
99 (env) => env.name === "ENV" && env.value === "prod",
100 ),
101 ).toEqual(true);
102
103 yield* stack.destroy();
104
105 const gone = yield* waitUntilGone(created.name);
106 expect(gone).toEqual("gone");
107 }).pipe(logLevel),
108 { tags: ["provider:gcp", "provider:gcp:run", "live"], timeout: 120_000 },
109);