| 1 | import * as GCP from "@/GCP"; |
| 2 | import * as Test from "@/Test/Alchemy"; |
| 3 | import * as logging from "@distilled.cloud/gcp/logging_v2"; |
| 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 | const projectNameOf = (project: string) => `projects/${project}`; |
| 17 | |
| 18 | const waitUntilGone = (name: string) => |
| 19 | logging.getFoldersLocationsLogScopes({ name }).pipe( |
| 20 | Effect.as("found" as const), |
| 21 | Effect.catchTag("NotFound", () => Effect.succeed("gone" as const)), |
| 22 | Effect.repeat({ |
| 23 | schedule: Schedule.spaced("1 second"), |
| 24 | until: (status) => status === "gone", |
| 25 | times: 10, |
| 26 | }), |
| 27 | ); |
| 28 | |
| 29 | test.provider( |
| 30 | "create, update, replace, and delete a folder logging log scope", |
| 31 | (stack) => |
| 32 | Effect.gen(function* () { |
| 33 | const { project } = yield* GcpEnvironment.current; |
| 34 | |
| 35 | yield* stack.destroy(); |
| 36 | |
| 37 | const created = yield* stack.deploy( |
| 38 | Effect.gen(function* () { |
| 39 | return yield* GCP.Logging.FolderLogScope("App", { |
| 40 | resourceNames: [projectNameOf(project)], |
| 41 | description: "application logs", |
| 42 | }); |
| 43 | }), |
| 44 | ); |
| 45 | |
| 46 | expect(created.logScopeId).toEqual(expect.any(String)); |
| 47 | expect(created.location).toEqual("global"); |
| 48 | expect(created.parent).toEqual(`projects/${project}`); |
| 49 | expect(created.name).toEqual( |
| 50 | `projects/${project}/locations/global/logScopes/${created.logScopeId}`, |
| 51 | ); |
| 52 | expect(created.resourceNames).toEqual([projectNameOf(project)]); |
| 53 | expect(created.description).toEqual("application logs"); |
| 54 | |
| 55 | const fetched = yield* logging.getFoldersLocationsLogScopes({ |
| 56 | name: created.name, |
| 57 | }); |
| 58 | expect(fetched.name).toEqual(created.name); |
| 59 | expect(fetched.resourceNames).toEqual([projectNameOf(project)]); |
| 60 | expect(fetched.description).toContain("alchemy-id="); |
| 61 | expect(fetched.description).toContain("application logs"); |
| 62 | |
| 63 | const updated = yield* stack.deploy( |
| 64 | Effect.gen(function* () { |
| 65 | return yield* GCP.Logging.FolderLogScope("App", { |
| 66 | logScopeId: created.logScopeId, |
| 67 | resourceNames: [projectNameOf(project)], |
| 68 | description: "all application logs", |
| 69 | }); |
| 70 | }), |
| 71 | ); |
| 72 | |
| 73 | expect(updated.name).toEqual(created.name); |
| 74 | expect(updated.logScopeId).toEqual(created.logScopeId); |
| 75 | expect(updated.description).toEqual("all application logs"); |
| 76 | |
| 77 | const last = created.logScopeId.at(-1) ?? "a"; |
| 78 | const nextId = `${created.logScopeId.slice(0, -1)}${last === "z" ? "0" : "z"}`; |
| 79 | |
| 80 | const replaced = yield* stack.deploy( |
| 81 | Effect.gen(function* () { |
| 82 | return yield* GCP.Logging.FolderLogScope("App", { |
| 83 | logScopeId: nextId, |
| 84 | resourceNames: [projectNameOf(project)], |
| 85 | description: "replaced scope", |
| 86 | }); |
| 87 | }), |
| 88 | ); |
| 89 | |
| 90 | expect(replaced.logScopeId).not.toEqual(created.logScopeId); |
| 91 | expect(replaced.description).toEqual("replaced scope"); |
| 92 | |
| 93 | const previousGone = yield* waitUntilGone(created.name); |
| 94 | expect(previousGone).toEqual("gone"); |
| 95 | |
| 96 | yield* stack.destroy(); |
| 97 | |
| 98 | const gone = yield* waitUntilGone(replaced.name); |
| 99 | expect(gone).toEqual("gone"); |
| 100 | }).pipe(logLevel), |
| 101 | { tags: ["provider:gcp", "provider:gcp:logging", "live"], timeout: 90_000 }, |
| 102 | ); |