1import * as GCP from "@/GCP";
2import * as Test from "@/Test/Alchemy";
3import * as oracle from "@distilled.cloud/gcp/oracledatabase_v1";
4import { expect } from "alchemy-test";
5import * as Effect from "effect/Effect";
6import { MinimumLogLevel } from "effect/References";
7import * as Schedule from "effect/Schedule";
8import { GcpEnvironment } from "@/GCP/Environment";
9
10const { test } = Test.make({ providers: GCP.providers() });
11
12const logLevel = Effect.provideService(
13 MinimumLogLevel,
14 process.env.DEBUG ? "Debug" : "Info",
15);
16
17// Oracle Database@Google Cloud is not enabled in the test project
18// (ServiceDisabled) and needs an Oracle Cloud subscription. Set
19// GCP_TEST_ORACLE=1 in a project that has one.
20const runLifecycle = !!process.env.GCP_TEST_ORACLE && !process.env.FAST;
21
22const waitUntilGone = (name: string) =>
23 oracle.getProjectsLocationsAutonomousDatabases({ name }).pipe(
24 Effect.as("found" as const),
25 Effect.catchTag("NotFound", () => Effect.succeed("gone" as const)),
26 Effect.repeat({
27 schedule: Schedule.spaced("2 seconds"),
28 until: (status) => status === "gone",
29 times: 10,
30 }),
31 );
32
33test.provider.skipIf(runLifecycle)(
34 "getProjectsLocationsAutonomousDatabases without Oracle Database enabled fails with ServiceDisabled",
35 (stack) =>
36 Effect.gen(function* () {
37 const { project } = yield* GcpEnvironment.current;
38
39 yield* stack.destroy();
40
41 const error = yield* Effect.flip(
42 oracle.getProjectsLocationsAutonomousDatabases({
43 name: `projects/${project}/locations/us-central1/autonomousDatabases/alchemy-oracle-missing`,
44 }),
45 );
46 expect(error._tag).toEqual("ServiceDisabled");
47
48 yield* stack.destroy();
49 }).pipe(logLevel),
50 {
51 tags: ["provider:gcp", "provider:gcp:oracledatabase", "live"],
52 timeout: 90_000,
53 },
54);
55
56test.provider.skipIf(!runLifecycle)(
57 "create, update, and delete an autonomous database",
58 (stack) =>
59 Effect.gen(function* () {
60 const { project } = yield* GcpEnvironment.current;
61
62 yield* stack.destroy();
63
64 const created = yield* stack.deploy(
65 Effect.gen(function* () {
66 return yield* GCP.OracleDatabase.AutonomousDatabase("AppDb", {
67 location: "us-central1",
68 network: "default",
69 cidr: "10.10.0.0/24",
70 adminPassword: "AlchemyTest1!",
71 displayName: "alchemy-test-adb",
72 labels: { env: "test" },
73 licenseType: "LICENSE_INCLUDED",
74 dbWorkload: "OLTP",
75 cpuCoreCount: 2,
76 dataStorageSizeGb: 20,
77 });
78 }),
79 );
80
81 expect(created.name).toContain("/autonomousDatabases/");
82 expect(created.autonomousDatabaseId).toEqual(expect.any(String));
83 expect(created.location).toEqual("us-central1");
84 expect(created.labels).toMatchObject({ env: "test" });
85
86 const fetched = yield* oracle.getProjectsLocationsAutonomousDatabases({
87 name: created.name,
88 });
89 expect(fetched.name).toEqual(created.name);
90 expect(fetched.labels?.env).toEqual("test");
91
92 const updated = yield* stack.deploy(
93 Effect.gen(function* () {
94 return yield* GCP.OracleDatabase.AutonomousDatabase("AppDb", {
95 autonomousDatabaseId: created.autonomousDatabaseId,
96 location: "us-central1",
97 network: "default",
98 cidr: "10.10.0.0/24",
99 licenseType: "LICENSE_INCLUDED",
100 dbWorkload: "OLTP",
101 cpuCoreCount: 2,
102 dataStorageSizeGb: 20,
103 labels: { env: "prod", role: "db" },
104 });
105 }),
106 );
107
108 expect(updated.name).toEqual(created.name);
109 expect(updated.labels).toMatchObject({ env: "prod", role: "db" });
110
111 yield* stack.destroy();
112
113 const gone = yield* waitUntilGone(created.name);
114 expect(gone).toEqual("gone");
115 }).pipe(logLevel),
116 {
117 tags: ["provider:gcp", "provider:gcp:oracledatabase", "live"],
118 timeout: 120_000,
119 },
120);