1import * as GCP from "@/GCP";
2import * as Test from "@/Test/Alchemy";
3import * as compute from "@distilled.cloud/gcp/compute_v1";
4import { expect } from "alchemy-test";
5import * as Effect from "effect/Effect";
6import { MinimumLogLevel } from "effect/References";
7import * as Schedule from "effect/Schedule";
8import { GcpEnvironment } from "@/GCP/Environment";
9
10const { test } = Test.make({ providers: GCP.providers() });
11
12const logLevel = Effect.provideService(
13 MinimumLogLevel,
14 process.env.DEBUG ? "Debug" : "Info",
15);
16
17const runLifecycle = !!process.env.GCP_TEST_BYOIP && !process.env.FAST;
18
19const parentPrefix = process.env.GCP_TEST_PAP_PARENT ?? "";
20const ipCidrRange = process.env.GCP_TEST_PDP_RANGE ?? "203.0.113.0/26";
21const region = "us-central1";
22
23const waitUntilGone = (
24 project: string,
25 regionName: string,
26 publicDelegatedPrefix: string,
27) =>
28 compute
29 .getPublicDelegatedPrefixes({
30 project,
31 region: regionName,
32 publicDelegatedPrefix,
33 })
34 .pipe(
35 Effect.as("found" as const),
36 Effect.catchTag("NotFound", () => Effect.succeed("gone" as const)),
37 Effect.repeat({
38 schedule: Schedule.spaced("2 seconds"),
39 until: (status) => status === "gone",
40 times: 10,
41 }),
42 );
43
44test.provider(
45 "insertPublicDelegatedPrefixes with a malformed parentPrefix fails with BadRequest",
46 () =>
47 Effect.gen(function* () {
48 const { project } = yield* GcpEnvironment.current;
49 const error = yield* Effect.flip(
50 compute.insertPublicDelegatedPrefixes({
51 project,
52 region,
53 body: {
54 name: "alchemy-pdp-probe",
55 description: "alchemy entitlement probe",
56 parentPrefix: parentPrefix || "does-not-exist",
57 ipCidrRange,
58 },
59 }),
60 );
61 expect(error._tag).toEqual("BadRequest");
62 }).pipe(logLevel),
63 { tags: ["provider:gcp", "provider:gcp:compute", "live"], timeout: 60_000 },
64);
65
66test.provider.skipIf(!runLifecycle || !parentPrefix)(
67 "create, update, and delete a regional public delegated prefix",
68 (stack) =>
69 Effect.gen(function* () {
70 yield* stack.destroy();
71
72 const created = yield* stack.deploy(
73 Effect.gen(function* () {
74 return yield* GCP.Compute.PublicDelegatedPrefix("Delegate", {
75 region,
76 parentPrefix,
77 ipCidrRange,
78 description: "delegated range",
79 });
80 }),
81 );
82
83 expect(created.prefixName).toEqual(expect.any(String));
84 expect(created.region).toEqual(region);
85 expect(created.ipCidrRange).toEqual(ipCidrRange);
86 expect(created.description).toEqual("delegated range");
87
88 const fetched = yield* compute.getPublicDelegatedPrefixes({
89 project: created.project,
90 region,
91 publicDelegatedPrefix: created.prefixName,
92 });
93 expect(fetched.name).toEqual(created.prefixName);
94 expect(fetched.description).toContain("[alchemy ");
95
96 const updated = yield* stack.deploy(
97 Effect.gen(function* () {
98 return yield* GCP.Compute.PublicDelegatedPrefix("Delegate", {
99 prefixName: created.prefixName,
100 region,
101 parentPrefix,
102 ipCidrRange,
103 description: "updated range",
104 });
105 }),
106 );
107
108 expect(updated.prefixName).toEqual(created.prefixName);
109 expect(updated.description).toEqual("updated range");
110
111 yield* stack.destroy();
112
113 const gone = yield* waitUntilGone(
114 created.project,
115 region,
116 created.prefixName,
117 );
118 expect(gone).toEqual("gone");
119 }).pipe(logLevel),
120 { tags: ["provider:gcp", "provider:gcp:compute", "live"], timeout: 90_000 },
121);