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