| 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.getOrganizationsExclusions({ 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 | "getOrganizationsExclusions on a missing exclusion fails with NotFound", |
| 37 | (stack) => |
| 38 | Effect.gen(function* () { |
| 39 | yield* stack.destroy(); |
| 40 | |
| 41 | const error = yield* Effect.flip( |
| 42 | logging.getOrganizationsExclusions({ |
| 43 | name: `${organization}/exclusions/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 exclusion", |
| 55 | (stack) => |
| 56 | Effect.gen(function* () { |
| 57 | yield* stack.destroy(); |
| 58 | |
| 59 | const created = yield* stack.deploy( |
| 60 | Effect.gen(function* () { |
| 61 | return yield* GCP.Logging.OrganizationExclusion("DropDebug", { |
| 62 | organization, |
| 63 | filter: "severity=DEBUG", |
| 64 | description: "drop debug entries", |
| 65 | }); |
| 66 | }), |
| 67 | ); |
| 68 | |
| 69 | expect(created.exclusionId).toEqual(expect.any(String)); |
| 70 | expect(created.organization).toEqual(organization); |
| 71 | expect(created.name).toEqual( |
| 72 | `${organization}/exclusions/${created.exclusionId}`, |
| 73 | ); |
| 74 | expect(created.filter).toEqual("severity=DEBUG"); |
| 75 | expect(created.description).toEqual("drop debug entries"); |
| 76 | |
| 77 | const fetched = yield* logging.getOrganizationsExclusions({ |
| 78 | name: created.name, |
| 79 | }); |
| 80 | expect(fetched.filter).toEqual("severity=DEBUG"); |
| 81 | expect(fetched.description).toContain("alchemy-id="); |
| 82 | expect(fetched.description).toContain("drop debug entries"); |
| 83 | |
| 84 | const updated = yield* stack.deploy( |
| 85 | Effect.gen(function* () { |
| 86 | return yield* GCP.Logging.OrganizationExclusion("DropDebug", { |
| 87 | organization, |
| 88 | exclusionId: created.exclusionId, |
| 89 | filter: "severity<ERROR", |
| 90 | description: "drop non-errors", |
| 91 | disabled: true, |
| 92 | }); |
| 93 | }), |
| 94 | ); |
| 95 | |
| 96 | expect(updated.filter).toEqual("severity<ERROR"); |
| 97 | expect(updated.disabled).toEqual(true); |
| 98 | |
| 99 | const last = created.exclusionId.at(-1) ?? "a"; |
| 100 | const nextExclusionId = `${created.exclusionId.slice(0, -1)}${last === "z" ? "0" : "z"}`; |
| 101 | |
| 102 | const replaced = yield* stack.deploy( |
| 103 | Effect.gen(function* () { |
| 104 | return yield* GCP.Logging.OrganizationExclusion("DropDebug", { |
| 105 | organization, |
| 106 | exclusionId: nextExclusionId, |
| 107 | filter: "severity=DEBUG", |
| 108 | description: "replaced exclusion", |
| 109 | }); |
| 110 | }), |
| 111 | ); |
| 112 | |
| 113 | expect(replaced.exclusionId).not.toEqual(created.exclusionId); |
| 114 | |
| 115 | const previousGone = yield* waitUntilGone(created.name); |
| 116 | expect(previousGone).toEqual("gone"); |
| 117 | |
| 118 | yield* stack.destroy(); |
| 119 | |
| 120 | const gone = yield* waitUntilGone(replaced.name); |
| 121 | expect(gone).toEqual("gone"); |
| 122 | }).pipe(logLevel), |
| 123 | { tags: ["provider:gcp", "provider:gcp:logging", "live"], timeout: 90_000 }, |
| 124 | ); |