| 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 { withNetworkSlot } from "../networkQuota.ts"; |
| 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 | networkconnectivity.getProjectsLocationsSpokes({ name }).pipe( |
| 19 | Effect.as("found" as const), |
| 20 | Effect.catchTag("NotFound", () => Effect.succeed("gone" as const)), |
| 21 | Effect.repeat({ |
| 22 | schedule: Schedule.spaced("2 seconds"), |
| 23 | until: (status) => status === "gone", |
| 24 | times: 10, |
| 25 | }), |
| 26 | ); |
| 27 | |
| 28 | test.provider.skipIf(!!process.env.FAST)( |
| 29 | "create, update, and delete a vpc spoke", |
| 30 | (stack) => |
| 31 | Effect.gen(function* () { |
| 32 | yield* stack.destroy(); |
| 33 | |
| 34 | const created = yield* stack.deploy( |
| 35 | Effect.gen(function* () { |
| 36 | const network = yield* GCP.Compute.Network("SpokeVpc", { |
| 37 | autoCreateSubnetworks: false, |
| 38 | }); |
| 39 | const hub = yield* GCP.NetworkConnectivity.Hub("Mesh", { |
| 40 | description: "ncc hub for spoke", |
| 41 | }); |
| 42 | const spoke = yield* GCP.NetworkConnectivity.Spoke("VpcSpoke", { |
| 43 | hub: hub.name, |
| 44 | description: "spoke a", |
| 45 | labels: { env: "test" }, |
| 46 | linkedVpcNetwork: { |
| 47 | uri: network.selfLink.as<string>(), |
| 48 | }, |
| 49 | }); |
| 50 | return { network, hub, spoke }; |
| 51 | }), |
| 52 | ); |
| 53 | |
| 54 | expect(created.spoke.name).toContain("/spokes/"); |
| 55 | expect(created.spoke.name).toContain("/locations/global/"); |
| 56 | expect(created.spoke.spokeId).toEqual(expect.any(String)); |
| 57 | expect(created.spoke.location).toEqual("global"); |
| 58 | expect(created.spoke.hub).toEqual(created.hub.name); |
| 59 | expect(created.spoke.description).toEqual("spoke a"); |
| 60 | expect(created.spoke.labels).toMatchObject({ env: "test" }); |
| 61 | expect(created.spoke.spokeType).toEqual("VPC_NETWORK"); |
| 62 | expect(created.spoke.state).toEqual("ACTIVE"); |
| 63 | expect(created.spoke.uniqueId).toEqual(expect.any(String)); |
| 64 | expect(created.spoke.createTime).toEqual(expect.any(String)); |
| 65 | expect(created.spoke.linkedVpcNetwork?.uri).toContain( |
| 66 | `networks/${created.network.networkName}`, |
| 67 | ); |
| 68 | |
| 69 | const fetched = yield* networkconnectivity.getProjectsLocationsSpokes({ |
| 70 | name: created.spoke.name, |
| 71 | }); |
| 72 | expect(fetched.name).toEqual(created.spoke.name); |
| 73 | expect(fetched.hub).toEqual(created.hub.name); |
| 74 | expect(fetched.description).toEqual("spoke a"); |
| 75 | expect(fetched.labels?.env).toEqual("test"); |
| 76 | expect(fetched.spokeType).toEqual("VPC_NETWORK"); |
| 77 | expect(fetched.state).toEqual("ACTIVE"); |
| 78 | expect( |
| 79 | Object.keys(fetched.labels ?? {}).some((key) => |
| 80 | key.startsWith("alchemy-"), |
| 81 | ), |
| 82 | ).toEqual(true); |
| 83 | |
| 84 | const updated = yield* stack.deploy( |
| 85 | Effect.gen(function* () { |
| 86 | const network = yield* GCP.Compute.Network("SpokeVpc", { |
| 87 | networkName: created.network.networkName, |
| 88 | autoCreateSubnetworks: false, |
| 89 | }); |
| 90 | const hub = yield* GCP.NetworkConnectivity.Hub("Mesh", { |
| 91 | hubId: created.hub.hubId, |
| 92 | description: "ncc hub for spoke", |
| 93 | }); |
| 94 | const spoke = yield* GCP.NetworkConnectivity.Spoke("VpcSpoke", { |
| 95 | spokeId: created.spoke.spokeId, |
| 96 | hub: hub.name, |
| 97 | description: "spoke b", |
| 98 | labels: { env: "prod", role: "spoke" }, |
| 99 | linkedVpcNetwork: { |
| 100 | uri: network.selfLink.as<string>(), |
| 101 | }, |
| 102 | }); |
| 103 | return { network, hub, spoke }; |
| 104 | }), |
| 105 | ); |
| 106 | |
| 107 | expect(updated.spoke.name).toEqual(created.spoke.name); |
| 108 | expect(updated.spoke.uniqueId).toEqual(created.spoke.uniqueId); |
| 109 | expect(updated.spoke.description).toEqual("spoke b"); |
| 110 | expect(updated.spoke.labels).toMatchObject({ |
| 111 | env: "prod", |
| 112 | role: "spoke", |
| 113 | }); |
| 114 | expect(updated.spoke.state).toEqual("ACTIVE"); |
| 115 | |
| 116 | const refetched = yield* networkconnectivity.getProjectsLocationsSpokes({ |
| 117 | name: created.spoke.name, |
| 118 | }); |
| 119 | expect(refetched.description).toEqual("spoke b"); |
| 120 | expect(refetched.labels?.env).toEqual("prod"); |
| 121 | expect(refetched.labels?.role).toEqual("spoke"); |
| 122 | expect(refetched.hub).toEqual(created.hub.name); |
| 123 | |
| 124 | yield* stack.destroy(); |
| 125 | |
| 126 | const gone = yield* waitUntilGone(created.spoke.name); |
| 127 | expect(gone).toEqual("gone"); |
| 128 | }).pipe(logLevel, withNetworkSlot), |
| 129 | { |
| 130 | tags: ["provider:gcp", "provider:gcp:networkconnectivity", "live"], |
| 131 | timeout: 600_000, |
| 132 | }, |
| 133 | ); |