| 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.getProjectsLocationsHttpRoutes({ 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 | "getProjectsLocationsHttpRoutes on a missing route 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.getProjectsLocationsHttpRoutes({ |
| 37 | name: `projects/${project}/locations/global/httpRoutes/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( |
| 51 | "create, update, and delete an http route", |
| 52 | (stack) => |
| 53 | Effect.gen(function* () { |
| 54 | yield* stack.destroy(); |
| 55 | |
| 56 | const created = yield* stack.deploy( |
| 57 | Effect.gen(function* () { |
| 58 | return yield* GCP.NetworkServices.HttpRoute("Web", { |
| 59 | location: "global", |
| 60 | hostnames: ["alchemy-http.example.com"], |
| 61 | description: "http route a", |
| 62 | labels: { env: "test" }, |
| 63 | rules: [ |
| 64 | { |
| 65 | matches: [{ prefixMatch: "/" }], |
| 66 | action: { |
| 67 | redirect: { |
| 68 | hostRedirect: "example.com", |
| 69 | httpsRedirect: true, |
| 70 | }, |
| 71 | }, |
| 72 | }, |
| 73 | ], |
| 74 | }); |
| 75 | }), |
| 76 | ); |
| 77 | |
| 78 | expect(created.name).toContain("/httpRoutes/"); |
| 79 | expect(created.httpRouteId).toEqual(expect.any(String)); |
| 80 | expect(created.location).toEqual("global"); |
| 81 | expect(created.hostnames).toContain("alchemy-http.example.com"); |
| 82 | expect(created.description).toEqual("http route a"); |
| 83 | expect(created.labels).toMatchObject({ env: "test" }); |
| 84 | expect(created.createTime).toEqual(expect.any(String)); |
| 85 | |
| 86 | const fetched = yield* networkservices.getProjectsLocationsHttpRoutes({ |
| 87 | name: created.name, |
| 88 | }); |
| 89 | expect(fetched.name).toEqual(created.name); |
| 90 | expect(fetched.description).toEqual("http route a"); |
| 91 | expect(fetched.labels?.env).toEqual("test"); |
| 92 | expect( |
| 93 | Object.keys(fetched.labels ?? {}).some((key) => |
| 94 | key.startsWith("alchemy-"), |
| 95 | ), |
| 96 | ).toEqual(true); |
| 97 | |
| 98 | const updated = yield* stack.deploy( |
| 99 | Effect.gen(function* () { |
| 100 | return yield* GCP.NetworkServices.HttpRoute("Web", { |
| 101 | httpRouteId: created.httpRouteId, |
| 102 | location: "global", |
| 103 | hostnames: ["alchemy-http.example.com"], |
| 104 | description: "http route b", |
| 105 | labels: { env: "prod", role: "http" }, |
| 106 | rules: [ |
| 107 | { |
| 108 | matches: [{ fullPathMatch: "/healthz" }], |
| 109 | action: { |
| 110 | directResponse: { status: 200, stringBody: "ok" }, |
| 111 | }, |
| 112 | }, |
| 113 | ], |
| 114 | }); |
| 115 | }), |
| 116 | ); |
| 117 | |
| 118 | expect(updated.name).toEqual(created.name); |
| 119 | expect(updated.description).toEqual("http route b"); |
| 120 | expect(updated.labels).toMatchObject({ env: "prod", role: "http" }); |
| 121 | expect(updated.rules[0]?.action?.directResponse?.status).toEqual(200); |
| 122 | |
| 123 | const refetched = yield* networkservices.getProjectsLocationsHttpRoutes({ |
| 124 | name: created.name, |
| 125 | }); |
| 126 | expect(refetched.description).toEqual("http route b"); |
| 127 | expect(refetched.labels?.env).toEqual("prod"); |
| 128 | expect(refetched.labels?.role).toEqual("http"); |
| 129 | |
| 130 | yield* stack.destroy(); |
| 131 | |
| 132 | const gone = yield* waitUntilGone(created.name); |
| 133 | expect(gone).toEqual("gone"); |
| 134 | }).pipe(logLevel), |
| 135 | { |
| 136 | tags: ["provider:gcp", "provider:gcp:networkservices", "live"], |
| 137 | timeout: 90_000, |
| 138 | }, |
| 139 | ); |