1import * as GCP from "@/GCP";
2import { GcpEnvironment } from "@/GCP/Environment";
3import * as Test from "@/Test/Alchemy";
4import * as memcache from "@distilled.cloud/gcp/memcache_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// Memcached instances need Private Service Access on the project's default
18// network — without it create fails with BadRequest "Google private service
19// access is not enabled." — and take ~10 minutes to provision. Set
20// GCP_TEST_PRIVATE_SERVICE_ACCESS=1 on a project with PSA configured.
21const privateServiceAccess = !!process.env.GCP_TEST_PRIVATE_SERVICE_ACCESS;
22const runLifecycle =
23 privateServiceAccess && !!process.env.GCP_TEST_SLOW && !process.env.FAST;
24
25const waitUntilGone = (name: string) =>
26 memcache.getProjectsLocationsInstances({ name }).pipe(
27 Effect.as("found" as const),
28 Effect.catchTag("NotFound", () => Effect.succeed("gone" as const)),
29 Effect.repeat({
30 schedule: Schedule.spaced("2 seconds"),
31 until: (status) => status === "gone",
32 times: 10,
33 }),
34 );
35
36test.provider(
37 "getProjectsLocationsInstances on a missing instance fails with NotFound",
38 (stack) =>
39 Effect.gen(function* () {
40 const { project } = yield* GcpEnvironment.current;
41 yield* stack.destroy();
42
43 const error = yield* Effect.flip(
44 memcache.getProjectsLocationsInstances({
45 name: `projects/${project}/locations/us-central1/instances/alchemy-memcache-missing`,
46 }),
47 );
48 expect(error._tag).toBe("NotFound");
49
50 const page = yield* memcache.listProjectsLocationsInstances({
51 parent: `projects/${project}/locations/-`,
52 pageSize: 10,
53 });
54 expect(
55 (page.instances ?? []).map((instance) => instance.name),
56 ).not.toContain(
57 `projects/${project}/locations/us-central1/instances/alchemy-memcache-missing`,
58 );
59
60 yield* stack.destroy();
61 }).pipe(logLevel),
62 { tags: ["provider:gcp", "provider:gcp:memcache", "live"], timeout: 90_000 },
63);
64
65test.provider.skipIf(privateServiceAccess)(
66 "createProjectsLocationsInstances without private service access fails with PrivateServiceAccessNotEnabled",
67 (stack) =>
68 Effect.gen(function* () {
69 const { project } = yield* GcpEnvironment.current;
70 yield* stack.destroy();
71 const error = yield* Effect.flip(
72 memcache.createProjectsLocationsInstances({
73 parent: `projects/${project}/locations/us-central1`,
74 instanceId: "alchemy-memcache-psa-probe",
75 body: {
76 nodeCount: 1,
77 nodeConfig: { cpuCount: 1, memorySizeMb: 1024 },
78 },
79 }),
80 );
81 expect(error._tag).toEqual("PrivateServiceAccessNotEnabled");
82 }).pipe(logLevel),
83 { tags: ["provider:gcp", "provider:gcp:memcache", "live"], timeout: 90_000 },
84);
85
86test.provider.skipIf(!runLifecycle)(
87 "create, update, and delete a memcache instance",
88 (stack) =>
89 Effect.gen(function* () {
90 yield* stack.destroy();
91
92 const created = yield* stack.deploy(
93 Effect.gen(function* () {
94 return yield* GCP.Memcache.Instance("Cache", {
95 location: "us-central1",
96 nodeCount: 1,
97 nodeConfig: { cpuCount: 1, memorySizeMb: 1024 },
98 displayName: "alchemy-test-memcache",
99 labels: { env: "test" },
100 });
101 }),
102 );
103
104 expect(created.name).toContain("/instances/");
105 expect(created.instanceId).toEqual(expect.any(String));
106 expect(created.location).toEqual("us-central1");
107 expect(created.nodeCount).toEqual(1);
108 expect(created.nodeConfig.cpuCount).toEqual(1);
109 expect(created.nodeConfig.memorySizeMb).toEqual(1024);
110 expect(created.displayName).toEqual("alchemy-test-memcache");
111 expect(created.labels).toMatchObject({ env: "test" });
112 expect(created.state).toEqual("READY");
113
114 const fetched = yield* memcache.getProjectsLocationsInstances({
115 name: created.name,
116 });
117 expect(fetched.name).toEqual(created.name);
118 expect(fetched.nodeCount).toEqual(1);
119 expect(fetched.labels?.env).toEqual("test");
120 expect(fetched.displayName).toEqual("alchemy-test-memcache");
121
122 const updated = yield* stack.deploy(
123 Effect.gen(function* () {
124 return yield* GCP.Memcache.Instance("Cache", {
125 instanceId: created.instanceId,
126 location: "us-central1",
127 nodeCount: 1,
128 nodeConfig: { cpuCount: 1, memorySizeMb: 1024 },
129 displayName: "alchemy-prod-memcache",
130 labels: { env: "prod", role: "cache" },
131 });
132 }),
133 );
134
135 expect(updated.name).toEqual(created.name);
136 expect(updated.displayName).toEqual("alchemy-prod-memcache");
137 expect(updated.labels).toMatchObject({ env: "prod", role: "cache" });
138
139 const refetched = yield* memcache.getProjectsLocationsInstances({
140 name: created.name,
141 });
142 expect(refetched.displayName).toEqual("alchemy-prod-memcache");
143 expect(refetched.labels?.env).toEqual("prod");
144 expect(refetched.labels?.role).toEqual("cache");
145
146 yield* stack.destroy();
147
148 const gone = yield* waitUntilGone(created.name);
149 expect(gone).toEqual("gone");
150 }).pipe(logLevel),
151 {
152 tags: ["provider:gcp", "provider:gcp:memcache", "live"],
153 timeout: 1_800_000,
154 },
155);