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 .getProjectsLocationsMulticastConsumerAssociations({ 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 "getProjectsLocationsMulticastConsumerAssociations on a missing association 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.getProjectsLocationsMulticastConsumerAssociations({
46 name: `projects/${project}/locations/us-central1-a/multicastConsumerAssociations/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 consumer association",
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 consumer 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 a",
78 labels: { env: "test" },
79 },
80 );
81 return { vpc, association };
82 }),
83 );
84
85 expect(created.association.name).toContain(
86 "/multicastConsumerAssociations/",
87 );
88 expect(created.association.multicastConsumerAssociationId).toEqual(
89 expect.any(String),
90 );
91 expect(created.association.location).toEqual("us-central1-a");
92 expect(created.association.description).toEqual("mcast assoc a");
93 expect(created.association.labels).toMatchObject({ env: "test" });
94 expect(created.association.createTime).toEqual(expect.any(String));
95
96 const fetched =
97 yield* networkservices.getProjectsLocationsMulticastConsumerAssociations(
98 {
99 name: created.association.name,
100 },
101 );
102 expect(fetched.name).toEqual(created.association.name);
103 expect(fetched.description).toEqual("mcast assoc a");
104 expect(fetched.labels?.env).toEqual("test");
105 expect(
106 Object.keys(fetched.labels ?? {}).some((key) =>
107 key.startsWith("alchemy-"),
108 ),
109 ).toEqual(true);
110
111 const updated = yield* stack.deploy(
112 Effect.gen(function* () {
113 const vpc = yield* GCP.Compute.Network("Vpc", {
114 networkName: created.vpc.networkName,
115 autoCreateSubnetworks: false,
116 description: "multicast consumer vpc",
117 });
118 const association =
119 yield* GCP.NetworkServices.MulticastConsumerAssociation(
120 "Consumers",
121 {
122 multicastConsumerAssociationId:
123 created.association.multicastConsumerAssociationId,
124 location: "us-central1-a",
125 network: vpc.selfLink.as<string>(),
126 description: "mcast assoc b",
127 labels: { env: "prod", role: "mcast" },
128 },
129 );
130 return { vpc, association };
131 }),
132 );
133
134 expect(updated.association.name).toEqual(created.association.name);
135 expect(updated.association.description).toEqual("mcast assoc b");
136 expect(updated.association.labels).toMatchObject({
137 env: "prod",
138 role: "mcast",
139 });
140
141 yield* stack.destroy();
142
143 const gone = yield* waitUntilGone(created.association.name);
144 expect(gone).toEqual("gone");
145 }).pipe(logLevel, withNetworkSlot),
146 {
147 tags: ["provider:gcp", "provider:gcp:networkservices", "live"],
148 timeout: 900_000,
149 },
150);