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.getProjectsLocationsConnectClusters({ 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 "getProjectsLocationsConnectClusters 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.getProjectsLocationsConnectClusters({
42 name: `projects/${project}/locations/us-central1/connectClusters/alchemy-missing-connect`,
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 kafka connect cluster",
57 (stack) =>
58 Effect.gen(function* () {
59 yield* stack.destroy();
60
61 const created = yield* stack.deploy(
62 Effect.gen(function* () {
63 const cluster = yield* GCP.ManagedKafka.Cluster("Brokers", {
64 location: "us-central1",
65 labels: { env: "test" },
66 });
67 const connect = yield* GCP.ManagedKafka.ConnectCluster("Connect", {
68 kafkaCluster: cluster.name,
69 labels: { env: "test" },
70 });
71 return { cluster, connect };
72 }),
73 );
74
75 expect(created.connect.name).toContain("/connectClusters/");
76 expect(created.connect.kafkaCluster).toEqual(created.cluster.name);
77
78 const fetched = yield* kafka.getProjectsLocationsConnectClusters({
79 name: created.connect.name,
80 });
81 expect(fetched.name).toEqual(created.connect.name);
82
83 const updated = yield* stack.deploy(
84 Effect.gen(function* () {
85 const cluster = yield* GCP.ManagedKafka.Cluster("Brokers", {
86 clusterId: created.cluster.clusterId,
87 location: "us-central1",
88 labels: { env: "test" },
89 });
90 const connect = yield* GCP.ManagedKafka.ConnectCluster("Connect", {
91 connectClusterId: created.connect.connectClusterId,
92 kafkaCluster: cluster.name,
93 labels: { env: "prod" },
94 });
95 return { cluster, connect };
96 }),
97 );
98 expect(updated.connect.connectClusterId).toEqual(
99 created.connect.connectClusterId,
100 );
101 expect(updated.connect.labels).toMatchObject({ env: "prod" });
102
103 yield* stack.destroy();
104
105 const gone = yield* waitUntilGone(created.connect.name);
106 expect(gone).toEqual("gone");
107 }).pipe(logLevel, withKafkaClusterSlot),
108 {
109 tags: ["provider:gcp", "provider:gcp:managedkafka", "live"],
110 timeout: 10_800_000,
111 retry: 0,
112 },
113);