| 1 | import { GcpEnvironment } from "@/GCP/Environment"; |
| 2 | import * as GCP from "@/GCP"; |
| 3 | import * as Test from "@/Test/Alchemy"; |
| 4 | import * as bigtable from "@distilled.cloud/gcp/bigtableadmin_v2"; |
| 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 | // Lifecycles provision a Bigtable instance; skipped with --fast. |
| 18 | const runLifecycle = !process.env.FAST; |
| 19 | |
| 20 | const expireInDays = (days: number) => |
| 21 | new Date(Date.now() + days * 24 * 60 * 60 * 1000).toISOString(); |
| 22 | |
| 23 | const waitUntilGone = (name: string) => |
| 24 | bigtable.getProjectsInstancesClustersBackups({ name }).pipe( |
| 25 | Effect.as("found" as const), |
| 26 | Effect.catchTag("NotFound", () => Effect.succeed("gone" as const)), |
| 27 | Effect.repeat({ |
| 28 | schedule: Schedule.spaced("2 seconds"), |
| 29 | until: (status) => status === "gone", |
| 30 | times: 10, |
| 31 | }), |
| 32 | ); |
| 33 | |
| 34 | test.provider( |
| 35 | "getProjectsInstancesClustersBackups on a missing instance fails with NotFound", |
| 36 | (stack) => |
| 37 | Effect.gen(function* () { |
| 38 | const { project } = yield* GcpEnvironment.current; |
| 39 | |
| 40 | yield* stack.destroy(); |
| 41 | |
| 42 | const error = yield* Effect.flip( |
| 43 | bigtable.getProjectsInstancesClustersBackups({ |
| 44 | name: `projects/${project}/instances/alchemybtmissing/clusters/cluster/backups/missing`, |
| 45 | }), |
| 46 | ); |
| 47 | expect(error._tag).toEqual("NotFound"); |
| 48 | |
| 49 | yield* stack.destroy(); |
| 50 | }).pipe(logLevel), |
| 51 | { tags: ["provider:gcp", "provider:gcp:bigtable", "live"], timeout: 90_000 }, |
| 52 | ); |
| 53 | |
| 54 | test.provider.skipIf(!runLifecycle)( |
| 55 | "create, update, and delete a bigtable backup", |
| 56 | (stack) => |
| 57 | Effect.gen(function* () { |
| 58 | yield* stack.destroy(); |
| 59 | |
| 60 | const firstExpire = expireInDays(8); |
| 61 | const secondExpire = expireInDays(14); |
| 62 | |
| 63 | const created = yield* stack.deploy( |
| 64 | Effect.gen(function* () { |
| 65 | const instance = yield* GCP.Bigtable.Instance("Data", { |
| 66 | displayName: "alchemy-test-bt-backup", |
| 67 | type: "PRODUCTION", |
| 68 | clusters: { |
| 69 | cluster: { |
| 70 | location: "us-central1-b", |
| 71 | serveNodes: 1, |
| 72 | defaultStorageType: "HDD", |
| 73 | }, |
| 74 | }, |
| 75 | }); |
| 76 | const table = yield* GCP.Bigtable.Table("Users", { |
| 77 | instance: instance.name, |
| 78 | columnFamilies: { cf: { gcRule: { maxNumVersions: 1 } } }, |
| 79 | }); |
| 80 | const backup = yield* GCP.Bigtable.InstancesClustersBackup( |
| 81 | "Nightly", |
| 82 | { |
| 83 | instance: instance.name, |
| 84 | cluster: "cluster", |
| 85 | sourceTable: table.name, |
| 86 | expireTime: firstExpire, |
| 87 | }, |
| 88 | ); |
| 89 | return { instance, table, backup }; |
| 90 | }), |
| 91 | ); |
| 92 | |
| 93 | expect(created.backup.name).toContain("/backups/"); |
| 94 | expect(created.backup.backupId).toEqual(expect.any(String)); |
| 95 | expect(created.backup.instance).toEqual(created.instance.name); |
| 96 | expect(created.backup.sourceTable).toEqual(created.table.name); |
| 97 | expect(created.backup.state).toEqual("READY"); |
| 98 | |
| 99 | const fetched = yield* bigtable.getProjectsInstancesClustersBackups({ |
| 100 | name: created.backup.name, |
| 101 | }); |
| 102 | expect(fetched.name).toEqual(created.backup.name); |
| 103 | expect(fetched.sourceTable).toEqual(created.table.name); |
| 104 | expect(fetched.state).toEqual("READY"); |
| 105 | |
| 106 | const updated = yield* stack.deploy( |
| 107 | Effect.gen(function* () { |
| 108 | const instance = yield* GCP.Bigtable.Instance("Data", { |
| 109 | instanceId: created.instance.instanceId, |
| 110 | displayName: "alchemy-test-bt-backup", |
| 111 | type: "PRODUCTION", |
| 112 | clusters: { |
| 113 | cluster: { |
| 114 | location: "us-central1-b", |
| 115 | serveNodes: 1, |
| 116 | defaultStorageType: "HDD", |
| 117 | }, |
| 118 | }, |
| 119 | }); |
| 120 | const table = yield* GCP.Bigtable.Table("Users", { |
| 121 | instance: instance.name, |
| 122 | tableId: created.table.tableId, |
| 123 | columnFamilies: { cf: { gcRule: { maxNumVersions: 1 } } }, |
| 124 | }); |
| 125 | const backup = yield* GCP.Bigtable.InstancesClustersBackup( |
| 126 | "Nightly", |
| 127 | { |
| 128 | instance: instance.name, |
| 129 | cluster: "cluster", |
| 130 | sourceTable: table.name, |
| 131 | backupId: created.backup.backupId, |
| 132 | expireTime: secondExpire, |
| 133 | }, |
| 134 | ); |
| 135 | return { instance, table, backup }; |
| 136 | }), |
| 137 | ); |
| 138 | |
| 139 | expect(updated.backup.name).toEqual(created.backup.name); |
| 140 | expect(updated.backup.expireTime).toBeDefined(); |
| 141 | |
| 142 | const refetched = yield* bigtable.getProjectsInstancesClustersBackups({ |
| 143 | name: created.backup.name, |
| 144 | }); |
| 145 | expect(refetched.name).toEqual(created.backup.name); |
| 146 | expect(refetched.expireTime).toBeDefined(); |
| 147 | |
| 148 | yield* stack.destroy(); |
| 149 | |
| 150 | const gone = yield* waitUntilGone(created.backup.name); |
| 151 | expect(gone).toEqual("gone"); |
| 152 | }).pipe(logLevel), |
| 153 | { tags: ["provider:gcp", "provider:gcp:bigtable", "live"], timeout: 120_000 }, |
| 154 | ); |