The best way to control Claude Code, Codex and more from any computer or phone
| 1 | import * as GCP from "@alchemy.run/gcp"; |
| 2 | import { GcpEnvironment } from "@alchemy.run/gcp/Environment"; |
| 3 | import { expect } from "@alchemy.run/test"; |
| 4 | import * as Test from "@alchemy.run/test/Alchemy"; |
| 5 | import * as sqladmin from "@distilled.cloud/gcp/sqladmin_v1"; |
| 6 | import { DestroyError } from "alchemy/Apply"; |
| 7 | import type { StackServices } from "alchemy/Stack"; |
| 8 | import * as Cause from "effect/Cause"; |
| 9 | import * as Effect from "effect/Effect"; |
| 10 | import * as Layer from "effect/Layer"; |
| 11 | import { MinimumLogLevel } from "effect/References"; |
| 12 | import * as Schedule from "effect/Schedule"; |
| 13 | import { withNetworkSlot } from "../networkQuota.ts"; |
| 14 | |
| 15 | const { test } = Test.make({ |
| 16 | providers: GCP.providers() as Layer.Layer<GCP.ProviderRequirements, never, StackServices>, |
| 17 | }); |
| 18 | |
| 19 | const logLevel = Effect.provideService(MinimumLogLevel, process.env.DEBUG ? "Debug" : "Info"); |
| 20 | |
| 21 | // Cloud SQL instances take well over 5 minutes to provision. |
| 22 | const runLifecycle = !!process.env.GCP_TEST_SLOW && !process.env.FAST; |
| 23 | |
| 24 | const waitUntilGone = (project: string, instance: string) => |
| 25 | sqladmin.getInstances({ project, instance }).pipe( |
| 26 | Effect.as("found" as const), |
| 27 | Effect.catchTag("NotFound", () => Effect.succeed("gone" as const)), |
| 28 | Effect.repeat({ |
| 29 | schedule: Schedule.spaced("2 seconds"), |
| 30 | until: (status) => status === "gone", |
| 31 | times: 10, |
| 32 | }), |
| 33 | ); |
| 34 | |
| 35 | test.provider( |
| 36 | "lists sql instances and treats a missing instance as NotFound", |
| 37 | (stack) => |
| 38 | Effect.gen(function* () { |
| 39 | yield* stack.destroy(); |
| 40 | |
| 41 | const { project } = yield* GcpEnvironment.current; |
| 42 | const page = yield* sqladmin.listInstances({ |
| 43 | project, |
| 44 | maxResults: 10, |
| 45 | }); |
| 46 | expect((page.items ?? []).map((instance) => instance.name)).not.toContain( |
| 47 | "alchemy-sql-instance-does-not-exist", |
| 48 | ); |
| 49 | |
| 50 | const error = yield* Effect.flip( |
| 51 | sqladmin.getInstances({ |
| 52 | project, |
| 53 | instance: "alchemy-sql-instance-does-not-exist", |
| 54 | }), |
| 55 | ); |
| 56 | expect(error._tag).toBe("NotFound"); |
| 57 | |
| 58 | yield* stack.destroy(); |
| 59 | }).pipe(logLevel), |
| 60 | { tags: ["provider:gcp", "provider:gcp:sql", "live"], timeout: 90_000 }, |
| 61 | ); |
| 62 | |
| 63 | test.provider.skipIf(!runLifecycle)( |
| 64 | "create, update, and delete a sql instance", |
| 65 | (stack) => |
| 66 | Effect.gen(function* () { |
| 67 | yield* stack.destroy(); |
| 68 | |
| 69 | const created = yield* stack.deploy( |
| 70 | Effect.gen(function* () { |
| 71 | return yield* GCP.SQL.Instance("AppDb", { |
| 72 | region: "us-central1", |
| 73 | databaseVersion: "MYSQL_8_0", |
| 74 | tier: "db-f1-micro", |
| 75 | backup: { enabled: false }, |
| 76 | deletionProtectionEnabled: false, |
| 77 | labels: { env: "test" }, |
| 78 | }); |
| 79 | }), |
| 80 | ); |
| 81 | |
| 82 | expect(created.instanceName).toEqual(expect.any(String)); |
| 83 | expect(created.region).toEqual("us-central1"); |
| 84 | expect(created.databaseVersion).toEqual("MYSQL_8_0"); |
| 85 | expect(created.tier).toEqual("db-f1-micro"); |
| 86 | expect(created.labels).toMatchObject({ env: "test" }); |
| 87 | expect(created.deletionProtectionEnabled).toEqual(false); |
| 88 | expect(created.state).toEqual("RUNNABLE"); |
| 89 | |
| 90 | const fetched = yield* sqladmin.getInstances({ |
| 91 | project: created.project, |
| 92 | instance: created.instanceName, |
| 93 | }); |
| 94 | expect(fetched.name).toEqual(created.instanceName); |
| 95 | expect(fetched.settings?.userLabels?.env).toEqual("test"); |
| 96 | expect(fetched.settings?.tier).toEqual("db-f1-micro"); |
| 97 | |
| 98 | const updated = yield* stack.deploy( |
| 99 | Effect.gen(function* () { |
| 100 | return yield* GCP.SQL.Instance("AppDb", { |
| 101 | instanceName: created.instanceName, |
| 102 | region: "us-central1", |
| 103 | databaseVersion: "MYSQL_8_0", |
| 104 | tier: "db-f1-micro", |
| 105 | backup: { enabled: false }, |
| 106 | deletionProtectionEnabled: false, |
| 107 | labels: { env: "prod", role: "db" }, |
| 108 | }); |
| 109 | }), |
| 110 | ); |
| 111 | |
| 112 | expect(updated.instanceName).toEqual(created.instanceName); |
| 113 | expect(updated.labels).toMatchObject({ env: "prod", role: "db" }); |
| 114 | |
| 115 | const refetched = yield* sqladmin.getInstances({ |
| 116 | project: created.project, |
| 117 | instance: created.instanceName, |
| 118 | }); |
| 119 | expect(refetched.settings?.userLabels?.env).toEqual("prod"); |
| 120 | expect(refetched.settings?.userLabels?.role).toEqual("db"); |
| 121 | |
| 122 | yield* stack.destroy(); |
| 123 | |
| 124 | const gone = yield* waitUntilGone(created.project, created.instanceName); |
| 125 | expect(gone).toEqual("gone"); |
| 126 | }).pipe(logLevel), |
| 127 | // Create 5–15 minutes, label patch, delete a few minutes. |
| 128 | { |
| 129 | tags: ["provider:gcp", "provider:gcp:sql", "live"], |
| 130 | timeout: 2_400_000, |
| 131 | retry: 0, |
| 132 | }, |
| 133 | ); |
| 134 | |
| 135 | test.provider.skipIf(!runLifecycle)( |
| 136 | "upgrades databaseVersion in place within the same engine", |
| 137 | (stack) => |
| 138 | Effect.gen(function* () { |
| 139 | yield* stack.destroy(); |
| 140 | |
| 141 | const created = yield* stack.deploy( |
| 142 | Effect.gen(function* () { |
| 143 | return yield* GCP.SQL.Instance("UpgradeDb", { |
| 144 | region: "us-central1", |
| 145 | databaseVersion: "POSTGRES_16", |
| 146 | tier: "db-f1-micro", |
| 147 | backup: { enabled: false }, |
| 148 | deletionProtectionEnabled: false, |
| 149 | }); |
| 150 | }), |
| 151 | ); |
| 152 | expect(created.databaseVersion).toEqual("POSTGRES_16"); |
| 153 | |
| 154 | const upgraded = yield* stack.deploy( |
| 155 | Effect.gen(function* () { |
| 156 | return yield* GCP.SQL.Instance("UpgradeDb", { |
| 157 | region: "us-central1", |
| 158 | databaseVersion: "POSTGRES_17", |
| 159 | tier: "db-f1-micro", |
| 160 | backup: { enabled: false }, |
| 161 | deletionProtectionEnabled: false, |
| 162 | }); |
| 163 | }), |
| 164 | ); |
| 165 | |
| 166 | // Same physical instance: upgraded, not replaced. |
| 167 | expect(upgraded.instanceName).toEqual(created.instanceName); |
| 168 | expect(upgraded.createTime).toEqual(created.createTime); |
| 169 | expect(upgraded.databaseVersion).toEqual("POSTGRES_17"); |
| 170 | |
| 171 | const fetched = yield* sqladmin.getInstances({ |
| 172 | project: created.project, |
| 173 | instance: created.instanceName, |
| 174 | }); |
| 175 | expect(fetched.databaseVersion).toEqual("POSTGRES_17"); |
| 176 | |
| 177 | yield* stack.destroy(); |
| 178 | |
| 179 | const gone = yield* waitUntilGone(created.project, created.instanceName); |
| 180 | expect(gone).toEqual("gone"); |
| 181 | }).pipe(logLevel), |
| 182 | // Create 5–15 minutes, major version upgrade 33–54 minutes, delete a few minutes. |
| 183 | { |
| 184 | tags: ["provider:gcp", "provider:gcp:sql", "live"], |
| 185 | timeout: 7_200_000, |
| 186 | retry: 0, |
| 187 | }, |
| 188 | ); |
| 189 | |
| 190 | test.provider.skipIf(!runLifecycle)( |
| 191 | "destroy fails on a deletion-protected instance until protection is disabled", |
| 192 | (stack) => |
| 193 | Effect.gen(function* () { |
| 194 | yield* stack.destroy(); |
| 195 | |
| 196 | const created = yield* stack.deploy( |
| 197 | Effect.gen(function* () { |
| 198 | return yield* GCP.SQL.Instance("ProtectedDb", { |
| 199 | region: "us-central1", |
| 200 | databaseVersion: "MYSQL_8_0", |
| 201 | tier: "db-f1-micro", |
| 202 | backup: { enabled: false }, |
| 203 | deletionProtectionEnabled: true, |
| 204 | }); |
| 205 | }), |
| 206 | ); |
| 207 | expect(created.deletionProtectionEnabled).toEqual(true); |
| 208 | |
| 209 | const error = yield* Effect.flip(stack.destroy()); |
| 210 | expect(error).toBeInstanceOf(DestroyError); |
| 211 | const failures = error instanceof DestroyError ? error.failures : []; |
| 212 | expect(failures.map((failure) => failure.resourceType)).toEqual(["GCP.SQL.Instance"]); |
| 213 | expect(failures[0]?.cause.reasons.find(Cause.isFailReason)?.error).toBeInstanceOf( |
| 214 | GCP.SQL.InstanceDeletionProtected, |
| 215 | ); |
| 216 | |
| 217 | const stillThere = yield* sqladmin.getInstances({ |
| 218 | project: created.project, |
| 219 | instance: created.instanceName, |
| 220 | }); |
| 221 | expect(stillThere.settings?.deletionProtectionEnabled).toEqual(true); |
| 222 | |
| 223 | yield* stack.deploy( |
| 224 | Effect.gen(function* () { |
| 225 | return yield* GCP.SQL.Instance("ProtectedDb", { |
| 226 | region: "us-central1", |
| 227 | databaseVersion: "MYSQL_8_0", |
| 228 | tier: "db-f1-micro", |
| 229 | backup: { enabled: false }, |
| 230 | deletionProtectionEnabled: false, |
| 231 | }); |
| 232 | }), |
| 233 | ); |
| 234 | |
| 235 | yield* stack.destroy(); |
| 236 | |
| 237 | const gone = yield* waitUntilGone(created.project, created.instanceName); |
| 238 | expect(gone).toEqual("gone"); |
| 239 | }).pipe(logLevel), |
| 240 | // Create 5–15 minutes, protection patch, delete a few minutes. |
| 241 | { |
| 242 | tags: ["provider:gcp", "provider:gcp:sql", "live"], |
| 243 | timeout: 2_400_000, |
| 244 | retry: 0, |
| 245 | }, |
| 246 | ); |
| 247 | |
| 248 | test.provider.skipIf(!!process.env.FAST)( |
| 249 | "private ip, ssl mode, backup retention, and storage auto-resize converge in place", |
| 250 | (stack) => |
| 251 | Effect.gen(function* () { |
| 252 | yield* stack.destroy(); |
| 253 | |
| 254 | const privateDb = (settings: { |
| 255 | sslMode: string; |
| 256 | retainedBackups: number; |
| 257 | transactionLogRetentionDays: number; |
| 258 | storageAutoResize: boolean; |
| 259 | }) => |
| 260 | Effect.gen(function* () { |
| 261 | const network = yield* GCP.Compute.Network("SqlVpc", { |
| 262 | autoCreateSubnetworks: false, |
| 263 | }); |
| 264 | const range = yield* GCP.Compute.GlobalAddress("SqlPsaRange", { |
| 265 | addressType: "INTERNAL", |
| 266 | purpose: "VPC_PEERING", |
| 267 | network: network.selfLink, |
| 268 | prefixLength: 24, |
| 269 | }); |
| 270 | const psa = yield* GCP.ServiceNetworking.Connection("SqlPsa", { |
| 271 | network: network.networkName, |
| 272 | reservedPeeringRanges: [range.addressName], |
| 273 | }); |
| 274 | const db = yield* GCP.SQL.Instance("PrivateDb", { |
| 275 | region: "us-central1", |
| 276 | databaseVersion: "POSTGRES_17", |
| 277 | tier: "db-f1-micro", |
| 278 | deletionProtectionEnabled: false, |
| 279 | ipConfiguration: { |
| 280 | ipv4Enabled: false, |
| 281 | privateNetwork: psa.network, |
| 282 | allocatedIpRange: range.addressName, |
| 283 | sslMode: settings.sslMode, |
| 284 | }, |
| 285 | backup: { |
| 286 | enabled: true, |
| 287 | retainedBackups: settings.retainedBackups, |
| 288 | transactionLogRetentionDays: settings.transactionLogRetentionDays, |
| 289 | }, |
| 290 | storageAutoResize: settings.storageAutoResize, |
| 291 | }); |
| 292 | return { network, range, db }; |
| 293 | }); |
| 294 | |
| 295 | const created = yield* stack.deploy( |
| 296 | privateDb({ |
| 297 | sslMode: "ENCRYPTED_ONLY", |
| 298 | retainedBackups: 7, |
| 299 | transactionLogRetentionDays: 3, |
| 300 | storageAutoResize: true, |
| 301 | }), |
| 302 | ); |
| 303 | |
| 304 | expect(created.db.ipv4Enabled).toEqual(false); |
| 305 | expect(created.db.ipAddress).toBeUndefined(); |
| 306 | expect(created.db.privateIpAddress).toEqual(expect.any(String)); |
| 307 | expect(created.db.sslMode).toEqual("ENCRYPTED_ONLY"); |
| 308 | expect(created.db.backupEnabled).toEqual(true); |
| 309 | expect(created.db.retainedBackups).toEqual(7); |
| 310 | expect(created.db.transactionLogRetentionDays).toEqual(3); |
| 311 | expect(created.db.storageAutoResize).toEqual(true); |
| 312 | |
| 313 | const fetched = yield* sqladmin.getInstances({ |
| 314 | project: created.db.project, |
| 315 | instance: created.db.instanceName, |
| 316 | }); |
| 317 | expect((fetched.ipAddresses ?? []).map((address) => address.type)).toEqual(["PRIVATE"]); |
| 318 | expect(fetched.settings?.ipConfiguration?.ipv4Enabled).toEqual(false); |
| 319 | expect(fetched.settings?.ipConfiguration?.privateNetwork?.split("/").pop()).toEqual( |
| 320 | created.network.networkName, |
| 321 | ); |
| 322 | expect(fetched.settings?.ipConfiguration?.allocatedIpRange).toEqual( |
| 323 | created.range.addressName, |
| 324 | ); |
| 325 | expect(fetched.settings?.ipConfiguration?.sslMode).toEqual("ENCRYPTED_ONLY"); |
| 326 | expect(fetched.settings?.backupConfiguration?.enabled).toEqual(true); |
| 327 | expect(fetched.settings?.backupConfiguration?.pointInTimeRecoveryEnabled).toEqual(true); |
| 328 | expect( |
| 329 | fetched.settings?.backupConfiguration?.backupRetentionSettings?.retainedBackups, |
| 330 | ).toEqual(7); |
| 331 | expect(fetched.settings?.backupConfiguration?.transactionLogRetentionDays).toEqual(3); |
| 332 | expect(fetched.settings?.storageAutoResize).toEqual(true); |
| 333 | |
| 334 | // Redeploying identical props observes no drift and patches nothing. |
| 335 | const redeployed = yield* stack.deploy( |
| 336 | privateDb({ |
| 337 | sslMode: "ENCRYPTED_ONLY", |
| 338 | retainedBackups: 7, |
| 339 | transactionLogRetentionDays: 3, |
| 340 | storageAutoResize: true, |
| 341 | }), |
| 342 | ); |
| 343 | const unchanged = yield* sqladmin.getInstances({ |
| 344 | project: created.db.project, |
| 345 | instance: created.db.instanceName, |
| 346 | }); |
| 347 | expect(redeployed.db.instanceName).toEqual(created.db.instanceName); |
| 348 | expect(unchanged.settings?.settingsVersion).toEqual(fetched.settings?.settingsVersion); |
| 349 | |
| 350 | const updated = yield* stack.deploy( |
| 351 | privateDb({ |
| 352 | sslMode: "TRUSTED_CLIENT_CERTIFICATE_REQUIRED", |
| 353 | retainedBackups: 14, |
| 354 | transactionLogRetentionDays: 7, |
| 355 | storageAutoResize: false, |
| 356 | }), |
| 357 | ); |
| 358 | |
| 359 | // Same physical instance: patched, not replaced. |
| 360 | expect(updated.db.instanceName).toEqual(created.db.instanceName); |
| 361 | expect(updated.db.createTime).toEqual(created.db.createTime); |
| 362 | expect(updated.db.sslMode).toEqual("TRUSTED_CLIENT_CERTIFICATE_REQUIRED"); |
| 363 | expect(updated.db.retainedBackups).toEqual(14); |
| 364 | expect(updated.db.transactionLogRetentionDays).toEqual(7); |
| 365 | expect(updated.db.storageAutoResize).toEqual(false); |
| 366 | |
| 367 | const refetched = yield* sqladmin.getInstances({ |
| 368 | project: created.db.project, |
| 369 | instance: created.db.instanceName, |
| 370 | }); |
| 371 | expect(refetched.settings?.ipConfiguration?.sslMode).toEqual( |
| 372 | "TRUSTED_CLIENT_CERTIFICATE_REQUIRED", |
| 373 | ); |
| 374 | expect(refetched.settings?.ipConfiguration?.requireSsl).toEqual(true); |
| 375 | expect(refetched.settings?.ipConfiguration?.ipv4Enabled).toEqual(false); |
| 376 | expect(refetched.settings?.ipConfiguration?.privateNetwork?.split("/").pop()).toEqual( |
| 377 | created.network.networkName, |
| 378 | ); |
| 379 | expect( |
| 380 | refetched.settings?.backupConfiguration?.backupRetentionSettings?.retainedBackups, |
| 381 | ).toEqual(14); |
| 382 | expect(refetched.settings?.backupConfiguration?.transactionLogRetentionDays).toEqual(7); |
| 383 | expect(refetched.settings?.storageAutoResize).toEqual(false); |
| 384 | expect(refetched.settings?.tier).toEqual("db-f1-micro"); |
| 385 | |
| 386 | yield* stack.destroy(); |
| 387 | |
| 388 | const gone = yield* waitUntilGone(created.db.project, created.db.instanceName); |
| 389 | expect(gone).toEqual("gone"); |
| 390 | }).pipe(withNetworkSlot, logLevel), |
| 391 | // Create 5–15 minutes, settings patch a few minutes, delete a few minutes. |
| 392 | { |
| 393 | tags: ["provider:gcp", "provider:gcp:sql", "live"], |
| 394 | timeout: 2_400_000, |
| 395 | retry: 0, |
| 396 | }, |
| 397 | ); |