The best way to control Claude Code, Codex and more from any computer or phone

main
ts 191 lines 7.21 KB
Raw
1 import * as GCP from "@alchemy.run/gcp";
2 import { GcpEnvironment } from "@alchemy.run/gcp/Environment";
3 import { expect } from "@alchemy.run/test";
4 import * as Test from "@alchemy.run/test/Alchemy";
5 import * as networkconnectivity from "@distilled.cloud/gcp/networkconnectivity_v1";
6 import * as Output from "alchemy/Output";
7 import * as Effect from "effect/Effect";
8 import { MinimumLogLevel } from "effect/References";
9 import * as Schedule from "effect/Schedule";
10
11 const { test } = Test.make({ providers: GCP.providers() });
12
13 const logLevel = Effect.provideService(MinimumLogLevel, process.env.DEBUG ? "Debug" : "Info");
14
15 // Gateway spokes provision managed routers and take well over 5 minutes.
16 const runLifecycle = !!process.env.GCP_TEST_SLOW && !process.env.FAST;
17
18 const waitUntilGone = (name: string) =>
19 networkconnectivity.getProjectsLocationsSpokesGatewayAdvertisedRoutes({ name }).pipe(
20 Effect.as("found" as const),
21 Effect.catchTag("NotFound", () => Effect.succeed("gone" as const)),
22 Effect.repeat({
23 schedule: Schedule.spaced("2 seconds"),
24 until: (status) => status === "gone",
25 times: 10,
26 }),
27 );
28
29 test.provider(
30 "getProjectsLocationsSpokesGatewayAdvertisedRoutes on a missing route fails with a typed tag",
31 (stack) =>
32 Effect.gen(function* () {
33 const { project } = yield* GcpEnvironment.current;
34 yield* stack.destroy();
35
36 const error = yield* Effect.flip(
37 networkconnectivity.getProjectsLocationsSpokesGatewayAdvertisedRoutes({
38 name: `projects/${project}/locations/us-central1/spokes/alchemy-missing/gatewayAdvertisedRoutes/alchemy-missing`,
39 }),
40 );
41 expect(error._tag).toEqual("NotFound");
42
43 yield* stack.destroy();
44 }).pipe(logLevel),
45 {
46 tags: ["provider:gcp", "provider:gcp:networkconnectivity", "live"],
47 timeout: 90_000,
48 },
49 );
50
51 test.provider.skipIf(!runLifecycle)(
52 "create, update, and delete a gateway advertised route",
53 (stack) =>
54 Effect.gen(function* () {
55 yield* stack.destroy();
56
57 const created = yield* stack.deploy(
58 Effect.gen(function* () {
59 const hub = yield* GCP.NetworkConnectivity.Hub("Mesh", {
60 description: "ncc hub for gateway route",
61 policyMode: "PRESET",
62 presetTopology: "HYBRID_INSPECTION",
63 });
64 const spoke = yield* GCP.NetworkConnectivity.Spoke("Gw", {
65 location: "us-central1",
66 hub: hub.name,
67 group: Output.interpolate`${hub.name}/groups/gateways`,
68 gateway: {
69 capacity: "CAPACITY_1_GBPS",
70 ipRangeReservations: [{ ipRange: "10.200.0.0/23" }],
71 },
72 });
73 const route = yield* GCP.NetworkConnectivity.SpokesGatewayAdvertisedRoute("OnPrem", {
74 parent: spoke.name,
75 ipRange: "192.168.0.0/16",
76 recipient: "ADVERTISE_TO_HUB",
77 description: "route a",
78 labels: { env: "test" },
79 });
80 return { hub, spoke, route };
81 }),
82 );
83
84 expect(created.route.name).toContain("/gatewayAdvertisedRoutes/");
85 expect(created.route.parent).toEqual(created.spoke.name);
86 expect(created.route.ipRange).toEqual("192.168.0.0/16");
87 expect(created.route.labels).toMatchObject({ env: "test" });
88
89 const fetched = yield* networkconnectivity.getProjectsLocationsSpokesGatewayAdvertisedRoutes({
90 name: created.route.name,
91 });
92 expect(fetched.name).toEqual(created.route.name);
93 expect(fetched.ipRange).toEqual("192.168.0.0/16");
94 expect(fetched.labels?.env).toEqual("test");
95 expect(Object.keys(fetched.labels ?? {}).some((key) => key.startsWith("alchemy-"))).toEqual(
96 true,
97 );
98
99 const updated = yield* stack.deploy(
100 Effect.gen(function* () {
101 const hub = yield* GCP.NetworkConnectivity.Hub("Mesh", {
102 hubId: created.hub.hubId,
103 description: "ncc hub for gateway route",
104 policyMode: "PRESET",
105 presetTopology: "HYBRID_INSPECTION",
106 });
107 const spoke = yield* GCP.NetworkConnectivity.Spoke("Gw", {
108 spokeId: created.spoke.spokeId,
109 location: "us-central1",
110 hub: hub.name,
111 group: Output.interpolate`${hub.name}/groups/gateways`,
112 gateway: {
113 capacity: "CAPACITY_1_GBPS",
114 ipRangeReservations: [{ ipRange: "10.200.0.0/23" }],
115 },
116 });
117 const route = yield* GCP.NetworkConnectivity.SpokesGatewayAdvertisedRoute("OnPrem", {
118 parent: spoke.name,
119 gatewayAdvertisedRouteId: created.route.gatewayAdvertisedRouteId,
120 ipRange: "192.168.0.0/16",
121 recipient: "ADVERTISE_TO_HUB",
122 priority: 200,
123 description: "route a",
124 labels: { env: "test" },
125 });
126 return { hub, spoke, route };
127 }),
128 );
129
130 // Priority updates in place.
131 expect(updated.route.name).toEqual(created.route.name);
132 expect(updated.route.uniqueId).toEqual(created.route.uniqueId);
133 expect(updated.route.description).toEqual("route a");
134 expect(updated.route.priority).toEqual(200);
135
136 const replaced = yield* stack.deploy(
137 Effect.gen(function* () {
138 const hub = yield* GCP.NetworkConnectivity.Hub("Mesh", {
139 hubId: created.hub.hubId,
140 description: "ncc hub for gateway route",
141 policyMode: "PRESET",
142 presetTopology: "HYBRID_INSPECTION",
143 });
144 const spoke = yield* GCP.NetworkConnectivity.Spoke("Gw", {
145 spokeId: created.spoke.spokeId,
146 location: "us-central1",
147 hub: hub.name,
148 group: Output.interpolate`${hub.name}/groups/gateways`,
149 gateway: {
150 capacity: "CAPACITY_1_GBPS",
151 ipRangeReservations: [{ ipRange: "10.200.0.0/23" }],
152 },
153 });
154 const route = yield* GCP.NetworkConnectivity.SpokesGatewayAdvertisedRoute("OnPrem", {
155 parent: spoke.name,
156 gatewayAdvertisedRouteId: created.route.gatewayAdvertisedRouteId,
157 ipRange: "192.168.0.0/16",
158 recipient: "ADVERTISE_TO_HUB",
159 priority: 200,
160 description: "route b",
161 labels: { env: "prod", role: "gw" },
162 });
163 return { hub, spoke, route };
164 }),
165 );
166
167 // Labels are immutable: changing them replaces the route (the pinned
168 // id is reused, so the old route is deleted first).
169 expect(replaced.route.name).toEqual(created.route.name);
170 expect(replaced.route.uniqueId).not.toEqual(created.route.uniqueId);
171 expect(replaced.route.labels).toMatchObject({ env: "prod", role: "gw" });
172
173 const refetched =
174 yield* networkconnectivity.getProjectsLocationsSpokesGatewayAdvertisedRoutes({
175 name: replaced.route.name,
176 });
177 expect(refetched.labels?.env).toEqual("prod");
178 expect(refetched.labels?.role).toEqual("gw");
179 expect(refetched.priority).toEqual(200);
180
181 yield* stack.destroy();
182
183 const gone = yield* waitUntilGone(created.route.name);
184 expect(gone).toEqual("gone");
185 }).pipe(logLevel),
186 {
187 tags: ["provider:gcp", "provider:gcp:networkconnectivity", "live"],
188 timeout: 1_800_000,
189 retry: 0,
190 },
191 );