| 1 | import { GcpEnvironment } from "@/GCP/Environment"; |
| 2 | import * as GCP from "@/GCP"; |
| 3 | import * as Test from "@/Test/Alchemy"; |
| 4 | import * as bigtableadmin 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 | const runLifecycle = !process.env.FAST; |
| 18 | |
| 19 | const lastSegment = (value: string) => { |
| 20 | const parts = value.replace(/\/+$/, "").split("/"); |
| 21 | return parts[parts.length - 1] || value; |
| 22 | }; |
| 23 | |
| 24 | const waitUntilGone = (name: string) => |
| 25 | bigtableadmin.getProjectsInstancesAppProfiles({ name }).pipe( |
| 26 | Effect.as("found" as const), |
| 27 | Effect.catchTag("NotFound", () => Effect.succeed("gone" as const)), |
| 28 | Effect.repeat({ |
| 29 | schedule: Schedule.spaced("1 second"), |
| 30 | until: (status) => status === "gone", |
| 31 | times: 10, |
| 32 | }), |
| 33 | ); |
| 34 | |
| 35 | test.provider( |
| 36 | "getProjectsInstancesAppProfiles on a missing instance fails with a typed tag", |
| 37 | (stack) => |
| 38 | Effect.gen(function* () { |
| 39 | const { project } = yield* GcpEnvironment.current; |
| 40 | |
| 41 | yield* stack.destroy(); |
| 42 | |
| 43 | const error = yield* Effect.flip( |
| 44 | bigtableadmin.getProjectsInstancesAppProfiles({ |
| 45 | name: `projects/${project}/instances/alchemy-bt-missing/appProfiles/missing`, |
| 46 | }), |
| 47 | ); |
| 48 | expect(error._tag).toEqual("NotFound"); |
| 49 | |
| 50 | yield* stack.destroy(); |
| 51 | }).pipe(logLevel), |
| 52 | { tags: ["provider:gcp", "provider:gcp:bigtable", "live"], timeout: 90_000 }, |
| 53 | ); |
| 54 | |
| 55 | test.provider.skipIf(!runLifecycle)( |
| 56 | "create, update, and delete a bigtable app profile", |
| 57 | (stack) => |
| 58 | Effect.gen(function* () { |
| 59 | const { project } = yield* GcpEnvironment.current; |
| 60 | |
| 61 | yield* stack.destroy(); |
| 62 | |
| 63 | const created = yield* stack.deploy( |
| 64 | Effect.gen(function* () { |
| 65 | const instance = yield* GCP.Bigtable.Instance("Store", { |
| 66 | displayName: "alchemy-appprofile-parent", |
| 67 | labels: { env: "test" }, |
| 68 | clusters: { |
| 69 | cluster: { |
| 70 | location: "us-central1-b", |
| 71 | serveNodes: 1, |
| 72 | defaultStorageType: "HDD", |
| 73 | }, |
| 74 | }, |
| 75 | }); |
| 76 | const profile = yield* GCP.Bigtable.AppProfile("Traffic", { |
| 77 | instance: instance.instanceId, |
| 78 | description: "alchemy-test-profile", |
| 79 | multiClusterRouting: {}, |
| 80 | }); |
| 81 | return { instance, profile }; |
| 82 | }), |
| 83 | ); |
| 84 | |
| 85 | expect(created.profile.name).toContain("/appProfiles/"); |
| 86 | expect(created.profile.appProfileId).toEqual(expect.any(String)); |
| 87 | expect(created.profile.instanceId).toEqual(created.instance.instanceId); |
| 88 | expect(created.profile.project).toEqual(project); |
| 89 | expect(created.profile.description).toEqual("alchemy-test-profile"); |
| 90 | expect(created.profile.multiClusterRouting).toBeDefined(); |
| 91 | |
| 92 | const fetched = yield* bigtableadmin.getProjectsInstancesAppProfiles({ |
| 93 | name: created.profile.name, |
| 94 | }); |
| 95 | expect(fetched.name).toEqual(created.profile.name); |
| 96 | expect(fetched.description).toEqual("alchemy-test-profile"); |
| 97 | expect(fetched.multiClusterRoutingUseAny).toBeDefined(); |
| 98 | |
| 99 | const clusters = yield* bigtableadmin.listProjectsInstancesClusters({ |
| 100 | parent: created.instance.name, |
| 101 | }); |
| 102 | const clusterId = lastSegment( |
| 103 | (clusters.clusters ?? [])[0]?.name ?? "cluster", |
| 104 | ); |
| 105 | |
| 106 | const updated = yield* stack.deploy( |
| 107 | Effect.gen(function* () { |
| 108 | const instance = yield* GCP.Bigtable.Instance("Store", { |
| 109 | instanceId: created.instance.instanceId, |
| 110 | displayName: "alchemy-appprofile-parent", |
| 111 | labels: { env: "test" }, |
| 112 | clusters: { |
| 113 | [clusterId]: { |
| 114 | location: "us-central1-b", |
| 115 | serveNodes: 1, |
| 116 | defaultStorageType: "HDD", |
| 117 | }, |
| 118 | }, |
| 119 | }); |
| 120 | const profile = yield* GCP.Bigtable.AppProfile("Traffic", { |
| 121 | instance: instance.instanceId, |
| 122 | appProfileId: created.profile.appProfileId, |
| 123 | description: "alchemy-prod-profile", |
| 124 | singleClusterRouting: { |
| 125 | clusterId, |
| 126 | allowTransactionalWrites: true, |
| 127 | }, |
| 128 | standardIsolation: { priority: "PRIORITY_HIGH" }, |
| 129 | }); |
| 130 | return { instance, profile }; |
| 131 | }), |
| 132 | ); |
| 133 | |
| 134 | expect(updated.profile.name).toEqual(created.profile.name); |
| 135 | expect(updated.profile.description).toEqual("alchemy-prod-profile"); |
| 136 | expect(updated.profile.singleClusterRouting?.clusterId).toEqual( |
| 137 | clusterId, |
| 138 | ); |
| 139 | expect( |
| 140 | updated.profile.singleClusterRouting?.allowTransactionalWrites, |
| 141 | ).toEqual(true); |
| 142 | expect(updated.profile.standardIsolation?.priority).toEqual( |
| 143 | "PRIORITY_HIGH", |
| 144 | ); |
| 145 | |
| 146 | const refetched = yield* bigtableadmin.getProjectsInstancesAppProfiles({ |
| 147 | name: created.profile.name, |
| 148 | }); |
| 149 | expect(refetched.description).toEqual("alchemy-prod-profile"); |
| 150 | expect(refetched.singleClusterRouting?.clusterId).toEqual(clusterId); |
| 151 | expect(refetched.standardIsolation?.priority).toEqual("PRIORITY_HIGH"); |
| 152 | |
| 153 | yield* stack.destroy(); |
| 154 | |
| 155 | const gone = yield* waitUntilGone(created.profile.name); |
| 156 | expect(gone).toEqual("gone"); |
| 157 | }).pipe(logLevel), |
| 158 | { tags: ["provider:gcp", "provider:gcp:bigtable", "live"], timeout: 120_000 }, |
| 159 | ); |