Search in sources :

Example 36 with HostConfig

use of com.spotify.docker.client.messages.HostConfig in project pravega by pravega.

the class DockerBasedTestExecutor method setContainerConfig.

private ContainerConfig setContainerConfig(String methodName, String className) {
    Map<String, String> labels = new HashMap<>(2);
    labels.put("testMethodName", methodName);
    labels.put("testClassName", className);
    HostConfig hostConfig = HostConfig.builder().portBindings(ImmutableMap.of(DOCKER_CLIENT_PORT + "/tcp", Arrays.asList(PortBinding.of(LoginClient.MESOS_MASTER, DOCKER_CLIENT_PORT)))).networkMode("docker_gwbridge").build();
    ContainerConfig containerConfig = ContainerConfig.builder().hostConfig(hostConfig).image(IMAGE).user("root").workingDir("/data").cmd("sh", "-c", "java -DmasterIP=" + LoginClient.MESOS_MASTER + " -DexecType=" + getConfig("execType", "LOCAL") + " -cp /data/build/libs/test-docker-collection.jar io.pravega.test.system." + "SingleJUnitTestRunner " + className + "#" + methodName + " > server.log 2>&1").labels(labels).build();
    return containerConfig;
}
Also used : ContainerConfig(com.spotify.docker.client.messages.ContainerConfig) HashMap(java.util.HashMap) HostConfig(com.spotify.docker.client.messages.HostConfig)

Example 37 with HostConfig

use of com.spotify.docker.client.messages.HostConfig in project docker-client by spotify.

the class DefaultDockerClientTest method testSsl.

@SuppressWarnings("EmptyCatchBlock")
@Test
public void testSsl() throws Exception {
    assumeFalse(TRAVIS);
    // Build a run a container that contains a Docker instance configured with our SSL cert/key
    final String imageName = "test-docker-ssl";
    final String expose = "2376/tcp";
    final Path dockerDirectory = getResource("dockerSslDirectory");
    sut.build(dockerDirectory, imageName);
    final HostConfig hostConfig = HostConfig.builder().privileged(true).publishAllPorts(true).build();
    final ContainerConfig containerConfig = ContainerConfig.builder().image(imageName).exposedPorts(expose).hostConfig(hostConfig).build();
    final String containerName = randomName();
    final ContainerCreation containerCreation = sut.createContainer(containerConfig, containerName);
    final String containerId = containerCreation.id();
    sut.startContainer(containerId);
    // Determine where the Docker instance inside the container we just started is exposed
    final String host;
    if (dockerEndpoint.getScheme().equalsIgnoreCase("unix")) {
        host = "localhost";
    } else {
        host = dockerEndpoint.getHost();
    }
    final ContainerInfo containerInfo = sut.inspectContainer(containerId);
    assertThat(containerInfo.state().running(), equalTo(true));
    final String port = containerInfo.networkSettings().ports().get(expose).get(0).hostPort();
    // Try to connect using SSL and our known cert/key
    final DockerCertificates certs = new DockerCertificates(dockerDirectory);
    final DockerClient c = new DefaultDockerClient(URI.create(format("https://%s:%s", host, port)), certs);
    // We need to wait for the docker process inside the docker container to be ready to accept
    // connections on the port. Otherwise, this test will fail.
    // Even though we've checked that the container is running, this doesn't mean the process
    // inside the container is ready.
    final long deadline = System.nanoTime() + TimeUnit.MILLISECONDS.toNanos(TimeUnit.MINUTES.toMillis(1));
    while (System.nanoTime() < deadline) {
        try (final Socket ignored = new Socket(host, Integer.parseInt(port))) {
            break;
        } catch (IOException ignored) {
        }
        Thread.sleep(500);
    }
    assertThat(c.ping(), equalTo("OK"));
    sut.stopContainer(containerId, 10);
}
Also used : Path(java.nio.file.Path) ContainerConfig(com.spotify.docker.client.messages.ContainerConfig) ContainerCreation(com.spotify.docker.client.messages.ContainerCreation) HostConfig(com.spotify.docker.client.messages.HostConfig) ContainerInfo(com.spotify.docker.client.messages.ContainerInfo) Long.toHexString(java.lang.Long.toHexString) Matchers.isEmptyOrNullString(org.hamcrest.Matchers.isEmptyOrNullString) Matchers.containsString(org.hamcrest.Matchers.containsString) IOException(java.io.IOException) ServerSocket(java.net.ServerSocket) Socket(java.net.Socket) Test(org.junit.Test)

