1import * as GCP from "@/GCP";
2import * as Test from "@/Test/Alchemy";
3import * as dataproc from "@distilled.cloud/gcp/dataproc_v1";
4import { expect } from "alchemy-test";
5import * as Effect from "effect/Effect";
6import { MinimumLogLevel } from "effect/References";
7import * as Schedule from "effect/Schedule";
8import { GcpEnvironment } from "@/GCP/Environment";
9
10const { test } = Test.make({ providers: GCP.providers() });
11
12const logLevel = Effect.provideService(
13 MinimumLogLevel,
14 process.env.DEBUG ? "Debug" : "Info",
15);
16
17const waitUntilGone = (name: string) =>
18 dataproc.getProjectsRegionsAutoscalingPolicies({ name }).pipe(
19 Effect.as("found" as const),
20 Effect.catchTag("NotFound", () => Effect.succeed("gone" as const)),
21 Effect.repeat({
22 schedule: Schedule.spaced("1 second"),
23 until: (status) => status === "gone",
24 times: 10,
25 }),
26 );
27
28test.provider(
29 "getProjectsRegionsAutoscalingPolicies on a missing policy fails with NotFound",
30 (stack) =>
31 Effect.gen(function* () {
32 const { project } = yield* GcpEnvironment.current;
33 yield* stack.destroy();
34
35 const error = yield* Effect.flip(
36 dataproc.getProjectsRegionsAutoscalingPolicies({
37 name: `projects/${project}/regions/us-central1/autoscalingPolicies/alchemy-dataproc-missing`,
38 }),
39 );
40 expect(error._tag).toEqual("NotFound");
41
42 yield* stack.destroy();
43 }).pipe(logLevel),
44 { tags: ["provider:gcp", "provider:gcp:dataproc", "live"], timeout: 90_000 },
45);
46
47test.provider(
48 "create, update, and delete a regional autoscaling policy",
49 (stack) =>
50 Effect.gen(function* () {
51 yield* stack.destroy();
52
53 const created = yield* stack.deploy(
54 Effect.gen(function* () {
55 return yield* GCP.Dataproc.RegionsAutoscalingPolicy("SparkScaleReg", {
56 region: "us-central1",
57 workerConfig: { minInstances: 2, maxInstances: 3 },
58 labels: { env: "test" },
59 });
60 }),
61 );
62
63 expect(created.name).toContain("/regions/");
64 expect(created.name).toContain("/autoscalingPolicies/");
65 expect(created.region).toEqual("us-central1");
66 expect(created.labels).toMatchObject({ env: "test" });
67 expect(created.workerMaxInstances).toEqual(3);
68
69 const fetched = yield* dataproc.getProjectsRegionsAutoscalingPolicies({
70 name: created.name,
71 });
72 expect(fetched.name).toEqual(created.name);
73 expect(fetched.labels?.env).toEqual("test");
74
75 const updated = yield* stack.deploy(
76 Effect.gen(function* () {
77 return yield* GCP.Dataproc.RegionsAutoscalingPolicy("SparkScaleReg", {
78 policyId: created.policyId,
79 region: "us-central1",
80 workerConfig: { minInstances: 2, maxInstances: 5 },
81 labels: { env: "prod" },
82 });
83 }),
84 );
85
86 expect(updated.name).toEqual(created.name);
87 expect(updated.workerMaxInstances).toEqual(5);
88 expect(updated.labels).toMatchObject({ env: "prod" });
89
90 yield* stack.destroy();
91
92 const gone = yield* waitUntilGone(created.name);
93 expect(gone).toEqual("gone");
94 }).pipe(logLevel),
95 { tags: ["provider:gcp", "provider:gcp:dataproc", "live"], timeout: 90_000 },
96);