1import * as GCP from "@/GCP";
2import { GcpEnvironment } from "@/GCP/Environment";
3import * as Test from "@/Test/Alchemy";
4import * as Core from "@/Test/Core";
5import * as alloydb from "@distilled.cloud/gcp/alloydb_v1";
6import * as resourcemanager from "@distilled.cloud/gcp/cloudresourcemanager_v3";
7import { describe, expect } from "alchemy-test";
8import * as Effect from "effect/Effect";
9import { dockerAvailable, expectProbe } from "../bindingHost.ts";
10import AlloyDbBindingsHost, {
11 AppUser,
12 Db,
13 Primary,
14 Snapshot,
15} from "./fixtures/bindings-host.ts";
16
17const testOptions = { providers: GCP.providers() };
18const { test, beforeAll, afterAll } = Test.make(testOptions);
19const sharedStack = Core.scratchStack(testOptions, "AlloyDbBindings");
20
21// AlloyDB clusters and instances take well over 5 minutes to provision.
22const runLifecycle = !!process.env.GCP_TEST_SLOW && !process.env.FAST;
23
24let baseUrl: string;
25let hostAccount: string;
26let names: { cluster: string; instance: string; backup: string; user: string };
27
28/** Every project-level role (and its IAM Condition) `account` holds. */
29const projectGrantsOf = (account: string) =>
30 Effect.gen(function* () {
31 const { project } = yield* GcpEnvironment.current;
32 const policy = yield* resourcemanager.getIamPolicyProjects({
33 resource: `projects/${project}`,
34 body: { options: { requestedPolicyVersion: 3 } },
35 });
36 return (policy.bindings ?? [])
37 .filter((binding) =>
38 (binding.members ?? []).includes(`serviceAccount:${account}`),
39 )
40 .map((binding) => ({
41 role: binding.role,
42 condition: binding.condition?.expression,
43 }))
44 .sort((left, right) => (left.role ?? "").localeCompare(right.role ?? ""));
45 });
46
47/**
48 * AlloyDB resources have no resource-level IAM policy: reads grant
49 * alloydb.viewer and GetConnectionInfo alloydb.client, on the project.
50 */
51const PROJECT_GRANTS = [
52 { role: "roles/alloydb.client", condition: undefined },
53 { role: "roles/alloydb.viewer", condition: undefined },
54];
55
56const expectProjectGrants = Effect.gen(function* () {
57 expect(yield* projectGrantsOf(hostAccount)).toEqual(PROJECT_GRANTS);
58});
59
60describe.skipIf(!dockerAvailable || !runLifecycle)(
61 "AlloyDB Bindings",
62 {
63 tags: ["provider:gcp", "provider:gcp:alloydb", "provider:gcp:run", "live"],
64 },
65 () => {
66 beforeAll(
67 Effect.gen(function* () {
68 yield* sharedStack.destroy();
69 const out = yield* sharedStack.deploy(
70 Effect.gen(function* () {
71 const host = yield* AlloyDbBindingsHost;
72 return {
73 uri: host.uri,
74 serviceAccount: host.serviceAccount,
75 names: {
76 cluster: (yield* Db).name,
77 instance: (yield* Primary).name,
78 backup: (yield* Snapshot).name,
79 user: (yield* AppUser).name,
80 },
81 };
82 }),
83 );
84 baseUrl = out.uri!;
85 hostAccount = out.serviceAccount!;
86 names = out.names;
87 }),
88 { timeout: 3_600_000 },
89 );
90
91 afterAll(sharedStack.destroy(), { timeout: 3_600_000 });
92
93 describe("GetCluster", () => {
94 test.provider(
95 "reads the cluster as the host's service account",
96 (_stack) =>
97 Effect.gen(function* () {
98 const live = yield* expectProbe<{ name?: string; uid?: string }>(
99 baseUrl,
100 "getCluster",
101 );
102 const expected = yield* alloydb.getProjectsLocationsClusters({
103 name: names.cluster,
104 });
105 expect(live.name).toEqual(names.cluster);
106 expect(live.uid).toEqual(expected.uid);
107 yield* expectProjectGrants;
108 }),
109 {
110 tags: ["provider:gcp", "provider:gcp:alloydb", "live"],
111 timeout: 600_000,
112 },
113 );
114 });
115
116 describe("GetInstance", () => {
117 test.provider(
118 "reads the instance as the host's service account",
119 (_stack) =>
120 Effect.gen(function* () {
121 const live = yield* expectProbe<{ name?: string; uid?: string }>(
122 baseUrl,
123 "getInstance",
124 );
125 const expected =
126 yield* alloydb.getProjectsLocationsClustersInstances({
127 name: names.instance,
128 });
129 expect(live.name).toEqual(names.instance);
130 expect(live.uid).toEqual(expected.uid);
131 yield* expectProjectGrants;
132 }),
133 {
134 tags: ["provider:gcp", "provider:gcp:alloydb", "live"],
135 timeout: 600_000,
136 },
137 );
138 });
139
140 describe("GetConnectionInfo", () => {
141 test.provider(
142 "reads the instance's connection info as the host's service account",
143 (_stack) =>
144 Effect.gen(function* () {
145 const live = yield* expectProbe<{
146 instanceUid?: string;
147 ipAddress?: string;
148 }>(baseUrl, "getConnectionInfo");
149 const expected =
150 yield* alloydb.getConnectionInfoProjectsLocationsClustersInstances(
151 { parent: names.instance },
152 );
153 const instance =
154 yield* alloydb.getProjectsLocationsClustersInstances({
155 name: names.instance,
156 });
157 expect(live.instanceUid).toEqual(instance.uid);
158 expect(live.instanceUid).toEqual(expect.any(String));
159 expect(live.ipAddress).toEqual(expected.ipAddress);
160 yield* expectProjectGrants;
161 }),
162 {
163 tags: ["provider:gcp", "provider:gcp:alloydb", "live"],
164 timeout: 600_000,
165 },
166 );
167 });
168
169 describe("GetBackup", () => {
170 test.provider(
171 "reads the backup as the host's service account",
172 (_stack) =>
173 Effect.gen(function* () {
174 const live = yield* expectProbe<{ name?: string; uid?: string }>(
175 baseUrl,
176 "getBackup",
177 );
178 const expected = yield* alloydb.getProjectsLocationsBackups({
179 name: names.backup,
180 });
181 expect(live.name).toEqual(names.backup);
182 expect(live.uid).toEqual(expected.uid);
183 yield* expectProjectGrants;
184 }),
185 {
186 tags: ["provider:gcp", "provider:gcp:alloydb", "live"],
187 timeout: 600_000,
188 },
189 );
190 });
191
192 describe("GetUser", () => {
193 test.provider(
194 "reads the database user as the host's service account",
195 (_stack) =>
196 Effect.gen(function* () {
197 const live = yield* expectProbe<{
198 name?: string;
199 databaseRoles?: string[];
200 }>(baseUrl, "getUser");
201 expect(live.name).toEqual(names.user);
202 expect(live.databaseRoles).toContain("alloydbsuperuser");
203 yield* expectProjectGrants;
204 }),
205 {
206 tags: ["provider:gcp", "provider:gcp:alloydb", "live"],
207 timeout: 600_000,
208 },
209 );
210 });
211 },
212);