Example 38 with HostConfig

use of com.spotify.docker.client.messages.HostConfig in project docker-client by spotify.

the class DefaultDockerClientTest method testContainerVolumeNoCopy.

@Test
public void testContainerVolumeNoCopy() throws Exception {
    requireDockerApiVersionAtLeast("1.23", "Creating a container with volumes with nocopy mode");
    sut.pull(BUSYBOX_LATEST);
    final String aVolumeName = "avolume";
    final String aVolumeTo = "/some/path";
    final String nocopyVolumeName = "avolume2";
    final String nocopyVolumeTo = "/some/other/path";
    sut.createVolume(Volume.builder().name(aVolumeName).build());
    sut.createVolume(Volume.builder().name(nocopyVolumeName).build());
    final HostConfig hostConfig = HostConfig.builder().appendBinds(Bind.from(aVolumeName).to(aVolumeTo).readOnly(true).build()).appendBinds(Bind.from(nocopyVolumeName).to(nocopyVolumeTo).noCopy(true).build()).build();
    final ContainerConfig config = ContainerConfig.builder().image(BUSYBOX_LATEST).hostConfig(hostConfig).build();
    final String id = sut.createContainer(config, randomName()).id();
    final ContainerInfo info = sut.inspectContainer(id);
    final List<ContainerMount> mounts = info.mounts();
    assertThat(mounts.size(), equalTo(2));
    {
        final ContainerMount aMount = Iterables.find(mounts, new Predicate<ContainerMount>() {

            @Override
            public boolean apply(ContainerMount mount) {
                return !("nocopy".equals(mount.mode()));
            }
        }, null);
        assertThat("Could not find a mount (without nocopy)", aMount, notNullValue());
        assertThat(aMount.mode(), is(equalTo("ro")));
        assertThat(aMount.rw(), is(false));
        assertThat(aMount.source(), containsString("/" + aVolumeName + "/"));
        assertThat(aMount.destination(), is(equalTo(aVolumeTo)));
    }
    {
        final ContainerMount nocopyMount = Iterables.find(mounts, new Predicate<ContainerMount>() {

            @Override
            public boolean apply(ContainerMount mount) {
                return "nocopy".equals(mount.mode());
            }
        }, null);
        assertThat("Could not find mount (with nocopy)", nocopyMount, notNullValue());
        assertThat(nocopyMount.mode(), is(equalTo("nocopy")));
        assertThat(nocopyMount.rw(), is(true));
        assertThat(nocopyMount.source(), containsString("/" + nocopyVolumeName + "/"));
        assertThat(nocopyMount.destination(), is(equalTo(nocopyVolumeTo)));
    }
}
Also used : ContainerConfig(com.spotify.docker.client.messages.ContainerConfig) HostConfig(com.spotify.docker.client.messages.HostConfig) ContainerInfo(com.spotify.docker.client.messages.ContainerInfo) Long.toHexString(java.lang.Long.toHexString) Matchers.isEmptyOrNullString(org.hamcrest.Matchers.isEmptyOrNullString) Matchers.containsString(org.hamcrest.Matchers.containsString) ContainerMount(com.spotify.docker.client.messages.ContainerMount) Predicate(com.google.common.base.Predicate) Test(org.junit.Test)

Example 39 with HostConfig

use of com.spotify.docker.client.messages.HostConfig in project docker-client by spotify.

the class DefaultDockerClientTest method testVolumesFrom.

