| 1 | import * as GCP from "@/GCP"; |
| 2 | import type { StackServices } from "@/Stack"; |
| 3 | import * as Test from "@/Test/Alchemy"; |
| 4 | import * as sqladmin from "@distilled.cloud/gcp/sqladmin_v1"; |
| 5 | import { expect } from "alchemy-test"; |
| 6 | import * as Effect from "effect/Effect"; |
| 7 | import * as Layer from "effect/Layer"; |
| 8 | import { MinimumLogLevel } from "effect/References"; |
| 9 | import * as Schedule from "effect/Schedule"; |
| 10 | import { GcpEnvironment } from "@/GCP/Environment"; |
| 11 | |
| 12 | const { test } = Test.make({ |
| 13 | providers: GCP.providers() as Layer.Layer< |
| 14 | GCP.ProviderRequirements, |
| 15 | never, |
| 16 | StackServices |
| 17 | >, |
| 18 | }); |
| 19 | |
| 20 | const 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. |
| 27 | const sqlInstance = |
| 28 | process.env.GCP_SQL_INSTANCE || process.env.GCP_TEST_SQL_INSTANCE; |
| 29 | const runLifecycle = !!sqlInstance && !process.env.FAST; |
| 30 | |
| 31 | const waitUntilGone = ( |
| 32 | instance: string, |
| 33 | userName: string, |
| 34 | host: string | undefined, |
| 35 | ) => |
| 36 | GcpEnvironment.current.pipe( |
| 37 | Effect.flatMap(({ project }) => |
| 38 | sqladmin |
| 39 | .getUsers({ |
| 40 | project, |
| 41 | instance, |
| 42 | name: userName, |
| 43 | ...(host ? { host } : {}), |
| 44 | }) |
| 45 | .pipe( |
| 46 | Effect.as("found" as const), |
| 47 | Effect.catchTag("NotFound", () => Effect.succeed("gone" as const)), |
| 48 | Effect.repeat({ |
| 49 | schedule: Schedule.spaced("1 second"), |
| 50 | until: (status) => status === "gone", |
| 51 | times: 10, |
| 52 | }), |
| 53 | ), |
| 54 | ), |
| 55 | ); |
| 56 | |
| 57 | test.provider( |
| 58 | "getUsers on a missing instance fails with SqlInstanceNotAuthorized", |
| 59 | (stack) => |
| 60 | Effect.gen(function* () { |
| 61 | const { project } = yield* GcpEnvironment.current; |
| 62 | yield* stack.destroy(); |
| 63 | |
| 64 | const error = yield* Effect.flip( |
| 65 | sqladmin.getUsers({ |
| 66 | project, |
| 67 | instance: "alchemy-sql-instance-does-not-exist", |
| 68 | name: "alchemy_user_does_not_exist", |
| 69 | }), |
| 70 | ); |
| 71 | // Cloud SQL hides unknown instances behind 403 rather than 404. |
| 72 | expect(error._tag).toBe("SqlInstanceNotAuthorized"); |
| 73 | |
| 74 | yield* stack.destroy(); |
| 75 | }).pipe(logLevel), |
| 76 | { tags: ["provider:gcp", "provider:gcp:sql", "live"], timeout: 90_000 }, |
| 77 | ); |
| 78 | |
| 79 | test.provider( |
| 80 | "listUsers on a missing instance fails with SqlInstanceNotAuthorized", |
| 81 | (stack) => |
| 82 | Effect.gen(function* () { |
| 83 | const { project } = yield* GcpEnvironment.current; |
| 84 | yield* stack.destroy(); |
| 85 | |
| 86 | const error = yield* Effect.flip( |
| 87 | sqladmin.listUsers({ |
| 88 | project, |
| 89 | instance: "alchemy-sql-instance-does-not-exist", |
| 90 | }), |
| 91 | ); |
| 92 | expect(error._tag).toBe("SqlInstanceNotAuthorized"); |
| 93 | |
| 94 | yield* stack.destroy(); |
| 95 | }).pipe(logLevel), |
| 96 | { tags: ["provider:gcp", "provider:gcp:sql", "live"], timeout: 90_000 }, |
| 97 | ); |
| 98 | |
| 99 | test.provider.skipIf(!runLifecycle)( |
| 100 | "create, update, and delete a sql user", |
| 101 | (stack) => |
| 102 | Effect.gen(function* () { |
| 103 | const { project } = yield* GcpEnvironment.current; |
| 104 | yield* stack.destroy(); |
| 105 | |
| 106 | const instance = sqlInstance!; |
| 107 | const live = yield* sqladmin.getInstances({ |
| 108 | project, |
| 109 | instance, |
| 110 | }); |
| 111 | const isMysql = (live.databaseVersion ?? "") |
| 112 | .toUpperCase() |
| 113 | .startsWith("MYSQL"); |
| 114 | const host = isMysql ? "%" : undefined; |
| 115 | |
| 116 | const created = yield* stack.deploy( |
| 117 | Effect.gen(function* () { |
| 118 | return yield* GCP.SQL.User("AppUser", { |
| 119 | instance, |
| 120 | host, |
| 121 | password: "Alchemy-test-pass-1", |
| 122 | }); |
| 123 | }), |
| 124 | ); |
| 125 | |
| 126 | expect(created.userName).toEqual(expect.any(String)); |
| 127 | expect(created.userName).toMatch(/^[a-z][a-z0-9_]{0,31}$/); |
| 128 | expect(created.instance).toEqual(instance); |
| 129 | expect(created.project).toEqual(project); |
| 130 | if (isMysql) { |
| 131 | expect(created.host).toEqual("%"); |
| 132 | } |
| 133 | |
| 134 | const fetched = yield* sqladmin.getUsers({ |
| 135 | project: created.project, |
| 136 | instance: created.instance, |
| 137 | name: created.userName, |
| 138 | ...(created.host ? { host: created.host } : {}), |
| 139 | }); |
| 140 | expect(fetched.name).toEqual(created.userName); |
| 141 | expect(fetched.instance).toEqual(created.instance); |
| 142 | |
| 143 | const updated = yield* stack.deploy( |
| 144 | Effect.gen(function* () { |
| 145 | return yield* GCP.SQL.User("AppUser", { |
| 146 | instance, |
| 147 | userName: created.userName, |
| 148 | host: created.host, |
| 149 | password: "Alchemy-test-pass-2", |
| 150 | }); |
| 151 | }), |
| 152 | ); |
| 153 | |
| 154 | expect(updated.userName).toEqual(created.userName); |
| 155 | expect(updated.instance).toEqual(created.instance); |
| 156 | |
| 157 | const refetched = yield* sqladmin.getUsers({ |
| 158 | project: updated.project, |
| 159 | instance: updated.instance, |
| 160 | name: updated.userName, |
| 161 | ...(updated.host ? { host: updated.host } : {}), |
| 162 | }); |
| 163 | expect(refetched.name).toEqual(updated.userName); |
| 164 | |
| 165 | yield* stack.destroy(); |
| 166 | |
| 167 | const gone = yield* waitUntilGone( |
| 168 | created.instance, |
| 169 | created.userName, |
| 170 | created.host, |
| 171 | ); |
| 172 | expect(gone).toEqual("gone"); |
| 173 | }).pipe(logLevel), |
| 174 | { tags: ["provider:gcp", "provider:gcp:sql", "live"], timeout: 120_000 }, |
| 175 | ); |