1import { GcpEnvironment } from "@/GCP/Environment";
2import * as GCP from "@/GCP";
3import * as Test from "@/Test/Alchemy";
4import * as networkmanagement from "@distilled.cloud/gcp/networkmanagement_v1";
5import { expect } from "alchemy-test";
6import * as Effect from "effect/Effect";
7import { MinimumLogLevel } from "effect/References";
8import * as Schedule from "effect/Schedule";
9import { defaultNetworkSelfLink } 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
18const runLifecycle = !process.env.FAST;
19
20const waitUntilGone = (name: string) =>
21 networkmanagement.getProjectsLocationsGlobalConnectivityTests({ name }).pipe(
22 Effect.as("found" as const),
23 Effect.catchTag("NotFound", () => Effect.succeed("gone" as const)),
24 Effect.repeat({
25 schedule: Schedule.spaced("1 second"),
26 until: (status) => status === "gone",
27 times: 10,
28 }),
29 );
30
31test.provider(
32 "getProjectsLocationsGlobalConnectivityTests on a missing test fails with a typed tag",
33 (stack) =>
34 Effect.gen(function* () {
35 const { project } = yield* GcpEnvironment.current;
36
37 yield* stack.destroy();
38
39 const error = yield* Effect.flip(
40 networkmanagement.getProjectsLocationsGlobalConnectivityTests({
41 name: `projects/${project}/locations/global/connectivityTests/alchemy-missing`,
42 }),
43 );
44 expect(error._tag).toEqual("NotFound");
45
46 yield* stack.destroy();
47 }).pipe(logLevel),
48 {
49 tags: ["provider:gcp", "provider:gcp:networkmanagement", "live"],
50 timeout: 90_000,
51 },
52);
53
54test.provider.skipIf(!runLifecycle)(
55 "create, update, and delete a connectivity test",
56 (stack) =>
57 Effect.gen(function* () {
58 const { project } = yield* GcpEnvironment.current;
59
60 yield* stack.destroy();
61
62 const created = yield* stack.deploy(
63 Effect.gen(function* () {
64 const test = yield* GCP.NetworkManagement.ConnectivityTest(
65 "DnsPath",
66 {
67 source: {
68 ipAddress: "10.8.0.1",
69 network: defaultNetworkSelfLink(project),
70 networkType: "GCP_NETWORK",
71 projectId: project,
72 },
73 destination: { ipAddress: "10.8.0.2", port: 443 },
74 protocol: "TCP",
75 description: "reach a",
76 labels: { env: "test" },
77 },
78 );
79 return { test };
80 }),
81 );
82
83 expect(created.test.name).toContain("/connectivityTests/");
84 expect(created.test.name).toContain("/locations/global/");
85 expect(created.test.testId).toEqual(expect.any(String));
86 expect(created.test.location).toEqual("global");
87 expect(created.test.description).toEqual("reach a");
88 expect(created.test.protocol).toEqual("TCP");
89 expect(created.test.source?.ipAddress).toEqual("10.8.0.1");
90 expect(created.test.destination?.ipAddress).toEqual("10.8.0.2");
91 expect(created.test.destination?.port).toEqual(443);
92 expect(created.test.labels).toMatchObject({ env: "test" });
93 expect(created.test.createTime).toEqual(expect.any(String));
94
95 const fetched =
96 yield* networkmanagement.getProjectsLocationsGlobalConnectivityTests({
97 name: created.test.name,
98 });
99 expect(fetched.name).toEqual(created.test.name);
100 expect(fetched.labels?.env).toEqual("test");
101 expect(fetched.description).toEqual("reach a");
102 expect(
103 Object.keys(fetched.labels ?? {}).some((key) =>
104 key.startsWith("alchemy-"),
105 ),
106 ).toEqual(true);
107
108 const updated = yield* stack.deploy(
109 Effect.gen(function* () {
110 const test = yield* GCP.NetworkManagement.ConnectivityTest(
111 "DnsPath",
112 {
113 testId: created.test.testId,
114 source: {
115 ipAddress: "10.8.0.1",
116 network: defaultNetworkSelfLink(project),
117 networkType: "GCP_NETWORK",
118 projectId: project,
119 },
120 destination: { ipAddress: "10.8.0.2", port: 80 },
121 protocol: "TCP",
122 description: "reach b",
123 labels: { env: "prod", role: "reach" },
124 bypassFirewallChecks: true,
125 },
126 );
127 return { test };
128 }),
129 );
130
131 expect(updated.test.name).toEqual(created.test.name);
132 expect(updated.test.description).toEqual("reach b");
133 expect(updated.test.destination?.port).toEqual(80);
134 expect(updated.test.bypassFirewallChecks).toEqual(true);
135 expect(updated.test.labels).toMatchObject({ env: "prod", role: "reach" });
136
137 const refetched =
138 yield* networkmanagement.getProjectsLocationsGlobalConnectivityTests({
139 name: created.test.name,
140 });
141 expect(refetched.description).toEqual("reach b");
142 expect(refetched.destination?.port).toEqual(80);
143 expect(refetched.labels?.env).toEqual("prod");
144 expect(refetched.labels?.role).toEqual("reach");
145
146 yield* stack.destroy();
147
148 const gone = yield* waitUntilGone(created.test.name);
149 expect(gone).toEqual("gone");
150 }).pipe(logLevel),
151 {
152 tags: ["provider:gcp", "provider:gcp:networkmanagement", "live"],
153 timeout: 600_000,
154 },
155);