forked from testcontainers/testcontainers-java
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix extra newlines in container log / exec output (testcontainers#3752)
Fixes testcontainers#1763 Fixes testcontainers#1854 Co-authored-by: Sergei Egorov <[email protected]>
- Loading branch information
Showing
2 changed files
with
89 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
89 changes: 89 additions & 0 deletions
89
core/src/test/java/org/testcontainers/containers/output/ToStringConsumerTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"); | ||
} | ||
} | ||
} |