| 1 | import * as GCP from "@/GCP"; |
| 2 | import * as Test from "@/Test/Alchemy"; |
| 3 | import * as networksecurity from "@distilled.cloud/gcp/networksecurity_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 | networksecurity.getProjectsLocationsServerTlsPolicies({ 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 | "getProjectsLocationsServerTlsPolicies on a missing policy 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 | networksecurity.getProjectsLocationsServerTlsPolicies({ |
| 37 | name: `projects/${project}/locations/global/serverTlsPolicies/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:networksecurity", "live"], |
| 46 | timeout: 90_000, |
| 47 | }, |
| 48 | ); |
| 49 | |
| 50 | test.provider( |
| 51 | "create, update, and delete a server tls policy", |
| 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.NetworkSecurity.ServerTlsPolicy("FrontendTls", { |
| 59 | location: "global", |
| 60 | description: "server tls a", |
| 61 | labels: { env: "test" }, |
| 62 | allowOpen: true, |
| 63 | }); |
| 64 | }), |
| 65 | ); |
| 66 | |
| 67 | expect(created.name).toContain("/serverTlsPolicies/"); |
| 68 | expect(created.name).toContain("/locations/global/"); |
| 69 | expect(created.serverTlsPolicyId).toEqual(expect.any(String)); |
| 70 | expect(created.location).toEqual("global"); |
| 71 | expect(created.description).toEqual("server tls a"); |
| 72 | expect(created.allowOpen).toEqual(true); |
| 73 | expect(created.labels).toMatchObject({ env: "test" }); |
| 74 | expect(created.createTime).toEqual(expect.any(String)); |
| 75 | |
| 76 | const fetched = |
| 77 | yield* networksecurity.getProjectsLocationsServerTlsPolicies({ |
| 78 | name: created.name, |
| 79 | }); |
| 80 | expect(fetched.name).toEqual(created.name); |
| 81 | expect(fetched.description).toEqual("server tls a"); |
| 82 | expect(fetched.labels?.env).toEqual("test"); |
| 83 | expect( |
| 84 | Object.keys(fetched.labels ?? {}).some((key) => |
| 85 | key.startsWith("alchemy-"), |
| 86 | ), |
| 87 | ).toEqual(true); |
| 88 | |
| 89 | const updated = yield* stack.deploy( |
| 90 | Effect.gen(function* () { |
| 91 | return yield* GCP.NetworkSecurity.ServerTlsPolicy("FrontendTls", { |
| 92 | serverTlsPolicyId: created.serverTlsPolicyId, |
| 93 | location: "global", |
| 94 | description: "server tls b", |
| 95 | labels: { env: "prod", role: "tls" }, |
| 96 | allowOpen: true, |
| 97 | }); |
| 98 | }), |
| 99 | ); |
| 100 | |
| 101 | expect(updated.name).toEqual(created.name); |
| 102 | expect(updated.description).toEqual("server tls b"); |
| 103 | expect(updated.labels).toMatchObject({ env: "prod", role: "tls" }); |
| 104 | |
| 105 | const refetched = |
| 106 | yield* networksecurity.getProjectsLocationsServerTlsPolicies({ |
| 107 | name: created.name, |
| 108 | }); |
| 109 | expect(refetched.description).toEqual("server tls b"); |
| 110 | expect(refetched.labels?.env).toEqual("prod"); |
| 111 | expect(refetched.labels?.role).toEqual("tls"); |
| 112 | |
| 113 | yield* stack.destroy(); |
| 114 | |
| 115 | const gone = yield* waitUntilGone(created.name); |
| 116 | expect(gone).toEqual("gone"); |
| 117 | }).pipe(logLevel), |
| 118 | { |
| 119 | tags: ["provider:gcp", "provider:gcp:networksecurity", "live"], |
| 120 | timeout: 90_000, |
| 121 | }, |
| 122 | ); |