|
| 1 | +import type { ChildProcess } from "node:child_process"; |
| 2 | +import { EventEmitter } from "node:events"; |
1 | 3 | import fs from "node:fs/promises"; |
2 | 4 | import { beforeEach, describe, expect, it, vi } from "vitest"; |
3 | 5 |
|
| 6 | +const spawnMock = vi.hoisted(() => vi.fn()); |
| 7 | + |
| 8 | +vi.mock("node:child_process", () => ({ |
| 9 | + spawn: spawnMock, |
| 10 | +})); |
| 11 | + |
4 | 12 | const profileMocks = vi.hoisted(() => ({ |
5 | 13 | buildCellRunArgs: vi.fn((_profile: unknown, options: { environmentFile: string }) => [ |
6 | 14 | "run", |
@@ -34,6 +42,23 @@ function successfulExecutor() { |
34 | 42 | })); |
35 | 43 | } |
36 | 44 |
|
| 45 | +function createStreamChild(pid?: number): ChildProcess { |
| 46 | + const stdout = Object.assign(new EventEmitter(), { |
| 47 | + pause: vi.fn(), |
| 48 | + resume: vi.fn(), |
| 49 | + }); |
| 50 | + const stderr = Object.assign(new EventEmitter(), { |
| 51 | + pause: vi.fn(), |
| 52 | + resume: vi.fn(), |
| 53 | + }); |
| 54 | + return Object.assign(new EventEmitter(), { |
| 55 | + pid, |
| 56 | + stdout, |
| 57 | + stderr, |
| 58 | + kill: vi.fn(() => true), |
| 59 | + }) as unknown as ChildProcess; |
| 60 | +} |
| 61 | + |
37 | 62 | describe("fleet container runtime", () => { |
38 | 63 | beforeEach(() => { |
39 | 64 | vi.clearAllMocks(); |
@@ -464,6 +489,39 @@ describe("fleet container runtime", () => { |
464 | 489 | ).rejects.toThrow(/podman logs failed with signal SIGSEGV/iu); |
465 | 490 | }); |
466 | 491 |
|
| 492 | + it("rejects a log stream spawn error when the child has no pid", async () => { |
| 493 | + const child = createStreamChild(); |
| 494 | + spawnMock.mockReturnValue(child); |
| 495 | + const runtime = createFleetContainerRuntime(successfulExecutor()); |
| 496 | + |
| 497 | + const logs = runtime.logs("podman", "cell-acme", { follow: true, redactValues: [] }); |
| 498 | + child.emit("error", new Error("spawn failed")); |
| 499 | + |
| 500 | + await expect(logs).rejects.toThrow("spawn failed"); |
| 501 | + child.emit("close", -2, null); |
| 502 | + }); |
| 503 | + |
| 504 | + it("waits for log stream close across repeated operational errors", async () => { |
| 505 | + const child = createStreamChild(4242); |
| 506 | + spawnMock.mockReturnValue(child); |
| 507 | + const runtime = createFleetContainerRuntime(successfulExecutor()); |
| 508 | + let settled = false; |
| 509 | + |
| 510 | + const logs = runtime |
| 511 | + .logs("podman", "cell-acme", { follow: true, redactValues: [] }) |
| 512 | + .finally(() => { |
| 513 | + settled = true; |
| 514 | + }); |
| 515 | + child.emit("error", new Error("first signal delivery failed")); |
| 516 | + child.emit("error", new Error("second signal delivery failed")); |
| 517 | + await Promise.resolve(); |
| 518 | + |
| 519 | + expect(settled).toBe(false); |
| 520 | + |
| 521 | + child.emit("close", 0, null); |
| 522 | + await expect(logs).resolves.toBeUndefined(); |
| 523 | + }); |
| 524 | + |
467 | 525 | it("creates and removes a labeled per-cell network", async () => { |
468 | 526 | const executor = successfulExecutor(); |
469 | 527 | const runtime = createFleetContainerRuntime(executor); |
|
0 commit comments