| 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 | |
| 9 | const { test } = Test.make({ providers: GCP.providers() }); |
| 10 | |
| 11 | // Organization-scoped: set GOOGLE_ORGANIZATION_ID when the credentials |
| 12 | // administer the organization (the testing service account does not). |
| 13 | const organizationId = process.env.GOOGLE_ORGANIZATION_ID?.trim().replace( |
| 14 | /^organizations\//, |
| 15 | "", |
| 16 | ); |
| 17 | const organization = `organizations/${organizationId}`; |
| 18 | |
| 19 | const logLevel = Effect.provideService( |
| 20 | MinimumLogLevel, |
| 21 | process.env.DEBUG ? "Debug" : "Info", |
| 22 | ); |
| 23 | |
| 24 | const waitUntilGone = (name: string) => |
| 25 | logging.getOrganizationsSinks({ sinkName: name }).pipe( |
| 26 | Effect.as("found" as const), |
| 27 | Effect.catchTag("NotFound", () => Effect.succeed("gone" as const)), |
| 28 | Effect.repeat({ |
| 29 | schedule: Schedule.spaced("1 second"), |
| 30 | until: (status) => status === "gone", |
| 31 | times: 10, |
| 32 | }), |
| 33 | ); |
| 34 | |
| 35 | test.provider.skipIf(!organizationId)( |
| 36 | "getOrganizationsSinks on a missing sink fails with NotFound", |
| 37 | (stack) => |
| 38 | Effect.gen(function* () { |
| 39 | yield* stack.destroy(); |
| 40 | |
| 41 | const error = yield* Effect.flip( |
| 42 | logging.getOrganizationsSinks({ |
| 43 | sinkName: `${organization}/sinks/alchemy-missing`, |
| 44 | }), |
| 45 | ); |
| 46 | expect(error._tag).toEqual("NotFound"); |
| 47 | |
| 48 | yield* stack.destroy(); |
| 49 | }).pipe(logLevel), |
| 50 | { tags: ["provider:gcp", "provider:gcp:logging", "live"], timeout: 90_000 }, |
| 51 | ); |
| 52 | |
| 53 | test.provider.skipIf(!organizationId)( |
| 54 | "create, update, replace, and delete an organization logging sink", |
| 55 | (stack) => |
| 56 | Effect.gen(function* () { |
| 57 | yield* stack.destroy(); |
| 58 | |
| 59 | const destination = `logging.googleapis.com/${organization}/locations/global/buckets/_Default`; |
| 60 | |
| 61 | const created = yield* stack.deploy( |
| 62 | Effect.gen(function* () { |
| 63 | return yield* GCP.Logging.OrganizationSink("Errors", { |
| 64 | organization, |
| 65 | destination, |
| 66 | filter: "severity>=ERROR", |
| 67 | description: "application errors", |
| 68 | }); |
| 69 | }), |
| 70 | ); |
| 71 | |
| 72 | expect(created.sinkId).toEqual(expect.any(String)); |
| 73 | expect(created.organization).toEqual(organization); |
| 74 | expect(created.name).toEqual(`${organization}/sinks/${created.sinkId}`); |
| 75 | expect(created.destination).toEqual(destination); |
| 76 | expect(created.filter).toEqual("severity>=ERROR"); |
| 77 | expect(created.description).toEqual("application errors"); |
| 78 | |
| 79 | const fetched = yield* logging.getOrganizationsSinks({ |
| 80 | sinkName: created.name, |
| 81 | }); |
| 82 | expect(fetched.destination).toEqual(destination); |
| 83 | expect(fetched.description).toContain("alchemy-id="); |
| 84 | |
| 85 | const updated = yield* stack.deploy( |
| 86 | Effect.gen(function* () { |
| 87 | return yield* GCP.Logging.OrganizationSink("Errors", { |
| 88 | organization, |
| 89 | sinkId: created.sinkId, |
| 90 | destination, |
| 91 | filter: "severity>=WARNING", |
| 92 | description: "warnings and errors", |
| 93 | disabled: true, |
| 94 | }); |
| 95 | }), |
| 96 | ); |
| 97 | |
| 98 | expect(updated.name).toEqual(created.name); |
| 99 | expect(updated.filter).toEqual("severity>=WARNING"); |
| 100 | expect(updated.disabled).toEqual(true); |
| 101 | |
| 102 | const last = created.sinkId.at(-1) ?? "a"; |
| 103 | const nextSinkId = `${created.sinkId.slice(0, -1)}${last === "z" ? "0" : "z"}`; |
| 104 | |
| 105 | const replaced = yield* stack.deploy( |
| 106 | Effect.gen(function* () { |
| 107 | return yield* GCP.Logging.OrganizationSink("Errors", { |
| 108 | organization, |
| 109 | sinkId: nextSinkId, |
| 110 | destination, |
| 111 | filter: "severity>=WARNING", |
| 112 | description: "replaced sink", |
| 113 | }); |
| 114 | }), |
| 115 | ); |
| 116 | |
| 117 | expect(replaced.sinkId).not.toEqual(created.sinkId); |
| 118 | |
| 119 | const previousGone = yield* waitUntilGone(created.name); |
| 120 | expect(previousGone).toEqual("gone"); |
| 121 | |
| 122 | yield* stack.destroy(); |
| 123 | |
| 124 | const gone = yield* waitUntilGone(replaced.name); |
| 125 | expect(gone).toEqual("gone"); |
| 126 | }).pipe(logLevel), |
| 127 | { tags: ["provider:gcp", "provider:gcp:logging", "live"], timeout: 90_000 }, |
| 128 | ); |