| 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, name: string) => |
| 17 | compute.getBackendServices({ project, backendService: name }).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 backend service", |
| 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.BackendService("Web", { |
| 36 | protocol: "HTTP", |
| 37 | loadBalancingScheme: "EXTERNAL", |
| 38 | description: "alchemy test", |
| 39 | labels: { env: "test" }, |
| 40 | }); |
| 41 | }), |
| 42 | ); |
| 43 | |
| 44 | expect(created.name).toEqual(expect.any(String)); |
| 45 | expect(created.protocol).toEqual("HTTP"); |
| 46 | expect(created.loadBalancingScheme).toEqual("EXTERNAL"); |
| 47 | expect(created.timeoutSec).toEqual(30); |
| 48 | expect(created.enableCDN).toEqual(false); |
| 49 | expect(created.description).toEqual("alchemy test"); |
| 50 | expect(created.labels).toMatchObject({ env: "test" }); |
| 51 | |
| 52 | const fetched = yield* compute.getBackendServices({ |
| 53 | project: created.project, |
| 54 | backendService: created.name, |
| 55 | }); |
| 56 | expect(fetched.name).toEqual(created.name); |
| 57 | expect(fetched.protocol).toEqual("HTTP"); |
| 58 | expect(fetched.description).toContain("alchemy-id="); |
| 59 | expect(fetched.description).toContain("env=test"); |
| 60 | expect(fetched.description).toContain("alchemy test"); |
| 61 | expect(fetched.timeoutSec).toEqual(30); |
| 62 | |
| 63 | const updated = yield* stack.deploy( |
| 64 | Effect.gen(function* () { |
| 65 | return yield* GCP.Compute.BackendService("Web", { |
| 66 | name: created.name, |
| 67 | protocol: "HTTP", |
| 68 | loadBalancingScheme: "EXTERNAL", |
| 69 | timeoutSec: 60, |
| 70 | enableCDN: true, |
| 71 | description: "updated", |
| 72 | labels: { env: "prod", role: "lb" }, |
| 73 | }); |
| 74 | }), |
| 75 | ); |
| 76 | |
| 77 | expect(updated.name).toEqual(created.name); |
| 78 | expect(updated.timeoutSec).toEqual(60); |
| 79 | expect(updated.enableCDN).toEqual(true); |
| 80 | expect(updated.description).toEqual("updated"); |
| 81 | expect(updated.labels).toMatchObject({ env: "prod", role: "lb" }); |
| 82 | |
| 83 | const fetchedUpdate = yield* compute.getBackendServices({ |
| 84 | project: created.project, |
| 85 | backendService: created.name, |
| 86 | }); |
| 87 | expect(fetchedUpdate.timeoutSec).toEqual(60); |
| 88 | expect(fetchedUpdate.enableCDN).toEqual(true); |
| 89 | expect(fetchedUpdate.description).toContain("env=prod"); |
| 90 | expect(fetchedUpdate.description).toContain("role=lb"); |
| 91 | expect(fetchedUpdate.description).toContain("updated"); |
| 92 | |
| 93 | yield* stack.destroy(); |
| 94 | |
| 95 | const gone = yield* waitUntilGone(created.project, created.name); |
| 96 | expect(gone).toEqual("gone"); |
| 97 | }).pipe(logLevel), |
| 98 | { tags: ["provider:gcp", "provider:gcp:compute", "live"], timeout: 120_000 }, |
| 99 | ); |