Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
import { PassThrough, Readable } from "stream";
import { DockerContainerClient } from "./docker-container-client";

describe("DockerContainerClient", () => {
describe("exec", () => {
it("should not truncate output when the demuxed streams flush after the raw stream ends", async () => {
const payload = "the-final-line-that-must-not-be-truncated\n";

const rawStream = new PassThrough();

const exec = {
start: vi.fn(async () => {
process.nextTick(() => {
rawStream.write(payload);
rawStream.end();
});
return rawStream;
}),
inspect: vi.fn(async () => ({ ExitCode: 0 })),
};

const container = {
id: "container-id",
exec: vi.fn(async () => exec),
};

const dockerode = {
modem: {
demuxStream: (raw: Readable, stdout: PassThrough) => {
stdout.cork();
raw.on("data", (chunk) => stdout.write(chunk));
setImmediate(() => stdout.uncork());
},
},
// eslint-disable-next-line @typescript-eslint/no-explicit-any
} as any;

const client = new DockerContainerClient(dockerode);

// eslint-disable-next-line @typescript-eslint/no-explicit-any
const result = await client.exec(container as any, ["echo", "hi"]);

expect(result.exitCode).toBe(0);
expect(result.stdout).toBe(payload);
expect(result.output).toBe(payload);
});
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import Dockerode, {
} from "dockerode";
import { IncomingMessage } from "http";
import { PassThrough, Readable } from "stream";
import { finished } from "stream/promises";
import { execLog, log, streamToString, toSeconds } from "../../../common";
import { CopyToContainerOptions } from "../../../types";
import { ContainerClient } from "./container-client";
Expand Down Expand Up @@ -253,11 +254,18 @@ export class DockerContainerClient implements ContainerClient {
processStream(stdoutStream, stdoutChunks);
processStream(stderrStream, stderrChunks);

await new Promise((res, rej) => {
stream.on("end", res);
stream.on("error", rej);
});
stream.destroy();
try {
await new Promise<void>((res, rej) => {
stream.on("end", res);
stream.on("error", rej);
});

stdoutStream.end();
stderrStream.end();
await Promise.all([finished(stdoutStream), finished(stderrStream)]);
} finally {
stream.destroy();
}

const inspectResult = await exec.inspect();
const exitCode = inspectResult.ExitCode ?? -1;
Expand Down
Loading