Skip to content

Commit

Permalink
Upgrade to com.spotify:docker-client:8.9.1 and fix default env
Browse files Browse the repository at this point in the history
(it now works on mac with docker for mac)

Fix #10, fix #8

We no longer use the shaded version of the jar because of
spotify/docker-client#900.
  • Loading branch information
geowarin committed Nov 13, 2017
1 parent 0947367 commit 304549e
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 31 deletions.
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ repositories {

dependencies {
compile 'org.slf4j:slf4j-api:1.7.12'
compile 'com.spotify:docker-client:3.4.0:shaded'
compile 'com.spotify:docker-client:8.9.1'
compile('junit:junit:4.12') { ext.optional = true }
compile 'com.google.guava:guava:18.0'
testCompile 'org.slf4j:slf4j-simple:1.7.12'
Expand Down
45 changes: 15 additions & 30 deletions src/main/java/com/github/geowarin/junit/DockerRule.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
package com.github.geowarin.junit;

import com.spotify.docker.client.*;
import com.spotify.docker.client.DefaultDockerClient;
import com.spotify.docker.client.DockerClient;
import com.spotify.docker.client.LogMessage;
import com.spotify.docker.client.LogStream;
import com.spotify.docker.client.exceptions.DockerCertificateException;
import com.spotify.docker.client.exceptions.DockerException;
import com.spotify.docker.client.messages.*;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
Expand All @@ -10,16 +15,16 @@
import java.io.UnsupportedEncodingException;
import java.net.InetSocketAddress;
import java.net.SocketAddress;
import java.net.URI;
import java.nio.ByteBuffer;
import java.nio.channels.SocketChannel;
import java.nio.file.Paths;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import static com.spotify.docker.client.DockerClient.LogsParam.*;
import static com.spotify.docker.client.DockerClient.LogsParam.follow;
import static com.spotify.docker.client.DockerClient.LogsParam.stdout;
import static com.spotify.docker.client.DockerClient.RemoveContainerParam.removeVolumes;

/**
* <p>
Expand Down Expand Up @@ -81,7 +86,7 @@ protected void after() {
super.after();
try {
dockerClient.killContainer(container.id());
dockerClient.removeContainer(container.id(), true);
dockerClient.removeContainer(container.id(), removeVolumes());
dockerClient.close();
} catch (DockerException | InterruptedException e) {
throw new RuntimeException("Unable to stop/remove docker container " + container.id(), e);
Expand All @@ -98,7 +103,7 @@ public String getDockerHost() {
return dockerClient.getHost();
}

public void waitForPort(int port, long timeoutInMillis) {
protected void waitForPort(int port, long timeoutInMillis) {
SocketAddress address = new InetSocketAddress(getDockerHost(), port);
long totalWait = 0;
while (true) {
Expand All @@ -119,30 +124,15 @@ public void waitForPort(int port, long timeoutInMillis) {
}
}

private DockerClient createDockerClient() {
if (isUnix() || System.getenv("DOCKER_HOST") != null) {
try {
return DefaultDockerClient.fromEnv().build();
} catch (DockerCertificateException e) {
System.err.println(e.getMessage());
}
}

logger.info("Could not create docker client from the environment. Assuming docker-machine environment with url " + DOCKER_MACHINE_SERVICE_URL);
DockerCertificates dockerCertificates = null;
protected DockerClient createDockerClient() {
try {
String userHome = System.getProperty("user.home");
dockerCertificates = new DockerCertificates(Paths.get(userHome, ".docker/machine/certs"));
return DefaultDockerClient.fromEnv().build();
} catch (DockerCertificateException e) {
System.err.println(e.getMessage());
throw new IllegalStateException("Could not create docker client from environement", e);
}
return DefaultDockerClient.builder()
.uri(URI.create(DOCKER_MACHINE_SERVICE_URL))
.dockerCertificates(dockerCertificates)
.build();
}

private ContainerConfig createContainerConfig(String imageName, String[] ports, String cmd) {
protected ContainerConfig createContainerConfig(String imageName, String[] ports, String cmd) {
Map<String, List<PortBinding>> portBindings = new HashMap<>();
for (String port : ports) {
List<PortBinding> hostPorts = Collections.singletonList(PortBinding.randomPort("0.0.0.0"));
Expand Down Expand Up @@ -173,11 +163,6 @@ public int getHostPort(String containerPort) {
return Integer.parseInt(portBindings.get(0).hostPort());
}

private static boolean isUnix() {
String os = System.getProperty("os.name").toLowerCase();
return os.contains("nix") || os.contains("nux") || os.contains("aix");
}

protected void waitForLog(String messageToMatch) throws DockerException, InterruptedException, UnsupportedEncodingException {
LogStream logs = dockerClient.logs(container.id(), follow(), stdout());
String log;
Expand Down

0 comments on commit 304549e

Please sign in to comment.