1import { GcpEnvironment } from "@/GCP/Environment";
2import * as GCP from "@/GCP";
3import * as Test from "@/Test/Alchemy";
4import * as file from "@distilled.cloud/gcp/file_v1";
5import { expect } from "alchemy-test";
6import * as Effect from "effect/Effect";
7import { MinimumLogLevel } from "effect/References";
8import * as Schedule from "effect/Schedule";
9import { CAPACITY_REGION, CAPACITY_ZONE } from "../zones.ts";
10
11const { test } = Test.make({ providers: GCP.providers() });
12
13const logLevel = Effect.provideService(
14 MinimumLogLevel,
15 process.env.DEBUG ? "Debug" : "Info",
16);
17
18// Filestore instances take 5–20 minutes to provision and several to delete.
19const runLifecycle = !!process.env.GCP_TEST_SLOW && !process.env.FAST;
20
21const waitUntilGone = (name: string) =>
22 file.getProjectsLocationsBackups({ name }).pipe(
23 Effect.as("found" as const),
24 Effect.catchTag("NotFound", () => Effect.succeed("gone" as const)),
25 Effect.repeat({
26 schedule: Schedule.spaced("2 seconds"),
27 until: (status) => status === "gone",
28 times: 10,
29 }),
30 );
31
32test.provider(
33 "getProjectsLocationsBackups on a missing backup fails with a typed tag",
34 (stack) =>
35 Effect.gen(function* () {
36 const { project } = yield* GcpEnvironment.current;
37
38 yield* stack.destroy();
39
40 const error = yield* Effect.flip(
41 file.getProjectsLocationsBackups({
42 name: `projects/${project}/locations/us-central1/backups/alchemy-filestore-missing`,
43 }),
44 );
45 expect(error._tag).toEqual("NotFound");
46
47 yield* stack.destroy();
48 }).pipe(logLevel),
49 { tags: ["provider:gcp", "provider:gcp:filestore", "live"], timeout: 90_000 },
50);
51
52test.provider.skipIf(!runLifecycle)(
53 "create, update, and delete a filestore backup",
54 (stack) =>
55 Effect.gen(function* () {
56 yield* stack.destroy();
57
58 const created = yield* stack.deploy(
59 Effect.gen(function* () {
60 const nfs = yield* GCP.Filestore.Instance("Nfs", {
61 location: CAPACITY_ZONE,
62 tier: "BASIC_HDD",
63 fileShares: [{ name: "share1", capacityGb: 1024 }],
64 networks: [{ network: "default", modes: ["MODE_IPV4"] }],
65 labels: { env: "test" },
66 });
67 const backup = yield* GCP.Filestore.Backup("Nightly", {
68 sourceInstance: nfs.name,
69 sourceFileShare: "share1",
70 location: CAPACITY_REGION,
71 description: "alchemy-test-backup",
72 labels: { env: "test" },
73 });
74 return { nfs, backup };
75 }),
76 );
77
78 expect(created.backup.name).toContain("/backups/");
79 expect(created.backup.backupId).toEqual(expect.any(String));
80 expect(created.backup.location).toEqual(CAPACITY_REGION);
81 expect(created.backup.sourceInstance).toEqual(created.nfs.name);
82 expect(created.backup.sourceFileShare).toEqual("share1");
83 expect(created.backup.description).toEqual("alchemy-test-backup");
84 expect(created.backup.labels).toMatchObject({ env: "test" });
85 expect(created.backup.state).toEqual("READY");
86
87 const fetched = yield* file.getProjectsLocationsBackups({
88 name: created.backup.name,
89 });
90 expect(fetched.name).toEqual(created.backup.name);
91 expect(fetched.description).toEqual("alchemy-test-backup");
92 expect(fetched.labels?.env).toEqual("test");
93 expect(fetched.sourceFileShare).toEqual("share1");
94
95 const updated = yield* stack.deploy(
96 Effect.gen(function* () {
97 const nfs = yield* GCP.Filestore.Instance("Nfs", {
98 instanceId: created.nfs.instanceId,
99 location: CAPACITY_ZONE,
100 tier: "BASIC_HDD",
101 fileShares: [{ name: "share1", capacityGb: 1024 }],
102 networks: [{ network: "default", modes: ["MODE_IPV4"] }],
103 labels: { env: "test" },
104 });
105 const backup = yield* GCP.Filestore.Backup("Nightly", {
106 sourceInstance: nfs.name,
107 sourceFileShare: "share1",
108 backupId: created.backup.backupId,
109 location: CAPACITY_REGION,
110 description: "alchemy-prod-backup",
111 labels: { env: "prod", role: "backup" },
112 });
113 return { nfs, backup };
114 }),
115 );
116
117 expect(updated.backup.name).toEqual(created.backup.name);
118 expect(updated.backup.description).toEqual("alchemy-prod-backup");
119 expect(updated.backup.labels).toMatchObject({
120 env: "prod",
121 role: "backup",
122 });
123
124 const refetched = yield* file.getProjectsLocationsBackups({
125 name: created.backup.name,
126 });
127 expect(refetched.description).toEqual("alchemy-prod-backup");
128 expect(refetched.labels?.env).toEqual("prod");
129 expect(refetched.labels?.role).toEqual("backup");
130
131 yield* stack.destroy();
132
133 const gone = yield* waitUntilGone(created.backup.name);
134 expect(gone).toEqual("gone");
135 }).pipe(logLevel),
136 {
137 tags: ["provider:gcp", "provider:gcp:filestore", "live"],
138 timeout: 2_700_000,
139 retry: 0,
140 },
141);