1import * as GCP from "@/GCP";
2import * as Test from "@/Test/Alchemy";
3import * as compute from "@distilled.cloud/gcp/compute_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 = (projectId: string, rolloutPlan: string) =>
18 compute.getRolloutPlans({ project: projectId, rolloutPlan }).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
28const wave = (locations: string[]): compute.RolloutPlanWave => ({
29 displayName: "central",
30 selectors: [
31 {
32 locationSelector: {
33 includedLocations: locations,
34 },
35 },
36 ],
37 validation: {
38 type: "time",
39 timeBasedValidationMetadata: { waitDuration: "0s" },
40 },
41});
42
43test.provider(
44 "getRolloutPlans on a missing plan fails with NotFound",
45 (stack) =>
46 Effect.gen(function* () {
47 const { project } = yield* GcpEnvironment.current;
48 yield* stack.destroy();
49
50 const error = yield* Effect.flip(
51 compute.getRolloutPlans({
52 project,
53 rolloutPlan: "alchemy-missing-rollout-plan",
54 }),
55 );
56 expect(error._tag).toBe("NotFound");
57
58 yield* stack.destroy();
59 }).pipe(logLevel),
60 { tags: ["provider:gcp", "provider:gcp:compute", "live"], timeout: 90_000 },
61);
62
63test.provider(
64 "create, replace, and delete a rollout plan",
65 (stack) =>
66 Effect.gen(function* () {
67 yield* stack.destroy();
68
69 const created = yield* stack.deploy(
70 Effect.gen(function* () {
71 return yield* GCP.Compute.RolloutPlan("Fleet", {
72 description: "ops-agent canary",
73 locationScope: "ZONAL",
74 waves: [wave(["us-central1-a"])],
75 });
76 }),
77 );
78
79 expect(created.rolloutPlanName).toEqual(expect.any(String));
80 expect(created.locationScope).toEqual("ZONAL");
81 expect(created.description).toEqual("ops-agent canary");
82 expect(created.waves.length).toBeGreaterThan(0);
83
84 const fetched = yield* compute.getRolloutPlans({
85 project: created.project,
86 rolloutPlan: created.rolloutPlanName,
87 });
88 expect(fetched.name).toEqual(created.rolloutPlanName);
89 expect(fetched.description).toContain("[alchemy ");
90 expect(fetched.description).toContain("ops-agent canary");
91
92 const updated = yield* stack.deploy(
93 Effect.gen(function* () {
94 return yield* GCP.Compute.RolloutPlan("Fleet", {
95 rolloutPlanName: created.rolloutPlanName,
96 description: "ops-agent rollout",
97 locationScope: "ZONAL",
98 waves: [wave(["us-central1-a", "us-central1-b"])],
99 });
100 }),
101 );
102
103 expect(updated.rolloutPlanName).toEqual(created.rolloutPlanName);
104 expect(updated.description).toEqual("ops-agent rollout");
105
106 const fetchedUpdated = yield* compute.getRolloutPlans({
107 project: updated.project,
108 rolloutPlan: updated.rolloutPlanName,
109 });
110 expect(fetchedUpdated.description).toContain("ops-agent rollout");
111
112 yield* stack.destroy();
113 const gone = yield* waitUntilGone(
114 created.project,
115 created.rolloutPlanName,
116 );
117 expect(gone).toEqual("gone");
118 }).pipe(logLevel),
119 { tags: ["provider:gcp", "provider:gcp:compute", "live"], timeout: 90_000 },
120);