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";
8
9const { test } = Test.make({ providers: GCP.providers() });
10
11const logLevel = Effect.provideService(
12 MinimumLogLevel,
13 process.env.DEBUG ? "Debug" : "Info",
14);
15
16const waitUntilGone = (project: string, region: string, name: string) =>
17 compute
18 .getRegionBackendServices({ project, region, backendService: name })
19 .pipe(
20 Effect.as("found" as const),
21 Effect.catchTag("NotFound", () => Effect.succeed("gone" as const)),
22 Effect.repeat({
23 schedule: Schedule.spaced("1 second"),
24 until: (status) => status === "gone",
25 times: 10,
26 }),
27 );
28
29test.provider(
30 "create, update, and delete a region backend service",
31 (stack) =>
32 Effect.gen(function* () {
33 yield* stack.destroy();
34
35 const created = yield* stack.deploy(
36 Effect.gen(function* () {
37 return yield* GCP.Compute.RegionBackendService("Web", {
38 protocol: "HTTP",
39 loadBalancingScheme: "INTERNAL_MANAGED",
40 description: "alchemy test",
41 labels: { env: "test" },
42 });
43 }),
44 );
45
46 expect(created.name).toEqual(expect.any(String));
47 expect(created.region).toEqual("us-central1");
48 expect(created.protocol).toEqual("HTTP");
49 expect(created.loadBalancingScheme).toEqual("INTERNAL_MANAGED");
50 expect(created.timeoutSec).toEqual(30);
51 expect(created.description).toEqual("alchemy test");
52 expect(created.labels).toMatchObject({ env: "test" });
53
54 const fetched = yield* compute.getRegionBackendServices({
55 project: created.project,
56 region: created.region,
57 backendService: created.name,
58 });
59 expect(fetched.name).toEqual(created.name);
60 expect(fetched.protocol).toEqual("HTTP");
61 expect(fetched.loadBalancingScheme).toEqual("INTERNAL_MANAGED");
62 expect(fetched.description).toContain("alchemy-id=");
63 expect(fetched.description).toContain("env=test");
64 expect(fetched.description).toContain("alchemy test");
65 expect(fetched.timeoutSec).toEqual(30);
66
67 const updated = yield* stack.deploy(
68 Effect.gen(function* () {
69 return yield* GCP.Compute.RegionBackendService("Web", {
70 name: created.name,
71 region: created.region,
72 protocol: "HTTP",
73 loadBalancingScheme: "INTERNAL_MANAGED",
74 timeoutSec: 60,
75 localityLbPolicy: "ROUND_ROBIN",
76 description: "updated",
77 labels: { env: "prod", role: "lb" },
78 });
79 }),
80 );
81
82 expect(updated.name).toEqual(created.name);
83 expect(updated.timeoutSec).toEqual(60);
84 expect(updated.localityLbPolicy).toEqual("ROUND_ROBIN");
85 expect(updated.description).toEqual("updated");
86 expect(updated.labels).toMatchObject({ env: "prod", role: "lb" });
87
88 const fetchedUpdate = yield* compute.getRegionBackendServices({
89 project: created.project,
90 region: created.region,
91 backendService: created.name,
92 });
93 expect(fetchedUpdate.timeoutSec).toEqual(60);
94 expect(fetchedUpdate.localityLbPolicy).toEqual("ROUND_ROBIN");
95 expect(fetchedUpdate.description).toContain("env=prod");
96 expect(fetchedUpdate.description).toContain("role=lb");
97 expect(fetchedUpdate.description).toContain("updated");
98
99 yield* stack.destroy();
100
101 const gone = yield* waitUntilGone(
102 created.project,
103 created.region,
104 created.name,
105 );
106 expect(gone).toEqual("gone");
107 }).pipe(logLevel),
108 { tags: ["provider:gcp", "provider:gcp:compute", "live"], timeout: 120_000 },
109);