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 region = "us-central1";
18
19const waitUntilGone = (project: string, instantSnapshotGroup: string) =>
20 compute
21 .getRegionInstantSnapshotGroups({
22 project,
23 region,
24 instantSnapshotGroup,
25 })
26 .pipe(
27 Effect.as("found" as const),
28 Effect.catchTag("NotFound", () => Effect.succeed("gone" as const)),
29 Effect.repeat({
30 schedule: Schedule.spaced("1 second"),
31 until: (status) => status === "gone",
32 times: 10,
33 }),
34 );
35
36test.provider(
37 "insertRegionInstantSnapshotGroups with a malformed consistency group fails with BadRequest",
38 () =>
39 Effect.gen(function* () {
40 const { project } = yield* GcpEnvironment.current;
41 const error = yield* Effect.flip(
42 compute.insertRegionInstantSnapshotGroups({
43 project,
44 region,
45 sourceConsistencyGroup: "does-not-exist",
46 body: {
47 name: "alchemy-risg-probe",
48 description: "alchemy entitlement probe",
49 sourceConsistencyGroup: "does-not-exist",
50 },
51 }),
52 );
53 expect(error._tag).toEqual("BadRequest");
54 }).pipe(logLevel),
55 { tags: ["provider:gcp", "provider:gcp:compute", "live"], timeout: 60_000 },
56);
57
58test.provider(
59 "create and delete a regional instant snapshot group",
60 (stack) =>
61 Effect.gen(function* () {
62 yield* stack.destroy();
63
64 const created = yield* stack.deploy(
65 Effect.gen(function* () {
66 const policy = yield* GCP.Compute.ResourcePolicy("Consistent", {
67 region,
68 diskConsistencyGroupPolicy: {},
69 });
70 const disk = yield* GCP.Compute.RegionDisk("Member", {
71 region,
72 replicaZones: ["us-central1-a", "us-central1-b"],
73 type: "hyperdisk-balanced-high-availability",
74 sizeGb: 4,
75 });
76 return { policy, disk };
77 }),
78 );
79
80 yield* compute.addResourcePoliciesRegionDisks({
81 project: created.disk.project,
82 region,
83 disk: created.disk.diskName,
84 body: {
85 resourcePolicies: [
86 created.policy.selfLink ??
87 `projects/${created.disk.project}/regions/${region}/resourcePolicies/${created.policy.resourcePolicyName}`,
88 ],
89 },
90 });
91
92 const withGroup = yield* stack.deploy(
93 Effect.gen(function* () {
94 const policy = yield* GCP.Compute.ResourcePolicy("Consistent", {
95 resourcePolicyName: created.policy.resourcePolicyName,
96 region,
97 diskConsistencyGroupPolicy: {},
98 });
99 const disk = yield* GCP.Compute.RegionDisk("Member", {
100 diskName: created.disk.diskName,
101 region,
102 replicaZones: ["us-central1-a", "us-central1-b"],
103 type: "hyperdisk-balanced-high-availability",
104 sizeGb: 4,
105 });
106 const group = yield* GCP.Compute.RegionInstantSnapshotGroup(
107 "Checkpoint",
108 {
109 region,
110 sourceConsistencyGroup: policy.selfLink.as<string>(),
111 description: "group checkpoint",
112 },
113 );
114 return { policy, disk, group };
115 }),
116 );
117
118 expect(withGroup.group.instantSnapshotGroupName).toEqual(
119 expect.any(String),
120 );
121 expect(withGroup.group.region).toEqual(region);
122 expect(withGroup.group.description).toEqual("group checkpoint");
123
124 const fetched = yield* compute.getRegionInstantSnapshotGroups({
125 project: withGroup.group.project,
126 region,
127 instantSnapshotGroup: withGroup.group.instantSnapshotGroupName,
128 });
129 expect(fetched.name).toEqual(withGroup.group.instantSnapshotGroupName);
130 expect(fetched.description).toContain("[alchemy ");
131
132 yield* stack.destroy();
133 const gone = yield* waitUntilGone(
134 withGroup.group.project,
135 withGroup.group.instantSnapshotGroupName,
136 );
137 expect(gone).toEqual("gone");
138 }).pipe(logLevel),
139 { tags: ["provider:gcp", "provider:gcp:compute", "live"], timeout: 120_000 },
140);