1import * as GCP from "@/GCP";
2import type { StackServices } from "@/Stack";
3import * as Test from "@/Test/Alchemy";
4import * as sqladmin from "@distilled.cloud/gcp/sqladmin_v1";
5import { expect } from "alchemy-test";
6import * as Effect from "effect/Effect";
7import * as Layer from "effect/Layer";
8import { MinimumLogLevel } from "effect/References";
9import * as Schedule from "effect/Schedule";
10import { GcpEnvironment } from "@/GCP/Environment";
11
12const { test } = Test.make({
13 providers: GCP.providers() as Layer.Layer<
14 GCP.ProviderRequirements,
15 never,
16 StackServices
17 >,
18});
19
20const logLevel = Effect.provideService(
21 MinimumLogLevel,
22 process.env.DEBUG ? "Debug" : "Info",
23);
24
25// Runs against an existing Cloud SQL instance (creating one takes well over
26// 5 minutes); set GCP_SQL_INSTANCE to its name.
27const sqlInstance =
28 process.env.GCP_SQL_INSTANCE || process.env.GCP_TEST_SQL_INSTANCE;
29const runLifecycle = !!sqlInstance && !process.env.FAST;
30
31const waitUntilGone = (instance: string, backupRunId: string) =>
32 GcpEnvironment.current.pipe(
33 Effect.flatMap(({ project }) =>
34 sqladmin
35 .getBackupRuns({
36 project,
37 instance,
38 id: backupRunId,
39 })
40 .pipe(
41 Effect.as("found" as const),
42 Effect.catchTag("NotFound", () => Effect.succeed("gone" as const)),
43 Effect.repeat({
44 schedule: Schedule.spaced("2 seconds"),
45 until: (status) => status === "gone",
46 times: 10,
47 }),
48 ),
49 ),
50 );
51
52test.provider(
53 "getBackupRuns on a missing instance fails with SqlInstanceNotAuthorized",
54 (stack) =>
55 Effect.gen(function* () {
56 const { project } = yield* GcpEnvironment.current;
57 yield* stack.destroy();
58
59 const error = yield* Effect.flip(
60 sqladmin.getBackupRuns({
61 project,
62 instance: "alchemy-sql-instance-does-not-exist",
63 id: "1",
64 }),
65 );
66 // Cloud SQL hides unknown instances behind 403 rather than 404.
67 expect(error._tag).toBe("SqlInstanceNotAuthorized");
68
69 yield* stack.destroy();
70 }).pipe(logLevel),
71 { tags: ["provider:gcp", "provider:gcp:sql", "live"], timeout: 90_000 },
72);
73
74test.provider(
75 "listBackupRuns on a missing instance fails with SqlInstanceNotAuthorized",
76 (stack) =>
77 Effect.gen(function* () {
78 const { project } = yield* GcpEnvironment.current;
79 yield* stack.destroy();
80
81 const error = yield* Effect.flip(
82 sqladmin.listBackupRuns({
83 project,
84 instance: "alchemy-sql-instance-does-not-exist",
85 maxResults: 10,
86 }),
87 );
88 expect(error._tag).toBe("SqlInstanceNotAuthorized");
89
90 yield* stack.destroy();
91 }).pipe(logLevel),
92 { tags: ["provider:gcp", "provider:gcp:sql", "live"], timeout: 90_000 },
93);
94
95test.provider.skipIf(!runLifecycle)(
96 "create, update, and delete a sql backup run",
97 (stack) =>
98 Effect.gen(function* () {
99 const { project } = yield* GcpEnvironment.current;
100 yield* stack.destroy();
101
102 const instance = sqlInstance!;
103
104 const created = yield* stack.deploy(
105 Effect.gen(function* () {
106 return yield* GCP.SQL.BackupRun("Nightly", {
107 instance,
108 description: "alchemy-on-demand",
109 });
110 }),
111 );
112
113 expect(created.backupRunId).toEqual(expect.any(String));
114 expect(created.backupRunId.length).toBeGreaterThan(0);
115 expect(created.instance).toEqual(instance);
116 expect(created.project).toEqual(project);
117 expect(created.description).toEqual("alchemy-on-demand");
118
119 const fetched = yield* sqladmin.getBackupRuns({
120 project: created.project,
121 instance: created.instance,
122 id: created.backupRunId,
123 });
124 expect(String(fetched.id)).toEqual(created.backupRunId);
125 expect(fetched.instance).toEqual(instance);
126 expect(fetched.description).toContain("alchemy-on-demand");
127
128 const updated = yield* stack.deploy(
129 Effect.gen(function* () {
130 return yield* GCP.SQL.BackupRun("Nightly", {
131 instance,
132 description: "alchemy-on-demand",
133 });
134 }),
135 );
136
137 expect(updated.backupRunId).toEqual(created.backupRunId);
138 expect(updated.instance).toEqual(created.instance);
139
140 const refetched = yield* sqladmin.getBackupRuns({
141 project: updated.project,
142 instance: updated.instance,
143 id: updated.backupRunId,
144 });
145 expect(String(refetched.id)).toEqual(updated.backupRunId);
146
147 yield* stack.destroy();
148
149 const gone = yield* waitUntilGone(created.instance, created.backupRunId);
150 expect(gone).toEqual("gone");
151 }).pipe(logLevel),
152 { tags: ["provider:gcp", "provider:gcp:sql", "live"], timeout: 120_000 },
153);