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