| 1 | import * as GCP from "@/GCP"; |
| 2 | import * as Test from "@/Test/Alchemy"; |
| 3 | import * as servicedirectory from "@distilled.cloud/gcp/servicedirectory_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 | servicedirectory.getProjectsLocationsNamespaces({ 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( |
| 28 | "create, update, replace, and delete a namespace", |
| 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.ServiceDirectory.Namespace("Services", { |
| 36 | location: "us-central1", |
| 37 | labels: { env: "test" }, |
| 38 | }); |
| 39 | }), |
| 40 | ); |
| 41 | |
| 42 | expect(created.name).toContain("/namespaces/"); |
| 43 | expect(created.namespaceId).toEqual(expect.any(String)); |
| 44 | expect(created.location).toEqual("us-central1"); |
| 45 | expect(created.labels).toMatchObject({ env: "test" }); |
| 46 | expect(created.uid).toEqual(expect.any(String)); |
| 47 | |
| 48 | const fetched = yield* servicedirectory.getProjectsLocationsNamespaces({ |
| 49 | name: created.name, |
| 50 | }); |
| 51 | expect(fetched.name).toEqual(created.name); |
| 52 | expect(fetched.labels?.env).toEqual("test"); |
| 53 | expect(fetched.uid).toEqual(created.uid); |
| 54 | |
| 55 | const updated = yield* stack.deploy( |
| 56 | Effect.gen(function* () { |
| 57 | return yield* GCP.ServiceDirectory.Namespace("Services", { |
| 58 | namespaceId: created.namespaceId, |
| 59 | location: "us-central1", |
| 60 | labels: { env: "prod", role: "services" }, |
| 61 | }); |
| 62 | }), |
| 63 | ); |
| 64 | |
| 65 | expect(updated.name).toEqual(created.name); |
| 66 | expect(updated.uid).toEqual(created.uid); |
| 67 | expect(updated.labels).toMatchObject({ env: "prod", role: "services" }); |
| 68 | |
| 69 | const refetched = yield* servicedirectory.getProjectsLocationsNamespaces({ |
| 70 | name: created.name, |
| 71 | }); |
| 72 | expect(refetched.labels?.env).toEqual("prod"); |
| 73 | expect(refetched.labels?.role).toEqual("services"); |
| 74 | |
| 75 | const replaced = yield* stack.deploy( |
| 76 | Effect.gen(function* () { |
| 77 | return yield* GCP.ServiceDirectory.Namespace("Services", { |
| 78 | namespaceId: created.namespaceId, |
| 79 | location: "us-east1", |
| 80 | labels: { env: "prod" }, |
| 81 | }); |
| 82 | }), |
| 83 | ); |
| 84 | |
| 85 | expect(replaced.location).toEqual("us-east1"); |
| 86 | expect(replaced.namespaceId).toEqual(created.namespaceId); |
| 87 | expect(replaced.name).not.toEqual(created.name); |
| 88 | expect(replaced.name).toContain("/locations/us-east1/"); |
| 89 | |
| 90 | const fetchedReplacement = |
| 91 | yield* servicedirectory.getProjectsLocationsNamespaces({ |
| 92 | name: replaced.name, |
| 93 | }); |
| 94 | expect(fetchedReplacement.name).toEqual(replaced.name); |
| 95 | expect(fetchedReplacement.labels?.env).toEqual("prod"); |
| 96 | |
| 97 | const previousGone = yield* waitUntilGone(created.name); |
| 98 | expect(previousGone).toEqual("gone"); |
| 99 | |
| 100 | yield* stack.destroy(); |
| 101 | |
| 102 | const gone = yield* waitUntilGone(replaced.name); |
| 103 | expect(gone).toEqual("gone"); |
| 104 | }).pipe(logLevel), |
| 105 | { |
| 106 | tags: ["provider:gcp", "provider:gcp:servicedirectory", "live"], |
| 107 | timeout: 90_000, |
| 108 | }, |
| 109 | ); |