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 Core from "@alchemy.run/test/Core"; |
| 6 | import * as artifactregistry from "@distilled.cloud/gcp/artifactregistry_v1"; |
| 7 | import * as resourcemanager from "@distilled.cloud/gcp/cloudresourcemanager_v3"; |
| 8 | import * as pubsub from "@distilled.cloud/gcp/pubsub_v1"; |
| 9 | import * as cloudrun from "@distilled.cloud/gcp/run_v2"; |
| 10 | import * as storage from "@distilled.cloud/gcp/storage_v1"; |
| 11 | import * as iam from "@distilled.cloud/gcp/unstable/iam_v1"; |
| 12 | import * as Data from "effect/Data"; |
| 13 | import * as Effect from "effect/Effect"; |
| 14 | import * as Exit from "effect/Exit"; |
| 15 | import * as HttpClient from "effect/http/HttpClient"; |
| 16 | import { MinimumLogLevel } from "effect/References"; |
| 17 | import * as Schedule from "effect/Schedule"; |
| 18 | import { DataBucket, Tweets } from "./fixtures/bound-resources.ts"; |
| 19 | import Worker from "./fixtures/container-worker.ts"; |
| 20 | import EffectApiCaller from "./fixtures/effect-apis/caller.ts"; |
| 21 | import BoundContainerService from "./fixtures/service-bound-container.ts"; |
| 22 | import PublishOnlyService from "./fixtures/service-publish-only.ts"; |
| 23 | import BoundRedisService from "./fixtures/service-redis.ts"; |
| 24 | import SidecarService from "./fixtures/service-sidecar.ts"; |
| 25 | import UnboundContainerService from "./fixtures/service-unbound-container.ts"; |
| 26 | import BoundService from "./fixtures/service.ts"; |
| 27 | import { Static } from "./fixtures/sidecars.ts"; |
| 28 | |
| 29 | const { test } = Test.make({ providers: GCP.providers() }); |
| 30 | |
| 31 | const logLevel = Effect.provideService(MinimumLogLevel, process.env.DEBUG ? "Debug" : "Info"); |
| 32 | |
| 33 | const HELLO_IMAGE = "us-docker.pkg.dev/cloudrun/container/hello"; |
| 34 | |
| 35 | const waitUntilGone = (name: string) => |
| 36 | cloudrun.getProjectsLocationsServices({ name }).pipe( |
| 37 | Effect.as("found" as const), |
| 38 | Effect.catchTag("NotFound", () => Effect.succeed("gone" as const)), |
| 39 | Effect.repeat({ |
| 40 | schedule: Schedule.spaced("2 seconds"), |
| 41 | until: (status) => status === "gone", |
| 42 | times: 10, |
| 43 | }), |
| 44 | ); |
| 45 | |
| 46 | test.provider( |
| 47 | "create, update, and delete a Cloud Run service", |
| 48 | (stack) => |
| 49 | Effect.gen(function* () { |
| 50 | yield* stack.destroy(); |
| 51 | |
| 52 | const created = yield* stack.deploy( |
| 53 | Effect.gen(function* () { |
| 54 | return yield* GCP.Run.Service("Api", { |
| 55 | location: "us-central1", |
| 56 | description: "test run service", |
| 57 | labels: { env: "test" }, |
| 58 | template: { |
| 59 | containers: [{ image: HELLO_IMAGE }], |
| 60 | }, |
| 61 | }); |
| 62 | }), |
| 63 | ); |
| 64 | |
| 65 | expect(created.name).toContain("/services/"); |
| 66 | expect(created.serviceId).toEqual(expect.any(String)); |
| 67 | expect(created.location).toEqual("us-central1"); |
| 68 | expect(created.description).toEqual("test run service"); |
| 69 | expect(created.labels).toMatchObject({ env: "test" }); |
| 70 | expect(created.uri).toEqual(expect.any(String)); |
| 71 | expect(created.terminalConditionState).toEqual("CONDITION_SUCCEEDED"); |
| 72 | |
| 73 | const fetched = yield* cloudrun.getProjectsLocationsServices({ |
| 74 | name: created.name, |
| 75 | }); |
| 76 | expect(fetched.name).toEqual(created.name); |
| 77 | expect(fetched.labels?.env).toEqual("test"); |
| 78 | expect(fetched.description).toEqual("test run service"); |
| 79 | expect(fetched.template?.containers?.[0]?.image).toEqual(HELLO_IMAGE); |
| 80 | expect(fetched.uri).toEqual(created.uri); |
| 81 | |
| 82 | const updated = yield* stack.deploy( |
| 83 | Effect.gen(function* () { |
| 84 | return yield* GCP.Run.Service("Api", { |
| 85 | serviceId: created.serviceId, |
| 86 | location: "us-central1", |
| 87 | description: "prod run service", |
| 88 | labels: { env: "prod", role: "api" }, |
| 89 | ingress: "INGRESS_TRAFFIC_INTERNAL_ONLY", |
| 90 | template: { |
| 91 | timeout: "60s", |
| 92 | containers: [ |
| 93 | { |
| 94 | image: HELLO_IMAGE, |
| 95 | env: [{ name: "ENV", value: "prod" }], |
| 96 | }, |
| 97 | ], |
| 98 | }, |
| 99 | }); |
| 100 | }), |
| 101 | ); |
| 102 | |
| 103 | expect(updated.name).toEqual(created.name); |
| 104 | expect(updated.description).toEqual("prod run service"); |
| 105 | expect(updated.labels).toMatchObject({ env: "prod", role: "api" }); |
| 106 | expect(updated.ingress).toEqual("INGRESS_TRAFFIC_INTERNAL_ONLY"); |
| 107 | expect(updated.uri).toEqual(expect.any(String)); |
| 108 | |
| 109 | const refetched = yield* cloudrun.getProjectsLocationsServices({ |
| 110 | name: created.name, |
| 111 | }); |
| 112 | expect(refetched.description).toEqual("prod run service"); |
| 113 | expect(refetched.labels?.env).toEqual("prod"); |
| 114 | expect(refetched.labels?.role).toEqual("api"); |
| 115 | expect(refetched.ingress).toEqual("INGRESS_TRAFFIC_INTERNAL_ONLY"); |
| 116 | expect(refetched.template?.timeout).toEqual("60s"); |
| 117 | expect( |
| 118 | refetched.template?.containers?.[0]?.env?.some( |
| 119 | (env) => env.name === "ENV" && env.value === "prod", |
| 120 | ), |
| 121 | ).toEqual(true); |
| 122 | |
| 123 | yield* stack.destroy(); |
| 124 | |
| 125 | const gone = yield* waitUntilGone(created.name); |
| 126 | expect(gone).toEqual("gone"); |
| 127 | }).pipe(logLevel), |
| 128 | { tags: ["provider:gcp", "provider:gcp:run", "live"], timeout: 120_000 }, |
| 129 | ); |
| 130 | |
| 131 | class ServiceNotReady extends Data.TaggedError("ServiceNotReady")<{ |
| 132 | status: number; |
| 133 | }> {} |
| 134 | |
| 135 | const membersOf = ( |
| 136 | policy: { |
| 137 | bindings?: ReadonlyArray<{ |
| 138 | role?: string; |
| 139 | members?: ReadonlyArray<string>; |
| 140 | }>; |
| 141 | }, |
| 142 | member: string, |
| 143 | ) => |
| 144 | new Set( |
| 145 | (policy.bindings ?? []) |
| 146 | .filter((binding) => (binding.members ?? []).includes(member)) |
| 147 | .map((binding) => binding.role), |
| 148 | ); |
| 149 | |
| 150 | const fetchJson = <A>(url: string) => |
| 151 | Effect.gen(function* () { |
| 152 | const client = yield* HttpClient.HttpClient; |
| 153 | const response = yield* client.get(url).pipe( |
| 154 | Effect.flatMap((response) => |
| 155 | response.status === 200 |
| 156 | ? Effect.succeed(response) |
| 157 | : Effect.fail(new ServiceNotReady({ status: response.status })), |
| 158 | ), |
| 159 | Effect.retry({ |
| 160 | while: (e): e is ServiceNotReady => e._tag === "ServiceNotReady", |
| 161 | schedule: Schedule.exponential("500 millis"), |
| 162 | times: 10, |
| 163 | }), |
| 164 | ); |
| 165 | return (yield* response.json) as A; |
| 166 | }); |
| 167 | |
| 168 | const NGINX_IMAGE = "docker.io/library/nginx:1.27-alpine"; |
| 169 | |
| 170 | const latestRevision = (service: { name: string; latestReadyRevision: string | undefined }) => |
| 171 | cloudrun.getProjectsLocationsServicesRevisions({ |
| 172 | name: `${service.name}/revisions/${service.latestReadyRevision?.split("/").pop()}`, |
| 173 | }); |
| 174 | |
| 175 | const probedContainers = ( |
| 176 | probe: { periodSeconds: number } | undefined, |
| 177 | ): GCP.Run.ContainerSpec[] => [ |
| 178 | { |
| 179 | name: "proxy", |
| 180 | image: HELLO_IMAGE, |
| 181 | ports: [{ name: "http1", containerPort: 8080 }], |
| 182 | ...(probe ? { dependsOn: ["app"] } : {}), |
| 183 | }, |
| 184 | { |
| 185 | name: "app", |
| 186 | image: NGINX_IMAGE, |
| 187 | ...(probe |
| 188 | ? { |
| 189 | startupProbe: { |
| 190 | httpGet: { path: "/", port: 80 }, |
| 191 | periodSeconds: probe.periodSeconds, |
| 192 | failureThreshold: 30, |
| 193 | }, |
| 194 | livenessProbe: { httpGet: { path: "/", port: 80 }, periodSeconds: 10 }, |
| 195 | } |
| 196 | : {}), |
| 197 | }, |
| 198 | ]; |
| 199 | |
| 200 | test.provider( |
| 201 | "startup probes, liveness probes, and dependsOn roll new revisions", |
| 202 | (stack) => |
| 203 | Effect.gen(function* () { |
| 204 | yield* stack.destroy(); |
| 205 | |
| 206 | const deploy = (probe: { periodSeconds: number } | undefined) => |
| 207 | stack.deploy( |
| 208 | Effect.gen(function* () { |
| 209 | return yield* GCP.Run.Service("Probed", { |
| 210 | location: "us-central1", |
| 211 | template: { containers: probedContainers(probe) }, |
| 212 | }); |
| 213 | }), |
| 214 | ); |
| 215 | |
| 216 | const created = yield* deploy({ periodSeconds: 2 }); |
| 217 | const first = yield* latestRevision(created); |
| 218 | const [proxy, app] = first.containers ?? []; |
| 219 | expect(proxy?.dependsOn).toEqual(["app"]); |
| 220 | expect(app?.startupProbe).toMatchObject({ |
| 221 | httpGet: { path: "/", port: 80 }, |
| 222 | periodSeconds: 2, |
| 223 | failureThreshold: 30, |
| 224 | }); |
| 225 | expect(app?.livenessProbe).toMatchObject({ |
| 226 | httpGet: { path: "/", port: 80 }, |
| 227 | periodSeconds: 10, |
| 228 | }); |
| 229 | |
| 230 | const retimed = yield* deploy({ periodSeconds: 5 }); |
| 231 | expect(retimed.latestReadyRevision).not.toEqual(created.latestReadyRevision); |
| 232 | const second = yield* latestRevision(retimed); |
| 233 | expect(second.containers?.[1]?.startupProbe).toMatchObject({ |
| 234 | httpGet: { path: "/", port: 80 }, |
| 235 | periodSeconds: 5, |
| 236 | }); |
| 237 | |
| 238 | // Dropping the probes and dependsOn resets them to Cloud Run's defaults. |
| 239 | const reset = yield* deploy(undefined); |
| 240 | expect(reset.latestReadyRevision).not.toEqual(retimed.latestReadyRevision); |
| 241 | const third = yield* latestRevision(reset); |
| 242 | expect(third.containers?.[0]?.dependsOn ?? []).toEqual([]); |
| 243 | expect(third.containers?.[1]?.startupProbe?.httpGet).toBeUndefined(); |
| 244 | expect(third.containers?.[1]?.livenessProbe).toBeUndefined(); |
| 245 | |
| 246 | // A redeploy of the reset props is a no-op, not another revision. |
| 247 | const again = yield* deploy(undefined); |
| 248 | expect(again.latestReadyRevision).toEqual(reset.latestReadyRevision); |
| 249 | |
| 250 | yield* stack.destroy(); |
| 251 | expect(yield* waitUntilGone(created.name)).toEqual("gone"); |
| 252 | }).pipe(logLevel), |
| 253 | { tags: ["provider:gcp", "provider:gcp:run", "live"], timeout: 600_000 }, |
| 254 | ); |
| 255 | |
| 256 | test.provider( |
| 257 | "an Effect-native service keeps its sidecars and probes", |
| 258 | (stack) => |
| 259 | Effect.gen(function* () { |
| 260 | yield* stack.destroy(); |
| 261 | |
| 262 | const deployed = yield* stack.deploy( |
| 263 | Effect.gen(function* () { |
| 264 | return yield* SidecarService; |
| 265 | }), |
| 266 | ); |
| 267 | |
| 268 | const revision = yield* latestRevision(deployed); |
| 269 | const [main, sidecar] = revision.containers ?? []; |
| 270 | expect(revision.containers?.length).toEqual(2); |
| 271 | expect(main?.dependsOn).toEqual(["sidecar"]); |
| 272 | expect(main?.startupProbe).toMatchObject({ |
| 273 | httpGet: { path: "/health", port: 8080 }, |
| 274 | periodSeconds: 2, |
| 275 | }); |
| 276 | expect(sidecar?.name).toEqual("sidecar"); |
| 277 | expect(sidecar?.image).toContain("nginx"); |
| 278 | |
| 279 | const client = yield* HttpClient.HttpClient; |
| 280 | const health = yield* client.get(`${deployed.uri}/health`).pipe( |
| 281 | Effect.flatMap((response) => |
| 282 | response.status === 200 |
| 283 | ? Effect.succeed(response) |
| 284 | : Effect.fail(new ServiceNotReady({ status: response.status })), |
| 285 | ), |
| 286 | Effect.flatMap((response) => response.text), |
| 287 | Effect.retry({ schedule: Schedule.exponential("500 millis"), times: 8 }), |
| 288 | ); |
| 289 | expect(health).toEqual("ok"); |
| 290 | |
| 291 | yield* stack.destroy(); |
| 292 | expect(yield* waitUntilGone(deployed.name)).toEqual("gone"); |
| 293 | }).pipe(logLevel), |
| 294 | { tags: ["provider:gcp", "provider:gcp:run", "live"], timeout: 900_000 }, |
| 295 | ); |
| 296 | |
| 297 | test.provider( |
| 298 | "a bound Container runs as a sidecar and answers RPC over localhost", |
| 299 | (stack) => |
| 300 | Effect.gen(function* () { |
| 301 | const { project } = yield* GcpEnvironment.current; |
| 302 | yield* stack.destroy(); |
| 303 | |
| 304 | const { service: deployed, worker } = yield* stack.deploy( |
| 305 | Effect.gen(function* () { |
| 306 | return { service: yield* BoundContainerService, worker: yield* Worker }; |
| 307 | }), |
| 308 | ); |
| 309 | expect(worker.repository).toEqual(expect.any(String)); |
| 310 | |
| 311 | // Api depends on Worker, which depends on Static: one revision, three containers. |
| 312 | const revision = yield* latestRevision(deployed); |
| 313 | const byName = new Map((revision.containers ?? []).map((c) => [c.name ?? "", c])); |
| 314 | expect(revision.containers?.length).toEqual(3); |
| 315 | expect(revision.containers?.[0]?.dependsOn).toEqual(["worker"]); |
| 316 | expect(revision.containers?.[0]?.startupProbe).toMatchObject({ |
| 317 | httpGet: { path: "/health", port: 8080 }, |
| 318 | }); |
| 319 | expect(byName.get("worker")?.dependsOn).toEqual(["static"]); |
| 320 | expect(byName.get("worker")?.startupProbe).toMatchObject({ |
| 321 | httpGet: { path: "/health", port: 3000 }, |
| 322 | }); |
| 323 | expect(byName.get("static")?.startupProbe).toMatchObject({ tcpSocket: { port: 80 } }); |
| 324 | |
| 325 | const client = yield* HttpClient.HttpClient; |
| 326 | const call = yield* client.get(`${deployed.uri}/call`).pipe( |
| 327 | Effect.flatMap((response) => |
| 328 | response.text.pipe(Effect.map((text) => ({ status: response.status, text }))), |
| 329 | ), |
| 330 | Effect.repeat({ |
| 331 | schedule: Schedule.spaced("2 seconds"), |
| 332 | until: (response) => response.status !== 404 && response.status < 502, |
| 333 | times: 15, |
| 334 | }), |
| 335 | ); |
| 336 | expect(call.text).not.toContain("Error"); |
| 337 | expect(call.status).toEqual(200); |
| 338 | const body = yield* fetchJson<{ |
| 339 | greeting: string; |
| 340 | counts: number[]; |
| 341 | published: number; |
| 342 | staticStatus: number; |
| 343 | health: string; |
| 344 | }>(`${deployed.uri}/call`); |
| 345 | expect(body).toEqual({ |
| 346 | greeting: "hello sam", |
| 347 | counts: [1, 2, 3], |
| 348 | published: 1, |
| 349 | staticStatus: 200, |
| 350 | health: "worker ok", |
| 351 | }); |
| 352 | |
| 353 | // Removing the binding drops the sidecars from the next revision. |
| 354 | const unbound = yield* stack.deploy( |
| 355 | Effect.gen(function* () { |
| 356 | return yield* UnboundContainerService; |
| 357 | }), |
| 358 | ); |
| 359 | const solo = yield* latestRevision(unbound); |
| 360 | expect(solo.containers?.length).toEqual(1); |
| 361 | expect(solo.containers?.[0]?.dependsOn ?? []).toEqual([]); |
| 362 | |
| 363 | yield* stack.destroy(); |
| 364 | expect(yield* waitUntilGone(deployed.name)).toEqual("gone"); |
| 365 | // The sidecar's image repository went with it. |
| 366 | const repository = yield* artifactregistry |
| 367 | .getProjectsLocationsRepositories({ |
| 368 | name: `projects/${project}/locations/us-central1/repositories/${worker.repository}`, |
| 369 | }) |
| 370 | .pipe( |
| 371 | Effect.as("found" as const), |
| 372 | Effect.catchTag("NotFound", () => Effect.succeed("gone" as const)), |
| 373 | ); |
| 374 | expect(repository).toEqual("gone"); |
| 375 | }).pipe(logLevel), |
| 376 | { tags: ["provider:gcp", "provider:gcp:run", "live"], timeout: 900_000 }, |
| 377 | ); |
| 378 | |
| 379 | test.provider( |
| 380 | "an image-only Service lists a Container in its template", |
| 381 | (stack) => |
| 382 | Effect.gen(function* () { |
| 383 | yield* stack.destroy(); |
| 384 | |
| 385 | const deploy = (withWorker: boolean) => |
| 386 | stack.deploy( |
| 387 | Effect.gen(function* () { |
| 388 | const worker = yield* Worker; |
| 389 | const service = yield* GCP.Run.Service("ImageWithContainer", { |
| 390 | location: "us-central1", |
| 391 | template: { |
| 392 | containers: withWorker |
| 393 | ? [ |
| 394 | { |
| 395 | image: HELLO_IMAGE, |
| 396 | ports: [{ name: "http1", containerPort: 8080 }], |
| 397 | dependsOn: [worker.name], |
| 398 | }, |
| 399 | worker, |
| 400 | ] |
| 401 | : [{ image: HELLO_IMAGE, ports: [{ name: "http1", containerPort: 8080 }] }], |
| 402 | }, |
| 403 | }); |
| 404 | return { service, topic: (yield* Tweets).name }; |
| 405 | }), |
| 406 | ); |
| 407 | |
| 408 | const { service, topic } = yield* deploy(true); |
| 409 | // Worker joins the revision, and so does the Static sidecar it binds. |
| 410 | const revision = yield* latestRevision(service); |
| 411 | const byName = new Map((revision.containers ?? []).map((c) => [c.name ?? "", c])); |
| 412 | expect(revision.containers?.length).toEqual(3); |
| 413 | expect(revision.containers?.[0]?.dependsOn).toEqual(["worker"]); |
| 414 | expect(byName.get("worker")?.dependsOn).toEqual(["static"]); |
| 415 | expect(byName.has("static")).toEqual(true); |
| 416 | |
| 417 | // Worker's Pub/Sub binding is granted to the service's account. |
| 418 | expect(service.managedServiceAccount).toEqual(true); |
| 419 | const topicPolicy = yield* pubsub.getIamPolicyProjectsTopics({ resource: topic }); |
| 420 | expect([...membersOf(topicPolicy, `serviceAccount:${service.serviceAccount}`)]).toEqual([ |
| 421 | "roles/pubsub.publisher", |
| 422 | ]); |
| 423 | |
| 424 | // Dropping Worker from the list drops its sidecars too. |
| 425 | const { service: solo } = yield* deploy(false); |
| 426 | const soloRevision = yield* latestRevision(solo); |
| 427 | expect(soloRevision.containers?.length).toEqual(1); |
| 428 | |
| 429 | yield* stack.destroy(); |
| 430 | expect(yield* waitUntilGone(service.name)).toEqual("gone"); |
| 431 | }).pipe(logLevel), |
| 432 | { tags: ["provider:gcp", "provider:gcp:run", "live"], timeout: 900_000 }, |
| 433 | ); |
| 434 | |
| 435 | test.provider( |
| 436 | "a listed Container can depend on another listed Container's name", |
| 437 | (stack) => |
| 438 | Effect.gen(function* () { |
| 439 | yield* stack.destroy(); |
| 440 | |
| 441 | const service = yield* stack.deploy( |
| 442 | Effect.gen(function* () { |
| 443 | const web = yield* Static; |
| 444 | const cache = yield* GCP.Run.Container("Cache", { |
| 445 | image: "docker.io/library/redis:7-alpine", |
| 446 | port: 6379, |
| 447 | startupProbe: { tcpSocket: {}, periodSeconds: 2, failureThreshold: 30 }, |
| 448 | dependsOn: [web.name], |
| 449 | }); |
| 450 | return yield* GCP.Run.Service("ListedContainers", { |
| 451 | location: "us-central1", |
| 452 | template: { |
| 453 | containers: [ |
| 454 | { |
| 455 | image: HELLO_IMAGE, |
| 456 | ports: [{ name: "http1", containerPort: 8080 }], |
| 457 | dependsOn: [cache.name], |
| 458 | }, |
| 459 | cache, |
| 460 | web, |
| 461 | ], |
| 462 | }, |
| 463 | }); |
| 464 | }), |
| 465 | ); |
| 466 | |
| 467 | // The names resolve, so the revision starts static → cache → the ingress. |
| 468 | const revision = yield* latestRevision(service); |
| 469 | const byName = new Map((revision.containers ?? []).map((c) => [c.name ?? "", c])); |
| 470 | expect(revision.containers?.length).toEqual(3); |
| 471 | expect(revision.containers?.[0]?.dependsOn).toEqual(["cache"]); |
| 472 | expect(byName.get("cache")?.dependsOn).toEqual(["static"]); |
| 473 | expect(byName.get("static")?.dependsOn ?? []).toEqual([]); |
| 474 | |
| 475 | yield* stack.destroy(); |
| 476 | expect(yield* waitUntilGone(service.name)).toEqual("gone"); |
| 477 | }).pipe(logLevel), |
| 478 | { tags: ["provider:gcp", "provider:gcp:run", "live"], timeout: 900_000 }, |
| 479 | ); |
| 480 | |
| 481 | test.provider( |
| 482 | "typed Effect RPC and HttpApi clients reach sidecars and private services", |
| 483 | (stack) => |
| 484 | Effect.gen(function* () { |
| 485 | yield* stack.destroy(); |
| 486 | |
| 487 | const caller = yield* stack.deploy( |
| 488 | Effect.gen(function* () { |
| 489 | return yield* EffectApiCaller; |
| 490 | }), |
| 491 | ); |
| 492 | |
| 493 | const client = yield* HttpClient.HttpClient; |
| 494 | // A fresh run.invoker grant takes a moment to reach the backends. |
| 495 | const call = yield* client.get(`${caller.uri}/`).pipe( |
| 496 | Effect.flatMap((response) => |
| 497 | response.text.pipe(Effect.map((text) => ({ status: response.status, text }))), |
| 498 | ), |
| 499 | Effect.repeat({ |
| 500 | schedule: Schedule.spaced("5 seconds"), |
| 501 | // Each call reports its own failure in the body. |
| 502 | until: (response) => response.status === 200 && !response.text.includes('"failed"'), |
| 503 | times: 24, |
| 504 | }), |
| 505 | ); |
| 506 | expect(call.status).toEqual(200); |
| 507 | expect(JSON.parse(call.text)).toEqual({ |
| 508 | rpcSidecar: { task: "from rpc sidecar", missing: "TaskNotFound", counts: [1, 2, 3] }, |
| 509 | apiSidecar: { task: "from api sidecar", missing: "TaskNotFound" }, |
| 510 | rpcService: { task: "from rpc service", missing: "TaskNotFound", counts: [1, 2, 3] }, |
| 511 | apiService: { task: "from api service", missing: "TaskNotFound" }, |
| 512 | }); |
| 513 | |
| 514 | yield* stack.destroy(); |
| 515 | expect(yield* waitUntilGone(caller.name)).toEqual("gone"); |
| 516 | }).pipe(logLevel), |
| 517 | { tags: ["provider:gcp", "provider:gcp:run", "live"], timeout: 900_000 }, |
| 518 | ); |
| 519 | |
| 520 | test.provider( |
| 521 | "effect-native Function grants resource-scoped IAM and revokes it when a binding is removed", |
| 522 | (stack) => |
| 523 | Effect.gen(function* () { |
| 524 | yield* stack.destroy(); |
| 525 | |
| 526 | const deployed = (program: typeof BoundService | typeof PublishOnlyService) => |
| 527 | stack.deploy( |
| 528 | Effect.gen(function* () { |
| 529 | const service = yield* program; |
| 530 | const topic = yield* Tweets; |
| 531 | const bucket = yield* DataBucket; |
| 532 | return { |
| 533 | uri: service.uri, |
| 534 | name: service.name, |
| 535 | project: service.project, |
| 536 | serviceAccount: service.serviceAccount, |
| 537 | managedServiceAccount: service.managedServiceAccount, |
| 538 | topic: topic.name, |
| 539 | bucket: bucket.bucketName, |
| 540 | }; |
| 541 | }), |
| 542 | ); |
| 543 | |
| 544 | const out = yield* deployed(BoundService); |
| 545 | expect(out.managedServiceAccount).toEqual(true); |
| 546 | expect(out.serviceAccount ?? "").toMatch(/^alch-/); |
| 547 | const member = `serviceAccount:${out.serviceAccount}`; |
| 548 | |
| 549 | const live = yield* cloudrun.getProjectsLocationsServices({ |
| 550 | name: out.name, |
| 551 | }); |
| 552 | expect(live.template?.serviceAccount).toEqual(out.serviceAccount); |
| 553 | |
| 554 | // Grants land on the bound resources, never on the project. |
| 555 | const projectPolicy = yield* resourcemanager.getIamPolicyProjects({ |
| 556 | resource: `projects/${out.project}`, |
| 557 | }); |
| 558 | expect([...membersOf(projectPolicy, member)]).toEqual([]); |
| 559 | const topicPolicy = yield* pubsub.getIamPolicyProjectsTopics({ |
| 560 | resource: out.topic, |
| 561 | }); |
| 562 | expect([...membersOf(topicPolicy, member)]).toEqual(["roles/pubsub.publisher"]); |
| 563 | const bucketPolicy = yield* storage.getIamPolicyBuckets({ |
| 564 | bucket: out.bucket, |
| 565 | }); |
| 566 | expect([...membersOf(bucketPolicy, member)].sort()).toEqual([ |
| 567 | "roles/storage.objectUser", |
| 568 | "roles/storage.objectViewer", |
| 569 | ]); |
| 570 | |
| 571 | // The runtime SA's own token publishes and round-trips object content. |
| 572 | const body = yield* fetchJson<{ published: boolean; read: string }>(out.uri!); |
| 573 | expect(body).toEqual({ published: true, read: "stored" }); |
| 574 | |
| 575 | // Step 2: drop the Storage bindings. The code change redeploys and the |
| 576 | // bucket grants are revoked; the topic grant stays. |
| 577 | const next = yield* deployed(PublishOnlyService); |
| 578 | expect(next.serviceAccount).toEqual(out.serviceAccount); |
| 579 | const bucketAfter = yield* storage.getIamPolicyBuckets({ |
| 580 | bucket: out.bucket, |
| 581 | }); |
| 582 | expect([...membersOf(bucketAfter, member)]).toEqual([]); |
| 583 | const topicAfter = yield* pubsub.getIamPolicyProjectsTopics({ |
| 584 | resource: out.topic, |
| 585 | }); |
| 586 | expect([...membersOf(topicAfter, member)]).toEqual(["roles/pubsub.publisher"]); |
| 587 | const step2 = yield* fetchJson<{ published: boolean; step?: number }>(next.uri!).pipe( |
| 588 | Effect.repeat({ |
| 589 | schedule: Schedule.spaced("2 seconds"), |
| 590 | until: (response) => response.step === 2, |
| 591 | times: 15, |
| 592 | }), |
| 593 | ); |
| 594 | expect(step2).toEqual({ published: true, step: 2 }); |
| 595 | |
| 596 | yield* stack.destroy(); |
| 597 | |
| 598 | const saGone = yield* iam |
| 599 | .getProjectsServiceAccounts({ |
| 600 | name: `projects/${out.project}/serviceAccounts/${out.serviceAccount}`, |
| 601 | }) |
| 602 | .pipe( |
| 603 | Effect.as("found" as const), |
| 604 | Effect.catchTag("NotFound", () => Effect.succeed("gone" as const)), |
| 605 | ); |
| 606 | expect(saGone).toEqual("gone"); |
| 607 | }).pipe(logLevel), |
| 608 | { tags: ["provider:gcp", "provider:gcp:run", "live"], timeout: 420_000 }, |
| 609 | ); |
| 610 | |
| 611 | // Memorystore Redis provisioning takes 5-10 minutes. |
| 612 | test.provider.skipIf(!process.env.GCP_TEST_SLOW || !!process.env.FAST)( |
| 613 | "effect-native Function with Memorystore Redis over Direct VPC", |
| 614 | (stack) => |
| 615 | Effect.gen(function* () { |
| 616 | yield* stack.destroy(); |
| 617 | |
| 618 | const out = yield* stack.deploy( |
| 619 | Effect.gen(function* () { |
| 620 | const service = yield* BoundRedisService; |
| 621 | return { uri: service.uri }; |
| 622 | }), |
| 623 | ); |
| 624 | |
| 625 | expect(out.uri).toEqual(expect.any(String)); |
| 626 | const client = yield* HttpClient.HttpClient; |
| 627 | const res = yield* client.get(out.uri!).pipe( |
| 628 | Effect.flatMap((response) => |
| 629 | response.status === 200 |
| 630 | ? Effect.succeed(response) |
| 631 | : Effect.fail(new ServiceNotReady({ status: response.status })), |
| 632 | ), |
| 633 | Effect.retry({ |
| 634 | while: (e): e is ServiceNotReady => e._tag === "ServiceNotReady", |
| 635 | schedule: Schedule.exponential("500 millis"), |
| 636 | times: 10, |
| 637 | }), |
| 638 | ); |
| 639 | const body = (yield* res.json) as { redis: string | null }; |
| 640 | expect(body.redis).toEqual("ok"); |
| 641 | |
| 642 | yield* stack.destroy(); |
| 643 | }).pipe(logLevel), |
| 644 | { tags: ["provider:gcp", "provider:gcp:run", "live"], timeout: 1_500_000 }, |
| 645 | ); |
| 646 | |
| 647 | test.provider( |
| 648 | "a failed first image build removes the repository it created", |
| 649 | (stack) => |
| 650 | Effect.gen(function* () { |
| 651 | const { project } = yield* GcpEnvironment.current; |
| 652 | yield* stack.destroy(); |
| 653 | |
| 654 | const serviceId = `alchemy-test-broken-build-${Core.defaultStage()}` |
| 655 | .toLowerCase() |
| 656 | .replace(/[^a-z0-9-]/g, "-") |
| 657 | .slice(0, 45) |
| 658 | .replace(/-+$/, ""); |
| 659 | const location = "us-central1"; |
| 660 | |
| 661 | // The per-host repository is created before bundling, so a `main` |
| 662 | // that cannot be bundled fails after the repository exists. |
| 663 | const exit = yield* stack |
| 664 | .deploy( |
| 665 | Effect.gen(function* () { |
| 666 | return yield* GCP.Run.Service("BrokenBuild", { |
| 667 | serviceId, |
| 668 | location, |
| 669 | main: new URL("./fixtures/does-not-exist.ts", import.meta.url).href, |
| 670 | }); |
| 671 | }), |
| 672 | ) |
| 673 | .pipe(Effect.exit); |
| 674 | expect(Exit.isFailure(exit)).toEqual(true); |
| 675 | |
| 676 | const repository = yield* artifactregistry |
| 677 | .getProjectsLocationsRepositories({ |
| 678 | name: `projects/${project}/locations/${location}/repositories/${serviceId}-src`, |
| 679 | }) |
| 680 | .pipe( |
| 681 | Effect.as("found" as const), |
| 682 | Effect.catchTag("NotFound", () => Effect.succeed("gone" as const)), |
| 683 | ); |
| 684 | expect(repository).toEqual("gone"); |
| 685 | |
| 686 | yield* stack.destroy(); |
| 687 | }).pipe(logLevel), |
| 688 | { tags: ["provider:gcp", "provider:gcp:run", "live"], timeout: 180_000 }, |
| 689 | ); |