| 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.getOrganizationsLocationsSavedQueries({ 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 | "getOrganizationsLocationsSavedQueries on a missing query fails with NotFound", |
| 37 | (stack) => |
| 38 | Effect.gen(function* () { |
| 39 | yield* stack.destroy(); |
| 40 | |
| 41 | const error = yield* Effect.flip( |
| 42 | logging.getOrganizationsLocationsSavedQueries({ |
| 43 | name: `${organization}/locations/global/savedQueries/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 saved query", |
| 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.OrganizationSavedQuery("Errors", { |
| 62 | organization, |
| 63 | displayName: "organization errors", |
| 64 | loggingQuery: { filter: "severity>=ERROR" }, |
| 65 | description: "error query", |
| 66 | }); |
| 67 | }), |
| 68 | ); |
| 69 | |
| 70 | expect(created.savedQueryId).toEqual(expect.any(String)); |
| 71 | expect(created.location).toEqual("global"); |
| 72 | expect(created.organization).toEqual(organization); |
| 73 | expect(created.name).toEqual( |
| 74 | `${organization}/locations/global/savedQueries/${created.savedQueryId}`, |
| 75 | ); |
| 76 | expect(created.displayName).toEqual("organization errors"); |
| 77 | expect(created.loggingQuery?.filter).toEqual("severity>=ERROR"); |
| 78 | expect(created.description).toEqual("error query"); |
| 79 | |
| 80 | const fetched = yield* logging.getOrganizationsLocationsSavedQueries({ |
| 81 | name: created.name, |
| 82 | }); |
| 83 | expect(fetched.displayName).toEqual("organization errors"); |
| 84 | expect(fetched.description).toContain("alchemy-id="); |
| 85 | |
| 86 | const updated = yield* stack.deploy( |
| 87 | Effect.gen(function* () { |
| 88 | return yield* GCP.Logging.OrganizationSavedQuery("Errors", { |
| 89 | organization, |
| 90 | savedQueryId: created.savedQueryId, |
| 91 | displayName: "organization warnings", |
| 92 | loggingQuery: { filter: "severity>=WARNING" }, |
| 93 | description: "warning query", |
| 94 | }); |
| 95 | }), |
| 96 | ); |
| 97 | |
| 98 | expect(updated.name).toEqual(created.name); |
| 99 | expect(updated.displayName).toEqual("organization warnings"); |
| 100 | expect(updated.loggingQuery?.filter).toEqual("severity>=WARNING"); |
| 101 | |
| 102 | const last = created.savedQueryId.at(-1) ?? "a"; |
| 103 | const nextId = `${created.savedQueryId.slice(0, -1)}${last === "z" ? "0" : "z"}`; |
| 104 | |
| 105 | const replaced = yield* stack.deploy( |
| 106 | Effect.gen(function* () { |
| 107 | return yield* GCP.Logging.OrganizationSavedQuery("Errors", { |
| 108 | organization, |
| 109 | savedQueryId: nextId, |
| 110 | displayName: "replaced query", |
| 111 | loggingQuery: { filter: "severity>=ERROR" }, |
| 112 | description: "replaced query", |
| 113 | }); |
| 114 | }), |
| 115 | ); |
| 116 | |
| 117 | expect(replaced.savedQueryId).not.toEqual(created.savedQueryId); |
| 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 | ); |