Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Command-based port check in HostPortWaitStrategy in case of Docker for Mac #236

Merged
Merged
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,44 +1,94 @@
package org.testcontainers.containers.wait;

import lombok.extern.slf4j.Slf4j;
import org.rnorth.ducttape.TimeoutException;
import org.rnorth.ducttape.unreliables.Unreliables;
import org.testcontainers.DockerClientFactory;
import org.testcontainers.containers.ContainerLaunchException;
import org.testcontainers.containers.GenericContainer;
import org.testcontainers.dockerclient.ProxiedUnixSocketClientProviderStrategy;

import java.io.IOException;
import java.net.Socket;
import java.util.List;
import java.util.concurrent.Callable;
import java.util.concurrent.TimeUnit;

/**
* Waits until a socket connection can be established on a port exposed or mapped by the container.
*
* @author richardnorth
*/
@Slf4j
public class HostPortWaitStrategy extends GenericContainer.AbstractWaitStrategy {

private static final String SUCCESS_MARKER = "TESTCONTAINERS_SUCCESS";
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we have to use string-based success marker because exit code is not available

@Override
protected void waitUntilReady() {
final Integer port = getLivenessCheckPort();
if (null == port) {
log.debug("Liveness check port of {} is empty. Not waiting.", container.getContainerName());
return;
}

final String ipAddress = container.getContainerIpAddress();
Callable<Boolean> checkStrategy;

// Special case for Docker for Mac, see #160
if (DockerClientFactory.instance().isUsing(ProxiedUnixSocketClientProviderStrategy.class)) {
List<Integer> exposedPorts = container.getExposedPorts();

Integer exposedPort = exposedPorts.stream()
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

unfortunately, we have to do some reverse engineering to find the port originally exposed since it's not private (protected, in fact)

.map(container::getMappedPort)
.filter(port::equals)
.findFirst()
.orElse(null);

if (null == exposedPort) {
log.warn("Liveness check port of {} is set to {}, but it's not listed in exposed ports.",
container.getContainerName(), port);
return;
}

String[][] commands = {
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

two strategies at this moment - one without BASH because it's not available in Alpine linux for example, and another one with BASH

{ "/bin/sh", "-c", "nc -vz -w 1 localhost " + exposedPort + " && echo " + SUCCESS_MARKER },
{ "/bin/bash", "-c", "</dev/tcp/localhost/" + exposedPort + " && echo " + SUCCESS_MARKER }
};

checkStrategy = () -> {
for (String[] command : commands) {
try {
if (container.execInContainer(command).getStdout().contains(SUCCESS_MARKER)) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This will cause a log message to come out at INFO level - might get a bit noisy when the check has to be repeated many times. I'm not quite sure of the best answer really, but maybe we should change the execInContainer method so that it logs at DEBUG instead? What do you think?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

sounds great! The INFO level is a bit verbose for it. Where should we change it?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

org/testcontainers/containers/GenericContainer.java:820 would be the place.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I mean, in this PR or as a separate change? :)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Aha, oops! In this PR is fine I think!

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

changed :)

return true;
}
} catch (InterruptedException e) {
throw new RuntimeException(e);
} catch (Exception e) {
continue;
}
}

return false;
};
} else {
checkStrategy = () -> {
new Socket(container.getContainerIpAddress(), port).close();
return true;
};
}

try {
Unreliables.retryUntilSuccess((int) startupTimeout.getSeconds(), TimeUnit.SECONDS, () -> {
getRateLimiter().doWhenReady(() -> {
Unreliables.retryUntilTrue((int) startupTimeout.getSeconds(), TimeUnit.SECONDS, () -> {
return getRateLimiter().getWhenReady(() -> {
try {
new Socket(ipAddress, port).close();
} catch (IOException e) {
return checkStrategy.call();
} catch (Exception e) {
throw new RuntimeException(e);
}
});
return true;
});

} catch (TimeoutException e) {
throw new ContainerLaunchException("Timed out waiting for container port to open (" +
ipAddress + ":" + port + " should be listening)");
container.getContainerIpAddress() + ":" + port + " should be listening)");
}
}
}