1import * as GCP from "@/GCP";
2import * as Test from "@/Test/Alchemy";
3import * as networkservices from "@distilled.cloud/gcp/networkservices_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 { withNetworkSlot } from "../networkQuota.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// Multicast needs a multicast domain activation in the project; without one
19// create fails with BadRequest "The request was invalid: missing
20// multicast_domain_activation". Set GCP_TEST_MULTICAST=1 on a project with
21// multicast activated.
22const runLifecycle = !process.env.FAST && !!process.env.GCP_TEST_MULTICAST;
23
24const waitUntilGone = (name: string) =>
25 networkservices
26 .getProjectsLocationsMulticastGroupConsumerActivations({ name })
27 .pipe(
28 Effect.as("found" as const),
29 Effect.catchTag("NotFound", () => Effect.succeed("gone" as const)),
30 Effect.repeat({
31 schedule: Schedule.spaced("2 seconds"),
32 until: (status) => status === "gone",
33 times: 10,
34 }),
35 );
36
37test.provider(
38 "getProjectsLocationsMulticastGroupConsumerActivations on a missing activation fails with a typed tag",
39 (stack) =>
40 Effect.gen(function* () {
41 yield* stack.destroy();
42
43 const { project } = yield* GcpEnvironment.current;
44 const error = yield* Effect.flip(
45 networkservices.getProjectsLocationsMulticastGroupConsumerActivations({
46 name: `projects/${project}/locations/us-central1-a/multicastGroupConsumerActivations/alchemy-missing`,
47 }),
48 );
49 expect(error._tag).toEqual("NotFound");
50
51 yield* stack.destroy();
52 }).pipe(logLevel),
53 {
54 tags: ["provider:gcp", "provider:gcp:networkservices", "live"],
55 timeout: 90_000,
56 },
57);
58
59test.provider.skipIf(!runLifecycle)(
60 "create, update, and delete a multicast group consumer activation",
61 (stack) =>
62 Effect.gen(function* () {
63 yield* stack.destroy();
64
65 const created = yield* stack.deploy(
66 Effect.gen(function* () {
67 const vpc = yield* GCP.Compute.Network("Vpc", {
68 autoCreateSubnetworks: false,
69 description: "multicast group vpc",
70 });
71 const association =
72 yield* GCP.NetworkServices.MulticastConsumerAssociation(
73 "Consumers",
74 {
75 location: "us-central1-a",
76 network: vpc.selfLink.as<string>(),
77 description: "mcast assoc",
78 labels: { env: "test" },
79 },
80 );
81 const activation =
82 yield* GCP.NetworkServices.MulticastGroupConsumerActivation(
83 "Join",
84 {
85 location: "us-central1-a",
86 multicastConsumerAssociation: association.name,
87 multicastGroupRangeActivation:
88 process.env.GCP_TEST_MULTICAST_RANGE_ACTIVATION ?? "",
89 description: "mcast gca a",
90 labels: { env: "test" },
91 logConfig: { enabled: true },
92 },
93 );
94 return { vpc, association, activation };
95 }),
96 );
97
98 expect(created.activation.name).toContain(
99 "/multicastGroupConsumerActivations/",
100 );
101 expect(created.activation.multicastGroupConsumerActivationId).toEqual(
102 expect.any(String),
103 );
104 expect(created.activation.location).toEqual("us-central1-a");
105 expect(created.activation.description).toEqual("mcast gca a");
106 expect(created.activation.labels).toMatchObject({ env: "test" });
107 expect(created.activation.logConfig?.enabled).toEqual(true);
108
109 const fetched =
110 yield* networkservices.getProjectsLocationsMulticastGroupConsumerActivations(
111 {
112 name: created.activation.name,
113 },
114 );
115 expect(fetched.name).toEqual(created.activation.name);
116 expect(fetched.description).toEqual("mcast gca a");
117 expect(fetched.labels?.env).toEqual("test");
118 expect(
119 Object.keys(fetched.labels ?? {}).some((key) =>
120 key.startsWith("alchemy-"),
121 ),
122 ).toEqual(true);
123
124 const updated = yield* stack.deploy(
125 Effect.gen(function* () {
126 const vpc = yield* GCP.Compute.Network("Vpc", {
127 networkName: created.vpc.networkName,
128 autoCreateSubnetworks: false,
129 description: "multicast group vpc",
130 });
131 const association =
132 yield* GCP.NetworkServices.MulticastConsumerAssociation(
133 "Consumers",
134 {
135 multicastConsumerAssociationId:
136 created.association.multicastConsumerAssociationId,
137 location: "us-central1-a",
138 network: vpc.selfLink.as<string>(),
139 description: "mcast assoc",
140 labels: { env: "test" },
141 },
142 );
143 const activation =
144 yield* GCP.NetworkServices.MulticastGroupConsumerActivation(
145 "Join",
146 {
147 multicastGroupConsumerActivationId:
148 created.activation.multicastGroupConsumerActivationId,
149 location: "us-central1-a",
150 multicastConsumerAssociation: association.name,
151 multicastGroupRangeActivation:
152 created.activation.multicastGroupRangeActivation ??
153 process.env.GCP_TEST_MULTICAST_RANGE_ACTIVATION ??
154 "",
155 description: "mcast gca b",
156 labels: { env: "prod", role: "mcast" },
157 logConfig: { enabled: false },
158 },
159 );
160 return { vpc, association, activation };
161 }),
162 );
163
164 expect(updated.activation.name).toEqual(created.activation.name);
165 expect(updated.activation.description).toEqual("mcast gca b");
166 expect(updated.activation.labels).toMatchObject({
167 env: "prod",
168 role: "mcast",
169 });
170
171 yield* stack.destroy();
172
173 const gone = yield* waitUntilGone(created.activation.name);
174 expect(gone).toEqual("gone");
175 }).pipe(logLevel, withNetworkSlot),
176 {
177 tags: ["provider:gcp", "provider:gcp:networkservices", "live"],
178 timeout: 90_000,
179 },
180);