The best way to control Claude Code, Codex and more from any computer or phone

main
ts 315 lines 11 KB
Raw
1 import * as GCP from "@alchemy.run/gcp";
2 import { GcpEnvironment } from "@alchemy.run/gcp/Environment";
3 import { describe, expect } from "@alchemy.run/test";
4 import * as Test from "@alchemy.run/test/Alchemy";
5 import * as Core from "@alchemy.run/test/Core";
6 import * as resourcemanager from "@distilled.cloud/gcp/cloudresourcemanager_v3";
7 import * as oracle from "@distilled.cloud/gcp/oracledatabase_v1";
8 import * as Effect from "effect/Effect";
9 import * as Schedule from "effect/Schedule";
10 import { expectProbe } from "../bindingHost.ts";
11 import OracleBindingsHost, {
12 AppDb,
13 Assign,
14 BaseDb,
15 Client,
16 Exa,
17 ExaVm,
18 OracleNet,
19 Replicat,
20 Src,
21 Vault,
22 Vms,
23 } from "./fixtures/bindings-host.ts";
24
25 const testOptions = { providers: GCP.providers() };
26 const { test, beforeAll, afterAll } = Test.make(testOptions);
27 const sharedStack = Core.scratchStack(testOptions, "OracleBindings");
28
29 // Oracle Database@Google Cloud is not enabled in the test project
30 // (ServiceDisabled) and needs an Oracle Cloud subscription. Set
31 // GCP_TEST_ORACLE=1 in a project that has one.
32 const runLifecycle = !!process.env.GCP_TEST_ORACLE && !process.env.FAST;
33
34 let baseUrl: string;
35 let hostAccount: string;
36 let names: Record<
37 | "network"
38 | "subnet"
39 | "database"
40 | "infra"
41 | "vmCluster"
42 | "dbSystem"
43 | "vault"
44 | "exadb"
45 | "connection"
46 | "deployment"
47 | "assignment",
48 string
49 >;
50
51 const ADB_ADMIN = "roles/oracledatabase.autonomousDatabaseAdmin";
52
53 /** Every role the host's bindings grant (project-level, unconditional). */
54 const expectedRoles = [
55 ADB_ADMIN,
56 "roles/oracledatabase.autonomousDatabaseViewer",
57 "roles/oracledatabase.cloudExadataInfrastructureViewer",
58 "roles/oracledatabase.cloudVmClusterViewer",
59 "roles/oracledatabase.dbSystemViewer",
60 "roles/oracledatabase.exadbVmClusterViewer",
61 "roles/oracledatabase.exascaleDbStorageVaultViewer",
62 "roles/oracledatabase.goldenGateConnectionAssignmentViewer",
63 "roles/oracledatabase.goldenGateConnectionViewer",
64 "roles/oracledatabase.goldenGateDeploymentViewer",
65 "roles/oracledatabase.odbNetworkViewer",
66 "roles/oracledatabase.odbSubnetViewer",
67 ].sort();
68
69 /**
70 * Oracle Database@Google Cloud has no resource-level IAM: each binding
71 * grants its role on the project, unconditionally.
72 */
73 const expectProjectGrant = (role: string) =>
74 Effect.gen(function* () {
75 const { project } = yield* GcpEnvironment.current;
76 const policy = yield* resourcemanager.getIamPolicyProjects({
77 resource: `projects/${project}`,
78 body: { options: { requestedPolicyVersion: 3 } },
79 });
80 const grants = (policy.bindings ?? []).filter((binding) =>
81 (binding.members ?? []).includes(`serviceAccount:${hostAccount}`),
82 );
83 expect(grants.every((binding) => binding.condition === undefined)).toBe(true);
84 const roles = grants.map((binding) => binding.role ?? "").sort();
85 expect(roles).toContain(role);
86 expect(roles).toEqual(expectedRoles);
87 });
88
89 /** Poll the autonomous database until it reaches `state` (bounded). */
90 const waitForDatabaseState = (state: string) =>
91 oracle.getProjectsLocationsAutonomousDatabases({ name: names.database }).pipe(
92 Effect.map((db) => db.properties?.state),
93 Effect.repeat({
94 schedule: Schedule.spaced("10 seconds"),
95 until: (current) => current === state,
96 times: 54,
97 }),
98 );
99
100 /** A read-only `Get*` binding: probe, compare with a direct read. */
101 const describeGet = <A extends { name?: string }>(options: {
102 binding: string;
103 route: string;
104 role: string;
105 name: () => string;
106 direct: (name: string) => Effect.Effect<A, { readonly _tag: string }, any>;
107 }) =>
108 describe(options.binding, () => {
109 test.provider(
110 "reads the resource as the host's service account",
111 (_stack) =>
112 Effect.gen(function* () {
113 const out = yield* expectProbe<A>(baseUrl, options.route);
114 const direct = yield* options.direct(options.name());
115 expect(out.name).toEqual(options.name());
116 expect(out.name).toEqual(direct.name);
117 yield* expectProjectGrant(options.role);
118 }),
119 {
120 tags: ["provider:gcp", "provider:gcp:oracledatabase", "live"],
121 timeout: 600_000,
122 },
123 );
124 });
125
126 describe.skipIf(!runLifecycle)(
127 "OracleDatabase Bindings",
128 {
129 tags: ["provider:gcp", "provider:gcp:oracledatabase", "provider:gcp:run", "live"],
130 },
131 () => {
132 beforeAll(
133 Effect.gen(function* () {
134 yield* sharedStack.destroy();
135 const out = yield* sharedStack.deploy(
136 Effect.gen(function* () {
137 const host = yield* OracleBindingsHost;
138 return {
139 uri: host.uri,
140 serviceAccount: host.serviceAccount,
141 network: (yield* OracleNet).name,
142 subnet: (yield* Client).name,
143 database: (yield* AppDb).name,
144 infra: (yield* Exa).name,
145 vmCluster: (yield* Vms).name,
146 dbSystem: (yield* BaseDb).name,
147 vault: (yield* Vault).name,
148 exadb: (yield* ExaVm).name,
149 connection: (yield* Src).name,
150 deployment: (yield* Replicat).name,
151 assignment: (yield* Assign).name,
152 };
153 }),
154 );
155 const { uri, serviceAccount, ...rest } = out;
156 baseUrl = uri!;
157 hostAccount = serviceAccount!;
158 names = rest;
159 }),
160 { timeout: 3_600_000 },
161 );
162
163 afterAll(sharedStack.destroy(), { timeout: 3_600_000 });
164
165 describeGet({
166 binding: "GetOdbNetwork",
167 route: "getOdbNetwork",
168 role: "roles/oracledatabase.odbNetworkViewer",
169 name: () => names.network,
170 direct: (name) => oracle.getProjectsLocationsOdbNetworks({ name }),
171 });
172 describeGet({
173 binding: "GetOdbNetworksOdbSubnet",
174 route: "getOdbNetworksOdbSubnet",
175 role: "roles/oracledatabase.odbSubnetViewer",
176 name: () => names.subnet,
177 direct: (name) => oracle.getProjectsLocationsOdbNetworksOdbSubnets({ name }),
178 });
179 describeGet({
180 binding: "GetAutonomousDatabase",
181 route: "getAutonomousDatabase",
182 role: "roles/oracledatabase.autonomousDatabaseViewer",
183 name: () => names.database,
184 direct: (name) => oracle.getProjectsLocationsAutonomousDatabases({ name }),
185 });
186 describeGet({
187 binding: "GetCloudExadataInfrastructure",
188 route: "getCloudExadataInfrastructure",
189 role: "roles/oracledatabase.cloudExadataInfrastructureViewer",
190 name: () => names.infra,
191 direct: (name) => oracle.getProjectsLocationsCloudExadataInfrastructures({ name }),
192 });
193 describeGet({
194 binding: "GetCloudVmCluster",
195 route: "getCloudVmCluster",
196 role: "roles/oracledatabase.cloudVmClusterViewer",
197 name: () => names.vmCluster,
198 direct: (name) => oracle.getProjectsLocationsCloudVmClusters({ name }),
199 });
200 describeGet({
201 binding: "GetDbSystem",
202 route: "getDbSystem",
203 role: "roles/oracledatabase.dbSystemViewer",
204 name: () => names.dbSystem,
205 direct: (name) => oracle.getProjectsLocationsDbSystems({ name }),
206 });
207 describeGet({
208 binding: "GetExascaleDbStorageVault",
209 route: "getExascaleDbStorageVault",
210 role: "roles/oracledatabase.exascaleDbStorageVaultViewer",
211 name: () => names.vault,
212 direct: (name) => oracle.getProjectsLocationsExascaleDbStorageVaults({ name }),
213 });
214 describeGet({
215 binding: "GetExadbVmCluster",
216 route: "getExadbVmCluster",
217 role: "roles/oracledatabase.exadbVmClusterViewer",
218 name: () => names.exadb,
219 direct: (name) => oracle.getProjectsLocationsExadbVmClusters({ name }),
220 });
221 describeGet({
222 binding: "GetGoldengateConnection",
223 route: "getGoldengateConnection",
224 role: "roles/oracledatabase.goldenGateConnectionViewer",
225 name: () => names.connection,
226 direct: (name) => oracle.getProjectsLocationsGoldengateConnections({ name }),
227 });
228 describeGet({
229 binding: "GetGoldengateDeployment",
230 route: "getGoldengateDeployment",
231 role: "roles/oracledatabase.goldenGateDeploymentViewer",
232 name: () => names.deployment,
233 direct: (name) => oracle.getProjectsLocationsGoldengateDeployments({ name }),
234 });
235 describeGet({
236 binding: "GetGoldengateConnectionAssignment",
237 route: "getGoldengateConnectionAssignment",
238 role: "roles/oracledatabase.goldenGateConnectionAssignmentViewer",
239 name: () => names.assignment,
240 direct: (name) => oracle.getProjectsLocationsGoldengateConnectionAssignments({ name }),
241 });
242
243 // Wallet → Stop → Start → Restart act on one database: run in order.
244 describe.sequential("AutonomousDatabase actions", () => {
245 describe("GenerateWallet", () => {
246 test.provider(
247 "generates the database wallet as the host's service account",
248 (_stack) =>
249 Effect.gen(function* () {
250 const out = yield* expectProbe<oracle.GenerateAutonomousDatabaseWalletResponse>(
251 baseUrl,
252 "generateWallet",
253 );
254 // The wallet is a base64 zip archive ("PK" magic).
255 const archive = Buffer.from(out.archiveContent ?? "", "base64");
256 expect(archive.subarray(0, 2).toString("latin1")).toEqual("PK");
257 yield* expectProjectGrant(ADB_ADMIN);
258 }),
259 {
260 tags: ["provider:gcp", "provider:gcp:oracledatabase", "live"],
261 timeout: 600_000,
262 },
263 );
264 });
265
266 describe("StopAutonomousDatabase", () => {
267 test.provider(
268 "stops the database as the host's service account",
269 (_stack) =>
270 Effect.gen(function* () {
271 yield* expectProbe(baseUrl, "stopAutonomousDatabase");
272 expect(yield* waitForDatabaseState("STOPPED")).toEqual("STOPPED");
273 yield* expectProjectGrant(ADB_ADMIN);
274 }),
275 {
276 tags: ["provider:gcp", "provider:gcp:oracledatabase", "live"],
277 timeout: 900_000,
278 },
279 );
280 });
281
282 describe("StartAutonomousDatabase", () => {
283 test.provider(
284 "starts the database as the host's service account",
285 (_stack) =>
286 Effect.gen(function* () {
287 yield* expectProbe(baseUrl, "startAutonomousDatabase");
288 expect(yield* waitForDatabaseState("AVAILABLE")).toEqual("AVAILABLE");
289 yield* expectProjectGrant(ADB_ADMIN);
290 }),
291 {
292 tags: ["provider:gcp", "provider:gcp:oracledatabase", "live"],
293 timeout: 900_000,
294 },
295 );
296 });
297
298 describe("RestartAutonomousDatabase", () => {
299 test.provider(
300 "restarts the database as the host's service account",
301 (_stack) =>
302 Effect.gen(function* () {
303 yield* expectProbe(baseUrl, "restartAutonomousDatabase");
304 expect(yield* waitForDatabaseState("AVAILABLE")).toEqual("AVAILABLE");
305 yield* expectProjectGrant(ADB_ADMIN);
306 }),
307 {
308 tags: ["provider:gcp", "provider:gcp:oracledatabase", "live"],
309 timeout: 900_000,
310 },
311 );
312 });
313 });
314 },
315 );