| 1 | import { GcpEnvironment } from "@/GCP/Environment"; |
| 2 | import * as GCP from "@/GCP"; |
| 3 | import * as Test from "@/Test/Alchemy"; |
| 4 | import * as redis from "@distilled.cloud/gcp/redis_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 | // Memorystore instance create + update + delete runs well past 5 minutes. |
| 18 | const runLifecycle = !!process.env.GCP_TEST_SLOW && !process.env.FAST; |
| 19 | |
| 20 | const waitUntilGone = (name: string) => |
| 21 | redis.getProjectsLocationsInstances({ 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 | "getProjectsLocationsInstances on a missing instance fails with NotFound", |
| 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 | redis.getProjectsLocationsInstances({ |
| 41 | name: `projects/${project}/locations/us-central1/instances/alchemy-redis-missing`, |
| 42 | }), |
| 43 | ); |
| 44 | expect(error._tag).toBe("NotFound"); |
| 45 | |
| 46 | const page = yield* redis.listProjectsLocationsInstances({ |
| 47 | parent: `projects/${project}/locations/-`, |
| 48 | pageSize: 10, |
| 49 | }); |
| 50 | expect( |
| 51 | (page.instances ?? []).map((instance) => |
| 52 | instance.name?.split("/").pop(), |
| 53 | ), |
| 54 | ).not.toContain("alchemy-redis-missing"); |
| 55 | |
| 56 | yield* stack.destroy(); |
| 57 | }).pipe(logLevel), |
| 58 | { tags: ["provider:gcp", "provider:gcp:redis", "live"], timeout: 90_000 }, |
| 59 | ); |
| 60 | |
| 61 | test.provider.skipIf(!runLifecycle)( |
| 62 | "create, update, and delete a redis instance", |
| 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.Redis.Instance("Cache", { |
| 70 | location: "us-central1", |
| 71 | tier: "BASIC", |
| 72 | memorySizeGb: 1, |
| 73 | displayName: "alchemy-test-cache", |
| 74 | labels: { env: "test" }, |
| 75 | }); |
| 76 | }), |
| 77 | ); |
| 78 | |
| 79 | expect(created.name).toContain("/instances/"); |
| 80 | expect(created.instanceId).toEqual(expect.any(String)); |
| 81 | expect(created.location).toEqual("us-central1"); |
| 82 | expect(created.tier).toEqual("BASIC"); |
| 83 | expect(created.memorySizeGb).toEqual(1); |
| 84 | expect(created.displayName).toEqual("alchemy-test-cache"); |
| 85 | expect(created.labels).toMatchObject({ env: "test" }); |
| 86 | expect(created.state).toEqual("READY"); |
| 87 | |
| 88 | const fetched = yield* redis.getProjectsLocationsInstances({ |
| 89 | name: created.name, |
| 90 | }); |
| 91 | expect(fetched.name).toEqual(created.name); |
| 92 | expect(fetched.tier).toEqual("BASIC"); |
| 93 | expect(fetched.memorySizeGb).toEqual(1); |
| 94 | expect(fetched.labels?.env).toEqual("test"); |
| 95 | expect(fetched.displayName).toEqual("alchemy-test-cache"); |
| 96 | |
| 97 | const updated = yield* stack.deploy( |
| 98 | Effect.gen(function* () { |
| 99 | return yield* GCP.Redis.Instance("Cache", { |
| 100 | instanceId: created.instanceId, |
| 101 | location: "us-central1", |
| 102 | tier: "BASIC", |
| 103 | memorySizeGb: 1, |
| 104 | displayName: "alchemy-prod-cache", |
| 105 | labels: { env: "prod", role: "cache" }, |
| 106 | }); |
| 107 | }), |
| 108 | ); |
| 109 | |
| 110 | expect(updated.name).toEqual(created.name); |
| 111 | expect(updated.displayName).toEqual("alchemy-prod-cache"); |
| 112 | expect(updated.labels).toMatchObject({ env: "prod", role: "cache" }); |
| 113 | |
| 114 | const refetched = yield* redis.getProjectsLocationsInstances({ |
| 115 | name: created.name, |
| 116 | }); |
| 117 | expect(refetched.displayName).toEqual("alchemy-prod-cache"); |
| 118 | expect(refetched.labels?.env).toEqual("prod"); |
| 119 | expect(refetched.labels?.role).toEqual("cache"); |
| 120 | |
| 121 | yield* stack.destroy(); |
| 122 | |
| 123 | const gone = yield* waitUntilGone(created.name); |
| 124 | expect(gone).toEqual("gone"); |
| 125 | }).pipe(logLevel), |
| 126 | { tags: ["provider:gcp", "provider:gcp:redis", "live"], timeout: 1_800_000 }, |
| 127 | ); |