org.testcontainers.containers.output.WaitingConsumer Java Examples

The following examples show how to use org.testcontainers.containers.output.WaitingConsumer. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: EtcdContainer.java    From etcd4j with Apache License 2.0 7 votes vote down vote up
private WaitStrategy waitStrategy() {
    return new AbstractWaitStrategy() {
        @Override
        protected void waitUntilReady() {
            final DockerClient client = DockerClientFactory.instance().client();
            final WaitingConsumer waitingConsumer = new WaitingConsumer();

            LogUtils.followOutput(client, waitStrategyTarget.getContainerId(), waitingConsumer);

            try {
                waitingConsumer.waitUntil(
                    f -> f.getUtf8String().contains("etcdserver: published"),
                    startupTimeout.getSeconds(),
                    TimeUnit.SECONDS,
                    1
                );
            } catch (TimeoutException e) {
                throw new ContainerLaunchException("Timed out");
            }
        }
    };
}
 
Example #2
Source File: LogUtils.java    From testcontainers-java with MIT License 6 votes vote down vote up
/**
 * Retrieve all previous log outputs for a container of the specified type(s).
 *
 * @param dockerClient a Docker client
 * @param containerId  container ID to attach to
 * @param types        types of {@link OutputFrame} to receive
 * @return all previous output frames (stdout/stderr being separated by newline characters)
 */
@SneakyThrows(IOException.class)
public String getOutput(DockerClient dockerClient,
                        String containerId,
                        OutputFrame.OutputType... types) {

    if (containerId == null) {
        return "";
    }

    if (types.length == 0) {
        types = new OutputFrame.OutputType[] { STDOUT, STDERR };
    }

    final ToStringConsumer consumer = new ToStringConsumer();
    final WaitingConsumer wait = new WaitingConsumer();
    try (Closeable closeable = attachConsumer(dockerClient, containerId, consumer.andThen(wait), false, types)) {
        wait.waitUntilEnd();
        return consumer.toUtf8String();
    }
}
 
Example #3
Source File: OutputStreamTest.java    From testcontainers-java with MIT License 6 votes vote down vote up
@Test(timeout = 60_000L)
public void testToStringConsumer() throws TimeoutException {

    WaitingConsumer waitingConsumer = new WaitingConsumer();
    ToStringConsumer toStringConsumer = new ToStringConsumer();

    Consumer<OutputFrame> composedConsumer = toStringConsumer.andThen(waitingConsumer);
    container.followOutput(composedConsumer);

    waitingConsumer.waitUntilEnd(30, TimeUnit.SECONDS);

    String utf8String = toStringConsumer.toUtf8String();
    assertTrue("the expected first value was found", utf8String.contains("seq=1"));
    assertTrue("the expected last value was found", utf8String.contains("seq=4"));
    assertFalse("a non-expected value was found", utf8String.contains("seq=42"));
}
 
Example #4
Source File: OutputStreamTest.java    From testcontainers-java with MIT License 5 votes vote down vote up
@Test(timeout = 60_000L)
public void testLogConsumer() throws TimeoutException {

    WaitingConsumer waitingConsumer = new WaitingConsumer();
    Slf4jLogConsumer logConsumer = new Slf4jLogConsumer(LOGGER);

    Consumer<OutputFrame> composedConsumer = logConsumer.andThen(waitingConsumer);
    container.followOutput(composedConsumer);

    waitingConsumer.waitUntil(frame -> frame.getType() == STDOUT && frame.getUtf8String().contains("seq=2"));
}
 
Example #5
Source File: DockerComposeLogConsumerTest.java    From testcontainers-java with MIT License 5 votes vote down vote up
@Test
public void testLogConsumer() throws TimeoutException {
    WaitingConsumer logConsumer = new WaitingConsumer();
    DockerComposeContainer environment = new DockerComposeContainer(new File("src/test/resources/v2-compose-test.yml"))
        .withExposedService("redis_1", 6379)
        .withLogConsumer("redis_1", logConsumer);

    try {
        environment.starting(Description.EMPTY);
        logConsumer.waitUntil(frame -> frame.getType() == STDOUT && frame.getUtf8String().contains("Ready to accept connections"), 5, TimeUnit.SECONDS);
    } finally {
        environment.finished(Description.EMPTY);
    }
}
 
Example #6
Source File: OutputStreamWithTTYTest.java    From testcontainers-java with MIT License 5 votes vote down vote up
@Test
public void testToStringConsumer() throws TimeoutException {
    WaitingConsumer waitingConsumer = new WaitingConsumer();
    ToStringConsumer toStringConsumer = new ToStringConsumer();

    Consumer<OutputFrame> composedConsumer = toStringConsumer.andThen(waitingConsumer);
    container.followOutput(composedConsumer);

    waitingConsumer.waitUntilEnd(4, TimeUnit.SECONDS);

    String utf8String = toStringConsumer.toUtf8String();
    assertTrue("the expected first value was found", utf8String.contains("home"));
    assertTrue("the expected last value was found", utf8String.contains("media"));
    assertFalse("a non-expected value was found", utf8String.contains("qqq"));
}
 
