1import * as GCP from "@/GCP";
2import * as Test from "@/Test/Alchemy";
3import * as logging from "@distilled.cloud/gcp/logging_v2";
4import { expect } from "alchemy-test";
5import * as Effect from "effect/Effect";
6import { MinimumLogLevel } from "effect/References";
7import * as Schedule from "effect/Schedule";
8import { GcpEnvironment } from "@/GCP/Environment";
9
10const { test } = Test.make({ providers: GCP.providers() });
11
12const logLevel = Effect.provideService(
13 MinimumLogLevel,
14 process.env.DEBUG ? "Debug" : "Info",
15);
16const destinationOf = (project: string) =>
17 `logging.googleapis.com/projects/${project}/locations/global/buckets/_Default`;
18
19const waitUntilGone = (name: string) =>
20 logging.getFoldersSinks({ sinkName: name }).pipe(
21 Effect.as("found" as const),
22 Effect.catchTag("NotFound", () => Effect.succeed("gone" as const)),
23 Effect.repeat({
24 schedule: Schedule.spaced("1 second"),
25 until: (status) => status === "gone",
26 times: 10,
27 }),
28 );
29
30test.provider(
31 "create, update, replace, and delete a folder logging sink",
32 (stack) =>
33 Effect.gen(function* () {
34 const { project } = yield* GcpEnvironment.current;
35
36 yield* stack.destroy();
37
38 const created = yield* stack.deploy(
39 Effect.gen(function* () {
40 return yield* GCP.Logging.FolderSink("Errors", {
41 destination: destinationOf(project),
42 filter: "severity>=ERROR",
43 description: "application errors",
44 });
45 }),
46 );
47
48 expect(created.sinkId).toEqual(expect.any(String));
49 expect(created.parent).toEqual(`projects/${project}`);
50 expect(created.name).toEqual(
51 `projects/${project}/sinks/${created.sinkId}`,
52 );
53 expect(created.destination).toEqual(destinationOf(project));
54 expect(created.filter).toEqual("severity>=ERROR");
55 expect(created.description).toEqual("application errors");
56 expect(created.disabled).toEqual(false);
57
58 const fetched = yield* logging.getFoldersSinks({
59 sinkName: created.name,
60 });
61 expect(fetched.destination).toEqual(destinationOf(project));
62 expect(fetched.filter).toEqual("severity>=ERROR");
63 expect(fetched.description).toContain("alchemy-id=");
64 expect(fetched.description).toContain("application errors");
65
66 const updated = yield* stack.deploy(
67 Effect.gen(function* () {
68 return yield* GCP.Logging.FolderSink("Errors", {
69 sinkId: created.sinkId,
70 destination: destinationOf(project),
71 filter: "severity>=WARNING",
72 description: "warnings and errors",
73 disabled: true,
74 exclusions: [
75 {
76 name: "drop-healthchecks",
77 filter: 'httpRequest.requestUrl="/healthz"',
78 description: "skip probes",
79 },
80 ],
81 });
82 }),
83 );
84
85 expect(updated.name).toEqual(created.name);
86 expect(updated.filter).toEqual("severity>=WARNING");
87 expect(updated.description).toEqual("warnings and errors");
88 expect(updated.disabled).toEqual(true);
89 expect(updated.exclusions).toEqual([
90 expect.objectContaining({
91 name: "drop-healthchecks",
92 filter: 'httpRequest.requestUrl="/healthz"',
93 }),
94 ]);
95
96 const last = created.sinkId.at(-1) ?? "a";
97 const nextSinkId = `${created.sinkId.slice(0, -1)}${last === "z" ? "0" : "z"}`;
98
99 const replaced = yield* stack.deploy(
100 Effect.gen(function* () {
101 return yield* GCP.Logging.FolderSink("Errors", {
102 sinkId: nextSinkId,
103 destination: destinationOf(project),
104 filter: "severity>=WARNING",
105 description: "replaced sink",
106 });
107 }),
108 );
109
110 expect(replaced.sinkId).not.toEqual(created.sinkId);
111 expect(replaced.description).toEqual("replaced sink");
112
113 const previousGone = yield* waitUntilGone(created.name);
114 expect(previousGone).toEqual("gone");
115
116 yield* stack.destroy();
117
118 const gone = yield* waitUntilGone(replaced.name);
119 expect(gone).toEqual("gone");
120 }).pipe(logLevel),
121 { tags: ["provider:gcp", "provider:gcp:logging", "live"], timeout: 90_000 },
122);