Skip to content

Remove extra newlines in container log output #3752

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Feb 10, 2021
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
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;
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This essentially reverts the change done here: https://github.com/testcontainers/testcontainers-java/pull/643/files#diff-321431024ed34199142ebf2d70a46ba89f8a09e66582d40527885123e429b4d9R22-R24

It's not entirely clear to me if this will work for all use cases; it could very well be that this breaks (so it might be that we'll have to conditionalize it). I was unable to run all the tests locally, so maybe CI will shed some light on this.

}
} 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");
}
}
}