Skip to content

Commit 87a5e5e

Browse files
Log stream fix for Podman (#968)
1 parent b92f669 commit 87a5e5e

File tree

3 files changed

+26
-15
lines changed

3 files changed

+26
-15
lines changed

packages/modules/kafka/src/kafka-container.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,7 @@ describe("KafkaContainer", { timeout: 240_000 }, () => {
148148

149149
const kafkaCliContainer = await new GenericContainer(KAFKA_IMAGE)
150150
.withNetwork(network)
151-
.withCommand(["bash", "-c", "echo 'START'; sleep infinity"])
151+
.withCommand(["bash", "-c", "sleep infinity"])
152152
.withCopyFilesToContainer([
153153
{
154154
source: path.resolve(certificatesDir, "kafka.client.truststore.pem"),

packages/modules/localstack/src/localstack-container.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ describe("LocalStackContainer", { timeout: 180_000 }, () => {
5151
const awsCliInDockerNetwork = await new GenericContainer("amazon/aws-cli:2.7.27")
5252
.withNetwork(network)
5353
.withEntrypoint(["bash"])
54-
.withCommand(["-c", "echo 'START'; sleep infinity"])
54+
.withCommand(["-c", "sleep infinity"])
5555
.withEnvironment({
5656
AWS_ACCESS_KEY_ID: "test",
5757
AWS_SECRET_ACCESS_KEY: "test",

packages/testcontainers/src/container-runtime/clients/container/docker-container-client.ts

Lines changed: 24 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -161,24 +161,35 @@ export class DockerContainerClient implements ContainerClient {
161161
}
162162
}
163163

164-
async logs(container: Container, opts?: ContainerLogsOptions): Promise<Readable> {
165-
try {
166-
log.debug(`Fetching container logs...`, { containerId: container.id });
167-
const stream = (await container.logs({
164+
logs(container: Container, opts?: ContainerLogsOptions): Promise<Readable> {
165+
log.debug(`Fetching container logs...`, { containerId: container.id });
166+
167+
const proxyStream = new PassThrough();
168+
proxyStream.setEncoding("utf8");
169+
170+
container
171+
.logs({
168172
follow: true,
169173
stdout: true,
170174
stderr: true,
171175
tail: opts?.tail ?? -1,
172176
since: opts?.since ?? 0,
173-
})) as IncomingMessage;
174-
stream.socket.unref();
175-
const demuxedStream = this.demuxStream(container.id, stream);
176-
log.debug(`Fetched container logs`, { containerId: container.id });
177-
return demuxedStream;
178-
} catch (err) {
179-
log.error(`Failed to fetch container logs: ${err}`, { containerId: container.id });
180-
throw err;
181-
}
177+
})
178+
.then(async (stream) => {
179+
const actualLogStream = stream as IncomingMessage;
180+
actualLogStream.socket?.unref();
181+
182+
const demuxedStream = await this.demuxStream(container.id, actualLogStream);
183+
demuxedStream.pipe(proxyStream);
184+
demuxedStream.on("error", (err) => proxyStream.emit("error", err));
185+
demuxedStream.on("end", () => proxyStream.end());
186+
})
187+
.catch((err) => {
188+
log.error(`Failed to fetch container logs: ${err}`, { containerId: container.id });
189+
proxyStream.end();
190+
});
191+
192+
return Promise.resolve(proxyStream);
182193
}
183194

184195
async exec(container: Container, command: string[], opts?: Partial<ExecOptions>): Promise<ExecResult> {

0 commit comments

Comments
 (0)