1import * as GCP from "@/GCP";
2import * as Test from "@/Test/Alchemy";
3import * as kafka from "@distilled.cloud/gcp/managedkafka_v1";
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";
9import { withKafkaClusterSlot } from "./quota.ts";
10
11const { test } = Test.make({ providers: GCP.providers() });
12
13const logLevel = Effect.provideService(
14 MinimumLogLevel,
15 process.env.DEBUG ? "Debug" : "Info",
16);
17
18// Kafka and Connect clusters take ~30 minutes to provision.
19const runLifecycle = !!process.env.GCP_TEST_SLOW && !process.env.FAST;
20
21const waitUntilGone = (name: string) =>
22 kafka.getProjectsLocationsClusters({ name }).pipe(
23 Effect.as("found" as const),
24 Effect.catchTag("NotFound", () => Effect.succeed("gone" as const)),
25 Effect.repeat({
26 schedule: Schedule.spaced("2 seconds"),
27 until: (status) => status === "gone",
28 times: 10,
29 }),
30 );
31
32test.provider(
33 "getProjectsLocationsClusters on a missing cluster fails with a typed tag",
34 (stack) =>
35 Effect.gen(function* () {
36 const { project } = yield* GcpEnvironment.current;
37
38 yield* stack.destroy();
39
40 const error = yield* Effect.flip(
41 kafka.getProjectsLocationsClusters({
42 name: `projects/${project}/locations/us-central1/clusters/alchemy-missing-kafka`,
43 }),
44 );
45 expect(error._tag).toEqual("NotFound");
46
47 yield* stack.destroy();
48 }).pipe(logLevel),
49 {
50 tags: ["provider:gcp", "provider:gcp:managedkafka", "live"],
51 timeout: 90_000,
52 },
53);
54
55test.provider.skipIf(!runLifecycle)(
56 "create, update, and delete a managed kafka cluster",
57 (stack) =>
58 Effect.gen(function* () {
59 yield* stack.destroy();
60
61 const created = yield* stack.deploy(
62 Effect.gen(function* () {
63 return yield* GCP.ManagedKafka.Cluster("Brokers", {
64 location: "us-central1",
65 capacityConfig: { vcpuCount: 3, memoryBytes: 3_221_225_472 },
66 labels: { env: "test" },
67 });
68 }),
69 );
70
71 expect(created.name).toContain("/clusters/");
72 expect(created.state).toEqual("ACTIVE");
73 expect(created.labels).toMatchObject({ env: "test" });
74
75 const fetched = yield* kafka.getProjectsLocationsClusters({
76 name: created.name,
77 });
78 expect(fetched.name).toEqual(created.name);
79 expect(fetched.labels?.env).toEqual("test");
80
81 const updated = yield* stack.deploy(
82 Effect.gen(function* () {
83 return yield* GCP.ManagedKafka.Cluster("Brokers", {
84 clusterId: created.clusterId,
85 location: "us-central1",
86 capacityConfig: { vcpuCount: 3, memoryBytes: 3_221_225_472 },
87 rebalanceConfig: { mode: "AUTO_REBALANCE_ON_SCALE_UP" },
88 labels: { env: "prod" },
89 });
90 }),
91 );
92 expect(updated.clusterId).toEqual(created.clusterId);
93 expect(updated.labels).toMatchObject({ env: "prod" });
94
95 yield* stack.destroy();
96
97 const gone = yield* waitUntilGone(created.name);
98 expect(gone).toEqual("gone");
99 }).pipe(logLevel, withKafkaClusterSlot),
100 {
101 tags: ["provider:gcp", "provider:gcp:managedkafka", "live"],
102 timeout: 10_800_000,
103 retry: 0,
104 },
105);