1import { GcpEnvironment } from "@/GCP/Environment";
2import * as GCP from "@/GCP";
3import * as Test from "@/Test/Alchemy";
4import * as redis from "@distilled.cloud/gcp/redis_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
17const waitUntilGone = (name: string) =>
18 redis.getProjectsLocationsAclPolicies({ name }).pipe(
19 Effect.as("found" as const),
20 Effect.catchTag("NotFound", () => Effect.succeed("gone" as const)),
21 Effect.repeat({
22 schedule: Schedule.spaced("2 seconds"),
23 until: (status) => status === "gone",
24 times: 10,
25 }),
26 );
27
28test.provider(
29 "getProjectsLocationsAclPolicies on a missing policy fails with NotFound",
30 (stack) =>
31 Effect.gen(function* () {
32 const { project } = yield* GcpEnvironment.current;
33
34 yield* stack.destroy();
35
36 const error = yield* Effect.flip(
37 redis.getProjectsLocationsAclPolicies({
38 name: `projects/${project}/locations/us-central1/aclPolicies/alchemy-acl-missing`,
39 }),
40 );
41 expect(error._tag).toBe("NotFound");
42
43 const page = yield* redis.listProjectsLocationsAclPolicies({
44 parent: `projects/${project}/locations/-`,
45 pageSize: 10,
46 });
47 expect(
48 (page.aclPolicies ?? []).map((policy) => policy.name?.split("/").pop()),
49 ).not.toContain("alchemy-acl-missing");
50
51 yield* stack.destroy();
52 }).pipe(logLevel),
53 { tags: ["provider:gcp", "provider:gcp:redis", "live"], timeout: 90_000 },
54);
55
56test.provider(
57 "create, update, and delete an ACL policy",
58 (stack) =>
59 Effect.gen(function* () {
60 yield* stack.destroy();
61
62 const created = yield* stack.deploy(
63 Effect.gen(function* () {
64 const policy = yield* GCP.Redis.AclPolicy("AppAcl", {
65 location: "us-central1",
66 rules: [{ username: "app", rule: "on ~keys:* +get" }],
67 });
68 return { policy };
69 }),
70 );
71
72 expect(created.policy.name).toContain("/aclPolicies/");
73 expect(created.policy.aclPolicyId).toEqual(expect.any(String));
74 expect(created.policy.location).toEqual("us-central1");
75 expect(created.policy.rules).toEqual([
76 { username: "app", rule: "on ~keys:* +get" },
77 ]);
78
79 const fetched = yield* redis.getProjectsLocationsAclPolicies({
80 name: created.policy.name,
81 });
82 expect(fetched.name).toEqual(created.policy.name);
83 expect(fetched.rules).toEqual([
84 { username: "app", rule: "on ~keys:* +get" },
85 ]);
86
87 const updated = yield* stack.deploy(
88 Effect.gen(function* () {
89 return yield* GCP.Redis.AclPolicy("AppAcl", {
90 location: "us-central1",
91 rules: [
92 { username: "app", rule: "on ~keys:* +get +set" },
93 { username: "readonly", rule: "off ~* -@all" },
94 ],
95 });
96 }),
97 );
98
99 expect(updated.name).toEqual(created.policy.name);
100 expect(updated.rules).toEqual([
101 { username: "app", rule: "on ~keys:* +get +set" },
102 { username: "readonly", rule: "off ~* -@all" },
103 ]);
104
105 const refetched = yield* redis.getProjectsLocationsAclPolicies({
106 name: created.policy.name,
107 });
108 expect(
109 (refetched.rules ?? []).some(
110 (rule) =>
111 rule.username === "app" && (rule.rule ?? "").includes("+set"),
112 ),
113 ).toEqual(true);
114 expect(
115 (refetched.rules ?? []).some((rule) => rule.username === "readonly"),
116 ).toEqual(true);
117
118 yield* stack.destroy();
119
120 const gone = yield* waitUntilGone(created.policy.name);
121 expect(gone).toEqual("gone");
122 }).pipe(logLevel),
123 { tags: ["provider:gcp", "provider:gcp:redis", "live"], timeout: 120_000 },
124);