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";
9
10const { test } = Test.make({ providers: GCP.providers() });
11
12const logLevel = Effect.provideService(
13 MinimumLogLevel,
14 process.env.DEBUG ? "Debug" : "Info",
15);
16
17const waitUntilGone = (name: string) =>
18 networkservices.getProjectsLocationsTcpRoutes({ name }).pipe(
19 Effect.as("found" as const),
20 Effect.catchTag("NotFound", () => Effect.succeed("gone" as const)),
21 Effect.repeat({
22 schedule: Schedule.spaced("1 second"),
23 until: (status) => status === "gone",
24 times: 10,
25 }),
26 );
27
28test.provider(
29 "getProjectsLocationsTcpRoutes on a missing route fails with a typed tag",
30 (stack) =>
31 Effect.gen(function* () {
32 yield* stack.destroy();
33
34 const { project } = yield* GcpEnvironment.current;
35 const error = yield* Effect.flip(
36 networkservices.getProjectsLocationsTcpRoutes({
37 name: `projects/${project}/locations/global/tcpRoutes/alchemy-missing`,
38 }),
39 );
40 expect(error._tag).toEqual("NotFound");
41
42 yield* stack.destroy();
43 }).pipe(logLevel),
44 {
45 tags: ["provider:gcp", "provider:gcp:networkservices", "live"],
46 timeout: 90_000,
47 },
48);
49
50test.provider(
51 "create, update, and delete a tcp route",
52 (stack) =>
53 Effect.gen(function* () {
54 yield* stack.destroy();
55
56 const created = yield* stack.deploy(
57 Effect.gen(function* () {
58 const mesh = yield* GCP.NetworkServices.Mesh("Sidecar", {
59 location: "global",
60 description: "tcp mesh",
61 labels: { env: "test" },
62 });
63 const route = yield* GCP.NetworkServices.TcpRoute("Passthrough", {
64 location: "global",
65 description: "tcp route a",
66 labels: { env: "test" },
67 meshes: [mesh.name],
68 rules: [
69 {
70 matches: [{ address: "0.0.0.0/0", port: "443" }],
71 action: { originalDestination: true },
72 },
73 ],
74 });
75 return { mesh, route };
76 }),
77 );
78
79 expect(created.route.name).toContain("/tcpRoutes/");
80 expect(created.route.tcpRouteId).toEqual(expect.any(String));
81 expect(created.route.location).toEqual("global");
82 expect(created.route.description).toEqual("tcp route a");
83 expect(created.route.meshes).toContain(created.mesh.name);
84 expect(created.route.rules[0]?.action?.originalDestination).toEqual(true);
85 expect(created.route.labels).toMatchObject({ env: "test" });
86 expect(created.route.createTime).toEqual(expect.any(String));
87
88 const fetched = yield* networkservices.getProjectsLocationsTcpRoutes({
89 name: created.route.name,
90 });
91 expect(fetched.name).toEqual(created.route.name);
92 expect(fetched.description).toEqual("tcp route a");
93 expect(fetched.labels?.env).toEqual("test");
94 expect(
95 Object.keys(fetched.labels ?? {}).some((key) =>
96 key.startsWith("alchemy-"),
97 ),
98 ).toEqual(true);
99
100 const updated = yield* stack.deploy(
101 Effect.gen(function* () {
102 const mesh = yield* GCP.NetworkServices.Mesh("Sidecar", {
103 meshId: created.mesh.meshId,
104 location: "global",
105 description: "tcp mesh",
106 labels: { env: "test" },
107 });
108 const route = yield* GCP.NetworkServices.TcpRoute("Passthrough", {
109 tcpRouteId: created.route.tcpRouteId,
110 location: "global",
111 description: "tcp route b",
112 labels: { env: "prod", role: "tcp" },
113 meshes: [mesh.name],
114 rules: [
115 {
116 matches: [{ address: "0.0.0.0/0", port: "80" }],
117 action: { originalDestination: true, idleTimeout: "60s" },
118 },
119 ],
120 });
121 return { mesh, route };
122 }),
123 );
124
125 expect(updated.route.name).toEqual(created.route.name);
126 expect(updated.route.description).toEqual("tcp route b");
127 expect(updated.route.labels).toMatchObject({ env: "prod", role: "tcp" });
128 expect(updated.route.rules[0]?.matches?.[0]?.port).toEqual("80");
129
130 const refetched = yield* networkservices.getProjectsLocationsTcpRoutes({
131 name: created.route.name,
132 });
133 expect(refetched.description).toEqual("tcp route b");
134 expect(refetched.labels?.env).toEqual("prod");
135 expect(refetched.labels?.role).toEqual("tcp");
136
137 yield* stack.destroy();
138
139 const gone = yield* waitUntilGone(created.route.name);
140 expect(gone).toEqual("gone");
141 }).pipe(logLevel),
142 {
143 tags: ["provider:gcp", "provider:gcp:networkservices", "live"],
144 timeout: 90_000,
145 },
146);