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_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.getProjectsLocationsInstances({ 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 "getProjectsLocationsInstances on a missing instance fails with NotFound",
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.getProjectsLocationsInstances({
42 name: `projects/${project}/locations/us-central1-a/instances/alchemy-filestore-missing`,
43 }),
44 );
45 expect(error._tag).toBe("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 instance",
54 (stack) =>
55 Effect.gen(function* () {
56 yield* stack.destroy();
57
58 const created = yield* stack.deploy(
59 Effect.gen(function* () {
60 return 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 description: "alchemy-test-nfs",
66 labels: { env: "test" },
67 });
68 }),
69 );
70
71 expect(created.name).toContain("/instances/");
72 expect(created.instanceId).toEqual(expect.any(String));
73 expect(created.location).toEqual(CAPACITY_ZONE);
74 expect(created.tier).toEqual("BASIC_HDD");
75 expect(created.description).toEqual("alchemy-test-nfs");
76 expect(created.labels).toMatchObject({ env: "test" });
77 expect(created.state).toEqual("READY");
78 expect(created.fileShares[0]?.name).toEqual("share1");
79 expect(created.fileShares[0]?.capacityGb).toEqual(1024);
80
81 const fetched = yield* file.getProjectsLocationsInstances({
82 name: created.name,
83 });
84 expect(fetched.name).toEqual(created.name);
85 expect(fetched.tier).toEqual("BASIC_HDD");
86 expect(fetched.description).toEqual("alchemy-test-nfs");
87 expect(fetched.labels?.env).toEqual("test");
88 expect(fetched.fileShares?.[0]?.name).toEqual("share1");
89 expect(fetched.fileShares?.[0]?.capacityGb).toEqual("1024");
90
91 const updated = yield* stack.deploy(
92 Effect.gen(function* () {
93 return yield* GCP.Filestore.Instance("Nfs", {
94 instanceId: created.instanceId,
95 location: CAPACITY_ZONE,
96 tier: "BASIC_HDD",
97 fileShares: [{ name: "share1", capacityGb: 1024 }],
98 networks: [{ network: "default", modes: ["MODE_IPV4"] }],
99 description: "alchemy-prod-nfs",
100 labels: { env: "prod", role: "nfs" },
101 });
102 }),
103 );
104
105 expect(updated.name).toEqual(created.name);
106 expect(updated.description).toEqual("alchemy-prod-nfs");
107 expect(updated.labels).toMatchObject({ env: "prod", role: "nfs" });
108
109 const refetched = yield* file.getProjectsLocationsInstances({
110 name: created.name,
111 });
112 expect(refetched.description).toEqual("alchemy-prod-nfs");
113 expect(refetched.labels?.env).toEqual("prod");
114 expect(refetched.labels?.role).toEqual("nfs");
115
116 yield* stack.destroy();
117
118 const gone = yield* waitUntilGone(created.name);
119 expect(gone).toEqual("gone");
120 }).pipe(logLevel),
121 {
122 tags: ["provider:gcp", "provider:gcp:filestore", "live"],
123 timeout: 2_700_000,
124 retry: 0,
125 },
126);