Example #7
Source File: OutputStreamWithTTYTest.java    From testcontainers-java with MIT License 5 votes vote down vote up
@Test
public void testLogConsumer() throws TimeoutException {
    WaitingConsumer waitingConsumer = new WaitingConsumer();
    Slf4jLogConsumer logConsumer = new Slf4jLogConsumer(log);

    Consumer<OutputFrame> composedConsumer = logConsumer.andThen(waitingConsumer);
    container.followOutput(composedConsumer);

    waitingConsumer.waitUntil(frame -> frame.getType() == STDOUT && frame.getUtf8String().contains("home"));
}
 
Example #8
Source File: OutputStreamWithTTYTest.java    From testcontainers-java with MIT License 5 votes vote down vote up
@Test
public void testFetchStdoutWithNoLimit() throws TimeoutException {
    WaitingConsumer consumer = new WaitingConsumer();

    container.followOutput(consumer, STDOUT);

    consumer.waitUntil(frame -> frame.getType() == STDOUT && frame.getUtf8String().contains("home"));
}
 
Example #9
Source File: OutputStreamWithTTYTest.java    From testcontainers-java with MIT License 5 votes vote down vote up
@Test
public void testFetchStdoutWithTimeout() {
    WaitingConsumer consumer = new WaitingConsumer();

    container.followOutput(consumer, STDOUT);

    assertThrows("a TimeoutException should be thrown", TimeoutException.class, () -> {
        consumer.waitUntil(frame -> frame.getType() == STDOUT && frame.getUtf8String().contains("qqq"), 1, TimeUnit.SECONDS);
        return true;
    });
}
 
Example #10
Source File: OutputStreamWithTTYTest.java    From testcontainers-java with MIT License 5 votes vote down vote up
@Test
public void testFetchStdout() throws TimeoutException {
    WaitingConsumer consumer = new WaitingConsumer();

    container.followOutput(consumer, STDOUT);

    consumer.waitUntil(frame -> frame.getType() == STDOUT && frame.getUtf8String().contains("home"), 4, TimeUnit.SECONDS);
}
 
Example #11
Source File: OutputStreamTest.java    From testcontainers-java with MIT License 5 votes vote down vote up
@Test(timeout = 60_000L)
public void testFetchStdoutWithTimeout() {

    WaitingConsumer consumer = new WaitingConsumer();

    container.followOutput(consumer, STDOUT);

    assertThrows("a TimeoutException should be thrown", TimeoutException.class, () -> {
        consumer.waitUntil(frame -> frame.getType() == STDOUT && frame.getUtf8String().contains("seq=5"),
                2, TimeUnit.SECONDS);
        return true;
    });
}
 
Example #12
Source File: LogMessageWaitStrategy.java    From testcontainers-java with MIT License 5 votes vote down vote up
@Override
@SneakyThrows(IOException.class)
protected void waitUntilReady() {
    WaitingConsumer waitingConsumer = new WaitingConsumer();

    LogContainerCmd cmd = DockerClientFactory.instance().client().logContainerCmd(waitStrategyTarget.getContainerId())
        .withFollowStream(true)
        .withSince(0)
        .withStdOut(true)
        .withStdErr(true);

    try (FrameConsumerResultCallback callback = new FrameConsumerResultCallback()) {
        callback.addConsumer(STDOUT, waitingConsumer);
        callback.addConsumer(STDERR, waitingConsumer);

        cmd.exec(callback);

        Predicate<OutputFrame> waitPredicate = outputFrame ->
            // (?s) enables line terminator matching (equivalent to Pattern.DOTALL)
            outputFrame.getUtf8String().matches("(?s)" + regEx);

        try {
            waitingConsumer.waitUntil(waitPredicate, startupTimeout.getSeconds(), TimeUnit.SECONDS, times);
        } catch (TimeoutException e) {
            throw new ContainerLaunchException("Timed out waiting for log output matching '" + regEx + "'");
        }
    }
}
 
Example #13
Source File: StartupCheckStrategyTest.java    From testcontainers-java with MIT License 5 votes vote down vote up
private static void waitForHello(GenericContainer container) throws TimeoutException {
    WaitingConsumer consumer = new WaitingConsumer();
    container.followOutput(consumer, STDOUT);

    consumer.waitUntil(frame ->
        frame.getUtf8String().contains(HELLO_TESTCONTAINERS), 30, TimeUnit.SECONDS);
}
 
Example #14
Source File: KafkaIntegrationTest.java    From kop with Apache License 2.0 4 votes vote down vote up
private static WaitingConsumer createLogFollower(final GenericContainer container) {
    final WaitingConsumer waitingConsumer = new WaitingConsumer();
    container.followOutput(waitingConsumer);
    return waitingConsumer;
}
 
