1import { GcpEnvironment } from "@/GCP/Environment";
2import * as GCP from "@/GCP";
3import * as Test from "@/Test/Alchemy";
4import * as netapp from "@distilled.cloud/gcp/netapp_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
17// Volumes need a Private Service Access connection to
18// `netapp.servicenetworking.goog` on the `default` network (otherwise volume
19// create fails with `Please create Service Networking connection with
20// service 'netapp.servicenetworking.goog' ...`), and each file provisions its
21// own 2 TiB STANDARD pool while the default regional quota
22// (StandardStoragePoolCapacityGiBPerRegion = 2048) fits one. Set
23// GCP_TEST_NETAPP_VOLUMES=1 on a project with both.
24const runLifecycle = !!process.env.GCP_TEST_NETAPP_VOLUMES && !process.env.FAST;
25
26const waitUntilGone = (name: string) =>
27 netapp.getProjectsLocationsBackupVaultsBackups({ name }).pipe(
28 Effect.as("found" as const),
29 Effect.catchTag("NotFound", () => Effect.succeed("gone" as const)),
30 Effect.repeat({
31 schedule: Schedule.spaced("2 seconds"),
32 until: (status) => status === "gone",
33 times: 10,
34 }),
35 );
36
37test.provider(
38 "getProjectsLocationsBackupVaultsBackups on a missing backup fails with a typed tag",
39 (stack) =>
40 Effect.gen(function* () {
41 const { project } = yield* GcpEnvironment.current;
42
43 yield* stack.destroy();
44
45 const error = yield* Effect.flip(
46 netapp.getProjectsLocationsBackupVaultsBackups({
47 name: `projects/${project}/locations/us-central1/backupVaults/alchemy-missing-vault/backups/alchemy-missing-backup`,
48 }),
49 );
50 expect(error._tag).toEqual("NotFound");
51
52 const page = yield* netapp.listProjectsLocationsBackupVaultsBackups({
53 parent: `projects/${project}/locations/-/backupVaults/-`,
54 pageSize: 10,
55 });
56 expect((page.backups ?? []).map((item) => item.name)).not.toContain(
57 `projects/${project}/locations/us-central1/backupVaults/alchemy-missing-vault/backups/alchemy-missing-backup`,
58 );
59
60 yield* stack.destroy();
61 }).pipe(logLevel),
62 { tags: ["provider:gcp", "provider:gcp:netapp", "live"], timeout: 90_000 },
63);
64
65test.provider.skipIf(!runLifecycle)(
66 "create, update, and delete a backup",
67 (stack) =>
68 Effect.gen(function* () {
69 yield* stack.destroy();
70
71 const created = yield* stack.deploy(
72 Effect.gen(function* () {
73 const pool = yield* GCP.NetApp.StoragePool("Pool", {
74 network: "default",
75 serviceLevel: "STANDARD",
76 capacityGib: 2048,
77 labels: { env: "test" },
78 });
79 const volume = yield* GCP.NetApp.Volume("Share", {
80 storagePool: pool.name,
81 protocols: ["NFSV3"],
82 capacityGib: 100,
83 labels: { env: "test" },
84 });
85 const vault = yield* GCP.NetApp.BackupVault("Vault", {
86 labels: { env: "test" },
87 });
88 return yield* GCP.NetApp.BackupVaultsBackup("Nightly", {
89 backupVault: vault.name,
90 sourceVolume: volume.name,
91 description: "alchemy-test-backup",
92 labels: { env: "test" },
93 });
94 }),
95 );
96
97 expect(created.name).toContain("/backups/");
98 expect(created.sourceVolume).toContain("/volumes/");
99 expect(created.labels).toMatchObject({ env: "test" });
100
101 const fetched = yield* netapp.getProjectsLocationsBackupVaultsBackups({
102 name: created.name,
103 });
104 expect(fetched.name).toEqual(created.name);
105 expect(fetched.labels?.env).toEqual("test");
106
107 const updated = yield* stack.deploy(
108 Effect.gen(function* () {
109 const pool = yield* GCP.NetApp.StoragePool("Pool", {
110 network: "default",
111 serviceLevel: "STANDARD",
112 capacityGib: 2048,
113 labels: { env: "test" },
114 });
115 const volume = yield* GCP.NetApp.Volume("Share", {
116 storagePool: pool.name,
117 protocols: ["NFSV3"],
118 capacityGib: 100,
119 labels: { env: "test" },
120 });
121 const vault = yield* GCP.NetApp.BackupVault("Vault", {
122 labels: { env: "test" },
123 });
124 return yield* GCP.NetApp.BackupVaultsBackup("Nightly", {
125 backupId: created.backupId,
126 backupVault: vault.name,
127 sourceVolume: volume.name,
128 description: "alchemy-prod-backup",
129 labels: { env: "prod", role: "backup" },
130 });
131 }),
132 );
133
134 expect(updated.name).toEqual(created.name);
135 expect(updated.description).toEqual("alchemy-prod-backup");
136 expect(updated.labels).toMatchObject({ env: "prod", role: "backup" });
137
138 yield* stack.destroy();
139
140 const gone = yield* waitUntilGone(created.name);
141 expect(gone).toEqual("gone");
142 }).pipe(logLevel),
143 { tags: ["provider:gcp", "provider:gcp:netapp", "live"], timeout: 900_000 },
144);