Skip to content

Commit

Permalink
Fix extra newlines in container log / exec output (#3752)
Browse files Browse the repository at this point in the history
Fixes #1763
Fixes #1854

Co-authored-by: Sergei Egorov <segorov@vmware.com>
  • Loading branch information
perlun and bsideup committed Feb 10, 2021
1 parent b0f1bbc commit 106463b
Show file tree
Hide file tree
Showing 2 changed files with 89 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,21 +10,14 @@
* Created by rnorth on 26/03/2016.
*/
public class ToStringConsumer extends BaseConsumer<ToStringConsumer> {
private static final byte[] NEW_LINE = "\n".getBytes();

private boolean firstLine = true;
private ByteArrayOutputStream stringBuffer = new ByteArrayOutputStream();

@Override
public void accept(OutputFrame outputFrame) {
try {
if (outputFrame.getBytes() != null) {
if (!firstLine) {
stringBuffer.write(NEW_LINE);
}
stringBuffer.write(outputFrame.getBytes());
stringBuffer.flush();
firstLine = false;
}
} catch (IOException e) {
throw new RuntimeException(e);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
package org.testcontainers.containers.output;

import static org.assertj.core.api.Assertions.assertThat;

import org.apache.commons.lang.RandomStringUtils;
import org.assertj.core.api.Assertions;
import org.junit.Test;
import org.testcontainers.containers.Container.ExecResult;
import org.testcontainers.containers.GenericContainer;
import org.testcontainers.containers.startupcheck.OneShotStartupCheckStrategy;

public class ToStringConsumerTest {

private static final String LARGE_PAYLOAD;

static {
StringBuilder builder = new StringBuilder(10_003 * 10);;
for (int i = 0; i < 10; i++) {
builder.append(' ').append(i).append(RandomStringUtils.randomAlphabetic(10000));
}
LARGE_PAYLOAD = builder.toString();
Assertions.assertThat(LARGE_PAYLOAD).doesNotContain("\n");
}

@Test
public void newlines_are_not_added_to_exec_output() throws Exception {
try (GenericContainer<?> container = new GenericContainer<>("alpine:3.13")) {
container.withCommand("sleep", "2m");
container.start();

ExecResult build = container.execInContainer("echo", "-n", LARGE_PAYLOAD);
Assertions.assertThat(build.getStdout())
.doesNotContain("\n")
.isEqualTo(LARGE_PAYLOAD);
}
}

@Test(timeout = 60_000L)
public void newlines_are_not_added_to_exec_output_with_tty() throws Exception {
try (GenericContainer<?> container = new GenericContainer<>("alpine:3.13")) {
container.withCreateContainerCmdModifier(cmd -> {
cmd
.withAttachStdin(true)
.withStdinOpen(true)
.withTty(true);
});
container.withCommand("sleep", "2m");
container.start();

ExecResult build = container.execInContainer("echo", "-n", LARGE_PAYLOAD);
assertThat(build.getStdout())
.isEqualTo(LARGE_PAYLOAD)
.doesNotContain("\n");
}
}

@Test
public void newlines_are_not_added_to_container_output() {
try (GenericContainer<?> container = new GenericContainer<>("alpine:3.13")) {
container.withCommand("echo", "-n", LARGE_PAYLOAD);
container.setStartupCheckStrategy(new OneShotStartupCheckStrategy());
container.start();

container.getDockerClient().waitContainerCmd(container.getContainerId()).start().awaitStatusCode();

assertThat(container.getLogs())
.isEqualTo(LARGE_PAYLOAD)
.doesNotContain("\n");
}
}

@Test
public void newlines_are_not_added_to_container_output_with_tty() {
try (GenericContainer<?> container = new GenericContainer<>("alpine:3.13")) {
container.withCreateContainerCmdModifier(cmd -> {
cmd.withTty(true);
});
container.withCommand("echo", "-n", LARGE_PAYLOAD);
container.setStartupCheckStrategy(new OneShotStartupCheckStrategy());
container.start();

container.getDockerClient().waitContainerCmd(container.getContainerId()).start().awaitStatusCode();

assertThat(container.getLogs())
.isEqualTo(LARGE_PAYLOAD)
.doesNotContain("\n");
}
}
}

0 comments on commit 106463b

Please sign in to comment.