The best way to control Claude Code, Codex and more from any computer or phone
| 1 | import * as GCP from "@alchemy.run/gcp"; |
| 2 | import { GcpEnvironment } from "@alchemy.run/gcp/Environment"; |
| 3 | import { expect } from "@alchemy.run/test"; |
| 4 | import * as Test from "@alchemy.run/test/Alchemy"; |
| 5 | import * as alloydb from "@distilled.cloud/gcp/alloydb_v1"; |
| 6 | import { Action } from "alchemy/Action"; |
| 7 | import * as Effect from "effect/Effect"; |
| 8 | import { MinimumLogLevel } from "effect/References"; |
| 9 | import * as Schedule from "effect/Schedule"; |
| 10 | |
| 11 | const { test } = Test.make({ providers: GCP.providers() }); |
| 12 | |
| 13 | const logLevel = Effect.provideService(MinimumLogLevel, process.env.DEBUG ? "Debug" : "Info"); |
| 14 | |
| 15 | // AlloyDB clusters and instances take well over 5 minutes to provision. |
| 16 | const runLifecycle = !!process.env.GCP_TEST_SLOW && !process.env.FAST; |
| 17 | |
| 18 | const waitUntilGone = (name: string) => |
| 19 | alloydb.getProjectsLocationsBackups({ name }).pipe( |
| 20 | Effect.as("found" as const), |
| 21 | Effect.catchTag("NotFound", () => Effect.succeed("gone" as const)), |
| 22 | Effect.repeat({ |
| 23 | schedule: Schedule.spaced("2 seconds"), |
| 24 | until: (status) => status === "gone", |
| 25 | times: 10, |
| 26 | }), |
| 27 | ); |
| 28 | |
| 29 | test.provider( |
| 30 | "getProjectsLocationsBackups on a missing backup fails with a typed tag", |
| 31 | (stack) => |
| 32 | Effect.gen(function* () { |
| 33 | const { project } = yield* GcpEnvironment.current; |
| 34 | |
| 35 | yield* stack.destroy(); |
| 36 | |
| 37 | const error = yield* Effect.flip( |
| 38 | alloydb.getProjectsLocationsBackups({ |
| 39 | name: `projects/${project}/locations/us-central1/backups/alchemy-alloydb-missing`, |
| 40 | }), |
| 41 | ); |
| 42 | expect(error._tag).toEqual("NotFound"); |
| 43 | |
| 44 | const page = yield* alloydb.listProjectsLocationsBackups({ |
| 45 | parent: `projects/${project}/locations/-`, |
| 46 | pageSize: 10, |
| 47 | }); |
| 48 | expect((page.backups ?? []).map((item) => item.name)).not.toContain( |
| 49 | `projects/${project}/locations/us-central1/backups/alchemy-alloydb-missing`, |
| 50 | ); |
| 51 | |
| 52 | yield* stack.destroy(); |
| 53 | }).pipe(logLevel), |
| 54 | { tags: ["provider:gcp", "provider:gcp:alloydb", "live"], timeout: 90_000 }, |
| 55 | ); |
| 56 | |
| 57 | test.provider.skipIf(!runLifecycle)( |
| 58 | "create, update, and delete an alloydb backup", |
| 59 | (stack) => |
| 60 | Effect.gen(function* () { |
| 61 | yield* stack.destroy(); |
| 62 | |
| 63 | const created = yield* stack.deploy( |
| 64 | Effect.gen(function* () { |
| 65 | const cluster = yield* GCP.AlloyDB.Cluster("AppDb", { |
| 66 | location: "us-central1", |
| 67 | pscConfig: { pscEnabled: true }, |
| 68 | displayName: "alchemy-test-backup-cluster", |
| 69 | labels: { env: "test" }, |
| 70 | initialUser: { user: "postgres", password: "AlchemyTest1" }, |
| 71 | automatedBackupPolicy: { enabled: false }, |
| 72 | continuousBackupConfig: { enabled: false }, |
| 73 | }); |
| 74 | // AlloyDB only backs up clusters with a primary instance. |
| 75 | const instance = yield* GCP.AlloyDB.Instance("Primary", { |
| 76 | cluster: cluster.name, |
| 77 | instanceType: "PRIMARY", |
| 78 | machineConfig: { cpuCount: 2 }, |
| 79 | }); |
| 80 | const backup = yield* GCP.AlloyDB.Backup("OnDemand", { |
| 81 | clusterName: instance.clusterName, |
| 82 | displayName: "alchemy-test-backup", |
| 83 | description: "test snapshot", |
| 84 | labels: { env: "test" }, |
| 85 | }); |
| 86 | const Probe = Action( |
| 87 | "Probe", |
| 88 | Effect.gen(function* () { |
| 89 | const getBackup = yield* GCP.AlloyDB.GetBackup(backup); |
| 90 | return Effect.fn(function* () { |
| 91 | return yield* getBackup(); |
| 92 | }); |
| 93 | }), |
| 94 | ); |
| 95 | return { cluster, backup, probe: yield* Probe({}) }; |
| 96 | }), |
| 97 | ); |
| 98 | |
| 99 | expect(created.backup.name).toContain("/backups/"); |
| 100 | expect(created.backup.clusterId).toEqual(created.cluster.clusterId); |
| 101 | expect(created.backup.location).toEqual("us-central1"); |
| 102 | expect(created.backup.displayName).toEqual("alchemy-test-backup"); |
| 103 | expect(created.backup.description).toEqual("test snapshot"); |
| 104 | expect(created.backup.labels).toMatchObject({ env: "test" }); |
| 105 | expect(created.backup.type).toEqual("ON_DEMAND"); |
| 106 | expect(created.backup.state).toEqual("READY"); |
| 107 | expect(created.probe.name).toEqual(created.backup.name); |
| 108 | |
| 109 | const fetched = yield* alloydb.getProjectsLocationsBackups({ |
| 110 | name: created.backup.name, |
| 111 | }); |
| 112 | expect(fetched.name).toEqual(created.backup.name); |
| 113 | // GCP omits displayName from backup reads; the attributes carry it. |
| 114 | expect(fetched.labels?.env).toEqual("test"); |
| 115 | expect(fetched.type).toEqual("ON_DEMAND"); |
| 116 | |
| 117 | const updated = yield* stack.deploy( |
| 118 | Effect.gen(function* () { |
| 119 | const cluster = yield* GCP.AlloyDB.Cluster("AppDb", { |
| 120 | clusterId: created.cluster.clusterId, |
| 121 | location: "us-central1", |
| 122 | pscConfig: { pscEnabled: true }, |
| 123 | displayName: "alchemy-test-backup-cluster", |
| 124 | labels: { env: "test" }, |
| 125 | automatedBackupPolicy: { enabled: false }, |
| 126 | continuousBackupConfig: { enabled: false }, |
| 127 | }); |
| 128 | const instance = yield* GCP.AlloyDB.Instance("Primary", { |
| 129 | cluster: cluster.name, |
| 130 | instanceType: "PRIMARY", |
| 131 | machineConfig: { cpuCount: 2 }, |
| 132 | }); |
| 133 | const backup = yield* GCP.AlloyDB.Backup("OnDemand", { |
| 134 | clusterName: instance.clusterName, |
| 135 | backupId: created.backup.backupId, |
| 136 | displayName: "alchemy-prod-backup", |
| 137 | // Description is create-only (a change replaces the backup), so |
| 138 | // this step only changes fields that update in place. |
| 139 | description: "test snapshot", |
| 140 | labels: { env: "prod", role: "backup" }, |
| 141 | }); |
| 142 | return { cluster, backup }; |
| 143 | }), |
| 144 | ); |
| 145 | |
| 146 | expect(updated.backup.name).toEqual(created.backup.name); |
| 147 | expect(updated.backup.displayName).toEqual("alchemy-prod-backup"); |
| 148 | expect(updated.backup.description).toEqual("test snapshot"); |
| 149 | expect(updated.backup.labels).toMatchObject({ |
| 150 | env: "prod", |
| 151 | role: "backup", |
| 152 | }); |
| 153 | |
| 154 | const refetched = yield* alloydb.getProjectsLocationsBackups({ |
| 155 | name: created.backup.name, |
| 156 | }); |
| 157 | // GCP omits displayName from backup reads; the attributes carry it. |
| 158 | expect(refetched.description).toEqual("test snapshot"); |
| 159 | expect(refetched.labels?.env).toEqual("prod"); |
| 160 | expect(refetched.labels?.role).toEqual("backup"); |
| 161 | |
| 162 | yield* stack.destroy(); |
| 163 | |
| 164 | const gone = yield* waitUntilGone(created.backup.name); |
| 165 | expect(gone).toEqual("gone"); |
| 166 | }).pipe(logLevel), |
| 167 | { |
| 168 | tags: ["provider:gcp", "provider:gcp:alloydb", "live"], |
| 169 | timeout: 3_600_000, |
| 170 | retry: 0, |
| 171 | }, |
| 172 | ); |