1import { GcpEnvironment } from "@/GCP/Environment";
2import * as GCP from "@/GCP";
3import * as Test from "@/Test/Alchemy";
4import * as clouddeploy from "@distilled.cloud/gcp/clouddeploy_v1";
5import { expect } from "alchemy-test";
6import * as Effect from "effect/Effect";
7import { MinimumLogLevel } from "effect/References";
8import * as Schedule from "effect/Schedule";
9
10const { test } = Test.make({ providers: GCP.providers() });
11
12const logLevel = Effect.provideService(
13 MinimumLogLevel,
14 process.env.DEBUG ? "Debug" : "Info",
15);
16
17const weekendFreeze = {
18 selectors: [{ target: { id: "*" } }],
19 rules: [
20 {
21 rolloutRestriction: {
22 id: "weekends",
23 timeWindows: {
24 timeZone: "America/Los_Angeles",
25 weeklyWindows: [{ daysOfWeek: ["SATURDAY", "SUNDAY"] }],
26 },
27 },
28 },
29 ],
30};
31
32const waitUntilGone = (name: string) =>
33 clouddeploy.getProjectsLocationsDeployPolicies({ name }).pipe(
34 Effect.as("found" as const),
35 Effect.catchTag("NotFound", () => Effect.succeed("gone" as const)),
36 Effect.repeat({
37 schedule: Schedule.spaced("2 seconds"),
38 until: (status) => status === "gone",
39 times: 10,
40 }),
41 );
42
43test.provider(
44 "getProjectsLocationsDeployPolicies on a missing policy fails with a typed tag",
45 (stack) =>
46 Effect.gen(function* () {
47 const { project } = yield* GcpEnvironment.current;
48
49 yield* stack.destroy();
50
51 const error = yield* Effect.flip(
52 clouddeploy.getProjectsLocationsDeployPolicies({
53 name: `projects/${project}/locations/us-central1/deployPolicies/alchemy-missing-policy`,
54 }),
55 );
56 expect(error._tag).toEqual("NotFound");
57
58 yield* stack.destroy();
59 }).pipe(logLevel),
60 {
61 tags: ["provider:gcp", "provider:gcp:clouddeploy", "live"],
62 timeout: 90_000,
63 },
64);
65
66test.provider(
67 "create, update, and delete a deploy policy",
68 (stack) =>
69 Effect.gen(function* () {
70 yield* stack.destroy();
71
72 const created = yield* stack.deploy(
73 Effect.gen(function* () {
74 return yield* GCP.CloudDeploy.DeployPolicy("Freeze", {
75 ...weekendFreeze,
76 description: "alchemy-test-policy",
77 labels: { env: "test" },
78 });
79 }),
80 );
81
82 expect(created.name).toContain("/deployPolicies/");
83 expect(created.deployPolicyId).toEqual(expect.any(String));
84 expect(created.location).toEqual("us-central1");
85 expect(created.description).toEqual("alchemy-test-policy");
86 expect(created.selectors[0]?.target?.id).toEqual("*");
87 expect(created.rules[0]?.rolloutRestriction?.id).toEqual("weekends");
88 expect(created.labels).toMatchObject({ env: "test" });
89 expect(created.suspended).toEqual(false);
90
91 const fetched = yield* clouddeploy.getProjectsLocationsDeployPolicies({
92 name: created.name,
93 });
94 expect(fetched.name).toEqual(created.name);
95 expect(fetched.description).toEqual("alchemy-test-policy");
96 expect(fetched.labels?.env).toEqual("test");
97
98 const updated = yield* stack.deploy(
99 Effect.gen(function* () {
100 return yield* GCP.CloudDeploy.DeployPolicy("Freeze", {
101 deployPolicyId: created.deployPolicyId,
102 ...weekendFreeze,
103 description: "alchemy-prod-policy",
104 labels: { env: "prod", role: "freeze" },
105 });
106 }),
107 );
108
109 expect(updated.name).toEqual(created.name);
110 expect(updated.description).toEqual("alchemy-prod-policy");
111 expect(updated.labels).toMatchObject({ env: "prod", role: "freeze" });
112
113 yield* stack.destroy();
114
115 const gone = yield* waitUntilGone(created.name);
116 expect(gone).toEqual("gone");
117 }).pipe(logLevel),
118 {
119 tags: ["provider:gcp", "provider:gcp:clouddeploy", "live"],
120 timeout: 90_000,
121 },
122);