| 1 | import * as GCP from "@/GCP"; |
| 2 | import * as Test from "@/Test/Alchemy"; |
| 3 | import * as networkconnectivity from "@distilled.cloud/gcp/networkconnectivity_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 | import { defaultNetworkSelfLink } from "../networkQuota.ts"; |
| 10 | |
| 11 | const { test } = Test.make({ providers: GCP.providers() }); |
| 12 | |
| 13 | const logLevel = Effect.provideService( |
| 14 | MinimumLogLevel, |
| 15 | process.env.DEBUG ? "Debug" : "Info", |
| 16 | ); |
| 17 | |
| 18 | const runLifecycle = !process.env.FAST; |
| 19 | |
| 20 | const waitUntilGone = (name: string) => |
| 21 | networkconnectivity |
| 22 | .getProjectsLocationsServiceConnectionTokens({ name }) |
| 23 | .pipe( |
| 24 | Effect.as("found" as const), |
| 25 | Effect.catchTag("NotFound", () => Effect.succeed("gone" as const)), |
| 26 | Effect.repeat({ |
| 27 | schedule: Schedule.spaced("2 seconds"), |
| 28 | until: (status) => status === "gone", |
| 29 | times: 10, |
| 30 | }), |
| 31 | ); |
| 32 | |
| 33 | test.provider( |
| 34 | "getProjectsLocationsServiceConnectionTokens on a missing token fails with a typed tag", |
| 35 | (stack) => |
| 36 | Effect.gen(function* () { |
| 37 | const { project } = yield* GcpEnvironment.current; |
| 38 | yield* stack.destroy(); |
| 39 | |
| 40 | const error = yield* Effect.flip( |
| 41 | networkconnectivity.getProjectsLocationsServiceConnectionTokens({ |
| 42 | name: `projects/${project}/locations/us-central1/serviceConnectionTokens/alchemy-missing`, |
| 43 | }), |
| 44 | ); |
| 45 | expect(error._tag).toEqual("NotFound"); |
| 46 | |
| 47 | yield* stack.destroy(); |
| 48 | }).pipe(logLevel), |
| 49 | { |
| 50 | tags: ["provider:gcp", "provider:gcp:networkconnectivity", "live"], |
| 51 | timeout: 90_000, |
| 52 | }, |
| 53 | ); |
| 54 | |
| 55 | test.provider.skipIf(!runLifecycle)( |
| 56 | "create and delete a service connection token", |
| 57 | (stack) => |
| 58 | Effect.gen(function* () { |
| 59 | const { project } = yield* GcpEnvironment.current; |
| 60 | yield* stack.destroy(); |
| 61 | |
| 62 | const created = yield* stack.deploy( |
| 63 | Effect.gen(function* () { |
| 64 | const token = yield* GCP.NetworkConnectivity.ServiceConnectionToken( |
| 65 | "Consumer", |
| 66 | { |
| 67 | network: defaultNetworkSelfLink(project), |
| 68 | description: "token a", |
| 69 | labels: { env: "test" }, |
| 70 | }, |
| 71 | ); |
| 72 | return { token }; |
| 73 | }), |
| 74 | ); |
| 75 | |
| 76 | expect(created.token.name).toContain("/serviceConnectionTokens/"); |
| 77 | expect(created.token.location).toEqual("us-central1"); |
| 78 | expect(created.token.description).toEqual("token a"); |
| 79 | expect(created.token.labels).toMatchObject({ env: "test" }); |
| 80 | expect(created.token.serviceConnectionTokenId).toEqual( |
| 81 | expect.any(String), |
| 82 | ); |
| 83 | |
| 84 | const fetched = |
| 85 | yield* networkconnectivity.getProjectsLocationsServiceConnectionTokens({ |
| 86 | name: created.token.name, |
| 87 | }); |
| 88 | expect(fetched.name).toEqual(created.token.name); |
| 89 | expect(fetched.labels?.env).toEqual("test"); |
| 90 | expect( |
| 91 | Object.keys(fetched.labels ?? {}).some((key) => |
| 92 | key.startsWith("alchemy-"), |
| 93 | ), |
| 94 | ).toEqual(true); |
| 95 | |
| 96 | yield* stack.destroy(); |
| 97 | |
| 98 | const gone = yield* waitUntilGone(created.token.name); |
| 99 | expect(gone).toEqual("gone"); |
| 100 | }).pipe(logLevel), |
| 101 | { |
| 102 | tags: ["provider:gcp", "provider:gcp:networkconnectivity", "live"], |
| 103 | timeout: 600_000, |
| 104 | }, |
| 105 | ); |