| 1 | import * as GCP from "@/GCP"; |
| 2 | import * as Test from "@/Test/Alchemy"; |
| 3 | import * as composer from "@distilled.cloud/gcp/composer_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 | import { defaultComputeServiceAccount } from "./serviceAccount.ts"; |
| 10 | import { CAPACITY_REGION } from "../zones.ts"; |
| 11 | |
| 12 | const { test } = Test.make({ providers: GCP.providers() }); |
| 13 | |
| 14 | const logLevel = Effect.provideService( |
| 15 | MinimumLogLevel, |
| 16 | process.env.DEBUG ? "Debug" : "Info", |
| 17 | ); |
| 18 | |
| 19 | // Composer environments take 20-45 minutes to provision. |
| 20 | const runLifecycle = !!process.env.GCP_TEST_SLOW && !process.env.FAST; |
| 21 | |
| 22 | const waitUntilGone = (name: string) => |
| 23 | composer.getProjectsLocationsEnvironments({ name }).pipe( |
| 24 | Effect.as("found" as const), |
| 25 | Effect.catchTag("NotFound", () => Effect.succeed("gone" as const)), |
| 26 | Effect.repeat({ |
| 27 | schedule: Schedule.spaced("2 seconds"), |
| 28 | until: (status) => status === "gone", |
| 29 | times: 10, |
| 30 | }), |
| 31 | ); |
| 32 | |
| 33 | test.provider( |
| 34 | "getProjectsLocationsEnvironments on a missing environment fails with NotFound", |
| 35 | (stack) => |
| 36 | Effect.gen(function* () { |
| 37 | const { project } = yield* GcpEnvironment.current; |
| 38 | yield* stack.destroy(); |
| 39 | |
| 40 | const error = yield* Effect.flip( |
| 41 | composer.getProjectsLocationsEnvironments({ |
| 42 | name: `projects/${project}/locations/${CAPACITY_REGION}/environments/alchemy-composer-missing`, |
| 43 | }), |
| 44 | ); |
| 45 | expect(error._tag).toEqual("NotFound"); |
| 46 | |
| 47 | const page = yield* composer.listProjectsLocationsEnvironments({ |
| 48 | parent: `projects/${project}/locations/${CAPACITY_REGION}`, |
| 49 | pageSize: 10, |
| 50 | }); |
| 51 | expect( |
| 52 | (page.environments ?? []).map((environment) => environment.name), |
| 53 | ).not.toContain( |
| 54 | `projects/${project}/locations/${CAPACITY_REGION}/environments/alchemy-composer-missing`, |
| 55 | ); |
| 56 | |
| 57 | yield* stack.destroy(); |
| 58 | }).pipe(logLevel), |
| 59 | { tags: ["provider:gcp", "provider:gcp:composer", "live"], timeout: 90_000 }, |
| 60 | ); |
| 61 | |
| 62 | test.provider.skipIf(!runLifecycle)( |
| 63 | "create, update, and delete a composer environment", |
| 64 | (stack) => |
| 65 | Effect.gen(function* () { |
| 66 | const serviceAccount = yield* defaultComputeServiceAccount; |
| 67 | yield* stack.destroy(); |
| 68 | |
| 69 | const created = yield* stack.deploy( |
| 70 | Effect.gen(function* () { |
| 71 | return yield* GCP.Composer.Environment("Airflow", { |
| 72 | location: CAPACITY_REGION, |
| 73 | labels: { env: "test" }, |
| 74 | config: { |
| 75 | environmentSize: "ENVIRONMENT_SIZE_SMALL", |
| 76 | nodeConfig: { serviceAccount }, |
| 77 | softwareConfig: { imageVersion: "composer-3-airflow-2" }, |
| 78 | }, |
| 79 | }); |
| 80 | }), |
| 81 | ); |
| 82 | |
| 83 | expect(created.name).toContain("/environments/"); |
| 84 | expect(created.environmentId).toEqual(expect.any(String)); |
| 85 | expect(created.location).toEqual(CAPACITY_REGION); |
| 86 | expect(created.labels).toMatchObject({ env: "test" }); |
| 87 | expect(created.state).toEqual("RUNNING"); |
| 88 | |
| 89 | const fetched = yield* composer.getProjectsLocationsEnvironments({ |
| 90 | name: created.name, |
| 91 | }); |
| 92 | expect(fetched.name).toEqual(created.name); |
| 93 | expect(fetched.labels?.env).toEqual("test"); |
| 94 | expect(fetched.state).toEqual("RUNNING"); |
| 95 | expect(fetched.config?.nodeConfig?.serviceAccount).toEqual( |
| 96 | serviceAccount, |
| 97 | ); |
| 98 | |
| 99 | const updated = yield* stack.deploy( |
| 100 | Effect.gen(function* () { |
| 101 | return yield* GCP.Composer.Environment("Airflow", { |
| 102 | environmentId: created.environmentId, |
| 103 | location: CAPACITY_REGION, |
| 104 | labels: { env: "prod", role: "airflow" }, |
| 105 | config: { |
| 106 | environmentSize: "ENVIRONMENT_SIZE_SMALL", |
| 107 | nodeConfig: { serviceAccount }, |
| 108 | softwareConfig: { imageVersion: "composer-3-airflow-2" }, |
| 109 | }, |
| 110 | }); |
| 111 | }), |
| 112 | ); |
| 113 | |
| 114 | expect(updated.name).toEqual(created.name); |
| 115 | expect(updated.labels).toMatchObject({ env: "prod", role: "airflow" }); |
| 116 | |
| 117 | const refetched = yield* composer.getProjectsLocationsEnvironments({ |
| 118 | name: created.name, |
| 119 | }); |
| 120 | expect(refetched.labels?.env).toEqual("prod"); |
| 121 | expect(refetched.labels?.role).toEqual("airflow"); |
| 122 | |
| 123 | yield* stack.destroy(); |
| 124 | |
| 125 | const gone = yield* waitUntilGone(created.name); |
| 126 | expect(gone).toEqual("gone"); |
| 127 | }).pipe(logLevel), |
| 128 | // Create (~45 min) + update + delete; a timed-out lifecycle is not retried. |
| 129 | { |
| 130 | tags: ["provider:gcp", "provider:gcp:composer", "live"], |
| 131 | timeout: 5_400_000, |
| 132 | retry: 0, |
| 133 | }, |
| 134 | ); |