@Test
public void testVolumesFrom() throws Exception {
    sut.pull(BUSYBOX_LATEST);
    final String volumeContainer = randomName();
    final String mountContainer = randomName();
    final ContainerConfig volumeConfig = ContainerConfig.builder().image(BUSYBOX_LATEST).addVolume("/foo").cmd("touch", "/foo/bar").build();
    sut.createContainer(volumeConfig, volumeContainer);
    sut.startContainer(volumeContainer);
    sut.waitContainer(volumeContainer);
    final HostConfig mountHostConfig = HostConfig.builder().volumesFrom(volumeContainer).build();
    final ContainerConfig mountConfig = ContainerConfig.builder().image(BUSYBOX_LATEST).hostConfig(mountHostConfig).cmd("ls", "/foo").build();
    sut.createContainer(mountConfig, mountContainer);
    sut.startContainer(mountContainer);
    sut.waitContainer(mountContainer);
    final ContainerInfo info = sut.inspectContainer(mountContainer);
    assertThat(info.state().running(), is(false));
    assertThat(info.state().exitCode(), is(0));
    final String logs;
    try (LogStream stream = sut.logs(info.id(), stdout(), stderr())) {
        logs = stream.readFully();
    }
    assertThat(logs, containsString("bar"));
}
Also used : ContainerConfig(com.spotify.docker.client.messages.ContainerConfig) HostConfig(com.spotify.docker.client.messages.HostConfig) ContainerInfo(com.spotify.docker.client.messages.ContainerInfo) Long.toHexString(java.lang.Long.toHexString) Matchers.isEmptyOrNullString(org.hamcrest.Matchers.isEmptyOrNullString) Matchers.containsString(org.hamcrest.Matchers.containsString) Test(org.junit.Test)

Example 40 with HostConfig

use of com.spotify.docker.client.messages.HostConfig in project docker-client by spotify.

the class DefaultDockerClientTest method testContainerWithMemoryOptions.

@Test
public void testContainerWithMemoryOptions() throws Exception {
    requireDockerApiVersionNot("1.21", "For some reason this test fails on TravisCI.");
    sut.pull(BUSYBOX_LATEST);
    final HostConfig.Builder hostConfigBuilder = HostConfig.builder().memory(4194304L).memorySwap(5000000L);
    if (dockerApiVersionAtLeast("1.20")) {
        hostConfigBuilder.memorySwappiness(42);
    }
    final HostConfig expected = hostConfigBuilder.build();
    final ContainerConfig config = ContainerConfig.builder().image(BUSYBOX_LATEST).hostConfig(expected).build();
    final String name = randomName();
    final ContainerCreation creation = sut.createContainer(config, name);
    final String id = creation.id();
    sut.startContainer(id);
    final HostConfig actual = sut.inspectContainer(id).hostConfig();
    if (dockerApiVersionAtLeast("1.19")) {
        // TODO (dxia) Although 1.18 docs implies these two settings are supported by that API
        // version, travis-CI fails on these two checks. It's not a big deal since we'll probably
        // stop supporting 1.18 soon.
        assertThat(actual.memory(), equalTo(expected.memory()));
        assertThat(actual.memorySwap(), equalTo(expected.memorySwap()));
    }
    if (dockerApiVersionAtLeast("1.20")) {
        assertThat(actual.memorySwappiness(), equalTo(expected.memorySwappiness()));
    }
}
Also used : ContainerConfig(com.spotify.docker.client.messages.ContainerConfig) ContainerCreation(com.spotify.docker.client.messages.ContainerCreation) HostConfig(com.spotify.docker.client.messages.HostConfig) Long.toHexString(java.lang.Long.toHexString) Matchers.isEmptyOrNullString(org.hamcrest.Matchers.isEmptyOrNullString) Matchers.containsString(org.hamcrest.Matchers.containsString) Test(org.junit.Test)

Aggregations

HostConfig (com.spotify.docker.client.messages.HostConfig)53 ContainerConfig (com.spotify.docker.client.messages.ContainerConfig)45 ContainerCreation (com.spotify.docker.client.messages.ContainerCreation)30 Test (org.junit.Test)29 Matchers.containsString (org.hamcrest.Matchers.containsString)22 Long.toHexString (java.lang.Long.toHexString)19 Matchers.isEmptyOrNullString (org.hamcrest.Matchers.isEmptyOrNullString)19 ContainerInfo (com.spotify.docker.client.messages.ContainerInfo)16 DockerClient (com.spotify.docker.client.DockerClient)12 List (java.util.List)11 DockerException (com.spotify.docker.client.exceptions.DockerException)8 HashMap (java.util.HashMap)8 ArrayList (java.util.ArrayList)7 DefaultDockerClient (com.spotify.docker.client.DefaultDockerClient)6 LogStream (com.spotify.docker.client.LogStream)4 ContainerMount (com.spotify.docker.client.messages.ContainerMount)4 PortBinding (com.spotify.docker.client.messages.PortBinding)4 IOException (java.io.IOException)4 JsonNode (com.fasterxml.jackson.databind.JsonNode)3 ImmutableList (com.google.common.collect.ImmutableList)3