@Before
public void dockerSetup() throws Exception {
final String portRange = System.getenv("DOCKER_PORT_RANGE");
final AllocatedPort allocatedPort;
final int probePort;
if (portRange != null) {
final String[] parts = portRange.split(":", 2);
dockerPortRange = Range.closedOpen(Integer.valueOf(parts[0]),
Integer.valueOf(parts[1]));
allocatedPort = Polling.await(LONG_WAIT_SECONDS, SECONDS, new Callable<AllocatedPort>() {
@Override
public AllocatedPort call() throws Exception {
final int port = ThreadLocalRandom.current().nextInt(dockerPortRange.lowerEndpoint(),
dockerPortRange.upperEndpoint());
return temporaryPorts.tryAcquire("docker-probe", port);
}
});
probePort = allocatedPort.port();
} else {
dockerPortRange = temporaryPorts.localPortRange("docker", 10);
probePort = dockerPortRange().lowerEndpoint();
allocatedPort = null;
}
try {
assertDockerReachable(probePort);
} finally {
if (allocatedPort != null) {
allocatedPort.release();
}
}
}