Search in sources :

Example 1 with ContainerLaunchException

use of org.testcontainers.containers.ContainerLaunchException in project testcontainers-java by testcontainers.

the class HttpWaitStrategy method waitUntilReady.

@Override
protected void waitUntilReady() {
    final String containerName = waitStrategyTarget.getContainerInfo().getName();
    final Set<Integer> livenessCheckPorts = getLivenessCheckPorts();
    if (livenessCheckPorts == null || livenessCheckPorts.isEmpty()) {
        log.warn("{}: No exposed ports or mapped ports - cannot wait for status", containerName);
        return;
    }
    final Integer livenessCheckPort = livenessCheckPorts.iterator().next();
    final String uri = buildLivenessUri(livenessCheckPort).toString();
    log.info("{}: Waiting for {} seconds for URL: {}", containerName, startupTimeout.getSeconds(), uri);
    // try to connect to the URL
    try {
        retryUntilSuccess((int) startupTimeout.getSeconds(), TimeUnit.SECONDS, () -> {
            getRateLimiter().doWhenReady(() -> {
                try {
                    final HttpURLConnection connection = (HttpURLConnection) new URL(uri).openConnection();
                    // authenticate
                    if (!Strings.isNullOrEmpty(username)) {
                        connection.setRequestProperty(HEADER_AUTHORIZATION, buildAuthString(username, password));
                        connection.setUseCaches(false);
                    }
                    connection.setRequestMethod("GET");
                    connection.connect();
                    if (statusCode != connection.getResponseCode()) {
                        throw new RuntimeException(String.format("HTTP response code was: %s", connection.getResponseCode()));
                    }
                    if (responsePredicate != null) {
                        String responseBody = getResponseBody(connection);
                        if (!responsePredicate.test(responseBody)) {
                            throw new RuntimeException(String.format("Response: %s did not match predicate", responseBody));
                        }
                    }
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
            });
            return true;
        });
    } catch (TimeoutException e) {
        throw new ContainerLaunchException(String.format("Timed out waiting for URL to be accessible (%s should return HTTP %s)", uri, statusCode));
    }
}
Also used : ContainerLaunchException(org.testcontainers.containers.ContainerLaunchException) HttpURLConnection(java.net.HttpURLConnection) IOException(java.io.IOException) URL(java.net.URL) TimeoutException(org.rnorth.ducttape.TimeoutException)

Example 2 with ContainerLaunchException

use of org.testcontainers.containers.ContainerLaunchException in project testcontainers-java by testcontainers.

the class HostPortWaitStrategy method waitUntilReady.

@Override
protected void waitUntilReady() {
    final Set<Integer> externalLivenessCheckPorts = getLivenessCheckPorts();
    if (externalLivenessCheckPorts.isEmpty()) {
        log.debug("Liveness check ports of {} is empty. Not waiting.", waitStrategyTarget.getContainerInfo().getName());
        return;
    }
    @SuppressWarnings("unchecked") List<Integer> exposedPorts = waitStrategyTarget.getExposedPorts();
    final Set<Integer> internalPorts = getInternalPorts(externalLivenessCheckPorts, exposedPorts);
    Callable<Boolean> internalCheck = new InternalCommandPortListeningCheck(waitStrategyTarget, internalPorts);
    Callable<Boolean> externalCheck = new ExternalPortListeningCheck(waitStrategyTarget, externalLivenessCheckPorts);
    try {
        Unreliables.retryUntilTrue((int) startupTimeout.getSeconds(), TimeUnit.SECONDS, () -> getRateLimiter().getWhenReady(() -> internalCheck.call() && externalCheck.call()));
    } catch (TimeoutException e) {
        throw new ContainerLaunchException("Timed out waiting for container port to open (" + waitStrategyTarget.getContainerIpAddress() + " ports: " + externalLivenessCheckPorts + " should be listening)");
    }
}
Also used : ExternalPortListeningCheck(org.testcontainers.containers.wait.internal.ExternalPortListeningCheck) ContainerLaunchException(org.testcontainers.containers.ContainerLaunchException) InternalCommandPortListeningCheck(org.testcontainers.containers.wait.internal.InternalCommandPortListeningCheck) TimeoutException(org.rnorth.ducttape.TimeoutException)

Example 3 with ContainerLaunchException

use of org.testcontainers.containers.ContainerLaunchException 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

ContainerLaunchException (org.testcontainers.containers.ContainerLaunchException)3 TimeoutException (org.rnorth.ducttape.TimeoutException)2 IOException (java.io.IOException)1 HttpURLConnection (java.net.HttpURLConnection)1 URL (java.net.URL)1 TimeUnit (java.util.concurrent.TimeUnit)1 TimeoutException (java.util.concurrent.TimeoutException)1 Predicate (java.util.function.Predicate)1 DockerClientFactory (org.testcontainers.DockerClientFactory)1 OutputFrame (org.testcontainers.containers.output.OutputFrame)1 WaitingConsumer (org.testcontainers.containers.output.WaitingConsumer)1 ExternalPortListeningCheck (org.testcontainers.containers.wait.internal.ExternalPortListeningCheck)1 InternalCommandPortListeningCheck (org.testcontainers.containers.wait.internal.InternalCommandPortListeningCheck)1 LogUtils (org.testcontainers.utility.LogUtils)1