| 1 | import * as GCP from "@/GCP"; |
| 2 | import * as Test from "@/Test/Alchemy"; |
| 3 | import * as dataproc from "@distilled.cloud/gcp/dataproc_v1"; |
| 4 | import { expect } from "alchemy-test"; |
| 5 | import * as Effect from "effect/Effect"; |
| 6 | import { MinimumLogLevel } from "effect/References"; |
| 7 | import * as Schedule from "effect/Schedule"; |
| 8 | import { GcpEnvironment } from "@/GCP/Environment"; |
| 9 | |
| 10 | const { test } = Test.make({ providers: GCP.providers() }); |
| 11 | |
| 12 | const logLevel = Effect.provideService( |
| 13 | MinimumLogLevel, |
| 14 | process.env.DEBUG ? "Debug" : "Info", |
| 15 | ); |
| 16 | |
| 17 | const waitUntilGone = (name: string) => |
| 18 | dataproc.getProjectsLocationsAutoscalingPolicies({ 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 | |
| 28 | test.provider( |
| 29 | "getProjectsLocationsAutoscalingPolicies 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.getProjectsLocationsAutoscalingPolicies({ |
| 37 | name: `projects/${project}/locations/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 | |
| 47 | test.provider( |
| 48 | "create, update, and delete an 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.AutoscalingPolicy("SparkScale", { |
| 56 | location: "us-central1", |
| 57 | workerConfig: { minInstances: 2, maxInstances: 3 }, |
| 58 | labels: { env: "test" }, |
| 59 | }); |
| 60 | }), |
| 61 | ); |
| 62 | |
| 63 | expect(created.name).toContain("/autoscalingPolicies/"); |
| 64 | expect(created.policyId).toEqual(expect.any(String)); |
| 65 | expect(created.location).toEqual("us-central1"); |
| 66 | expect(created.labels).toMatchObject({ env: "test" }); |
| 67 | expect(created.workerMaxInstances).toEqual(3); |
| 68 | |
| 69 | const fetched = yield* dataproc.getProjectsLocationsAutoscalingPolicies({ |
| 70 | name: created.name, |
| 71 | }); |
| 72 | expect(fetched.name).toEqual(created.name); |
| 73 | expect(fetched.labels?.env).toEqual("test"); |
| 74 | expect(fetched.workerConfig?.maxInstances).toEqual(3); |
| 75 | |
| 76 | const updated = yield* stack.deploy( |
| 77 | Effect.gen(function* () { |
| 78 | return yield* GCP.Dataproc.AutoscalingPolicy("SparkScale", { |
| 79 | policyId: created.policyId, |
| 80 | location: "us-central1", |
| 81 | workerConfig: { minInstances: 2, maxInstances: 4 }, |
| 82 | labels: { env: "prod", role: "scale" }, |
| 83 | }); |
| 84 | }), |
| 85 | ); |
| 86 | |
| 87 | expect(updated.name).toEqual(created.name); |
| 88 | expect(updated.workerMaxInstances).toEqual(4); |
| 89 | expect(updated.labels).toMatchObject({ env: "prod", role: "scale" }); |
| 90 | |
| 91 | const refetched = yield* dataproc.getProjectsLocationsAutoscalingPolicies( |
| 92 | { |
| 93 | name: created.name, |
| 94 | }, |
| 95 | ); |
| 96 | expect(refetched.workerConfig?.maxInstances).toEqual(4); |
| 97 | expect(refetched.labels?.env).toEqual("prod"); |
| 98 | |
| 99 | yield* stack.destroy(); |
| 100 | |
| 101 | const gone = yield* waitUntilGone(created.name); |
| 102 | expect(gone).toEqual("gone"); |
| 103 | }).pipe(logLevel), |
| 104 | { tags: ["provider:gcp", "provider:gcp:dataproc", "live"], timeout: 90_000 }, |
| 105 | ); |