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