Search in sources :

Example 1 with OutputFrame

use of org.testcontainers.containers.output.OutputFrame in project testcontainers-java by testcontainers.

the class DockerNetworkModeTest method getContainerOutput.

private String getContainerOutput(GenericContainer container) throws TimeoutException {
    WaitingConsumer waitingConsumer = new WaitingConsumer();
    ToStringConsumer toStringConsumer = new ToStringConsumer();
    Consumer<OutputFrame> composedConsumer = waitingConsumer.andThen(toStringConsumer);
    container.followOutput(composedConsumer);
    waitingConsumer.waitUntilEnd(10, TimeUnit.SECONDS);
    return toStringConsumer.toUtf8String();
}
Also used : ToStringConsumer(org.testcontainers.containers.output.ToStringConsumer) OutputFrame(org.testcontainers.containers.output.OutputFrame) WaitingConsumer(org.testcontainers.containers.output.WaitingConsumer)

Example 2 with OutputFrame

use of org.testcontainers.containers.output.OutputFrame in project testcontainers-java by testcontainers.

the class OutputStreamTest method testToStringConsumer.

@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(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"));
}
Also used : ToStringConsumer(org.testcontainers.containers.output.ToStringConsumer) OutputFrame(org.testcontainers.containers.output.OutputFrame) WaitingConsumer(org.testcontainers.containers.output.WaitingConsumer) Test(org.junit.Test)

Example 3 with OutputFrame

use of org.testcontainers.containers.output.OutputFrame in project testcontainers-java by testcontainers.

the class OutputStreamTest method testLogConsumer.

@Test
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"));
}
Also used : OutputFrame(org.testcontainers.containers.output.OutputFrame) Slf4jLogConsumer(org.testcontainers.containers.output.Slf4jLogConsumer) WaitingConsumer(org.testcontainers.containers.output.WaitingConsumer) Test(org.junit.Test)

Example 4 with OutputFrame

use of org.testcontainers.containers.output.OutputFrame in project testcontainers-java by testcontainers.

the class LogUtils method followOutput.

/**
 * {@inheritDoc}
 */
public void followOutput(DockerClient dockerClient, String containerId, Consumer<OutputFrame> consumer, OutputFrame.OutputType... types) {
    final LogContainerCmd cmd = dockerClient.logContainerCmd(containerId).withFollowStream(true).withSince(0);
    final FrameConsumerResultCallback callback = new FrameConsumerResultCallback();
    for (OutputFrame.OutputType type : types) {
        callback.addConsumer(type, consumer);
        if (type == STDOUT)
            cmd.withStdOut(true);
        if (type == STDERR)
            cmd.withStdErr(true);
    }
    cmd.exec(callback);
}
Also used : FrameConsumerResultCallback(org.testcontainers.containers.output.FrameConsumerResultCallback) OutputFrame(org.testcontainers.containers.output.OutputFrame) LogContainerCmd(com.github.dockerjava.api.command.LogContainerCmd)

Example 5 with OutputFrame

use of org.testcontainers.containers.output.OutputFrame in project testcontainers-java by testcontainers.

the class LogMessageWaitStrategy method waitUntilReady.

@Override
protected void waitUntilReady() {
    WaitingConsumer waitingConsumer = new WaitingConsumer();
    LogUtils.followOutput(DockerClientFactory.instance().client(), waitStrategyTarget.getContainerId(), waitingConsumer);
    Predicate<OutputFrame> waitPredicate = outputFrame -> outputFrame.getUtf8String().matches(regEx);
    try {
        waitingConsumer.waitUntil(waitPredicate, startupTimeout.getSeconds(), TimeUnit.SECONDS, times);
    } catch (TimeoutException e) {
        throw new ContainerLaunchException("Timed out waiting for log output matching '" + regEx + "'");
    }
}
Also used : ContainerLaunchException(org.testcontainers.containers.ContainerLaunchException) TimeUnit(java.util.concurrent.TimeUnit) WaitingConsumer(org.testcontainers.containers.output.WaitingConsumer) Predicate(java.util.function.Predicate) LogUtils(org.testcontainers.utility.LogUtils) TimeoutException(java.util.concurrent.TimeoutException) DockerClientFactory(org.testcontainers.DockerClientFactory) OutputFrame(org.testcontainers.containers.output.OutputFrame) ContainerLaunchException(org.testcontainers.containers.ContainerLaunchException) OutputFrame(org.testcontainers.containers.output.OutputFrame) WaitingConsumer(org.testcontainers.containers.output.WaitingConsumer) TimeoutException(java.util.concurrent.TimeoutException)

Aggregations

OutputFrame (org.testcontainers.containers.output.OutputFrame)5 WaitingConsumer (org.testcontainers.containers.output.WaitingConsumer)4 Test (org.junit.Test)2 ToStringConsumer (org.testcontainers.containers.output.ToStringConsumer)2 LogContainerCmd (com.github.dockerjava.api.command.LogContainerCmd)1 TimeUnit (java.util.concurrent.TimeUnit)1 TimeoutException (java.util.concurrent.TimeoutException)1 Predicate (java.util.function.Predicate)1 DockerClientFactory (org.testcontainers.DockerClientFactory)1 ContainerLaunchException (org.testcontainers.containers.ContainerLaunchException)1 FrameConsumerResultCallback (org.testcontainers.containers.output.FrameConsumerResultCallback)1 Slf4jLogConsumer (org.testcontainers.containers.output.Slf4jLogConsumer)1 LogUtils (org.testcontainers.utility.LogUtils)1