The best way to control Claude Code, Codex and more from any computer or phone
Mirrored from https://github.com/Camo67/t3code.git
Synced Nov 21, 04:45 UTC
| 1 | import * as Docker from "@alchemy.run/docker"; |
| 2 | import { describe, expect } from "@alchemy.run/test"; |
| 3 | import * as Test from "@alchemy.run/test/Alchemy"; |
| 4 | import { adopt, OwnedBySomeoneElse } from "alchemy/AdoptPolicy"; |
| 5 | import { inMemoryState } from "alchemy/State"; |
| 6 | import * as Cause from "effect/Cause"; |
| 7 | import * as Effect from "effect/Effect"; |
| 8 | import * as FileSystem from "effect/FileSystem"; |
| 9 | import * as Path from "effect/Path"; |
| 10 | import { ensureDockerSwarm } from "./Runtime.ts"; |
| 11 | |
| 12 | const { test } = Test.make({ providers: Docker.providers(), state: inMemoryState() }); |
| 13 | |
| 14 | // Every test deploys real swarm services through the engine (plan → apply → |
| 15 | // destroy) — no provider-method unit tests. The suite self-provisions a |
| 16 | // single-node swarm on the local engine (`ensureDockerSwarm`), so it runs |
| 17 | // anywhere Docker runs; deactivate afterwards with `docker swarm leave |
| 18 | // --force` if you don't want the node to stay a swarm manager. |
| 19 | describe( |
| 20 | "Docker.Service", |
| 21 | { tags: ["provider:docker", "provider:docker:service", "local"], concurrent: false }, |
| 22 | () => { |
| 23 | test.provider( |
| 24 | "creates a replicated service with labels", |
| 25 | (stack) => |
| 26 | Effect.gen(function* () { |
| 27 | yield* ensureDockerSwarm; |
| 28 | const serviceName = "alchemy-test-service-create"; |
| 29 | const service = yield* stack.deploy( |
| 30 | Docker.Service("created-service", { |
| 31 | name: serviceName, |
| 32 | image: "nginx:alpine", |
| 33 | replicas: 1, |
| 34 | labels: { "com.alchemy.test": "true" }, |
| 35 | }), |
| 36 | ); |
| 37 | |
| 38 | expect(service.name).toBe(serviceName); |
| 39 | expect(service.id.length).toBeGreaterThan(0); |
| 40 | expect(service.image).toContain("nginx:alpine"); |
| 41 | expect(service.replicas).toBe(1); |
| 42 | expect(service.endpointMode).toBe("vip"); |
| 43 | expect(service.labels["com.alchemy.test"]).toBe("true"); |
| 44 | |
| 45 | yield* stack.destroy(); |
| 46 | |
| 47 | const docker = yield* Docker.Docker; |
| 48 | const gone = yield* docker.service.inspect(service.id).pipe( |
| 49 | Effect.map(() => false), |
| 50 | Effect.catchReason("PlatformError", "NotFound", () => Effect.succeed(true)), |
| 51 | ); |
| 52 | expect(gone).toBe(true); |
| 53 | }), |
| 54 | { timeout: 240_000 }, |
| 55 | ); |
| 56 | |
| 57 | test.provider( |
| 58 | "scales a service in place when replicas change", |
| 59 | (stack) => |
| 60 | Effect.gen(function* () { |
| 61 | yield* ensureDockerSwarm; |
| 62 | const serviceName = "alchemy-test-service-replicas"; |
| 63 | |
| 64 | const first = yield* stack.deploy( |
| 65 | Docker.Service("scaled-service", { |
| 66 | name: serviceName, |
| 67 | image: "nginx:alpine", |
| 68 | replicas: 1, |
| 69 | }), |
| 70 | ); |
| 71 | expect(first.replicas).toBe(1); |
| 72 | |
| 73 | const second = yield* stack.deploy( |
| 74 | Docker.Service("scaled-service", { |
| 75 | name: serviceName, |
| 76 | image: "nginx:alpine", |
| 77 | replicas: 2, |
| 78 | }), |
| 79 | ); |
| 80 | |
| 81 | expect(second.id).toBe(first.id); |
| 82 | expect(second.replicas).toBe(2); |
| 83 | |
| 84 | const third = yield* stack.deploy( |
| 85 | Docker.Service("scaled-service", { |
| 86 | name: serviceName, |
| 87 | image: "nginx:alpine", |
| 88 | replicas: 0, |
| 89 | }), |
| 90 | ); |
| 91 | expect(third.id).toBe(first.id); |
| 92 | expect(third.replicas).toBe(0); |
| 93 | |
| 94 | yield* stack.destroy(); |
| 95 | }), |
| 96 | { timeout: 240_000 }, |
| 97 | ); |
| 98 | |
| 99 | test.provider( |
| 100 | "rolls a new image onto the existing service", |
| 101 | (stack) => |
| 102 | Effect.gen(function* () { |
| 103 | yield* ensureDockerSwarm; |
| 104 | const docker = yield* Docker.Docker; |
| 105 | const service = (image: string) => |
| 106 | Docker.Service("rolled-service", { |
| 107 | name: "alchemy-test-service-rollout", |
| 108 | image, |
| 109 | labels: { tier: "web" }, |
| 110 | environment: { MODE: "test" }, |
| 111 | constraints: ["node.role==manager"], |
| 112 | updateConfig: { order: "start-first", failureAction: "rollback", parallelism: 0 }, |
| 113 | }); |
| 114 | |
| 115 | const first = yield* stack.deploy(service("nginx:1.27-alpine")); |
| 116 | const plan = yield* stack.plan(service("nginx:1.28-alpine")); |
| 117 | expect(plan.resources["rolled-service"]).toMatchObject({ action: "update" }); |
| 118 | const second = yield* stack.deploy(service("nginx:1.28-alpine")); |
| 119 | |
| 120 | expect(second.id).toBe(first.id); |
| 121 | expect(second.image).toContain("nginx:1.28-alpine"); |
| 122 | expect(second.labels.tier).toBe("web"); |
| 123 | |
| 124 | // The previous spec is kept by swarm only when the service was |
| 125 | // updated, not recreated. |
| 126 | const previous = yield* docker |
| 127 | .run([ |
| 128 | "service", |
| 129 | "inspect", |
| 130 | second.id, |
| 131 | "--format", |
| 132 | "{{json .PreviousSpec.TaskTemplate.ContainerSpec.Image}}", |
| 133 | ]) |
| 134 | .pipe(Effect.map((output) => JSON.parse(output.stdout) as string)); |
| 135 | expect(previous).toContain("nginx:1.27-alpine"); |
| 136 | |
| 137 | const order = yield* docker |
| 138 | .run(["service", "inspect", second.id, "--format", "{{.Spec.UpdateConfig.Order}}"]) |
| 139 | .pipe(Effect.map((output) => output.stdout.trim())); |
| 140 | expect(order).toBe("start-first"); |
| 141 | |
| 142 | // `--constraint-add` appends, so re-adding an existing constraint |
| 143 | // would duplicate it; `0` (update every task at once) must reach |
| 144 | // the CLI rather than being dropped as falsy. |
| 145 | const spec = yield* docker |
| 146 | .run(["service", "inspect", second.id, "--format", "{{json .Spec}}"]) |
| 147 | .pipe( |
| 148 | Effect.map( |
| 149 | (output) => |
| 150 | JSON.parse(output.stdout) as { |
| 151 | TaskTemplate: { Placement?: { Constraints?: string[] } }; |
| 152 | UpdateConfig?: { Parallelism?: number }; |
| 153 | }, |
| 154 | ), |
| 155 | ); |
| 156 | expect(spec.TaskTemplate.Placement?.Constraints).toEqual(["node.role==manager"]); |
| 157 | expect(spec.UpdateConfig?.Parallelism).toBe(0); |
| 158 | |
| 159 | yield* stack.destroy(); |
| 160 | }), |
| 161 | { timeout: 240_000 }, |
| 162 | ); |
| 163 | |
| 164 | test.provider( |
| 165 | "rolls back and fails the deploy when updated tasks are unhealthy", |
| 166 | (stack) => |
| 167 | Effect.gen(function* () { |
| 168 | yield* ensureDockerSwarm; |
| 169 | const docker = yield* Docker.Docker; |
| 170 | const deploy = (cmd: string[], labels?: Record<string, string>) => |
| 171 | stack.deploy( |
| 172 | Docker.Service("rollback-service", { |
| 173 | name: "alchemy-test-service-rollback", |
| 174 | image: "nginx:alpine", |
| 175 | labels, |
| 176 | healthcheck: { cmd, interval: "1s", timeout: "1s", retries: 1 }, |
| 177 | updateConfig: { failureAction: "rollback", monitor: "10s" }, |
| 178 | }), |
| 179 | ); |
| 180 | |
| 181 | const first = yield* deploy(["CMD-SHELL", "test -d /etc"]); |
| 182 | |
| 183 | const failure = yield* deploy(["CMD", "false"]).pipe( |
| 184 | Effect.as(""), |
| 185 | Effect.catchCause((cause) => Effect.succeed(Cause.pretty(cause))), |
| 186 | ); |
| 187 | expect(failure).toContain("DockerServiceUpdateFailed"); |
| 188 | expect(failure).toContain("rollback_completed"); |
| 189 | |
| 190 | const healthcheck = yield* docker |
| 191 | .run([ |
| 192 | "service", |
| 193 | "inspect", |
| 194 | first.id, |
| 195 | "--format", |
| 196 | "{{json .Spec.TaskTemplate.ContainerSpec.Healthcheck.Test}}", |
| 197 | ]) |
| 198 | .pipe(Effect.map((output) => JSON.parse(output.stdout))); |
| 199 | expect(healthcheck).toEqual(["CMD-SHELL", "test -d /etc"]); |
| 200 | |
| 201 | // The rolled-back deploy doesn't block the next one: swarm clears |
| 202 | // `rollback_completed` when it accepts this update. |
| 203 | const relabeled = yield* deploy(["CMD-SHELL", "test -d /etc"], { after: "rollback" }); |
| 204 | expect(relabeled.id).toBe(first.id); |
| 205 | expect(relabeled.labels.after).toBe("rollback"); |
| 206 | |
| 207 | yield* stack.destroy(); |
| 208 | }), |
| 209 | { timeout: 240_000 }, |
| 210 | ); |
| 211 | |
| 212 | test.provider( |
| 213 | "fails the deploy when swarm pauses an unhealthy update", |
| 214 | (stack) => |
| 215 | Effect.gen(function* () { |
| 216 | yield* ensureDockerSwarm; |
| 217 | // No `failureAction`: swarm's default is to pause the rollout. |
| 218 | const deploy = (cmd: string[]) => |
| 219 | stack.deploy( |
| 220 | Docker.Service("paused-service", { |
| 221 | name: "alchemy-test-service-paused", |
| 222 | image: "nginx:alpine", |
| 223 | healthcheck: { cmd, interval: "1s", timeout: "1s", retries: 1 }, |
| 224 | updateConfig: { monitor: "10s" }, |
| 225 | }), |
| 226 | ); |
| 227 | |
| 228 | yield* deploy(["CMD-SHELL", "test -d /etc"]); |
| 229 | const failure = yield* deploy(["CMD", "false"]).pipe( |
| 230 | Effect.as(""), |
| 231 | Effect.catchCause((cause) => Effect.succeed(Cause.pretty(cause))), |
| 232 | ); |
| 233 | expect(failure).toContain("DockerServiceUpdateFailed"); |
| 234 | expect(failure).toContain("paused"); |
| 235 | |
| 236 | yield* stack.destroy(); |
| 237 | }), |
| 238 | { timeout: 240_000 }, |
| 239 | ); |
| 240 | |
| 241 | test.provider( |
| 242 | "removes environment variables, ports and mounts dropped from props", |
| 243 | (stack) => |
| 244 | Effect.gen(function* () { |
| 245 | yield* ensureDockerSwarm; |
| 246 | const docker = yield* Docker.Docker; |
| 247 | const name = "alchemy-test-service-remove-entries"; |
| 248 | // Swarm creates the named volumes on first mount; they outlive the service. |
| 249 | yield* Effect.addFinalizer(() => |
| 250 | docker |
| 251 | .run(["volume", "rm", "alchemy-test-service-keep", "alchemy-test-service-drop"]) |
| 252 | .pipe(Effect.ignore), |
| 253 | ); |
| 254 | |
| 255 | const first = yield* stack.deploy( |
| 256 | Docker.Service("pruned-service", { |
| 257 | name, |
| 258 | image: "nginx:alpine", |
| 259 | environment: { KEEP: "1", DROP: "1" }, |
| 260 | ports: [{ internal: 80 }, { internal: 443 }], |
| 261 | volumes: [ |
| 262 | { hostPath: "alchemy-test-service-keep", containerPath: "/keep" }, |
| 263 | { hostPath: "alchemy-test-service-drop", containerPath: "/drop" }, |
| 264 | ], |
| 265 | }), |
| 266 | ); |
| 267 | |
| 268 | const second = yield* stack.deploy( |
| 269 | Docker.Service("pruned-service", { |
| 270 | name, |
| 271 | image: "nginx:alpine", |
| 272 | environment: { KEEP: "2" }, |
| 273 | ports: [{ internal: 80 }], |
| 274 | volumes: [ |
| 275 | { hostPath: "alchemy-test-service-keep", containerPath: "/keep", readOnly: true }, |
| 276 | ], |
| 277 | }), |
| 278 | ); |
| 279 | expect(second.id).toBe(first.id); |
| 280 | |
| 281 | const spec = yield* docker |
| 282 | .run(["service", "inspect", second.id, "--format", "{{json .Spec}}"]) |
| 283 | .pipe( |
| 284 | Effect.map( |
| 285 | (output) => |
| 286 | JSON.parse(output.stdout) as { |
| 287 | TaskTemplate: { |
| 288 | ContainerSpec: { |
| 289 | Env?: string[]; |
| 290 | Mounts?: Array<{ Target: string; ReadOnly?: boolean }>; |
| 291 | }; |
| 292 | }; |
| 293 | EndpointSpec?: { Ports?: Array<{ TargetPort: number }> }; |
| 294 | }, |
| 295 | ), |
| 296 | ); |
| 297 | expect(spec.TaskTemplate.ContainerSpec.Env).toEqual(["KEEP=2"]); |
| 298 | // `--mount-add` on an existing target replaces that mount. |
| 299 | expect( |
| 300 | (spec.TaskTemplate.ContainerSpec.Mounts ?? []).map((m) => [m.Target, m.ReadOnly]), |
| 301 | ).toEqual([["/keep", true]]); |
| 302 | expect((spec.EndpointSpec?.Ports ?? []).map((port) => port.TargetPort)).toEqual([80]); |
| 303 | |
| 304 | yield* stack.destroy(); |
| 305 | }), |
| 306 | { timeout: 240_000 }, |
| 307 | ); |
| 308 | |
| 309 | test.provider( |
| 310 | "replaces a service when one of two ports sharing a target is dropped", |
| 311 | (stack) => |
| 312 | Effect.gen(function* () { |
| 313 | yield* ensureDockerSwarm; |
| 314 | const docker = yield* Docker.Docker; |
| 315 | const service = (external: number[]) => |
| 316 | Docker.Service("shared-target-service", { |
| 317 | name: "alchemy-test-service-shared-target", |
| 318 | image: "nginx:alpine", |
| 319 | ports: external.map((port) => ({ external: port, internal: 80 })), |
| 320 | }); |
| 321 | |
| 322 | yield* stack.deploy(service([43121, 43122])); |
| 323 | // `--publish-rm target=80` would drop both ports and |
| 324 | // `--publish-add` skips the one already in the spec. |
| 325 | const plan = yield* stack.plan(service([43122])); |
| 326 | expect(plan.resources["shared-target-service"]).toMatchObject({ action: "replace" }); |
| 327 | const second = yield* stack.deploy(service([43122])); |
| 328 | |
| 329 | const published = yield* docker |
| 330 | .run(["service", "inspect", second.id, "--format", "{{json .Spec.EndpointSpec.Ports}}"]) |
| 331 | .pipe( |
| 332 | Effect.map((output) => |
| 333 | (JSON.parse(output.stdout) as Array<{ PublishedPort: number }>).map( |
| 334 | (port) => port.PublishedPort, |
| 335 | ), |
| 336 | ), |
| 337 | ); |
| 338 | expect(published).toEqual([43122]); |
| 339 | |
| 340 | yield* stack.destroy(); |
| 341 | }), |
| 342 | { timeout: 240_000 }, |
| 343 | ); |
| 344 | |
| 345 | test.provider( |
| 346 | "updates the image of a service with secrets, configs and networks attached", |
| 347 | (stack) => |
| 348 | Effect.gen(function* () { |
| 349 | yield* ensureDockerSwarm; |
| 350 | const docker = yield* Docker.Docker; |
| 351 | const secretName = "alchemy-test-service-update-secret"; |
| 352 | const configName = "alchemy-test-service-update-config"; |
| 353 | |
| 354 | // Alchemy has no swarm secret/config resource; services reference |
| 355 | // existing ones by name. |
| 356 | yield* docker |
| 357 | .run(["secret", "rm", secretName]) |
| 358 | .pipe(Effect.catchCause(() => Effect.void)); |
| 359 | yield* docker |
| 360 | .run(["config", "rm", configName]) |
| 361 | .pipe(Effect.catchCause(() => Effect.void)); |
| 362 | const fs = yield* FileSystem.FileSystem; |
| 363 | const path = yield* Path.Path; |
| 364 | const dir = yield* fs.makeTempDirectoryScoped({ prefix: "alchemy-docker-secret-" }); |
| 365 | const secretFile = path.join(dir, "secret"); |
| 366 | yield* fs.writeFileString(secretFile, "value"); |
| 367 | yield* docker.run(["secret", "create", secretName, secretFile]); |
| 368 | yield* docker.run(["config", "create", configName, secretFile]); |
| 369 | yield* Effect.addFinalizer(() => |
| 370 | Effect.all([ |
| 371 | docker.run(["secret", "rm", secretName]).pipe(Effect.ignore), |
| 372 | docker.run(["config", "rm", configName]).pipe(Effect.ignore), |
| 373 | ]), |
| 374 | ); |
| 375 | |
| 376 | // Re-adding an attached secret, config or network fails, so this |
| 377 | // update exercises the detach-and-reattach path for all three. |
| 378 | const deploy = (image: string) => |
| 379 | stack.deploy( |
| 380 | Effect.gen(function* () { |
| 381 | const network = yield* Docker.Network("secret-service-network", { |
| 382 | name: "alchemy-test-service-secret-update-net", |
| 383 | driver: "overlay", |
| 384 | }); |
| 385 | const service = yield* Docker.Service("secret-service", { |
| 386 | name: "alchemy-test-service-secret-update", |
| 387 | image, |
| 388 | secrets: [{ source: secretName, target: "app_secret" }], |
| 389 | configs: [{ source: configName, target: "/etc/app.conf" }], |
| 390 | networks: [{ name: network.name, aliases: ["app"] }], |
| 391 | }); |
| 392 | return { network, service }; |
| 393 | }), |
| 394 | ); |
| 395 | |
| 396 | const { service: first } = yield* deploy("nginx:1.27-alpine"); |
| 397 | const { network, service: second } = yield* deploy("nginx:1.28-alpine"); |
| 398 | expect(second.id).toBe(first.id); |
| 399 | expect(second.image).toContain("nginx:1.28-alpine"); |
| 400 | |
| 401 | const spec = yield* docker |
| 402 | .run(["service", "inspect", second.id, "--format", "{{json .Spec.TaskTemplate}}"]) |
| 403 | .pipe( |
| 404 | Effect.map( |
| 405 | (output) => |
| 406 | JSON.parse(output.stdout) as { |
| 407 | ContainerSpec: { |
| 408 | Secrets?: Array<{ File: { Name: string } }>; |
| 409 | Configs?: Array<{ File: { Name: string } }>; |
| 410 | }; |
| 411 | Networks?: Array<{ Target: string; Aliases?: string[] }>; |
| 412 | }, |
| 413 | ), |
| 414 | ); |
| 415 | expect((spec.ContainerSpec.Secrets ?? []).map((s) => s.File.Name)).toEqual([ |
| 416 | "app_secret", |
| 417 | ]); |
| 418 | expect((spec.ContainerSpec.Configs ?? []).map((c) => c.File.Name)).toEqual([ |
| 419 | "/etc/app.conf", |
| 420 | ]); |
| 421 | expect(spec.Networks).toEqual([{ Target: network.id, Aliases: ["app"] }]); |
| 422 | |
| 423 | yield* stack.destroy(); |
| 424 | }), |
| 425 | { timeout: 240_000 }, |
| 426 | ); |
| 427 | |
| 428 | test.provider( |
| 429 | "keeps an auto-assigned published port across an update", |
| 430 | (stack) => |
| 431 | Effect.gen(function* () { |
| 432 | yield* ensureDockerSwarm; |
| 433 | const docker = yield* Docker.Docker; |
| 434 | const service = (image: string) => |
| 435 | Docker.Service("dynamic-port-service", { |
| 436 | name: "alchemy-test-service-dynamic-port", |
| 437 | image, |
| 438 | ports: [{ internal: 80 }], |
| 439 | }); |
| 440 | // Swarm records the port it picked on `Endpoint`, not in the spec. |
| 441 | const publishedPorts = (id: string) => |
| 442 | docker |
| 443 | .run(["service", "inspect", id, "--format", "{{json .Endpoint.Ports}}"]) |
| 444 | .pipe( |
| 445 | Effect.map((output) => |
| 446 | ((JSON.parse(output.stdout) ?? []) as Array<{ PublishedPort: number }>).map( |
| 447 | (port) => port.PublishedPort, |
| 448 | ), |
| 449 | ), |
| 450 | ); |
| 451 | |
| 452 | const first = yield* stack.deploy(service("nginx:1.27-alpine")); |
| 453 | const before = yield* publishedPorts(first.id); |
| 454 | expect(before).toHaveLength(1); |
| 455 | |
| 456 | const second = yield* stack.deploy(service("nginx:1.28-alpine")); |
| 457 | expect(second.id).toBe(first.id); |
| 458 | expect(yield* publishedPorts(second.id)).toEqual(before); |
| 459 | |
| 460 | yield* stack.destroy(); |
| 461 | }), |
| 462 | { timeout: 240_000 }, |
| 463 | ); |
| 464 | |
| 465 | test.provider( |
| 466 | "creates a service with zero replicas", |
| 467 | (stack) => |
| 468 | Effect.gen(function* () { |
| 469 | yield* ensureDockerSwarm; |
| 470 | const service = yield* stack.deploy( |
| 471 | Docker.Service("idle-service", { |
| 472 | name: "alchemy-test-service-idle", |
| 473 | image: "nginx:alpine", |
| 474 | replicas: 0, |
| 475 | }), |
| 476 | ); |
| 477 | expect(service.replicas).toBe(0); |
| 478 | |
| 479 | yield* stack.destroy(); |
| 480 | }), |
| 481 | { timeout: 240_000 }, |
| 482 | ); |
| 483 | |
| 484 | test.provider( |
| 485 | "replaces a service when its Docker context changes", |
| 486 | (stack) => |
| 487 | Effect.gen(function* () { |
| 488 | yield* ensureDockerSwarm; |
| 489 | const serviceName = "alchemy-test-service-context"; |
| 490 | |
| 491 | const first = yield* stack.deploy( |
| 492 | Docker.Service("context-service", { name: serviceName, image: "nginx:alpine" }), |
| 493 | ); |
| 494 | expect(first.context).toBeUndefined(); |
| 495 | |
| 496 | // `default` is the engine's built-in context — same daemon, but a |
| 497 | // different context ref, so the replace path (delete-first, recreate |
| 498 | // under `--context`) runs against a real named context. |
| 499 | const second = yield* stack.deploy( |
| 500 | Docker.Service("context-service", { |
| 501 | name: serviceName, |
| 502 | image: "nginx:alpine", |
| 503 | context: "default", |
| 504 | }), |
| 505 | ); |
| 506 | |
| 507 | expect(second.id).not.toBe(first.id); |
| 508 | expect(second.context).toBe("default"); |
| 509 | |
| 510 | yield* stack.destroy(); |
| 511 | }), |
| 512 | { timeout: 240_000 }, |
| 513 | ); |
| 514 | |
| 515 | test.provider( |
| 516 | "refuses a pre-existing service unless explicitly adopted", |
| 517 | (stack) => |
| 518 | Effect.gen(function* () { |
| 519 | yield* ensureDockerSwarm; |
| 520 | const docker = yield* Docker.Docker; |
| 521 | const serviceName = "alchemy-test-service-adopt-existing"; |
| 522 | |
| 523 | yield* Effect.addFinalizer(() => docker.service.remove(serviceName).pipe(Effect.ignore)); |
| 524 | |
| 525 | yield* docker.service |
| 526 | .remove(serviceName) |
| 527 | .pipe(Effect.catchReason("PlatformError", "NotFound", () => Effect.void)); |
| 528 | |
| 529 | yield* docker.service.create({ name: serviceName, image: "nginx:alpine", replicas: 1 }); |
| 530 | |
| 531 | const error = yield* stack |
| 532 | .deploy( |
| 533 | Docker.Service("existing-service", { |
| 534 | name: serviceName, |
| 535 | image: "nginx:alpine", |
| 536 | replicas: 1, |
| 537 | }), |
| 538 | ) |
| 539 | .pipe( |
| 540 | Effect.as(undefined), |
| 541 | Effect.catchCause((cause) => Effect.succeed(findOwnedError(cause))), |
| 542 | ); |
| 543 | |
| 544 | expect(error).toBeInstanceOf(OwnedBySomeoneElse); |
| 545 | |
| 546 | const adopted = yield* stack.deploy( |
| 547 | Docker.Service("existing-service", { |
| 548 | name: serviceName, |
| 549 | image: "nginx:alpine", |
| 550 | replicas: 1, |
| 551 | }).pipe(adopt(true)), |
| 552 | ); |
| 553 | |
| 554 | expect(adopted.name).toBe(serviceName); |
| 555 | expect(adopted.id.length).toBeGreaterThan(0); |
| 556 | |
| 557 | yield* stack.destroy(); |
| 558 | }), |
| 559 | { timeout: 240_000 }, |
| 560 | ); |
| 561 | |
| 562 | test.provider( |
| 563 | "updates labels in place", |
| 564 | (stack) => |
| 565 | Effect.gen(function* () { |
| 566 | yield* ensureDockerSwarm; |
| 567 | const serviceName = "alchemy-test-service-labels"; |
| 568 | |
| 569 | const first = yield* stack.deploy( |
| 570 | Docker.Service("labeled-service", { |
| 571 | name: serviceName, |
| 572 | image: "nginx:alpine", |
| 573 | labels: { generation: "1", stale: "true" }, |
| 574 | }), |
| 575 | ); |
| 576 | |
| 577 | const second = yield* stack.deploy( |
| 578 | Docker.Service("labeled-service", { |
| 579 | name: serviceName, |
| 580 | image: "nginx:alpine", |
| 581 | labels: { generation: "2" }, |
| 582 | }), |
| 583 | ); |
| 584 | |
| 585 | expect(second.id).toBe(first.id); |
| 586 | expect(second.labels.generation).toBe("2"); |
| 587 | expect(second.labels.stale).toBeUndefined(); |
| 588 | |
| 589 | yield* stack.destroy(); |
| 590 | }), |
| 591 | { timeout: 240_000 }, |
| 592 | ); |
| 593 | |
| 594 | test.provider( |
| 595 | "attaches a network created in the same deploy in place", |
| 596 | (stack) => |
| 597 | Effect.gen(function* () { |
| 598 | yield* ensureDockerSwarm; |
| 599 | const serviceName = "alchemy-test-service-replace"; |
| 600 | |
| 601 | const first = yield* stack.deploy( |
| 602 | Docker.Service("replaceable-service", { |
| 603 | name: serviceName, |
| 604 | image: "nginx:alpine", |
| 605 | }), |
| 606 | ); |
| 607 | |
| 608 | const { network, service: second } = yield* stack.deploy( |
| 609 | Effect.gen(function* () { |
| 610 | const network = yield* Docker.Network("replaceable-service-network", { |
| 611 | name: "alchemy-test-service-replace-net", |
| 612 | driver: "overlay", |
| 613 | }); |
| 614 | const service = yield* Docker.Service("replaceable-service", { |
| 615 | name: serviceName, |
| 616 | image: "nginx:alpine", |
| 617 | networks: [network.name], |
| 618 | }); |
| 619 | return { network, service }; |
| 620 | }), |
| 621 | ); |
| 622 | |
| 623 | expect(second.id).toBe(first.id); |
| 624 | expect(second.networks).toContain(network.id); |
| 625 | |
| 626 | yield* stack.destroy(); |
| 627 | }), |
| 628 | { timeout: 240_000 }, |
| 629 | ); |
| 630 | |
| 631 | test.provider( |
| 632 | "applies array healthchecks and re-enables one after NONE", |
| 633 | (stack) => |
| 634 | Effect.gen(function* () { |
| 635 | yield* ensureDockerSwarm; |
| 636 | const docker = yield* Docker.Docker; |
| 637 | const deploy = (cmd: string[]) => |
| 638 | stack.deploy( |
| 639 | Docker.Service("healthcheck-array-service", { |
| 640 | name: "alchemy-test-service-healthcheck-array", |
| 641 | image: "nginx:alpine", |
| 642 | healthcheck: { cmd }, |
| 643 | }), |
| 644 | ); |
| 645 | const healthcheck = (id: string) => |
| 646 | docker |
| 647 | .run([ |
| 648 | "service", |
| 649 | "inspect", |
| 650 | id, |
| 651 | "--format", |
| 652 | "{{json .Spec.TaskTemplate.ContainerSpec.Healthcheck}}", |
| 653 | ]) |
| 654 | .pipe(Effect.map((output) => JSON.parse(output.stdout).Test)); |
| 655 | |
| 656 | const shell = yield* deploy(["CMD-SHELL", "test -d /etc"]); |
| 657 | expect(yield* healthcheck(shell.id)).toEqual(["CMD-SHELL", "test -d /etc"]); |
| 658 | |
| 659 | // Disable, then turn it back on through `docker service update`. |
| 660 | const disabled = yield* deploy(["NONE"]); |
| 661 | expect(yield* healthcheck(disabled.id)).toEqual(["NONE"]); |
| 662 | const restored = yield* deploy(["CMD", "test", "-d", "/etc"]); |
| 663 | expect(yield* healthcheck(restored.id)).toEqual(["CMD-SHELL", "test -d /etc"]); |
| 664 | |
| 665 | yield* stack.destroy(); |
| 666 | }), |
| 667 | { timeout: 240_000 }, |
| 668 | ); |
| 669 | |
| 670 | test.provider( |
| 671 | "disables the image healthcheck with NONE", |
| 672 | (stack) => |
| 673 | Effect.gen(function* () { |
| 674 | yield* ensureDockerSwarm; |
| 675 | const docker = yield* Docker.Docker; |
| 676 | const service = yield* stack.deploy( |
| 677 | Docker.Service("healthcheck-none-service", { |
| 678 | name: "alchemy-test-service-healthcheck-none", |
| 679 | image: "nginx:alpine", |
| 680 | healthcheck: { cmd: ["NONE"] }, |
| 681 | }), |
| 682 | ); |
| 683 | |
| 684 | const output = yield* docker.run([ |
| 685 | "service", |
| 686 | "inspect", |
| 687 | service.id, |
| 688 | "--format", |
| 689 | "{{json .Spec.TaskTemplate.ContainerSpec.Healthcheck}}", |
| 690 | ]); |
| 691 | expect(JSON.parse(output.stdout)).toEqual({ Test: ["NONE"] }); |
| 692 | |
| 693 | yield* stack.destroy(); |
| 694 | }), |
| 695 | { timeout: 240_000 }, |
| 696 | ); |
| 697 | }, |
| 698 | ); |
| 699 | |
| 700 | const findOwnedError = (cause: Cause.Cause<unknown>): OwnedBySomeoneElse | undefined => |
| 701 | cause.reasons |
| 702 | .map((reason) => |
| 703 | Cause.isFailReason(reason) |
| 704 | ? reason.error |
| 705 | : Cause.isDieReason(reason) |
| 706 | ? reason.defect |
| 707 | : undefined, |
| 708 | ) |
| 709 | .find((value): value is OwnedBySomeoneElse => value instanceof OwnedBySomeoneElse); |