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.getProjectsLocationsGrpcRoutes({ 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 "getProjectsLocationsGrpcRoutes 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.getProjectsLocationsGrpcRoutes({
37 name: `projects/${project}/locations/global/grpcRoutes/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 grpc route",
52 (stack) =>
53 Effect.gen(function* () {
54 yield* stack.destroy();
55
56 const created = yield* stack.deploy(
57 Effect.gen(function* () {
58 return yield* GCP.NetworkServices.GrpcRoute("Api", {
59 location: "global",
60 hostnames: ["alchemy-grpc.example.com"],
61 description: "grpc route a",
62 labels: { env: "test" },
63 rules: [
64 {
65 action: {
66 retryPolicy: {
67 retryConditions: ["cancelled"],
68 numRetries: 1,
69 },
70 },
71 },
72 ],
73 });
74 }),
75 );
76
77 expect(created.name).toContain("/grpcRoutes/");
78 expect(created.grpcRouteId).toEqual(expect.any(String));
79 expect(created.location).toEqual("global");
80 expect(created.hostnames).toContain("alchemy-grpc.example.com");
81 expect(created.description).toEqual("grpc route a");
82 expect(created.labels).toMatchObject({ env: "test" });
83 expect(created.createTime).toEqual(expect.any(String));
84
85 const fetched = yield* networkservices.getProjectsLocationsGrpcRoutes({
86 name: created.name,
87 });
88 expect(fetched.name).toEqual(created.name);
89 expect(fetched.description).toEqual("grpc route a");
90 expect(fetched.labels?.env).toEqual("test");
91 expect(
92 Object.keys(fetched.labels ?? {}).some((key) =>
93 key.startsWith("alchemy-"),
94 ),
95 ).toEqual(true);
96
97 const updated = yield* stack.deploy(
98 Effect.gen(function* () {
99 return yield* GCP.NetworkServices.GrpcRoute("Api", {
100 grpcRouteId: created.grpcRouteId,
101 location: "global",
102 hostnames: ["alchemy-grpc.example.com"],
103 description: "grpc route b",
104 labels: { env: "prod", role: "grpc" },
105 rules: [
106 {
107 matches: [
108 {
109 method: {
110 grpcService: "api.v1.Orders",
111 grpcMethod: "Get",
112 },
113 },
114 ],
115 action: {
116 retryPolicy: {
117 retryConditions: ["unavailable"],
118 numRetries: 2,
119 },
120 },
121 },
122 ],
123 });
124 }),
125 );
126
127 expect(updated.name).toEqual(created.name);
128 expect(updated.description).toEqual("grpc route b");
129 expect(updated.labels).toMatchObject({ env: "prod", role: "grpc" });
130 expect(updated.rules[0]?.action?.retryPolicy?.numRetries).toEqual(2);
131
132 const refetched = yield* networkservices.getProjectsLocationsGrpcRoutes({
133 name: created.name,
134 });
135 expect(refetched.description).toEqual("grpc route b");
136 expect(refetched.labels?.env).toEqual("prod");
137 expect(refetched.labels?.role).toEqual("grpc");
138
139 yield* stack.destroy();
140
141 const gone = yield* waitUntilGone(created.name);
142 expect(gone).toEqual("gone");
143 }).pipe(logLevel),
144 {
145 tags: ["provider:gcp", "provider:gcp:networkservices", "live"],
146 timeout: 90_000,
147 },
148);