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.getSinks({ 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 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.Sink("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.name).toEqual(
50 `projects/${project}/sinks/${created.sinkId}`,
51 );
52 expect(created.project).toEqual(project);
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 expect(created.exclusions).toEqual([]);
58
59 const fetched = yield* logging.getSinks({ sinkName: created.name });
60 expect(fetched.name).toEqual(created.sinkId);
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.Sink("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.sinkId).toEqual(created.sinkId);
87 expect(updated.filter).toEqual("severity>=WARNING");
88 expect(updated.description).toEqual("warnings and errors");
89 expect(updated.disabled).toEqual(true);
90 expect(updated.exclusions).toEqual([
91 expect.objectContaining({
92 name: "drop-healthchecks",
93 filter: 'httpRequest.requestUrl="/healthz"',
94 description: "skip probes",
95 disabled: false,
96 }),
97 ]);
98
99 const fetchedUpdate = yield* logging.getSinks({
100 sinkName: created.name,
101 });
102 expect(fetchedUpdate.filter).toEqual("severity>=WARNING");
103 expect(fetchedUpdate.disabled).toEqual(true);
104 expect(fetchedUpdate.exclusions?.[0]?.name).toEqual("drop-healthchecks");
105
106 const last = created.sinkId.at(-1) ?? "a";
107 const nextSinkId = `${created.sinkId.slice(0, -1)}${last === "z" ? "0" : "z"}`;
108
109 const replaced = yield* stack.deploy(
110 Effect.gen(function* () {
111 return yield* GCP.Logging.Sink("Errors", {
112 sinkId: nextSinkId,
113 destination: destinationOf(project),
114 filter: "severity>=WARNING",
115 description: "replaced sink",
116 });
117 }),
118 );
119
120 expect(replaced.sinkId).not.toEqual(created.sinkId);
121 expect(replaced.name).toEqual(
122 `projects/${project}/sinks/${replaced.sinkId}`,
123 );
124 expect(replaced.description).toEqual("replaced sink");
125
126 const fetchedReplacement = yield* logging.getSinks({
127 sinkName: replaced.name,
128 });
129 expect(fetchedReplacement.name).toEqual(replaced.sinkId);
130
131 const previousGone = yield* waitUntilGone(created.name);
132 expect(previousGone).toEqual("gone");
133
134 yield* stack.destroy();
135
136 const gone = yield* waitUntilGone(replaced.name);
137 expect(gone).toEqual("gone");
138 }).pipe(logLevel),
139 { tags: ["provider:gcp", "provider:gcp:logging", "live"], timeout: 90_000 },
140);