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.getProjectsLocationsOdbNetworksOdbSubnets({ 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 "getProjectsLocationsOdbNetworksOdbSubnets 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.getProjectsLocationsOdbNetworksOdbSubnets({
44 name: `projects/${project}/locations/${location}/odbNetworks/alchemy-odb-missing/odbSubnets/alchemy-subnet-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 subnet",
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 const net = yield* GCP.OracleDatabase.OdbNetwork("OracleNet", {
68 location,
69 network: "default",
70 labels: { env: "test" },
71 });
72 const subnet = yield* GCP.OracleDatabase.OdbNetworksOdbSubnet(
73 "Client",
74 {
75 odbNetwork: net.name,
76 location,
77 cidrRange: "10.250.0.0/27",
78 purpose: "CLIENT_SUBNET",
79 labels: { env: "test" },
80 },
81 );
82 return { net, subnet };
83 }),
84 );
85
86 expect(created.subnet.name).toContain("/odbSubnets/");
87 expect(created.subnet.odbSubnetId).toEqual(expect.any(String));
88 expect(created.subnet.odbNetwork).toEqual(created.net.name);
89 expect(created.subnet.cidrRange).toEqual("10.250.0.0/27");
90 expect(created.subnet.purpose).toEqual("CLIENT_SUBNET");
91 expect(created.subnet.labels).toMatchObject({ env: "test" });
92
93 const fetched = yield* oracle.getProjectsLocationsOdbNetworksOdbSubnets({
94 name: created.subnet.name,
95 });
96 expect(fetched.name).toEqual(created.subnet.name);
97 expect(fetched.cidrRange).toEqual("10.250.0.0/27");
98 expect(fetched.labels?.env).toEqual("test");
99
100 const updated = yield* stack.deploy(
101 Effect.gen(function* () {
102 const net = yield* GCP.OracleDatabase.OdbNetwork("OracleNet", {
103 odbNetworkId: created.net.odbNetworkId,
104 location,
105 network: "default",
106 labels: { env: "prod" },
107 });
108 const subnet = yield* GCP.OracleDatabase.OdbNetworksOdbSubnet(
109 "Client",
110 {
111 odbNetwork: net.name,
112 odbSubnetId: created.subnet.odbSubnetId,
113 location,
114 cidrRange: "10.250.0.0/27",
115 purpose: "CLIENT_SUBNET",
116 labels: { env: "prod", role: "client" },
117 },
118 );
119 return { net, subnet };
120 }),
121 );
122
123 expect(updated.subnet.name).toEqual(created.subnet.name);
124 expect(updated.subnet.odbSubnetId).toEqual(created.subnet.odbSubnetId);
125
126 yield* stack.destroy();
127
128 const gone = yield* waitUntilGone(created.subnet.name);
129 expect(gone).toEqual("gone");
130 }).pipe(logLevel),
131 {
132 tags: ["provider:gcp", "provider:gcp:oracledatabase", "live"],
133 timeout: 120_000,
134 },
135);