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.getLocationsBuckets({ 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 locations 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.LocationsBucket("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.parent).toEqual(`projects/${project}`);
52 expect(created.name).toEqual(
53 `projects/${project}/locations/global/buckets/${created.bucketId}`,
54 );
55 expect(created.description).toEqual("application logs");
56 expect(created.retentionDays).toEqual(31);
57 expect(created.lifecycleState).toEqual("ACTIVE");
58
59 const fetched = yield* logging.getLocationsBuckets({
60 name: created.name,
61 });
62 expect(fetched.name).toEqual(created.name);
63 expect(fetched.retentionDays).toEqual(31);
64 expect(fetched.description).toContain("alchemy-id=");
65 expect(fetched.description).toContain("application logs");
66
67 const updated = yield* stack.deploy(
68 Effect.gen(function* () {
69 return yield* GCP.Logging.LocationsBucket("AppLogs", {
70 bucketId: created.bucketId,
71 location: created.location,
72 description: "retained application logs",
73 retentionDays: 60,
74 });
75 }),
76 );
77
78 expect(updated.name).toEqual(created.name);
79 expect(updated.description).toEqual("retained application logs");
80 expect(updated.retentionDays).toEqual(60);
81
82 const last = created.bucketId.at(-1) ?? "a";
83 const nextBucketId = `${created.bucketId.slice(0, -1)}${last === "z" ? "0" : "z"}`;
84
85 const replaced = yield* stack.deploy(
86 Effect.gen(function* () {
87 return yield* GCP.Logging.LocationsBucket("AppLogs", {
88 bucketId: nextBucketId,
89 location: "global",
90 description: "replaced bucket",
91 retentionDays: 31,
92 });
93 }),
94 );
95
96 expect(replaced.bucketId).not.toEqual(created.bucketId);
97 expect(replaced.description).toEqual("replaced bucket");
98
99 const previousGone = yield* waitUntilGone(created.name);
100 expect(previousGone).toEqual("gone");
101
102 yield* stack.destroy();
103
104 const gone = yield* waitUntilGone(replaced.name);
105 expect(gone).toEqual("gone");
106 }).pipe(logLevel),
107 { tags: ["provider:gcp", "provider:gcp:logging", "live"], timeout: 90_000 },
108);