| 1 | import * as GCP from "@/GCP"; |
| 2 | import * as Test from "@/Test/Alchemy"; |
| 3 | import * as networkservices from "@distilled.cloud/gcp/networkservices_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 waitUntilGone = (name: string) => |
| 18 | networkservices.getProjectsLocationsServiceLbPolicies({ name }).pipe( |
| 19 | Effect.as("found" as const), |
| 20 | Effect.catchTag("NotFound", () => Effect.succeed("gone" as const)), |
| 21 | Effect.repeat({ |
| 22 | schedule: Schedule.spaced("1 second"), |
| 23 | until: (status) => status === "gone", |
| 24 | times: 10, |
| 25 | }), |
| 26 | ); |
| 27 | |
| 28 | test.provider( |
| 29 | "getProjectsLocationsServiceLbPolicies on a missing policy fails with a typed tag", |
| 30 | (stack) => |
| 31 | Effect.gen(function* () { |
| 32 | yield* stack.destroy(); |
| 33 | |
| 34 | const { project } = yield* GcpEnvironment.current; |
| 35 | const error = yield* Effect.flip( |
| 36 | networkservices.getProjectsLocationsServiceLbPolicies({ |
| 37 | name: `projects/${project}/locations/global/serviceLbPolicies/alchemy-missing`, |
| 38 | }), |
| 39 | ); |
| 40 | expect(error._tag).toEqual("NotFound"); |
| 41 | |
| 42 | yield* stack.destroy(); |
| 43 | }).pipe(logLevel), |
| 44 | { |
| 45 | tags: ["provider:gcp", "provider:gcp:networkservices", "live"], |
| 46 | timeout: 90_000, |
| 47 | }, |
| 48 | ); |
| 49 | |
| 50 | test.provider( |
| 51 | "create, update, and delete a service lb policy", |
| 52 | (stack) => |
| 53 | Effect.gen(function* () { |
| 54 | yield* stack.destroy(); |
| 55 | |
| 56 | const created = yield* stack.deploy( |
| 57 | Effect.gen(function* () { |
| 58 | return yield* GCP.NetworkServices.ServiceLbPolicy("Spread", { |
| 59 | location: "global", |
| 60 | description: "lb policy a", |
| 61 | labels: { env: "test" }, |
| 62 | loadBalancingAlgorithm: "SPRAY_TO_REGION", |
| 63 | autoCapacityDrain: { enable: true }, |
| 64 | }); |
| 65 | }), |
| 66 | ); |
| 67 | |
| 68 | expect(created.name).toContain("/serviceLbPolicies/"); |
| 69 | expect(created.serviceLbPolicyId).toEqual(expect.any(String)); |
| 70 | expect(created.location).toEqual("global"); |
| 71 | expect(created.description).toEqual("lb policy a"); |
| 72 | expect(created.loadBalancingAlgorithm).toEqual("SPRAY_TO_REGION"); |
| 73 | expect(created.autoCapacityDrain?.enable).toEqual(true); |
| 74 | expect(created.labels).toMatchObject({ env: "test" }); |
| 75 | expect(created.createTime).toEqual(expect.any(String)); |
| 76 | |
| 77 | const fetched = |
| 78 | yield* networkservices.getProjectsLocationsServiceLbPolicies({ |
| 79 | name: created.name, |
| 80 | }); |
| 81 | expect(fetched.name).toEqual(created.name); |
| 82 | expect(fetched.description).toEqual("lb policy a"); |
| 83 | expect(fetched.loadBalancingAlgorithm).toEqual("SPRAY_TO_REGION"); |
| 84 | expect(fetched.labels?.env).toEqual("test"); |
| 85 | expect( |
| 86 | Object.keys(fetched.labels ?? {}).some((key) => |
| 87 | key.startsWith("alchemy-"), |
| 88 | ), |
| 89 | ).toEqual(true); |
| 90 | |
| 91 | const updated = yield* stack.deploy( |
| 92 | Effect.gen(function* () { |
| 93 | return yield* GCP.NetworkServices.ServiceLbPolicy("Spread", { |
| 94 | serviceLbPolicyId: created.serviceLbPolicyId, |
| 95 | location: "global", |
| 96 | description: "lb policy b", |
| 97 | labels: { env: "prod", role: "lb" }, |
| 98 | loadBalancingAlgorithm: "WATERFALL_BY_REGION", |
| 99 | autoCapacityDrain: { enable: false }, |
| 100 | failoverConfig: { failoverHealthThreshold: 70 }, |
| 101 | }); |
| 102 | }), |
| 103 | ); |
| 104 | |
| 105 | expect(updated.name).toEqual(created.name); |
| 106 | expect(updated.description).toEqual("lb policy b"); |
| 107 | expect(updated.loadBalancingAlgorithm).toEqual("WATERFALL_BY_REGION"); |
| 108 | expect(updated.failoverConfig?.failoverHealthThreshold).toEqual(70); |
| 109 | expect(updated.labels).toMatchObject({ env: "prod", role: "lb" }); |
| 110 | |
| 111 | const refetched = |
| 112 | yield* networkservices.getProjectsLocationsServiceLbPolicies({ |
| 113 | name: created.name, |
| 114 | }); |
| 115 | expect(refetched.description).toEqual("lb policy b"); |
| 116 | expect(refetched.loadBalancingAlgorithm).toEqual("WATERFALL_BY_REGION"); |
| 117 | expect(refetched.labels?.env).toEqual("prod"); |
| 118 | expect(refetched.labels?.role).toEqual("lb"); |
| 119 | |
| 120 | yield* stack.destroy(); |
| 121 | |
| 122 | const gone = yield* waitUntilGone(created.name); |
| 123 | expect(gone).toEqual("gone"); |
| 124 | }).pipe(logLevel), |
| 125 | { |
| 126 | tags: ["provider:gcp", "provider:gcp:networkservices", "live"], |
| 127 | timeout: 90_000, |
| 128 | }, |
| 129 | ); |