| 1 | import * as GCP from "@/GCP"; |
| 2 | import * as Test from "@/Test/Alchemy"; |
| 3 | import * as resourcemanager from "@distilled.cloud/gcp/cloudresourcemanager_v3"; |
| 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 | resourcemanager.getLiens({ 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 | "getLiens on a missing lien fails with NotFound", |
| 30 | (stack) => |
| 31 | Effect.gen(function* () { |
| 32 | const { project } = yield* GcpEnvironment.current; |
| 33 | yield* stack.destroy(); |
| 34 | |
| 35 | // Lien names are `liens/p{projectNumber}-l{uuid}`. |
| 36 | const resource = yield* resourcemanager.getProjects({ |
| 37 | name: `projects/${project}`, |
| 38 | }); |
| 39 | const projectNumber = (resource.name ?? "").split("/").pop(); |
| 40 | const error = yield* Effect.flip( |
| 41 | resourcemanager.getLiens({ |
| 42 | name: `liens/p${projectNumber}-l00000000-0000-0000-0000-000000000000`, |
| 43 | }), |
| 44 | ); |
| 45 | expect(error._tag).toEqual("NotFound"); |
| 46 | |
| 47 | yield* stack.destroy(); |
| 48 | }).pipe(logLevel), |
| 49 | { |
| 50 | tags: ["provider:gcp", "provider:gcp:resourcemanager", "live"], |
| 51 | timeout: 90_000, |
| 52 | }, |
| 53 | ); |
| 54 | |
| 55 | test.provider( |
| 56 | "create, replace, and delete a lien", |
| 57 | (stack) => |
| 58 | Effect.gen(function* () { |
| 59 | yield* stack.destroy(); |
| 60 | |
| 61 | const created = yield* stack.deploy( |
| 62 | Effect.gen(function* () { |
| 63 | return yield* GCP.ResourceManager.Lien("Hold", { |
| 64 | reason: "production API key", |
| 65 | }); |
| 66 | }), |
| 67 | ); |
| 68 | |
| 69 | expect(created.name).toMatch(/^liens\//); |
| 70 | expect(created.parent).toContain("projects/"); |
| 71 | expect(created.origin).toEqual("alchemy.effect"); |
| 72 | expect(created.reason).toEqual("production API key"); |
| 73 | expect(created.restrictions).toContain("resourcemanager.projects.delete"); |
| 74 | |
| 75 | const fetched = yield* resourcemanager.getLiens({ |
| 76 | name: created.name, |
| 77 | }); |
| 78 | expect(fetched.name).toEqual(created.name); |
| 79 | expect(fetched.reason).toEqual("production API key"); |
| 80 | |
| 81 | const replaced = yield* stack.deploy( |
| 82 | Effect.gen(function* () { |
| 83 | return yield* GCP.ResourceManager.Lien("Hold", { |
| 84 | parent: created.parent, |
| 85 | origin: created.origin, |
| 86 | reason: "holds billing export", |
| 87 | restrictions: created.restrictions, |
| 88 | }); |
| 89 | }), |
| 90 | ); |
| 91 | |
| 92 | expect(replaced.name).not.toEqual(created.name); |
| 93 | expect(replaced.reason).toEqual("holds billing export"); |
| 94 | expect(replaced.parent).toEqual(created.parent); |
| 95 | |
| 96 | const fetchedReplace = yield* resourcemanager.getLiens({ |
| 97 | name: replaced.name, |
| 98 | }); |
| 99 | expect(fetchedReplace.reason).toEqual("holds billing export"); |
| 100 | |
| 101 | const oldGone = yield* waitUntilGone(created.name); |
| 102 | expect(oldGone).toEqual("gone"); |
| 103 | |
| 104 | yield* stack.destroy(); |
| 105 | |
| 106 | const gone = yield* waitUntilGone(replaced.name); |
| 107 | expect(gone).toEqual("gone"); |
| 108 | }).pipe(logLevel), |
| 109 | { |
| 110 | tags: ["provider:gcp", "provider:gcp:resourcemanager", "live"], |
| 111 | timeout: 90_000, |
| 112 | }, |
| 113 | ); |