|
| 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