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;
21const location = "us-central1";
22
23const waitUntilGone = (name: string) =>
24 oracle.getProjectsLocationsOdbNetworks({ name }).pipe(
25 Effect.as("found" as const),
26 Effect.catchTag("NotFound", () => Effect.succeed("gone" as const)),
27 Effect.repeat({
28 schedule: Schedule.spaced("2 seconds"),
29 until: (status) => status === "gone",
30 times: 10,
31 }),
32 );
33
34test.provider.skipIf(runLifecycle)(
35 "getProjectsLocationsOdbNetworks without Oracle Database enabled fails with ServiceDisabled",
36 (stack) =>
37 Effect.gen(function* () {
38 const { project } = yield* GcpEnvironment.current;
39
40 yield* stack.destroy();
41
42 const error = yield* Effect.flip(
43 oracle.getProjectsLocationsOdbNetworks({
44 name: `projects/${project}/locations/${location}/odbNetworks/alchemy-odb-missing`,
45 }),
46 );
47 expect(error._tag).toEqual("ServiceDisabled");
48
49 yield* stack.destroy();
50 }).pipe(logLevel),
51 {
52 tags: ["provider:gcp", "provider:gcp:oracledatabase", "live"],
53 timeout: 90_000,
54 },
55);
56
57test.provider.skipIf(!runLifecycle)(
58 "create, update, and delete an odb network",
59 (stack) =>
60 Effect.gen(function* () {
61 const { project } = yield* GcpEnvironment.current;
62
63 yield* stack.destroy();
64
65 const created = yield* stack.deploy(
66 Effect.gen(function* () {
67 return yield* GCP.OracleDatabase.OdbNetwork("OracleNet", {
68 location,
69 network: "default",
70 labels: { env: "test" },
71 });
72 }),
73 );
74
75 expect(created.name).toContain("/odbNetworks/");
76 expect(created.odbNetworkId).toEqual(expect.any(String));
77 expect(created.location).toEqual(location);
78 expect(created.network).toContain("/networks/");
79 expect(created.labels).toMatchObject({ env: "test" });
80
81 const fetched = yield* oracle.getProjectsLocationsOdbNetworks({
82 name: created.name,
83 });
84 expect(fetched.name).toEqual(created.name);
85 expect(fetched.labels?.env).toEqual("test");
86
87 const updated = yield* stack.deploy(
88 Effect.gen(function* () {
89 return yield* GCP.OracleDatabase.OdbNetwork("OracleNet", {
90 odbNetworkId: created.odbNetworkId,
91 location,
92 network: "default",
93 labels: { env: "prod", role: "oracle" },
94 });
95 }),
96 );
97
98 expect(updated.name).toEqual(created.name);
99 expect(updated.odbNetworkId).toEqual(created.odbNetworkId);
100
101 yield* stack.destroy();
102
103 const gone = yield* waitUntilGone(created.name);
104 expect(gone).toEqual("gone");
105 }).pipe(logLevel),
106 {
107 tags: ["provider:gcp", "provider:gcp:oracledatabase", "live"],
108 timeout: 120_000,
109 },
110);