| 1 | import { GcpEnvironment } from "@/GCP/Environment"; |
| 2 | import * as GCP from "@/GCP"; |
| 3 | import * as Test from "@/Test/Alchemy"; |
| 4 | import * as backupdr from "@distilled.cloud/gcp/backupdr_v1"; |
| 5 | import { expect } from "alchemy-test"; |
| 6 | import * as Effect from "effect/Effect"; |
| 7 | import { MinimumLogLevel } from "effect/References"; |
| 8 | import * as Schedule from "effect/Schedule"; |
| 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 | // Management servers take well over 5 minutes to provision. |
| 18 | const runLifecycle = !!process.env.GCP_TEST_SLOW && !process.env.FAST; |
| 19 | |
| 20 | const waitUntilGone = (name: string) => |
| 21 | backupdr.getProjectsLocationsManagementServers({ name }).pipe( |
| 22 | Effect.as("found" as const), |
| 23 | Effect.catchTag("NotFound", () => Effect.succeed("gone" as const)), |
| 24 | Effect.repeat({ |
| 25 | schedule: Schedule.spaced("2 seconds"), |
| 26 | until: (status) => status === "gone", |
| 27 | times: 10, |
| 28 | }), |
| 29 | ); |
| 30 | |
| 31 | test.provider( |
| 32 | "getProjectsLocationsManagementServers on a missing server fails with a typed tag", |
| 33 | (stack) => |
| 34 | Effect.gen(function* () { |
| 35 | const { project } = yield* GcpEnvironment.current; |
| 36 | |
| 37 | yield* stack.destroy(); |
| 38 | |
| 39 | const error = yield* Effect.flip( |
| 40 | backupdr.getProjectsLocationsManagementServers({ |
| 41 | name: `projects/${project}/locations/us-central1/managementServers/alchemy-backupdr-missing`, |
| 42 | }), |
| 43 | ); |
| 44 | expect(error._tag).toEqual("NotFound"); |
| 45 | |
| 46 | const page = yield* backupdr.listProjectsLocationsManagementServers({ |
| 47 | parent: `projects/${project}/locations/-`, |
| 48 | pageSize: 10, |
| 49 | }); |
| 50 | expect( |
| 51 | (page.managementServers ?? []).map((item) => item.name), |
| 52 | ).not.toContain( |
| 53 | `projects/${project}/locations/us-central1/managementServers/alchemy-backupdr-missing`, |
| 54 | ); |
| 55 | |
| 56 | yield* stack.destroy(); |
| 57 | }).pipe(logLevel), |
| 58 | { tags: ["provider:gcp", "provider:gcp:backupdr", "live"], timeout: 90_000 }, |
| 59 | ); |
| 60 | |
| 61 | test.provider.skipIf(!runLifecycle)( |
| 62 | "create, update, and delete a management server", |
| 63 | (stack) => |
| 64 | Effect.gen(function* () { |
| 65 | yield* stack.destroy(); |
| 66 | |
| 67 | const created = yield* stack.deploy( |
| 68 | Effect.gen(function* () { |
| 69 | return yield* GCP.BackupDR.ManagementServer("Console", { |
| 70 | description: "alchemy-test-console", |
| 71 | labels: { env: "test" }, |
| 72 | }); |
| 73 | }), |
| 74 | ); |
| 75 | |
| 76 | expect(created.name).toContain("/managementServers/"); |
| 77 | expect(created.managementServerId).toEqual(expect.any(String)); |
| 78 | expect(created.location).toEqual("us-central1"); |
| 79 | expect(created.labels).toMatchObject({ env: "test" }); |
| 80 | |
| 81 | const fetched = yield* backupdr.getProjectsLocationsManagementServers({ |
| 82 | name: created.name, |
| 83 | }); |
| 84 | expect(fetched.name).toEqual(created.name); |
| 85 | expect(fetched.labels?.env).toEqual("test"); |
| 86 | |
| 87 | const updated = yield* stack.deploy( |
| 88 | Effect.gen(function* () { |
| 89 | return yield* GCP.BackupDR.ManagementServer("Console", { |
| 90 | managementServerId: created.managementServerId, |
| 91 | description: "alchemy-test-console", |
| 92 | labels: { env: "test" }, |
| 93 | }); |
| 94 | }), |
| 95 | ); |
| 96 | |
| 97 | expect(updated.name).toEqual(created.name); |
| 98 | |
| 99 | yield* stack.destroy(); |
| 100 | |
| 101 | const gone = yield* waitUntilGone(created.name); |
| 102 | expect(gone).toEqual("gone"); |
| 103 | }).pipe(logLevel), |
| 104 | // Provisioning a management server takes up to ~30 minutes, deletion more. |
| 105 | { |
| 106 | tags: ["provider:gcp", "provider:gcp:backupdr", "live"], |
| 107 | timeout: 3_600_000, |
| 108 | retry: 0, |
| 109 | }, |
| 110 | ); |