| 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 | |
| 17 | const waitUntilGone = (name: string) => |
| 18 | logging.getProjectsLocationsBucketsViews({ name }).pipe( |
| 19 | Effect.as("found" as const), |
| 20 | Effect.catchTag("NotFound", () => Effect.succeed("gone" as const)), |
| 21 | Effect.repeat({ |
| 22 | schedule: Schedule.spaced("1 second"), |
| 23 | until: (status) => status === "gone", |
| 24 | times: 10, |
| 25 | }), |
| 26 | ); |
| 27 | |
| 28 | test.provider( |
| 29 | "create, update, replace, and delete a logging bucket view", |
| 30 | (stack) => |
| 31 | Effect.gen(function* () { |
| 32 | const { project } = yield* GcpEnvironment.current; |
| 33 | |
| 34 | yield* stack.destroy(); |
| 35 | |
| 36 | const created = yield* stack.deploy( |
| 37 | Effect.gen(function* () { |
| 38 | const bucket = yield* GCP.Logging.LogBucket("AppLogs", { |
| 39 | description: "view parent", |
| 40 | }); |
| 41 | const view = yield* GCP.Logging.BucketsView("Stdout", { |
| 42 | bucket: bucket.name, |
| 43 | filter: 'LOG_ID("stdout")', |
| 44 | description: "stdout only", |
| 45 | }); |
| 46 | return { bucket, view }; |
| 47 | }), |
| 48 | ); |
| 49 | |
| 50 | expect(created.view.viewId).toEqual(expect.any(String)); |
| 51 | expect(created.view.bucket).toEqual(created.bucket.name); |
| 52 | expect(created.view.project).toEqual(project); |
| 53 | expect(created.view.location).toEqual("global"); |
| 54 | expect(created.view.name).toEqual( |
| 55 | `${created.bucket.name}/views/${created.view.viewId}`, |
| 56 | ); |
| 57 | expect(created.view.filter).toEqual('LOG_ID("stdout")'); |
| 58 | expect(created.view.description).toEqual("stdout only"); |
| 59 | |
| 60 | const fetched = yield* logging.getProjectsLocationsBucketsViews({ |
| 61 | name: created.view.name, |
| 62 | }); |
| 63 | expect(fetched.name).toEqual(created.view.name); |
| 64 | expect(fetched.filter).toEqual('LOG_ID("stdout")'); |
| 65 | expect(fetched.description).toContain("alchemy-id="); |
| 66 | expect(fetched.description).toContain("stdout only"); |
| 67 | |
| 68 | const updated = yield* stack.deploy( |
| 69 | Effect.gen(function* () { |
| 70 | const bucket = yield* GCP.Logging.LogBucket("AppLogs", { |
| 71 | bucketId: created.bucket.bucketId, |
| 72 | location: created.bucket.location, |
| 73 | description: "view parent", |
| 74 | }); |
| 75 | const view = yield* GCP.Logging.BucketsView("Stdout", { |
| 76 | bucket: bucket.name, |
| 77 | viewId: created.view.viewId, |
| 78 | filter: 'LOG_ID("stderr")', |
| 79 | description: "stderr only", |
| 80 | }); |
| 81 | return { bucket, view }; |
| 82 | }), |
| 83 | ); |
| 84 | |
| 85 | expect(updated.view.name).toEqual(created.view.name); |
| 86 | expect(updated.view.viewId).toEqual(created.view.viewId); |
| 87 | expect(updated.view.filter).toEqual('LOG_ID("stderr")'); |
| 88 | expect(updated.view.description).toEqual("stderr only"); |
| 89 | |
| 90 | const fetchedUpdate = yield* logging.getProjectsLocationsBucketsViews({ |
| 91 | name: created.view.name, |
| 92 | }); |
| 93 | expect(fetchedUpdate.filter).toEqual('LOG_ID("stderr")'); |
| 94 | expect(fetchedUpdate.description).toContain("stderr only"); |
| 95 | |
| 96 | const last = created.view.viewId.at(-1) ?? "a"; |
| 97 | const nextViewId = `${created.view.viewId.slice(0, -1)}${last === "z" ? "0" : "z"}`; |
| 98 | |
| 99 | const replaced = yield* stack.deploy( |
| 100 | Effect.gen(function* () { |
| 101 | const bucket = yield* GCP.Logging.LogBucket("AppLogs", { |
| 102 | bucketId: created.bucket.bucketId, |
| 103 | location: created.bucket.location, |
| 104 | description: "view parent", |
| 105 | }); |
| 106 | const view = yield* GCP.Logging.BucketsView("Stdout", { |
| 107 | bucket: bucket.name, |
| 108 | viewId: nextViewId, |
| 109 | filter: 'LOG_ID("stderr")', |
| 110 | description: "replaced view", |
| 111 | }); |
| 112 | return { bucket, view }; |
| 113 | }), |
| 114 | ); |
| 115 | |
| 116 | expect(replaced.view.viewId).not.toEqual(created.view.viewId); |
| 117 | expect(replaced.view.name).toEqual( |
| 118 | `${created.bucket.name}/views/${replaced.view.viewId}`, |
| 119 | ); |
| 120 | expect(replaced.view.description).toEqual("replaced view"); |
| 121 | |
| 122 | const fetchedReplacement = |
| 123 | yield* logging.getProjectsLocationsBucketsViews({ |
| 124 | name: replaced.view.name, |
| 125 | }); |
| 126 | expect(fetchedReplacement.name).toEqual(replaced.view.name); |
| 127 | |
| 128 | const previousGone = yield* waitUntilGone(created.view.name); |
| 129 | expect(previousGone).toEqual("gone"); |
| 130 | |
| 131 | yield* stack.destroy(); |
| 132 | |
| 133 | const gone = yield* waitUntilGone(replaced.view.name); |
| 134 | expect(gone).toEqual("gone"); |
| 135 | }).pipe(logLevel), |
| 136 | { tags: ["provider:gcp", "provider:gcp:logging", "live"], timeout: 90_000 }, |
| 137 | ); |