Skip to content

Commit 106463b

Browse files
perlunbsideup
andauthored
Fix extra newlines in container log / exec output (#3752)
Fixes #1763 Fixes #1854 Co-authored-by: Sergei Egorov <segorov@vmware.com>
1 parent b0f1bbc commit 106463b

File tree

2 files changed

+89
-7
lines changed

2 files changed

+89
-7
lines changed

core/src/main/java/org/testcontainers/containers/output/ToStringConsumer.java

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,21 +10,14 @@
1010
* Created by rnorth on 26/03/2016.
1111
*/
1212
public class ToStringConsumer extends BaseConsumer<ToStringConsumer> {
13-
private static final byte[] NEW_LINE = "\n".getBytes();
14-
15-
private boolean firstLine = true;
1613
private ByteArrayOutputStream stringBuffer = new ByteArrayOutputStream();
1714

1815
@Override
1916
public void accept(OutputFrame outputFrame) {
2017
try {
2118
if (outputFrame.getBytes() != null) {
22-
if (!firstLine) {
23-
stringBuffer.write(NEW_LINE);
24-
}
2519
stringBuffer.write(outputFrame.getBytes());
2620
stringBuffer.flush();
27-
firstLine = false;
2821
}
2922
} catch (IOException e) {
3023
throw new RuntimeException(e);
Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
package org.testcontainers.containers.output;
2+
3+
import static org.assertj.core.api.Assertions.assertThat;
4+
5+
import org.apache.commons.lang.RandomStringUtils;
6+
import org.assertj.core.api.Assertions;
7+
import org.junit.Test;
8+
import org.testcontainers.containers.Container.ExecResult;
9+
import org.testcontainers.containers.GenericContainer;
10+
import org.testcontainers.containers.startupcheck.OneShotStartupCheckStrategy;
11+
12+
public class ToStringConsumerTest {
13+
14+
private static final String LARGE_PAYLOAD;
15+
16+
static {
17+
StringBuilder builder = new StringBuilder(10_003 * 10);;
18+
for (int i = 0; i < 10; i++) {
19+
builder.append(' ').append(i).append(RandomStringUtils.randomAlphabetic(10000));
20+
}
21+
LARGE_PAYLOAD = builder.toString();
22+
Assertions.assertThat(LARGE_PAYLOAD).doesNotContain("\n");
23+
}
24+
25+
@Test
26+
public void newlines_are_not_added_to_exec_output() throws Exception {
27+
try (GenericContainer<?> container = new GenericContainer<>("alpine:3.13")) {
28+
container.withCommand("sleep", "2m");
29+
container.start();
30+
31+
ExecResult build = container.execInContainer("echo", "-n", LARGE_PAYLOAD);
32+
Assertions.assertThat(build.getStdout())
33+
.doesNotContain("\n")
34+
.isEqualTo(LARGE_PAYLOAD);
35+
}
36+
}
37+
38+
@Test(timeout = 60_000L)
39+
public void newlines_are_not_added_to_exec_output_with_tty() throws Exception {
40+
try (GenericContainer<?> container = new GenericContainer<>("alpine:3.13")) {
41+
container.withCreateContainerCmdModifier(cmd -> {
42+
cmd
43+
.withAttachStdin(true)
44+
.withStdinOpen(true)
45+
.withTty(true);
46+
});
47+
container.withCommand("sleep", "2m");
48+
container.start();
49+
50+
ExecResult build = container.execInContainer("echo", "-n", LARGE_PAYLOAD);
51+
assertThat(build.getStdout())
52+
.isEqualTo(LARGE_PAYLOAD)
53+
.doesNotContain("\n");
54+
}
55+
}
56+
57+
@Test
58+
public void newlines_are_not_added_to_container_output() {
59+
try (GenericContainer<?> container = new GenericContainer<>("alpine:3.13")) {
60+
container.withCommand("echo", "-n", LARGE_PAYLOAD);
61+
container.setStartupCheckStrategy(new OneShotStartupCheckStrategy());
62+
container.start();
63+
64+
container.getDockerClient().waitContainerCmd(container.getContainerId()).start().awaitStatusCode();
65+
66+
assertThat(container.getLogs())
67+
.isEqualTo(LARGE_PAYLOAD)
68+
.doesNotContain("\n");
69+
}
70+
}
71+
72+
@Test
73+
public void newlines_are_not_added_to_container_output_with_tty() {
74+
try (GenericContainer<?> container = new GenericContainer<>("alpine:3.13")) {
75+
container.withCreateContainerCmdModifier(cmd -> {
76+
cmd.withTty(true);
77+
});
78+
container.withCommand("echo", "-n", LARGE_PAYLOAD);
79+
container.setStartupCheckStrategy(new OneShotStartupCheckStrategy());
80+
container.start();
81+
82+
container.getDockerClient().waitContainerCmd(container.getContainerId()).start().awaitStatusCode();
83+
84+
assertThat(container.getLogs())
85+
.isEqualTo(LARGE_PAYLOAD)
86+
.doesNotContain("\n");
87+
}
88+
}
89+
}

0 commit comments

Comments
 (0)