| 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 region = "us-central1"; |
| 17 | |
| 18 | const waitUntilGone = ( |
| 19 | project: string, |
| 20 | regionName: string, |
| 21 | healthAggregationPolicy: string, |
| 22 | ) => |
| 23 | compute |
| 24 | .getRegionHealthAggregationPolicies({ |
| 25 | project, |
| 26 | region: regionName, |
| 27 | healthAggregationPolicy, |
| 28 | }) |
| 29 | .pipe( |
| 30 | Effect.as("found" as const), |
| 31 | Effect.catchTag("NotFound", () => Effect.succeed("gone" as const)), |
| 32 | Effect.repeat({ |
| 33 | schedule: Schedule.spaced("1 second"), |
| 34 | until: (status) => status === "gone", |
| 35 | times: 10, |
| 36 | }), |
| 37 | ); |
| 38 | |
| 39 | test.provider( |
| 40 | "create, update, and delete a region health aggregation policy", |
| 41 | (stack) => |
| 42 | Effect.gen(function* () { |
| 43 | yield* stack.destroy(); |
| 44 | |
| 45 | const created = yield* stack.deploy( |
| 46 | Effect.gen(function* () { |
| 47 | return yield* GCP.Compute.RegionHealthAggregationPolicy("Agg", { |
| 48 | region, |
| 49 | description: "backend rollup", |
| 50 | minHealthyThreshold: 1, |
| 51 | healthyPercentThreshold: 60, |
| 52 | }); |
| 53 | }), |
| 54 | ); |
| 55 | |
| 56 | expect(created.policyName).toEqual(expect.any(String)); |
| 57 | expect(created.region).toEqual(region); |
| 58 | expect(created.policyType).toEqual("BACKEND_SERVICE_POLICY"); |
| 59 | expect(created.description).toEqual("backend rollup"); |
| 60 | expect(created.minHealthyThreshold).toEqual(1); |
| 61 | expect(created.healthyPercentThreshold).toEqual(60); |
| 62 | |
| 63 | const fetched = yield* compute.getRegionHealthAggregationPolicies({ |
| 64 | project: created.project, |
| 65 | region, |
| 66 | healthAggregationPolicy: created.policyName, |
| 67 | }); |
| 68 | expect(fetched.name).toEqual(created.policyName); |
| 69 | expect(fetched.policyType).toEqual("BACKEND_SERVICE_POLICY"); |
| 70 | expect(fetched.description).toContain("[alchemy "); |
| 71 | expect(fetched.description).toContain("backend rollup"); |
| 72 | |
| 73 | const updated = yield* stack.deploy( |
| 74 | Effect.gen(function* () { |
| 75 | return yield* GCP.Compute.RegionHealthAggregationPolicy("Agg", { |
| 76 | policyName: created.policyName, |
| 77 | region, |
| 78 | description: "updated rollup", |
| 79 | minHealthyThreshold: 2, |
| 80 | healthyPercentThreshold: 80, |
| 81 | }); |
| 82 | }), |
| 83 | ); |
| 84 | |
| 85 | expect(updated.policyName).toEqual(created.policyName); |
| 86 | expect(updated.description).toEqual("updated rollup"); |
| 87 | expect(updated.minHealthyThreshold).toEqual(2); |
| 88 | expect(updated.healthyPercentThreshold).toEqual(80); |
| 89 | |
| 90 | const refetched = yield* compute.getRegionHealthAggregationPolicies({ |
| 91 | project: updated.project, |
| 92 | region, |
| 93 | healthAggregationPolicy: updated.policyName, |
| 94 | }); |
| 95 | expect(refetched.description).toContain("updated rollup"); |
| 96 | expect(refetched.minHealthyThreshold).toEqual(2); |
| 97 | expect(refetched.healthyPercentThreshold).toEqual(80); |
| 98 | |
| 99 | yield* stack.destroy(); |
| 100 | |
| 101 | const gone = yield* waitUntilGone( |
| 102 | created.project, |
| 103 | region, |
| 104 | created.policyName, |
| 105 | ); |
| 106 | expect(gone).toEqual("gone"); |
| 107 | }).pipe(logLevel), |
| 108 | { tags: ["provider:gcp", "provider:gcp:compute", "live"], timeout: 90_000 }, |
| 109 | ); |