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