| 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 | |
| 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 = (name: string) => |
| 17 | networkconnectivity.getProjectsLocationsGlobalHubs({ 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.skipIf(!!process.env.FAST)( |
| 28 | "create, update, and delete a hub", |
| 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.NetworkConnectivity.Hub("Mesh", { |
| 36 | description: "ncc hub a", |
| 37 | labels: { env: "test" }, |
| 38 | }); |
| 39 | }), |
| 40 | ); |
| 41 | |
| 42 | expect(created.name).toContain("/hubs/"); |
| 43 | expect(created.name).toContain("/locations/global/"); |
| 44 | expect(created.hubId).toEqual(expect.any(String)); |
| 45 | expect(created.location).toEqual("global"); |
| 46 | expect(created.description).toEqual("ncc hub a"); |
| 47 | expect(created.exportPsc).toEqual(false); |
| 48 | expect(created.labels).toMatchObject({ env: "test" }); |
| 49 | expect(created.uniqueId).toEqual(expect.any(String)); |
| 50 | expect(created.createTime).toEqual(expect.any(String)); |
| 51 | expect(created.state).toEqual("ACTIVE"); |
| 52 | |
| 53 | const fetched = yield* networkconnectivity.getProjectsLocationsGlobalHubs( |
| 54 | { |
| 55 | name: created.name, |
| 56 | }, |
| 57 | ); |
| 58 | expect(fetched.name).toEqual(created.name); |
| 59 | expect(fetched.labels?.env).toEqual("test"); |
| 60 | expect(fetched.description).toEqual("ncc hub a"); |
| 61 | expect( |
| 62 | Object.keys(fetched.labels ?? {}).some((key) => |
| 63 | key.startsWith("alchemy-"), |
| 64 | ), |
| 65 | ).toEqual(true); |
| 66 | |
| 67 | const updated = yield* stack.deploy( |
| 68 | Effect.gen(function* () { |
| 69 | return yield* GCP.NetworkConnectivity.Hub("Mesh", { |
| 70 | hubId: created.hubId, |
| 71 | description: "ncc hub b", |
| 72 | exportPsc: true, |
| 73 | labels: { env: "prod", role: "ncc" }, |
| 74 | }); |
| 75 | }), |
| 76 | ); |
| 77 | |
| 78 | expect(updated.name).toEqual(created.name); |
| 79 | expect(updated.uniqueId).toEqual(created.uniqueId); |
| 80 | expect(updated.description).toEqual("ncc hub b"); |
| 81 | expect(updated.exportPsc).toEqual(true); |
| 82 | expect(updated.labels).toMatchObject({ env: "prod", role: "ncc" }); |
| 83 | |
| 84 | const refetched = |
| 85 | yield* networkconnectivity.getProjectsLocationsGlobalHubs({ |
| 86 | name: created.name, |
| 87 | }); |
| 88 | expect(refetched.description).toEqual("ncc hub b"); |
| 89 | expect(refetched.exportPsc).toEqual(true); |
| 90 | expect(refetched.labels?.env).toEqual("prod"); |
| 91 | expect(refetched.labels?.role).toEqual("ncc"); |
| 92 | |
| 93 | yield* stack.destroy(); |
| 94 | |
| 95 | const gone = yield* waitUntilGone(created.name); |
| 96 | expect(gone).toEqual("gone"); |
| 97 | }).pipe(logLevel), |
| 98 | { |
| 99 | tags: ["provider:gcp", "provider:gcp:networkconnectivity", "live"], |
| 100 | timeout: 120_000, |
| 101 | }, |
| 102 | ); |