| 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 parentPrefix = process.env.GCP_TEST_PAP_PARENT ?? ""; |
| 20 | const ipCidrRange = process.env.GCP_TEST_PDP_RANGE ?? "203.0.113.0/24"; |
| 21 | |
| 22 | const waitUntilGone = (project: string, publicDelegatedPrefix: string) => |
| 23 | compute |
| 24 | .getGlobalPublicDelegatedPrefixes({ project, publicDelegatedPrefix }) |
| 25 | .pipe( |
| 26 | Effect.as("found" as const), |
| 27 | Effect.catchTag("NotFound", () => Effect.succeed("gone" as const)), |
| 28 | Effect.repeat({ |
| 29 | schedule: Schedule.spaced("2 seconds"), |
| 30 | until: (status) => status === "gone", |
| 31 | times: 10, |
| 32 | }), |
| 33 | ); |
| 34 | |
| 35 | test.provider( |
| 36 | "insertGlobalPublicDelegatedPrefixes with a malformed parentPrefix fails with BadRequest", |
| 37 | () => |
| 38 | Effect.gen(function* () { |
| 39 | const { project } = yield* GcpEnvironment.current; |
| 40 | const error = yield* Effect.flip( |
| 41 | compute.insertGlobalPublicDelegatedPrefixes({ |
| 42 | project, |
| 43 | body: { |
| 44 | name: "alchemy-pdp-probe", |
| 45 | description: "alchemy entitlement probe", |
| 46 | parentPrefix: parentPrefix || "does-not-exist", |
| 47 | ipCidrRange, |
| 48 | }, |
| 49 | }), |
| 50 | ); |
| 51 | expect(error._tag).toEqual("BadRequest"); |
| 52 | }).pipe(logLevel), |
| 53 | { tags: ["provider:gcp", "provider:gcp:compute", "live"], timeout: 60_000 }, |
| 54 | ); |
| 55 | |
| 56 | test.provider.skipIf(!runLifecycle || !parentPrefix)( |
| 57 | "create, update, and delete a global public delegated prefix", |
| 58 | (stack) => |
| 59 | Effect.gen(function* () { |
| 60 | yield* stack.destroy(); |
| 61 | |
| 62 | const created = yield* stack.deploy( |
| 63 | Effect.gen(function* () { |
| 64 | return yield* GCP.Compute.GlobalPublicDelegatedPrefix("Byoip", { |
| 65 | parentPrefix, |
| 66 | ipCidrRange, |
| 67 | description: "delegated range", |
| 68 | }); |
| 69 | }), |
| 70 | ); |
| 71 | |
| 72 | expect(created.prefixName).toEqual(expect.any(String)); |
| 73 | expect(created.ipCidrRange).toEqual(ipCidrRange); |
| 74 | expect(created.description).toEqual("delegated range"); |
| 75 | |
| 76 | const fetched = yield* compute.getGlobalPublicDelegatedPrefixes({ |
| 77 | project: created.project, |
| 78 | publicDelegatedPrefix: created.prefixName, |
| 79 | }); |
| 80 | expect(fetched.name).toEqual(created.prefixName); |
| 81 | expect(fetched.description).toContain("[alchemy "); |
| 82 | |
| 83 | const updated = yield* stack.deploy( |
| 84 | Effect.gen(function* () { |
| 85 | return yield* GCP.Compute.GlobalPublicDelegatedPrefix("Byoip", { |
| 86 | prefixName: created.prefixName, |
| 87 | parentPrefix, |
| 88 | ipCidrRange, |
| 89 | description: "updated range", |
| 90 | }); |
| 91 | }), |
| 92 | ); |
| 93 | expect(updated.description).toEqual("updated range"); |
| 94 | |
| 95 | yield* stack.destroy(); |
| 96 | const gone = yield* waitUntilGone(created.project, created.prefixName); |
| 97 | expect(gone).toEqual("gone"); |
| 98 | }).pipe(logLevel), |
| 99 | { tags: ["provider:gcp", "provider:gcp:compute", "live"], timeout: 90_000 }, |
| 100 | ); |