The best way to control Claude Code, Codex and more from any computer or phone

main
ts 824 lines 28.4 KB
Raw
1 import * as Fly from "@alchemy.run/fly";
2 import type { MachineContainer } from "@alchemy.run/fly/Machine";
3 import { ensureStarted } from "@alchemy.run/fly/replicas";
4 import { expect } from "@alchemy.run/test";
5 import * as Test from "@alchemy.run/test/Alchemy";
6 import * as machines from "@distilled.cloud/fly-io/machines";
7 import * as Retry from "@distilled.cloud/fly-io/Retry";
8 import * as Provider from "alchemy/Provider";
9 import * as Effect from "effect/Effect";
10 import { MinimumLogLevel } from "effect/References";
11 import * as Result from "effect/Result";
12 import * as Schedule from "effect/Schedule";
13
14 const { test } = Test.make({ providers: Fly.providers() });
15
16 const logLevel = Effect.provideService(MinimumLogLevel, process.env.DEBUG ? "Debug" : "Info");
17
18 const image =
19 "docker-hub-mirror.fly.io/library/nginx@sha256:7396be67b6f53012a5cf955fa9040619294c25ccacf11e22af5de1b572fc756e";
20 const nextImage =
21 "docker-hub-mirror.fly.io/library/nginx@sha256:ef8676b33d681f272ba429b27658bdd7e640963279714c96bddf1dc76307f7b6";
22
23 const workload = (sidecarVersion: string, includeObsolete = true): MachineContainer[] => [
24 {
25 name: "web",
26 image,
27 env: { ROLE: "web" },
28 healthChecks: [
29 {
30 name: "web-tcp",
31 kind: "readiness" as const,
32 tcp: { port: 80 },
33 interval: 5,
34 timeout: 2,
35 },
36 ],
37 },
38 {
39 name: "sidecar",
40 image: sidecarVersion === "two" ? nextImage : image,
41 cmd: ["sh", "-c", "sleep 300"],
42 env: { ROLE: "sidecar", VERSION: sidecarVersion },
43 dependsOn: [{ name: "web", condition: "healthy" as const }],
44 },
45 ...(includeObsolete ? [{ name: "obsolete", image, cmd: ["sh", "-c", "sleep 300"] }] : []),
46 ];
47
48 const waitUntilGone = (appName: string, machineId: string) =>
49 machines
50 .getMachine({
51 app_name: appName,
52 machine_id: machineId,
53 })
54 .pipe(
55 Effect.map((machine) =>
56 machine.state === "destroyed" ? ("gone" as const) : ("found" as const),
57 ),
58 Effect.catchTag("NotFound", () => Effect.succeed("gone" as const)),
59 Effect.repeat({
60 schedule: Schedule.spaced("2 seconds"),
61 until: (status) => status === "gone",
62 times: 10,
63 }),
64 );
65
66 test.provider(
67 "create, update, and delete a machine",
68 (stack) =>
69 Effect.gen(function* () {
70 yield* stack.destroy();
71
72 const created = yield* stack.deploy(
73 Effect.gen(function* () {
74 const app = yield* Fly.App("Site");
75 return yield* Fly.Machine("Web", {
76 app,
77 region: "iad",
78 image: "nginx:alpine",
79 guest: { cpus: 1, memoryMb: 256 },
80 });
81 }),
82 );
83
84 expect(created.machineId).toEqual(expect.any(String));
85 expect(created.machineId.length).toBeGreaterThan(0);
86 expect(created.machineIds).toEqual([created.machineId]);
87 expect(created.count).toEqual(1);
88 expect(created.appName).toEqual(expect.any(String));
89 expect(created.name).toEqual(expect.any(String));
90 expect(created.region).toEqual("iad");
91 expect(created.state).toEqual("started");
92 expect(created.privateIp).toEqual(expect.any(String));
93 expect(created.guest?.cpus).toEqual(1);
94 expect(created.guest?.memoryMb).toEqual(256);
95
96 const fetched = yield* machines.getMachine({
97 app_name: created.appName,
98 machine_id: created.machineId,
99 });
100 expect(fetched.id).toEqual(created.machineId);
101 expect(fetched.name).toEqual(created.name);
102 expect(fetched.region).toEqual("iad");
103 expect(fetched.state).toEqual("started");
104 expect(fetched.config?.image).toEqual(expect.stringContaining("nginx"));
105 expect(fetched.config?.guest?.cpus).toEqual(1);
106 expect(fetched.config?.guest?.memory_mb).toEqual(256);
107 expect(fetched.config?.metadata?.["alchemy.type"]).toEqual("Fly.Machine");
108 expect(fetched.config?.metadata?.["alchemy.replica"]).toEqual("0");
109 expect(fetched.config?.metadata?.["alchemy.stack"]).toEqual(expect.any(String));
110
111 const updated = yield* stack.deploy(
112 Effect.gen(function* () {
113 const app = yield* Fly.App("Site");
114 return yield* Fly.Machine("Web", {
115 app,
116 region: "iad",
117 image: "nginx:alpine",
118 guest: { cpus: 1, memoryMb: 256 },
119 env: { ALCHEMY_MARK: "updated" },
120 metadata: { role: "web" },
121 restart: { policy: "on-failure", maxRetries: 3 },
122 services: [
123 {
124 protocol: "tcp",
125 internalPort: 80,
126 ports: [{ port: 80, handlers: ["http"] }],
127 checks: [
128 {
129 type: "http",
130 port: 80,
131 interval: "20s",
132 timeout: "3s",
133 gracePeriod: "6s",
134 method: "HEAD",
135 path: "/",
136 protocol: "http",
137 headers: [
138 {
139 name: "X-Alchemy-Check",
140 values: ["ready", "routing"],
141 },
142 ],
143 tlsServerName: "example.com",
144 tlsSkipVerify: true,
145 },
146 ],
147 },
148 ],
149 });
150 }),
151 );
152
153 expect(updated.machineId).toEqual(created.machineId);
154 expect(updated.appName).toEqual(created.appName);
155 expect(updated.name).toEqual(created.name);
156 expect(updated.region).toEqual("iad");
157 expect(updated.state).toEqual("started");
158
159 const refetched = yield* machines.getMachine({
160 app_name: updated.appName,
161 machine_id: updated.machineId,
162 });
163 expect(refetched.id).toEqual(created.machineId);
164 expect(refetched.config?.env?.ALCHEMY_MARK).toEqual("updated");
165 expect(refetched.config?.metadata?.role).toEqual("web");
166 expect(refetched.config?.metadata?.["alchemy.type"]).toEqual("Fly.Machine");
167 expect(refetched.config?.restart?.policy).toEqual("on-failure");
168 expect(refetched.config?.restart?.max_retries).toEqual(3);
169 expect(refetched.config?.services?.[0]?.internal_port).toEqual(80);
170 expect(refetched.config?.services?.[0]?.ports?.[0]?.port).toEqual(80);
171 const check = refetched.config?.services?.[0]?.checks?.[0];
172 expect(check?.type).toEqual("http");
173 expect(check?.port).toEqual(80);
174 expect(check?.interval).toEqual("20s");
175 expect(check?.timeout).toEqual("3s");
176 expect(check?.grace_period).toEqual("6s");
177 expect(check?.method).toEqual("HEAD");
178 expect(check?.path).toEqual("/");
179 expect(check?.protocol).toEqual("http");
180 expect(check?.headers?.[0]?.name).toEqual("X-Alchemy-Check");
181 expect(check?.headers?.[0]?.values).toEqual(["ready", "routing"]);
182 expect(check?.tls_server_name).toEqual("example.com");
183 expect(check?.tls_skip_verify).toEqual(true);
184
185 yield* stack.destroy();
186
187 const gone = yield* waitUntilGone(created.appName, created.machineId);
188 expect(gone).toEqual("gone");
189 }).pipe(logLevel),
190 {
191 tags: ["provider:fly", "provider:fly:app", "provider:fly:machine", "live"],
192 timeout: 180_000,
193 },
194 );
195
196 test.provider(
197 "starts an unlaunched machine and recovers from stale state",
198 (stack) =>
199 Effect.gen(function* () {
200 yield* stack.destroy();
201
202 const deploy = (skipLaunch: boolean) =>
203 stack.deploy(
204 Effect.gen(function* () {
205 const app = yield* Fly.App("LaunchSite");
206 return yield* Fly.Machine("LaunchWeb", {
207 app,
208 region: "iad",
209 image: "nginx:alpine",
210 guest: { cpus: 1, memoryMb: 256 },
211 skipLaunch,
212 services: [
213 {
214 protocol: "tcp",
215 internalPort: 80,
216 checks: [
217 {
218 type: "http",
219 port: 80,
220 path: skipLaunch ? "/missing" : "/",
221 interval: "5s",
222 timeout: "2s",
223 gracePeriod: "1s",
224 },
225 ],
226 },
227 ],
228 });
229 }),
230 );
231 const created = yield* deploy(true);
232 const target = {
233 app_name: created.appName,
234 machine_id: created.machineId,
235 };
236 const unlaunched = yield* machines.getMachine(target);
237 expect(["created", "stopped"]).toContain(unlaunched.state);
238 yield* ensureStarted(created.appName, unlaunched, true);
239 expect(["created", "stopped"]).toContain((yield* machines.getMachine(target)).state);
240
241 const waitError = yield* machines
242 .waitMachine({ ...target, state: "started", timeout: 1 })
243 .pipe(Retry.none, Effect.flip);
244 expect(["MachineWaitTimeout", "GatewayTimeout"]).toContain(waitError._tag);
245
246 const launched = yield* deploy(false);
247 expect(launched.machineId).toBe(created.machineId);
248 expect(launched.state).toBe("started");
249 const running = yield* machines.getMachine(target);
250
251 const alreadyStarted = yield* ensureStarted(created.appName, unlaunched, false);
252 expect(alreadyStarted.state).toBe("started");
253 expect(alreadyStarted.instance_id).toBe(running.instance_id);
254
255 yield* machines.stopMachine({ ...target, signal: "SIGTERM" });
256 yield* machines
257 .waitMachine({
258 ...target,
259 instance_id: running.instance_id,
260 state: "stopped",
261 timeout: 8,
262 })
263 .pipe(
264 Effect.retry({
265 while: (error) =>
266 error._tag === "GatewayTimeout" || error._tag === "MachineWaitTimeout",
267 schedule: Schedule.spaced("1 second"),
268 times: 3,
269 }),
270 );
271 expect((yield* machines.getMachine(target)).state).toBe("stopped");
272
273 const restarted = yield* ensureStarted(created.appName, running, false);
274 expect(restarted.state).toBe("started");
275 expect((yield* machines.getMachine(target)).state).toBe("started");
276
277 yield* stack.destroy();
278 expect(yield* waitUntilGone(created.appName, created.machineId)).toBe("gone");
279 }).pipe(logLevel),
280 {
281 tags: ["provider:fly", "provider:fly:app", "provider:fly:machine", "live"],
282 timeout: 120_000,
283 },
284 );
285
286 test.provider(
287 "replace when name changes",
288 (stack) =>
289 Effect.gen(function* () {
290 yield* stack.destroy();
291
292 const created = yield* stack.deploy(
293 Effect.gen(function* () {
294 const app = yield* Fly.App("ReplaceSite");
295 return yield* Fly.Machine("ReplaceWeb", {
296 app,
297 region: "iad",
298 image: "nginx:alpine",
299 guest: { cpus: 1, memoryMb: 256 },
300 });
301 }),
302 );
303
304 expect(created.region).toEqual("iad");
305
306 const nextName = created.name.slice(0, -1) + (created.name.endsWith("z") ? "y" : "z");
307
308 const replaced = yield* stack.deploy(
309 Effect.gen(function* () {
310 const app = yield* Fly.App("ReplaceSite");
311 return yield* Fly.Machine("ReplaceWeb", {
312 app,
313 name: nextName,
314 region: "iad",
315 image: "nginx:alpine",
316 guest: { cpus: 1, memoryMb: 256 },
317 });
318 }),
319 );
320
321 expect(replaced.machineId).not.toEqual(created.machineId);
322 expect(replaced.name).toEqual(nextName);
323 expect(replaced.appName).toEqual(created.appName);
324 expect(replaced.region).toEqual("iad");
325 expect(replaced.state).toEqual("started");
326
327 const fetched = yield* machines.getMachine({
328 app_name: replaced.appName,
329 machine_id: replaced.machineId,
330 });
331 expect(fetched.id).toEqual(replaced.machineId);
332 expect(fetched.name).toEqual(nextName);
333
334 const oldGone = yield* waitUntilGone(created.appName, created.machineId);
335 expect(oldGone).toEqual("gone");
336
337 yield* stack.destroy();
338
339 const gone = yield* waitUntilGone(replaced.appName, replaced.machineId);
340 expect(gone).toEqual("gone");
341 }).pipe(logLevel),
342 {
343 tags: ["provider:fly", "provider:fly:app", "provider:fly:machine", "live"],
344 timeout: 120_000,
345 },
346 );
347
348 test.provider(
349 "list enumerates the deployed machine",
350 (stack) =>
351 Effect.gen(function* () {
352 yield* stack.destroy();
353
354 const deployed = yield* stack.deploy(
355 Effect.gen(function* () {
356 const app = yield* Fly.App("ListSite");
357 return yield* Fly.Machine("ListWeb", {
358 app,
359 region: "iad",
360 image: "nginx:alpine",
361 guest: { cpus: 1, memoryMb: 256 },
362 });
363 }),
364 );
365
366 const provider = yield* Provider.findProvider(Fly.Machine);
367 const all = yield* provider.list();
368 const found = all.find((machine) => machine.machineId === deployed.machineId);
369 expect(found).toBeDefined();
370 expect(found?.appName).toEqual(deployed.appName);
371 expect(found?.name).toEqual(deployed.name);
372 expect(found?.region).toEqual("iad");
373 expect(found?.state).toEqual("started");
374
375 yield* stack.destroy();
376
377 const gone = yield* waitUntilGone(deployed.appName, deployed.machineId);
378 expect(gone).toEqual("gone");
379 }).pipe(logLevel),
380 {
381 tags: ["provider:fly", "provider:fly:app", "provider:fly:machine", "live"],
382 timeout: 120_000,
383 },
384 );
385
386 test.provider(
387 "destroy recovers a machine whose initial service checks failed",
388 (stack) =>
389 Effect.gen(function* () {
390 yield* stack.destroy();
391 const app = yield* stack.deploy(Fly.App("UnhealthySite"));
392 const result = yield* stack
393 .deploy(
394 Effect.gen(function* () {
395 const app = yield* Fly.App("UnhealthySite");
396 return yield* Fly.Machine("UnhealthyWeb", {
397 app,
398 region: "iad",
399 image: "nginx:alpine",
400 guest: { cpus: 1, memoryMb: 256 },
401 count: 2,
402 services: [
403 {
404 protocol: "tcp",
405 internalPort: 80,
406 checks: [
407 {
408 type: "http",
409 port: 80,
410 path: "/missing",
411 interval: "5s",
412 timeout: "2s",
413 gracePeriod: "1s",
414 },
415 ],
416 },
417 ],
418 });
419 }),
420 )
421 .pipe(Effect.result);
422 expect(Result.isFailure(result)).toBe(true);
423 if (Result.isFailure(result)) {
424 expect(result.failure).toMatchObject({
425 _tag: "Fly.ReplicaChecksNotPassing",
426 });
427 }
428 const live = yield* machines.listMachines({ app_name: app.appName });
429 expect(live.filter((machine) => machine.state !== "destroyed")).toHaveLength(1);
430 yield* stack.destroy();
431 for (const machine of live) {
432 expect(yield* waitUntilGone(app.appName, machine.id!)).toBe("gone");
433 }
434 }).pipe(logLevel),
435 {
436 tags: ["provider:fly", "provider:fly:app", "provider:fly:machine", "live"],
437 timeout: 120_000,
438 },
439 );
440
441 test.provider(
442 "count 2 waits for checks and leaves the next replica unchanged on failure",
443 (stack) =>
444 Effect.gen(function* () {
445 yield* stack.destroy();
446
447 const deploy = (path: string) =>
448 stack.deploy(
449 Effect.gen(function* () {
450 const app = yield* Fly.App("CheckSite");
451 return yield* Fly.Machine("CheckWeb", {
452 app,
453 region: "iad",
454 image: "nginx:alpine",
455 guest: { cpus: 1, memoryMb: 256 },
456 count: 2,
457 services: [
458 {
459 protocol: "tcp",
460 internalPort: 80,
461 ports: [{ port: 80, handlers: ["http"] }],
462 checks: [
463 {
464 type: "http",
465 port: 80,
466 method: "GET",
467 path,
468 protocol: "http",
469 interval: "5s",
470 timeout: "2s",
471 gracePeriod: "5s",
472 },
473 {
474 type: "tcp",
475 port: 80,
476 interval: "5s",
477 timeout: "2s",
478 gracePeriod: "5s",
479 },
480 ],
481 },
482 ],
483 });
484 }),
485 );
486
487 const deployed = yield* deploy("/");
488 expect(deployed.count).toEqual(2);
489 expect(deployed.machineIds).toHaveLength(2);
490 expect(deployed.replicas).toHaveLength(2);
491 expect(deployed.state).toEqual("started");
492 expect(deployed.replicas[0]?.state).toEqual("started");
493 expect(deployed.replicas[1]?.state).toEqual("started");
494
495 for (const machineId of deployed.machineIds) {
496 const live = yield* machines.getMachine({
497 app_name: deployed.appName,
498 machine_id: machineId,
499 });
500 expect(live.state).toEqual("started");
501 const serviceChecks =
502 live.checks?.filter((check) => check.name?.startsWith("servicecheck-")) ?? [];
503 expect(serviceChecks).toHaveLength(2);
504 expect(serviceChecks.every((check) => check.status === "passing")).toEqual(true);
505 }
506
507 const secondBefore = yield* machines.getMachine({
508 app_name: deployed.appName,
509 machine_id: deployed.machineIds[1]!,
510 });
511 const failed = yield* deploy("/missing").pipe(Effect.result);
512 expect(Result.isFailure(failed)).toBe(true);
513 if (Result.isFailure(failed)) {
514 expect(failed.failure).toMatchObject({
515 _tag: "Fly.ReplicaChecksNotPassing",
516 });
517 }
518 const first = yield* machines.getMachine({
519 app_name: deployed.appName,
520 machine_id: deployed.machineIds[0]!,
521 });
522 const second = yield* machines.getMachine({
523 app_name: deployed.appName,
524 machine_id: deployed.machineIds[1]!,
525 });
526 expect(first.config?.services?.[0]?.checks?.[0]?.path).toBe("/missing");
527 expect(first.checks?.some((check) => check.status !== "passing")).toBe(true);
528 expect(second.config?.services?.[0]?.checks?.[0]?.path).toBe("/");
529 expect(second.instance_id).toBe(secondBefore.instance_id);
530
531 const recovered = yield* deploy("/");
532 expect(recovered.machineIds).toEqual(deployed.machineIds);
533 for (const machineId of recovered.machineIds) {
534 const live = yield* machines.getMachine({
535 app_name: recovered.appName,
536 machine_id: machineId,
537 });
538 expect(live.checks?.filter((check) => check.name?.startsWith("servicecheck-"))).toEqual([
539 expect.objectContaining({ status: "passing" }),
540 expect.objectContaining({ status: "passing" }),
541 ]);
542 }
543 yield* stack.destroy();
544
545 for (const machineId of deployed.machineIds) {
546 const gone = yield* waitUntilGone(deployed.appName, machineId);
547 expect(gone).toEqual("gone");
548 }
549 }).pipe(logLevel),
550 {
551 tags: ["provider:fly", "provider:fly:app", "provider:fly:machine", "live"],
552 timeout: 180_000,
553 },
554 );
555
556 test.provider(
557 "rolling named containers preserve group, digest pins, and mounted data",
558 (stack) =>
559 Effect.gen(function* () {
560 yield* stack.destroy();
561 const deploy = (version: string, includeObsolete = true) =>
562 stack.deploy(
563 Effect.gen(function* () {
564 const app = yield* Fly.App("MultiContainerSite");
565 return yield* Fly.Machine("Group", {
566 app,
567 region: "fra",
568 guest: { cpus: 1, memoryMb: 256 },
569 env: { COMMON: "machine", ROLE: "machine" },
570 containers: workload(version, includeObsolete),
571 mounts: [{ path: "/data", sizeGb: 1, encrypted: true }],
572 });
573 }),
574 );
575 const first = yield* deploy("one");
576 expect(first.state).toEqual("started");
577 const target = { app_name: first.appName, machine_id: first.machineId };
578 const observed = yield* machines.getMachine(target);
579 expect(observed.config?.containers?.map((container) => container.name)).toEqual([
580 "web",
581 "sidecar",
582 "obsolete",
583 ]);
584 expect(observed.config?.containers?.map((container) => container.image)).toEqual([
585 image,
586 image,
587 image,
588 ]);
589 expect(observed.config?.env?.COMMON).toEqual("machine");
590 expect(observed.config?.containers?.[0]?.env?.ROLE).toEqual("web");
591 expect(observed.config?.containers?.[1]?.env?.ROLE).toEqual("sidecar");
592 expect(observed.config?.containers?.[1]?.depends_on).toEqual([
593 { name: "web", condition: "healthy" },
594 ]);
595
596 const connected = yield* machines
597 .execMachine({
598 ...target,
599 container: "sidecar",
600 command: [
601 "sh",
602 "-c",
603 "wget -qO- http://127.0.0.1:80/ | grep -q 'Welcome to nginx' && printf connected",
604 ],
605 timeout: 10,
606 })
607 .pipe(
608 Effect.repeat({
609 schedule: Schedule.spaced("2 seconds"),
610 times: 8,
611 until: (result) => result.stdout?.includes("connected") === true,
612 }),
613 );
614 expect(connected.stdout).toContain("connected");
615 expect(connected.exit_code).toBe(0);
616 const environment = yield* machines.execMachine({
617 ...target,
618 container: "sidecar",
619 command: ["sh", "-c", 'printf "%s|%s" "$COMMON" "$ROLE"'],
620 timeout: 10,
621 });
622 expect(environment.stdout).toContain("machine|sidecar");
623 expect(environment.exit_code).toBe(0);
624 const written = yield* machines.execMachine({
625 ...target,
626 container: "web",
627 command: [
628 "sh",
629 "-c",
630 "printf persistent > /data/ogm-1764-marker && cat /data/ogm-1764-marker",
631 ],
632 timeout: 10,
633 });
634 expect(written.stdout).toContain("persistent");
635
636 const unchanged = yield* deploy("one");
637 expect(unchanged.machineId).toEqual(first.machineId);
638 const same = yield* machines.getMachine(target);
639 expect(same.instance_id).toEqual(observed.instance_id);
640
641 const updated = yield* deploy("two", false);
642 expect(updated.machineId).toEqual(first.machineId);
643 expect(updated.mounts[0]?.volumeId).toEqual(first.mounts[0]?.volumeId);
644 const after = yield* machines.getMachine(target);
645 expect(after.config?.containers?.map((container) => container.name)).toEqual([
646 "web",
647 "sidecar",
648 ]);
649 expect(after.config?.containers?.map((container) => container.image)).toEqual([
650 image,
651 nextImage,
652 ]);
653 expect(after.config?.containers?.[1]?.env?.VERSION).toEqual("two");
654 const persisted = yield* machines.execMachine({
655 ...target,
656 container: "web",
657 command: ["cat", "/data/ogm-1764-marker"],
658 timeout: 10,
659 });
660 expect(persisted.stdout).toContain("persistent");
661 yield* stack.destroy();
662 const gone = yield* machines.getApp({ app_name: first.appName }).pipe(
663 Effect.as(false),
664 Effect.catchTag("NotFound", () => Effect.succeed(true)),
665 );
666 expect(gone).toBe(true);
667 }).pipe(logLevel),
668 {
669 tags: ["provider:fly", "provider:fly:app", "provider:fly:machine", "live"],
670 timeout: 120_000,
671 },
672 );
673
674 test.provider(
675 "rerun converges on a named group already updated outside Alchemy",
676 (stack) =>
677 Effect.gen(function* () {
678 yield* stack.destroy();
679 const deploy = (sidecarImage: string) =>
680 stack.deploy(
681 Effect.gen(function* () {
682 const app = yield* Fly.App("InterruptedMultiContainerSite");
683 return yield* Fly.Machine("Group", {
684 app,
685 region: "fra",
686 guest: { cpus: 1, memoryMb: 256 },
687 mounts: [{ path: "/data", sizeGb: 1, encrypted: true }],
688 containers: [
689 { name: "web", image },
690 {
691 name: "sidecar",
692 image: sidecarImage,
693 cmd: ["sh", "-c", "sleep 300"],
694 },
695 ],
696 });
697 }),
698 );
699 const first = yield* deploy(image);
700 const target = { app_name: first.appName, machine_id: first.machineId };
701
702 // Apply the next deployment directly, as if Fly accepted Alchemy's
703 // update but the response never arrived.
704 const before = yield* machines.getMachine(target);
705 const accepted = yield* machines.updateMachine({
706 ...target,
707 config: {
708 ...before.config,
709 containers: before.config?.containers?.map((container) =>
710 container.name === "sidecar" ? { ...container, image: nextImage } : container,
711 ),
712 },
713 });
714 yield* machines.waitMachine({
715 ...target,
716 state: "started",
717 instance_id: accepted.instance_id,
718 timeout: 30,
719 });
720
721 const converged = yield* deploy(nextImage);
722 expect(converged.machineId).toBe(first.machineId);
723 expect(converged.mounts[0]?.volumeId).toBe(first.mounts[0]?.volumeId);
724 const observed = yield* machines.getMachine(target);
725 expect(observed.instance_id).toBe(accepted.instance_id);
726 expect(
727 observed.config?.containers?.map(({ name, image }) => ({
728 name,
729 image,
730 })),
731 ).toEqual([
732 { name: "web", image },
733 { name: "sidecar", image: nextImage },
734 ]);
735 const inventory = (yield* machines.listMachines({
736 app_name: first.appName,
737 })).filter((machine) => machine.state !== "destroyed");
738 expect(inventory.map(({ id }) => id)).toEqual([first.machineId]);
739 const volumes = yield* machines.listVolumes({ app_name: first.appName });
740 expect(volumes.map(({ id }) => id)).toEqual([first.mounts[0]?.volumeId]);
741 yield* stack.destroy();
742 const gone = yield* machines.getApp({ app_name: first.appName }).pipe(
743 Effect.as(false),
744 Effect.catchTag("NotFound", () => Effect.succeed(true)),
745 );
746 expect(gone).toBe(true);
747 }).pipe(logLevel),
748 {
749 tags: ["provider:fly", "provider:fly:app", "provider:fly:machine", "live"],
750 timeout: 120_000,
751 },
752 );
753
754 test.provider(
755 "invalid container groups fail before any Machine is created",
756 (stack) =>
757 Effect.gen(function* () {
758 yield* stack.destroy();
759 const app = yield* stack.deploy(Fly.App("InvalidContainerSite"));
760 const invalid: Array<[string, MachineContainer[]]> = [
761 ["empty group", []],
762 ["empty image", [{ name: "web", image: "" }]],
763 [
764 "duplicate names",
765 [
766 { name: "web", image },
767 { name: "web", image: nextImage },
768 ],
769 ],
770 ["undeclared dependency", [{ name: "web", image, dependsOn: [{ name: "absent" }] }]],
771 [
772 "exec override",
773 // @ts-expect-error Fly accepts container exec without running it
774 [{ name: "web", image, exec: ["nginx", "-g", "daemon off;"] }],
775 ],
776 ];
777 for (const [label, containers] of invalid) {
778 const failed = yield* stack
779 .deploy(
780 Effect.gen(function* () {
781 const app = yield* Fly.App("InvalidContainerSite");
782 return yield* Fly.Machine("Group", {
783 app,
784 region: "iad",
785 guest: { cpus: 1, memoryMb: 256 },
786 containers,
787 });
788 }),
789 )
790 .pipe(Effect.flip);
791 expect(failed, label).toMatchObject({
792 _tag: "Fly.InvalidMachineContainers",
793 });
794 }
795 const failed = yield* stack
796 .deploy(
797 Effect.gen(function* () {
798 const app = yield* Fly.App("InvalidContainerSite");
799 return yield* Fly.Machine("Group", {
800 app,
801 region: "iad",
802 guest: { cpus: 1, memoryMb: 256 },
803 containers: [
804 {
805 name: "web",
806 image: "docker-hub-mirror.fly.io/library/nginx:alpine",
807 },
808 ],
809 checks: { ready: { type: "tcp", port: 80 } },
810 deploy: { strategy: "bluegreen" },
811 });
812 }),
813 )
814 .pipe(Effect.flip);
815 expect(failed).toMatchObject({ _tag: "Fly.InvalidDeployment" });
816 const live = yield* machines.listMachines({ app_name: app.appName });
817 expect(live.filter((machine) => machine.state !== "destroyed")).toEqual([]);
818 yield* stack.destroy();
819 }).pipe(logLevel),
820 {
821 tags: ["provider:fly", "provider:fly:app", "provider:fly:machine", "live"],
822 timeout: 120_000,
823 },
824 );