| 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.getProjectsLocationsAuthzExtensions({ 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 | "getProjectsLocationsAuthzExtensions on a missing extension 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.getProjectsLocationsAuthzExtensions({ |
| 37 | name: `projects/${project}/locations/us-central1/authzExtensions/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.skipIf(!!process.env.FAST)( |
| 51 | "create, update, and delete an authz extension", |
| 52 | (stack) => |
| 53 | Effect.gen(function* () { |
| 54 | yield* stack.destroy(); |
| 55 | |
| 56 | const created = yield* stack.deploy( |
| 57 | Effect.gen(function* () { |
| 58 | const health = yield* GCP.Compute.RegionHealthCheck("AuthzHc", { |
| 59 | region: "us-central1", |
| 60 | type: "HTTP", |
| 61 | httpHealthCheck: { port: 80 }, |
| 62 | }); |
| 63 | const backend = yield* GCP.Compute.RegionBackendService("AuthzBe", { |
| 64 | region: "us-central1", |
| 65 | protocol: "HTTP2", |
| 66 | loadBalancingScheme: "INTERNAL_MANAGED", |
| 67 | healthChecks: [health.selfLink.as<string>()], |
| 68 | }); |
| 69 | return yield* GCP.NetworkServices.AuthzExtension("Authz", { |
| 70 | location: "us-central1", |
| 71 | description: "authz extension a", |
| 72 | labels: { env: "test" }, |
| 73 | service: backend.selfLink.as<string>(), |
| 74 | authority: "authz.example.com", |
| 75 | timeout: "0.1s", |
| 76 | loadBalancingScheme: "INTERNAL_MANAGED", |
| 77 | }); |
| 78 | }), |
| 79 | ); |
| 80 | |
| 81 | expect(created.name).toContain("/authzExtensions/"); |
| 82 | expect(created.authzExtensionId).toEqual(expect.any(String)); |
| 83 | expect(created.location).toEqual("us-central1"); |
| 84 | expect(created.authority).toEqual("authz.example.com"); |
| 85 | expect(created.description).toEqual("authz extension a"); |
| 86 | expect(created.labels).toMatchObject({ env: "test" }); |
| 87 | expect(created.createTime).toEqual(expect.any(String)); |
| 88 | |
| 89 | const fetched = |
| 90 | yield* networkservices.getProjectsLocationsAuthzExtensions({ |
| 91 | name: created.name, |
| 92 | }); |
| 93 | expect(fetched.name).toEqual(created.name); |
| 94 | expect(fetched.description).toEqual("authz extension a"); |
| 95 | expect(fetched.labels?.env).toEqual("test"); |
| 96 | expect( |
| 97 | Object.keys(fetched.labels ?? {}).some((key) => |
| 98 | key.startsWith("alchemy-"), |
| 99 | ), |
| 100 | ).toEqual(true); |
| 101 | |
| 102 | const updated = yield* stack.deploy( |
| 103 | Effect.gen(function* () { |
| 104 | const health = yield* GCP.Compute.RegionHealthCheck("AuthzHc", { |
| 105 | region: "us-central1", |
| 106 | type: "HTTP", |
| 107 | httpHealthCheck: { port: 80 }, |
| 108 | }); |
| 109 | const backend = yield* GCP.Compute.RegionBackendService("AuthzBe", { |
| 110 | region: "us-central1", |
| 111 | protocol: "HTTP2", |
| 112 | loadBalancingScheme: "INTERNAL_MANAGED", |
| 113 | healthChecks: [health.selfLink.as<string>()], |
| 114 | }); |
| 115 | return yield* GCP.NetworkServices.AuthzExtension("Authz", { |
| 116 | authzExtensionId: created.authzExtensionId, |
| 117 | location: "us-central1", |
| 118 | description: "authz extension b", |
| 119 | labels: { env: "prod", role: "authz" }, |
| 120 | service: backend.selfLink.as<string>(), |
| 121 | authority: "authz.example.com", |
| 122 | timeout: "0.2s", |
| 123 | failOpen: true, |
| 124 | loadBalancingScheme: "INTERNAL_MANAGED", |
| 125 | }); |
| 126 | }), |
| 127 | ); |
| 128 | |
| 129 | expect(updated.name).toEqual(created.name); |
| 130 | expect(updated.description).toEqual("authz extension b"); |
| 131 | expect(updated.failOpen).toEqual(true); |
| 132 | expect(updated.labels).toMatchObject({ env: "prod", role: "authz" }); |
| 133 | |
| 134 | const refetched = |
| 135 | yield* networkservices.getProjectsLocationsAuthzExtensions({ |
| 136 | name: created.name, |
| 137 | }); |
| 138 | expect(refetched.description).toEqual("authz extension b"); |
| 139 | expect(refetched.labels?.env).toEqual("prod"); |
| 140 | expect(refetched.failOpen).toEqual(true); |
| 141 | |
| 142 | yield* stack.destroy(); |
| 143 | |
| 144 | const gone = yield* waitUntilGone(created.name); |
| 145 | expect(gone).toEqual("gone"); |
| 146 | }).pipe(logLevel), |
| 147 | { |
| 148 | tags: ["provider:gcp", "provider:gcp:networkservices", "live"], |
| 149 | timeout: 180_000, |
| 150 | }, |
| 151 | ); |