| 1 | import * as GCP from "@/GCP"; |
| 2 | import * as Test from "@/Test/Alchemy"; |
| 3 | import * as compute from "@distilled.cloud/gcp/compute_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 | |
| 9 | const { test } = Test.make({ providers: GCP.providers() }); |
| 10 | |
| 11 | const logLevel = Effect.provideService( |
| 12 | MinimumLogLevel, |
| 13 | process.env.DEBUG ? "Debug" : "Info", |
| 14 | ); |
| 15 | |
| 16 | const waitUntilGone = ( |
| 17 | project: string, |
| 18 | region: string, |
| 19 | targetPoolName: string, |
| 20 | ) => |
| 21 | compute.getTargetPools({ project, region, targetPool: targetPoolName }).pipe( |
| 22 | Effect.as("found" as const), |
| 23 | Effect.catchTag("NotFound", () => Effect.succeed("gone" as const)), |
| 24 | Effect.repeat({ |
| 25 | schedule: Schedule.spaced("1 second"), |
| 26 | until: (status) => status === "gone", |
| 27 | times: 10, |
| 28 | }), |
| 29 | ); |
| 30 | |
| 31 | test.provider( |
| 32 | "create, replace, and delete a target pool", |
| 33 | (stack) => |
| 34 | Effect.gen(function* () { |
| 35 | yield* stack.destroy(); |
| 36 | |
| 37 | const created = yield* stack.deploy( |
| 38 | Effect.gen(function* () { |
| 39 | return yield* GCP.Compute.TargetPool("Pool", { |
| 40 | region: "us-central1", |
| 41 | description: "nlb backends", |
| 42 | }); |
| 43 | }), |
| 44 | ); |
| 45 | |
| 46 | expect(created.targetPoolName).toEqual(expect.any(String)); |
| 47 | expect(created.region).toEqual("us-central1"); |
| 48 | expect(created.description).toEqual("nlb backends"); |
| 49 | expect(created.sessionAffinity).toEqual("NONE"); |
| 50 | expect(created.selfLink).toEqual(expect.stringContaining("targetPools")); |
| 51 | |
| 52 | const fetched = yield* compute.getTargetPools({ |
| 53 | project: created.project, |
| 54 | region: created.region, |
| 55 | targetPool: created.targetPoolName, |
| 56 | }); |
| 57 | expect(fetched.name).toEqual(created.targetPoolName); |
| 58 | expect(fetched.sessionAffinity).toEqual("NONE"); |
| 59 | expect(fetched.description).toContain("[alchemy "); |
| 60 | expect(fetched.description).toContain("nlb backends"); |
| 61 | |
| 62 | const replaced = yield* stack.deploy( |
| 63 | Effect.gen(function* () { |
| 64 | return yield* GCP.Compute.TargetPool("Pool", { |
| 65 | targetPoolName: created.targetPoolName, |
| 66 | region: "us-central1", |
| 67 | description: "nlb backends", |
| 68 | sessionAffinity: "CLIENT_IP", |
| 69 | }); |
| 70 | }), |
| 71 | ); |
| 72 | |
| 73 | expect(replaced.targetPoolName).toEqual(created.targetPoolName); |
| 74 | expect(replaced.sessionAffinity).toEqual("CLIENT_IP"); |
| 75 | expect(replaced.description).toEqual("nlb backends"); |
| 76 | |
| 77 | const refetched = yield* compute.getTargetPools({ |
| 78 | project: replaced.project, |
| 79 | region: replaced.region, |
| 80 | targetPool: replaced.targetPoolName, |
| 81 | }); |
| 82 | expect(refetched.sessionAffinity).toEqual("CLIENT_IP"); |
| 83 | expect(refetched.description).toContain("[alchemy "); |
| 84 | expect(refetched.description).toContain("nlb backends"); |
| 85 | |
| 86 | yield* stack.destroy(); |
| 87 | |
| 88 | const gone = yield* waitUntilGone( |
| 89 | created.project, |
| 90 | created.region, |
| 91 | created.targetPoolName, |
| 92 | ); |
| 93 | expect(gone).toEqual("gone"); |
| 94 | }).pipe(logLevel), |
| 95 | { tags: ["provider:gcp", "provider:gcp:compute", "live"], timeout: 90_000 }, |
| 96 | ); |