| 1 | import * as GCP from "@/GCP"; |
| 2 | import * as Test from "@/Test/Alchemy"; |
| 3 | import * as spanner from "@distilled.cloud/gcp/spanner_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 | spanner.getProjectsInstancesDatabasesBackupSchedules({ name }).pipe( |
| 19 | Effect.as("found" as const), |
| 20 | Effect.catchTag("NotFound", () => Effect.succeed("gone" as const)), |
| 21 | Effect.repeat({ |
| 22 | schedule: Schedule.spaced("2 seconds"), |
| 23 | until: (status) => status === "gone", |
| 24 | times: 10, |
| 25 | }), |
| 26 | ); |
| 27 | |
| 28 | test.provider( |
| 29 | "getProjectsInstancesDatabasesBackupSchedules on a missing schedule 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 | spanner.getProjectsInstancesDatabasesBackupSchedules({ |
| 37 | name: `projects/${project}/instances/alchemy-spanner-missing/databases/alchemy-missing/backupSchedules/alchemy-missing`, |
| 38 | }), |
| 39 | ); |
| 40 | expect(error._tag).toBe("NotFound"); |
| 41 | |
| 42 | yield* stack.destroy(); |
| 43 | }).pipe(logLevel), |
| 44 | { tags: ["provider:gcp", "provider:gcp:spanner", "live"], timeout: 90_000 }, |
| 45 | ); |
| 46 | |
| 47 | test.provider( |
| 48 | "create, update, and delete a spanner backup schedule", |
| 49 | (stack) => |
| 50 | Effect.gen(function* () { |
| 51 | yield* stack.destroy(); |
| 52 | |
| 53 | const created = yield* stack.deploy( |
| 54 | Effect.gen(function* () { |
| 55 | const instance = yield* GCP.Spanner.Instance("App", { |
| 56 | config: "regional-us-central1", |
| 57 | processingUnits: 100, |
| 58 | displayName: "alchemy-sch-spnr", |
| 59 | defaultBackupScheduleType: "NONE", |
| 60 | }); |
| 61 | const database = yield* GCP.Spanner.Database("AppDb", { |
| 62 | instance: instance.instanceId, |
| 63 | }); |
| 64 | const schedule = yield* GCP.Spanner.InstancesDatabasesBackupSchedule( |
| 65 | "Nightly", |
| 66 | { |
| 67 | instance: instance.instanceId, |
| 68 | database: database.databaseId, |
| 69 | spec: { cron: "0 2 * * *" }, |
| 70 | retentionDuration: "604800s", |
| 71 | }, |
| 72 | ); |
| 73 | return { instance, database, schedule }; |
| 74 | }), |
| 75 | ); |
| 76 | |
| 77 | expect(created.schedule.name).toContain("/backupSchedules/"); |
| 78 | expect(created.schedule.backupScheduleId).toEqual(expect.any(String)); |
| 79 | expect(created.schedule.instanceId).toEqual(created.instance.instanceId); |
| 80 | expect(created.schedule.databaseId).toEqual(created.database.databaseId); |
| 81 | expect(created.schedule.cron).toEqual("0 2 * * *"); |
| 82 | expect(created.schedule.retentionDuration).toEqual("604800s"); |
| 83 | expect(created.schedule.incremental).toEqual(false); |
| 84 | |
| 85 | const fetched = |
| 86 | yield* spanner.getProjectsInstancesDatabasesBackupSchedules({ |
| 87 | name: created.schedule.name, |
| 88 | }); |
| 89 | expect(fetched.name).toEqual(created.schedule.name); |
| 90 | expect(fetched.spec?.cronSpec?.text).toEqual("0 2 * * *"); |
| 91 | expect(fetched.retentionDuration).toEqual("604800s"); |
| 92 | |
| 93 | const updated = yield* stack.deploy( |
| 94 | Effect.gen(function* () { |
| 95 | const instance = yield* GCP.Spanner.Instance("App", { |
| 96 | instanceId: created.instance.instanceId, |
| 97 | config: "regional-us-central1", |
| 98 | processingUnits: 100, |
| 99 | displayName: "alchemy-sch-spnr", |
| 100 | defaultBackupScheduleType: "NONE", |
| 101 | }); |
| 102 | const database = yield* GCP.Spanner.Database("AppDb", { |
| 103 | instance: instance.instanceId, |
| 104 | databaseId: created.database.databaseId, |
| 105 | }); |
| 106 | const schedule = yield* GCP.Spanner.InstancesDatabasesBackupSchedule( |
| 107 | "Nightly", |
| 108 | { |
| 109 | instance: instance.instanceId, |
| 110 | database: database.databaseId, |
| 111 | backupScheduleId: created.schedule.backupScheduleId, |
| 112 | spec: { cron: "0 14 * * *" }, |
| 113 | retentionDuration: "1209600s", |
| 114 | }, |
| 115 | ); |
| 116 | return { instance, database, schedule }; |
| 117 | }), |
| 118 | ); |
| 119 | |
| 120 | expect(updated.schedule.name).toEqual(created.schedule.name); |
| 121 | expect(updated.schedule.cron).toEqual("0 14 * * *"); |
| 122 | expect(updated.schedule.retentionDuration).toEqual("1209600s"); |
| 123 | |
| 124 | const refetched = |
| 125 | yield* spanner.getProjectsInstancesDatabasesBackupSchedules({ |
| 126 | name: created.schedule.name, |
| 127 | }); |
| 128 | expect(refetched.spec?.cronSpec?.text).toEqual("0 14 * * *"); |
| 129 | expect(refetched.retentionDuration).toEqual("1209600s"); |
| 130 | |
| 131 | yield* stack.destroy(); |
| 132 | |
| 133 | const gone = yield* waitUntilGone(created.schedule.name); |
| 134 | expect(gone).toEqual("gone"); |
| 135 | }).pipe(logLevel), |
| 136 | { tags: ["provider:gcp", "provider:gcp:spanner", "live"], timeout: 180_000 }, |
| 137 | ); |