|
| 1 | +package org.testcontainers.junit; |
| 2 | + |
| 3 | +import lombok.extern.slf4j.Slf4j; |
| 4 | +import org.junit.Rule; |
| 5 | +import org.junit.Test; |
| 6 | +import org.junit.rules.Timeout; |
| 7 | +import org.testcontainers.containers.GenericContainer; |
| 8 | +import org.testcontainers.containers.output.OutputFrame; |
| 9 | +import org.testcontainers.containers.output.Slf4jLogConsumer; |
| 10 | +import org.testcontainers.containers.output.ToStringConsumer; |
| 11 | +import org.testcontainers.containers.output.WaitingConsumer; |
| 12 | +import org.testcontainers.containers.startupcheck.OneShotStartupCheckStrategy; |
| 13 | + |
| 14 | +import java.util.concurrent.TimeUnit; |
| 15 | +import java.util.concurrent.TimeoutException; |
| 16 | +import java.util.function.Consumer; |
| 17 | + |
| 18 | +import static org.rnorth.visibleassertions.VisibleAssertions.*; |
| 19 | +import static org.testcontainers.containers.output.OutputFrame.OutputType.STDOUT; |
| 20 | + |
| 21 | +@Slf4j |
| 22 | +public class OutputStreamWithTTYTest { |
| 23 | + |
| 24 | + @Rule |
| 25 | + public GenericContainer container = new GenericContainer<>("alpine:3.2") |
| 26 | + .withCommand("ls -1") |
| 27 | + .withStartupCheckStrategy(new OneShotStartupCheckStrategy()) |
| 28 | + .withCreateContainerCmdModifier(command -> command.withTty(true)); |
| 29 | + |
| 30 | + @Rule |
| 31 | + public Timeout globalTimeout = Timeout.seconds(10); |
| 32 | + |
| 33 | + @Test |
| 34 | + public void testFetchStdout() throws TimeoutException { |
| 35 | + WaitingConsumer consumer = new WaitingConsumer(); |
| 36 | + |
| 37 | + container.followOutput(consumer, STDOUT); |
| 38 | + |
| 39 | + consumer.waitUntil(frame -> frame.getType() == STDOUT && frame.getUtf8String().contains("home"), 4, TimeUnit.SECONDS); |
| 40 | + } |
| 41 | + |
| 42 | + @Test |
| 43 | + public void testFetchStdoutWithTimeout() { |
| 44 | + WaitingConsumer consumer = new WaitingConsumer(); |
| 45 | + |
| 46 | + container.followOutput(consumer, STDOUT); |
| 47 | + |
| 48 | + assertThrows("a TimeoutException should be thrown", TimeoutException.class, () -> { |
| 49 | + consumer.waitUntil(frame -> frame.getType() == STDOUT && frame.getUtf8String().contains("qqq"), 1, TimeUnit.SECONDS); |
| 50 | + return true; |
| 51 | + }); |
| 52 | + } |
| 53 | + |
| 54 | + @Test |
| 55 | + public void testFetchStdoutWithNoLimit() throws TimeoutException { |
| 56 | + WaitingConsumer consumer = new WaitingConsumer(); |
| 57 | + |
| 58 | + container.followOutput(consumer, STDOUT); |
| 59 | + |
| 60 | + consumer.waitUntil(frame -> frame.getType() == STDOUT && frame.getUtf8String().contains("home")); |
| 61 | + } |
| 62 | + |
| 63 | + @Test |
| 64 | + public void testLogConsumer() throws TimeoutException { |
| 65 | + WaitingConsumer waitingConsumer = new WaitingConsumer(); |
| 66 | + Slf4jLogConsumer logConsumer = new Slf4jLogConsumer(log); |
| 67 | + |
| 68 | + Consumer<OutputFrame> composedConsumer = logConsumer.andThen(waitingConsumer); |
| 69 | + container.followOutput(composedConsumer); |
| 70 | + |
| 71 | + waitingConsumer.waitUntil(frame -> frame.getType() == STDOUT && frame.getUtf8String().contains("home")); |
| 72 | + } |
| 73 | + |
| 74 | + @Test |
| 75 | + public void testToStringConsumer() throws TimeoutException { |
| 76 | + WaitingConsumer waitingConsumer = new WaitingConsumer(); |
| 77 | + ToStringConsumer toStringConsumer = new ToStringConsumer(); |
| 78 | + |
| 79 | + Consumer<OutputFrame> composedConsumer = toStringConsumer.andThen(waitingConsumer); |
| 80 | + container.followOutput(composedConsumer); |
| 81 | + |
| 82 | + waitingConsumer.waitUntilEnd(4, TimeUnit.SECONDS); |
| 83 | + |
| 84 | + String utf8String = toStringConsumer.toUtf8String(); |
| 85 | + assertTrue("the expected first value was found", utf8String.contains("home")); |
| 86 | + assertTrue("the expected last value was found", utf8String.contains("media")); |
| 87 | + assertFalse("a non-expected value was found", utf8String.contains("qqq")); |
| 88 | + } |
| 89 | +} |
0 commit comments