| 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 = (project: string, address: string) => |
| 17 | compute.getGlobalAddresses({ project, address }).pipe( |
| 18 | Effect.as("found" as const), |
| 19 | Effect.catchTag("NotFound", () => Effect.succeed("gone" as const)), |
| 20 | Effect.repeat({ |
| 21 | schedule: Schedule.spaced("1 second"), |
| 22 | until: (status) => status === "gone", |
| 23 | times: 10, |
| 24 | }), |
| 25 | ); |
| 26 | |
| 27 | test.provider( |
| 28 | "create, update, and delete a global address", |
| 29 | (stack) => |
| 30 | Effect.gen(function* () { |
| 31 | yield* stack.destroy(); |
| 32 | |
| 33 | const created = yield* stack.deploy( |
| 34 | Effect.gen(function* () { |
| 35 | return yield* GCP.Compute.GlobalAddress("FrontendIp", { |
| 36 | description: "alchemy test global address", |
| 37 | labels: { env: "test" }, |
| 38 | }); |
| 39 | }), |
| 40 | ); |
| 41 | |
| 42 | expect(created.addressName).toEqual(expect.any(String)); |
| 43 | expect(created.address).toEqual(expect.any(String)); |
| 44 | expect(created.addressType).toEqual("EXTERNAL"); |
| 45 | expect(created.labels).toMatchObject({ env: "test" }); |
| 46 | expect(created.status).toEqual(expect.stringMatching(/RESERVED|IN_USE/)); |
| 47 | |
| 48 | const fetched = yield* compute.getGlobalAddresses({ |
| 49 | project: created.project, |
| 50 | address: created.addressName, |
| 51 | }); |
| 52 | expect(fetched.name).toEqual(created.addressName); |
| 53 | expect(fetched.address).toEqual(created.address); |
| 54 | expect(fetched.labels?.env).toEqual("test"); |
| 55 | |
| 56 | const updated = yield* stack.deploy( |
| 57 | Effect.gen(function* () { |
| 58 | return yield* GCP.Compute.GlobalAddress("FrontendIp", { |
| 59 | addressName: created.addressName, |
| 60 | description: "alchemy test global address", |
| 61 | labels: { env: "prod", role: "frontend" }, |
| 62 | }); |
| 63 | }), |
| 64 | ); |
| 65 | |
| 66 | expect(updated.addressName).toEqual(created.addressName); |
| 67 | expect(updated.address).toEqual(created.address); |
| 68 | expect(updated.labels).toMatchObject({ env: "prod", role: "frontend" }); |
| 69 | |
| 70 | const fetchedUpdated = yield* compute.getGlobalAddresses({ |
| 71 | project: updated.project, |
| 72 | address: updated.addressName, |
| 73 | }); |
| 74 | expect(fetchedUpdated.labels?.env).toEqual("prod"); |
| 75 | expect(fetchedUpdated.labels?.role).toEqual("frontend"); |
| 76 | |
| 77 | yield* stack.destroy(); |
| 78 | |
| 79 | const gone = yield* waitUntilGone(created.project, created.addressName); |
| 80 | expect(gone).toEqual("gone"); |
| 81 | }).pipe(logLevel), |
| 82 | { tags: ["provider:gcp", "provider:gcp:compute", "live"], timeout: 90_000 }, |
| 83 | ); |