| 1 | import { GcpEnvironment } from "@/GCP/Environment"; |
| 2 | import * as GCP from "@/GCP"; |
| 3 | import * as Test from "@/Test/Alchemy"; |
| 4 | import * as vpcaccess from "@distilled.cloud/gcp/vpcaccess_v1"; |
| 5 | import { expect } from "alchemy-test"; |
| 6 | import * as Effect from "effect/Effect"; |
| 7 | import { MinimumLogLevel } from "effect/References"; |
| 8 | import * as Schedule from "effect/Schedule"; |
| 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 | // Create + patch + delete each take ~2–4 minutes (~7 minutes end to end). |
| 18 | const runLifecycle = !!process.env.GCP_TEST_SLOW && !process.env.FAST; |
| 19 | |
| 20 | const waitUntilGone = (name: string) => |
| 21 | vpcaccess.getProjectsLocationsConnectors({ name }).pipe( |
| 22 | Effect.as("found" as const), |
| 23 | Effect.catchTag("NotFound", () => Effect.succeed("gone" as const)), |
| 24 | Effect.repeat({ |
| 25 | schedule: Schedule.spaced("2 seconds"), |
| 26 | until: (status) => status === "gone", |
| 27 | times: 10, |
| 28 | }), |
| 29 | ); |
| 30 | |
| 31 | test.provider( |
| 32 | "getProjectsLocationsConnectors on a missing connector fails with NotFound", |
| 33 | (stack) => |
| 34 | Effect.gen(function* () { |
| 35 | const { project } = yield* GcpEnvironment.current; |
| 36 | |
| 37 | yield* stack.destroy(); |
| 38 | |
| 39 | const error = yield* Effect.flip( |
| 40 | vpcaccess.getProjectsLocationsConnectors({ |
| 41 | name: `projects/${project}/locations/us-central1/connectors/alchemy-vpc-con-missing`, |
| 42 | }), |
| 43 | ); |
| 44 | expect(error._tag).toBe("NotFound"); |
| 45 | |
| 46 | const page = yield* vpcaccess.listProjectsLocationsConnectors({ |
| 47 | parent: `projects/${project}/locations/us-central1`, |
| 48 | pageSize: 100, |
| 49 | }); |
| 50 | expect( |
| 51 | (page.connectors ?? []).map((connector) => connector.name), |
| 52 | ).not.toContain( |
| 53 | `projects/${project}/locations/us-central1/connectors/alchemy-vpc-con-missing`, |
| 54 | ); |
| 55 | |
| 56 | const locations = yield* vpcaccess.listProjectsLocations({ |
| 57 | name: `projects/${project}`, |
| 58 | pageSize: 100, |
| 59 | }); |
| 60 | expect( |
| 61 | (locations.locations ?? []).map((location) => location.locationId), |
| 62 | ).toContain("us-central1"); |
| 63 | |
| 64 | yield* stack.destroy(); |
| 65 | }).pipe(logLevel), |
| 66 | { tags: ["provider:gcp", "provider:gcp:vpcaccess", "live"], timeout: 90_000 }, |
| 67 | ); |
| 68 | |
| 69 | test.provider.skipIf(!runLifecycle)( |
| 70 | "create, update, and delete a vpc access connector", |
| 71 | (stack) => |
| 72 | Effect.gen(function* () { |
| 73 | const { project } = yield* GcpEnvironment.current; |
| 74 | |
| 75 | yield* stack.destroy(); |
| 76 | |
| 77 | const created = yield* stack.deploy( |
| 78 | Effect.gen(function* () { |
| 79 | return yield* GCP.VpcAccess.Connector("Egress", { |
| 80 | location: "us-central1", |
| 81 | network: "default", |
| 82 | ipCidrRange: "10.88.0.0/28", |
| 83 | machineType: "e2-micro", |
| 84 | minInstances: 2, |
| 85 | maxInstances: 3, |
| 86 | }); |
| 87 | }), |
| 88 | ); |
| 89 | |
| 90 | expect(created.connectorId).toEqual(expect.any(String)); |
| 91 | expect(created.location).toEqual("us-central1"); |
| 92 | expect(created.project).toEqual(project); |
| 93 | expect(created.name).toEqual( |
| 94 | `projects/${project}/locations/us-central1/connectors/${created.connectorId}`, |
| 95 | ); |
| 96 | expect(created.state).toEqual("READY"); |
| 97 | expect(created.network).toEqual("default"); |
| 98 | expect(created.ipCidrRange).toEqual("10.88.0.0/28"); |
| 99 | expect(created.minInstances).toEqual(2); |
| 100 | expect(created.maxInstances).toEqual(3); |
| 101 | |
| 102 | const fetched = yield* vpcaccess.getProjectsLocationsConnectors({ |
| 103 | name: created.name, |
| 104 | }); |
| 105 | expect(fetched.name).toEqual(created.name); |
| 106 | expect(fetched.state).toEqual("READY"); |
| 107 | expect(fetched.network).toEqual("default"); |
| 108 | expect(fetched.ipCidrRange).toEqual("10.88.0.0/28"); |
| 109 | expect(fetched.minInstances).toEqual(2); |
| 110 | expect(fetched.maxInstances).toEqual(3); |
| 111 | |
| 112 | const updated = yield* stack.deploy( |
| 113 | Effect.gen(function* () { |
| 114 | return yield* GCP.VpcAccess.Connector("Egress", { |
| 115 | connectorId: created.connectorId, |
| 116 | location: "us-central1", |
| 117 | network: "default", |
| 118 | ipCidrRange: "10.88.0.0/28", |
| 119 | machineType: "e2-micro", |
| 120 | minInstances: 2, |
| 121 | maxInstances: 4, |
| 122 | }); |
| 123 | }), |
| 124 | ); |
| 125 | |
| 126 | expect(updated.name).toEqual(created.name); |
| 127 | expect(updated.maxInstances).toEqual(4); |
| 128 | expect(updated.minInstances).toEqual(2); |
| 129 | expect(updated.state).toEqual("READY"); |
| 130 | |
| 131 | const fetchedUpdate = yield* vpcaccess.getProjectsLocationsConnectors({ |
| 132 | name: created.name, |
| 133 | }); |
| 134 | expect(fetchedUpdate.maxInstances).toEqual(4); |
| 135 | expect(fetchedUpdate.minInstances).toEqual(2); |
| 136 | |
| 137 | yield* stack.destroy(); |
| 138 | |
| 139 | const gone = yield* waitUntilGone(created.name); |
| 140 | expect(gone).toEqual("gone"); |
| 141 | }).pipe(logLevel), |
| 142 | { |
| 143 | tags: ["provider:gcp", "provider:gcp:vpcaccess", "live"], |
| 144 | timeout: 540_000, |
| 145 | }, |
| 146 | ); |