| 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 | import { GcpEnvironment } from "@/GCP/Environment"; |
| 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.GCP_TEST_BYOIP && !process.env.FAST; |
| 18 | |
| 19 | const ipCidrRange = process.env.GCP_TEST_PAP_RANGE ?? "203.0.113.0/24"; |
| 20 | const dnsVerificationIp = process.env.GCP_TEST_PAP_DNS_IP ?? "203.0.113.1"; |
| 21 | |
| 22 | const waitUntilGone = (project: string, publicAdvertisedPrefix: string) => |
| 23 | compute.getPublicAdvertisedPrefixes({ project, publicAdvertisedPrefix }).pipe( |
| 24 | Effect.as("found" as const), |
| 25 | Effect.catchTag("NotFound", () => Effect.succeed("gone" as const)), |
| 26 | Effect.repeat({ |
| 27 | schedule: Schedule.spaced("2 seconds"), |
| 28 | until: (status) => status === "gone", |
| 29 | times: 10, |
| 30 | }), |
| 31 | ); |
| 32 | |
| 33 | test.provider.skipIf(!runLifecycle)( |
| 34 | "create, update, and delete a public advertised prefix", |
| 35 | (stack) => |
| 36 | Effect.gen(function* () { |
| 37 | yield* stack.destroy(); |
| 38 | |
| 39 | const created = yield* stack.deploy( |
| 40 | Effect.gen(function* () { |
| 41 | return yield* GCP.Compute.PublicAdvertisedPrefix("Byoip", { |
| 42 | ipCidrRange, |
| 43 | dnsVerificationIp, |
| 44 | pdpScope: "REGIONAL", |
| 45 | description: "lab prefix", |
| 46 | }); |
| 47 | }), |
| 48 | ); |
| 49 | |
| 50 | expect(created.prefixName).toEqual(expect.any(String)); |
| 51 | expect(created.ipCidrRange).toEqual(ipCidrRange); |
| 52 | expect(created.description).toEqual("lab prefix"); |
| 53 | |
| 54 | const fetched = yield* compute.getPublicAdvertisedPrefixes({ |
| 55 | project: created.project, |
| 56 | publicAdvertisedPrefix: created.prefixName, |
| 57 | }); |
| 58 | expect(fetched.name).toEqual(created.prefixName); |
| 59 | expect(fetched.description).toContain("[alchemy "); |
| 60 | |
| 61 | const updated = yield* stack.deploy( |
| 62 | Effect.gen(function* () { |
| 63 | return yield* GCP.Compute.PublicAdvertisedPrefix("Byoip", { |
| 64 | prefixName: created.prefixName, |
| 65 | ipCidrRange, |
| 66 | dnsVerificationIp, |
| 67 | pdpScope: "REGIONAL", |
| 68 | description: "updated prefix", |
| 69 | }); |
| 70 | }), |
| 71 | ); |
| 72 | |
| 73 | expect(updated.prefixName).toEqual(created.prefixName); |
| 74 | expect(updated.description).toEqual("updated prefix"); |
| 75 | |
| 76 | yield* stack.destroy(); |
| 77 | |
| 78 | const gone = yield* waitUntilGone(created.project, created.prefixName); |
| 79 | expect(gone).toEqual("gone"); |
| 80 | }).pipe(logLevel), |
| 81 | { tags: ["provider:gcp", "provider:gcp:compute", "live"], timeout: 90_000 }, |
| 82 | ); |