Skip to content
Closed
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
fixed remaining problems
  • Loading branch information
alexkamp committed Oct 28, 2014
commit 5f1737b6f2260ca9042c3377cdeecbee0594063d
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ public class DockerClientConfig {
.put(DOCKER_CERT_PATH_PROPERTY, DOCKER_IO_DOCKER_CERT_PATH_PROPERTY)
.build();
private static final String DOCKER_IO_PROPERTIES_PROPERTY = "docker.io.properties";
private final URI uri;
private URI uri;
private final String version, username, password, email, dockerCertPath;
private final Integer readTimeout;
private final boolean loggingFilterEnabled;
Expand Down Expand Up @@ -176,7 +176,11 @@ public URI getUri() {
return uri;
}

public String getVersion() {
public void setUri(URI uri) {
this.uri = uri;
}

public String getVersion() {
return version;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

import java.io.IOException;
import java.net.URI;
import java.rmi.registry.Registry;
import java.security.KeyStore;
import java.security.Security;
import java.util.logging.Logger;
Expand All @@ -21,6 +20,7 @@
import org.glassfish.jersey.CommonProperties;
import org.glassfish.jersey.SslConfigurator;
import org.glassfish.jersey.apache.connector.ApacheClientProperties;
import org.glassfish.jersey.apache.connector.ApacheConnectorProvider;
import org.glassfish.jersey.client.ClientConfig;
import org.glassfish.jersey.client.ClientProperties;

Expand Down Expand Up @@ -76,6 +76,7 @@ public void init(DockerClientConfig dockerClientConfig) {
Preconditions.checkNotNull(dockerClientConfig, "config was not specified");

ClientConfig clientConfig = new ClientConfig();
clientConfig.connectorProvider(new ApacheConnectorProvider());
clientConfig.property(CommonProperties.FEATURE_AUTO_DISCOVERY_DISABLE, true);

clientConfig.register(ResponseStatusExceptionFilter.class);
Expand All @@ -91,8 +92,9 @@ public void init(DockerClientConfig dockerClientConfig) {
clientConfig.property(ClientProperties.READ_TIMEOUT, readTimeout);
}

clientConfig.property(ApacheClientProperties.CONNECTION_MANAGER,
new PoolingHttpClientConnectionManager(getSchemeRegistry(dockerClientConfig.getUri())));
URI originalUri = dockerClientConfig.getUri();
clientConfig.property(ApacheClientProperties.CONNECTION_MANAGER,
new PoolingHttpClientConnectionManager(getSchemeRegistry(originalUri)));

ClientBuilder clientBuilder = ClientBuilder.newBuilder().withConfig(clientConfig);

Expand Down Expand Up @@ -135,6 +137,10 @@ public void init(DockerClientConfig dockerClientConfig) {

client = clientBuilder.build();

if (originalUri.getScheme().equals("unix")) {
dockerClientConfig.setUri(UnixConnectionSocketFactory.sanitizeUri(originalUri));
}

WebTarget webResource = client.target(dockerClientConfig.getUri());

if (dockerClientConfig.getVersion() == null || dockerClientConfig.getVersion().isEmpty()) {
Expand Down
104 changes: 98 additions & 6 deletions src/test/java/com/github/dockerjava/core/UnixSocketTest.java
Original file line number Diff line number Diff line change
@@ -1,17 +1,109 @@
package com.github.dockerjava.core;

import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.endsWith;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;

import org.apache.commons.compress.archivers.tar.TarArchiveEntry;
import org.apache.commons.compress.archivers.tar.TarArchiveOutputStream;
import org.apache.commons.io.IOUtils;
import org.apache.commons.io.LineIterator;
import org.testng.Assert;
import org.testng.annotations.Test;

import com.github.dockerjava.api.DockerClient;
import com.github.dockerjava.api.command.CreateContainerResponse;

@Test(groups = "unix")
public class UnixSocketTest {

@Test
public void testUnixSocket() {
DockerClientConfig config = DockerClientConfig.createDefaultConfigBuilder()
.withUri("unix://localhost/var/run/docker.sock")
.build();
DockerClient docker = DockerClientBuilder.getInstance(config).build();
docker.pingCmd();
public void testUnixSocketPing() {
DockerClientConfig config = DockerClientConfig
.createDefaultConfigBuilder()
.withVersion("1.14")
.withUri("unix://localhost/var/run/docker.sock").build();
DockerClient docker = DockerClientBuilder.getInstance(config)
.build();
docker.pingCmd();
}

@Test
public void testUnixSocketCat() throws IOException {
String image = "test";
String containerId = null;

DockerClientConfig config = DockerClientConfig
.createDefaultConfigBuilder()
.withVersion("1.14")
.withUri("unix://localhost/var/run/docker.sock").build();
DockerClient docker = DockerClientBuilder.getInstance(config).build();
try {

// construct a tar archive
ByteArrayOutputStream os = new ByteArrayOutputStream();
String text = "Hello Docker!\n";
TarArchiveOutputStream tarStream = new TarArchiveOutputStream(os);
// add some files
putToArchive(tarStream, "Dockerfile",
"FROM busybox\nADD testdata /testdata\n".getBytes());
putToArchive(tarStream, "testdata/hello1", text.getBytes());
tarStream.close();

ByteArrayInputStream tarIn = new ByteArrayInputStream(os.toByteArray());

InputStream in = docker.buildImageCmd(tarIn).withTag(image).exec();
readStream(System.out, in);

CreateContainerResponse resp = docker.createContainerCmd("test")
.withCmd("cat", "hello1")
.withWorkingDir("/testdata")
.exec();
containerId = resp.getId();
Assert.assertEquals(null, resp.getWarnings());

docker.startContainerCmd(resp.getId()).exec();

int ex = docker.waitContainerCmd(resp.getId()).exec();
Assert.assertEquals(ex, 0);

StringBuilder sb = new StringBuilder();
InputStream stream = docker.logContainerCmd(resp.getId())
.withStdErr()
.withStdOut()
.exec();
readStream(sb, stream);
assertThat(sb.toString(), endsWith(text));
} finally {
if(null != containerId) {
docker.removeContainerCmd(containerId).exec();
}
docker.removeImageCmd(image).exec();

}
}

private void readStream(Appendable sb, InputStream stream)
throws IOException {
LineIterator itr = IOUtils.lineIterator(
stream, "UTF-8");

while(itr.hasNext()) {
sb.append(itr.next());
sb.append("\n");
}
}

private void putToArchive(TarArchiveOutputStream tarStream, String name,
byte[] content) throws IOException {
TarArchiveEntry dockerfile = new TarArchiveEntry(name);
dockerfile.setSize(content.length);
tarStream.putArchiveEntry(dockerfile);
tarStream.write(content);
tarStream.closeArchiveEntry();
}
}