Example #15
Source File: DockerfileTest.java    From testcontainers-java with MIT License 4 votes vote down vote up
@Test
public void filePermissions() throws TimeoutException {

    WaitingConsumer consumer = new WaitingConsumer();

    ImageFromDockerfile image = new ImageFromDockerfile()
            .withFileFromTransferable("/someFile.txt", new Transferable() {
                @Override
                public long getSize() {
                    return 0;
                }

                @Override
                public byte[] getBytes() {
                    return new byte[0];
                }

                @Override
                public String getDescription() {
                    return "test file";
                }

                @Override
                public int getFileMode() {
                    return 0123;
                }


            })
            .withDockerfileFromBuilder(builder -> builder
                    .from("alpine:3.2")
                    .copy("someFile.txt", "/someFile.txt")
                    .cmd("stat -c \"%a\" /someFile.txt")
            );

    GenericContainer container = new GenericContainer(image)
            .withStartupCheckStrategy(new OneShotStartupCheckStrategy())
            .withLogConsumer(consumer);

    try {
        container.start();

        consumer.waitUntil(frame -> frame.getType() == STDOUT && frame.getUtf8String().contains("123"), 5, TimeUnit.SECONDS);
    } finally {
        container.stop();
    }
}
 
Example #16
Source File: TekuCLI.java    From teku with Apache License 2.0 4 votes vote down vote up
public void waitForOutput(final String output) throws TimeoutException {
  WaitingConsumer consumer = new WaitingConsumer();
  this.followOutput(consumer, OutputType.STDOUT);
  consumer.waitUntil(frame -> frame.getUtf8String().contains(output), 30, TimeUnit.SECONDS);
}
 
Example #17
Source File: KafkaIntegrationTest.java    From kop with Apache License 2.0 4 votes vote down vote up
@Test(timeOut = 3 * 60_000, dataProvider = "integrations")
void simpleProduceAndConsume(final String integration, final Optional<String> topic,
                             final boolean shouldProduce, final boolean shouldConsume) throws Exception {
    String topicName = topic.orElse(integration);
    System.out.println("starting integration " + integration + " with topicName " + topicName);

    admin.topics().createPartitionedTopic(topicName, 1);

    System.out.println("topic created");

    final GenericContainer producer = new GenericContainer<>("streamnative/kop-test-" + integration)
            .withEnv("KOP_BROKER", "host.testcontainers.internal:" + super.kafkaBrokerPort)
            .withEnv("KOP_PRODUCE", "true")
            .withEnv("KOP_TOPIC", topic.orElse(integration))
            .withEnv("KOP_LIMIT", "10")
            .withLogConsumer(new org.testcontainers.containers.output.Slf4jLogConsumer(KafkaIntegrationTest.log))
            .waitingFor(Wait.forLogMessage("starting to produce\\n", 1))
            .withNetworkMode("host");

    final GenericContainer consumer = new GenericContainer<>("streamnative/kop-test-" + integration)
            .withEnv("KOP_BROKER", "host.testcontainers.internal:" + super.kafkaBrokerPort)
            .withEnv("KOP_TOPIC", topic.orElse(integration))
            .withEnv("KOP_CONSUME", "true")
            .withEnv("KOP_LIMIT", "10")
            .withLogConsumer(new org.testcontainers.containers.output.Slf4jLogConsumer(KafkaIntegrationTest.log))
            .waitingFor(Wait.forLogMessage("starting to consume\\n", 1))
            .withNetworkMode("host");

    WaitingConsumer producerWaitingConsumer = null;
    WaitingConsumer consumerWaitingConsumer = null;
    if (shouldProduce) {
        producer.start();
        producerWaitingConsumer = KafkaIntegrationTest.createLogFollower(producer);
        System.out.println("producer started");
    }

    if (shouldConsume) {
        consumer.start();
        consumerWaitingConsumer = KafkaIntegrationTest.createLogFollower(consumer);
        System.out.println("consumer started");
    }

    if (shouldProduce) {
        producerWaitingConsumer.waitUntil(frame ->
                frame.getUtf8String().contains("ExitCode"), 30, TimeUnit.SECONDS);
        KafkaIntegrationTest.checkForErrorsInLogs(producer.getLogs());
    }

    if (shouldConsume) {
        consumerWaitingConsumer.waitUntil(frame ->
                frame.getUtf8String().contains("ExitCode"), 30, TimeUnit.SECONDS);
        KafkaIntegrationTest.checkForErrorsInLogs(consumer.getLogs());
    }
}
 
Example #18
Source File: OutputStreamTest.java    From testcontainers-java with MIT License 3 votes vote down vote up
@Test(timeout = 60_000L)
public void testFetchStdoutWithNoLimit() throws TimeoutException {

    WaitingConsumer consumer = new WaitingConsumer();

    container.followOutput(consumer, STDOUT);

    consumer.waitUntil(frame -> frame.getType() == STDOUT && frame.getUtf8String().contains("seq=2"));
}
 
Example #19
Source File: OutputStreamTest.java    From testcontainers-java with MIT License 3 votes vote down vote up
@Test(timeout = 60_000L)
public void testFetchStdout() throws TimeoutException {

    WaitingConsumer consumer = new WaitingConsumer();

    container.followOutput(consumer, STDOUT);

    consumer.waitUntil(frame -> frame.getType() == STDOUT && frame.getUtf8String().contains("seq=2"),
            30, TimeUnit.SECONDS);
}