Search in sources :

Example 11 with HostConfig

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

the class DefaultDockerClientTest method testContainerWithMoreCpuOptions.

@Test
public void testContainerWithMoreCpuOptions() throws Exception {
    requireDockerApiVersionAtLeast("1.19", "Container creation with more cpu options");
    sut.pull(BUSYBOX_LATEST);
    final HostConfig expected = HostConfig.builder().cpuShares(4096L).cpuPeriod(100000L).cpuQuota(50000L).cpusetCpus("0,1").cpusetMems("0").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();
    assertThat(actual.cpuShares(), equalTo(expected.cpuShares()));
    assertThat(actual.cpuPeriod(), equalTo(expected.cpuPeriod()));
    assertThat(actual.cpuQuota(), equalTo(expected.cpuQuota()));
    assertThat(actual.cpusetCpus(), equalTo(expected.cpusetCpus()));
}
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)

Example 12 with HostConfig

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

the class DefaultDockerClientTest method testContainerWithHostConfig.

@Test
public void testContainerWithHostConfig() throws Exception {
    requireDockerApiVersionAtLeast("1.18", "Container creation with HostConfig");
    sut.pull(BUSYBOX_LATEST);
    final boolean privileged = true;
    final boolean publishAllPorts = true;
    final String dns = "1.2.3.4";
    final List<Ulimit> ulimits = newArrayList(Ulimit.builder().name("nofile").soft(1024L).hard(2048L).build());
    final Device expectedDevice = Device.builder().pathOnHost(".").pathInContainer("/foo").cgroupPermissions("mrw").build();
    final HostConfig.Builder hostConfigBuilder = HostConfig.builder().privileged(privileged).publishAllPorts(publishAllPorts).dns(dns).dnsSearch("domain1", "domain2").devices(expectedDevice).ulimits(ulimits);
    if (dockerApiVersionAtLeast("1.21")) {
        hostConfigBuilder.dnsOptions("some", "options");
    }
    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();
    assertThat(actual.privileged(), equalTo(expected.privileged()));
    assertThat(actual.publishAllPorts(), equalTo(expected.publishAllPorts()));
    assertThat(actual.dns(), equalTo(expected.dns()));
    if (dockerApiVersionAtLeast("1.21")) {
        assertThat(actual.dnsOptions(), equalTo(expected.dnsOptions()));
    }
    assertThat(actual.dnsSearch(), equalTo(expected.dnsSearch()));
    assertEquals(ulimits, actual.ulimits());
    assertThat(actual.devices(), contains(expectedDevice));
}
Also used : ContainerConfig(com.spotify.docker.client.messages.ContainerConfig) ContainerCreation(com.spotify.docker.client.messages.ContainerCreation) Device(com.spotify.docker.client.messages.Device) HostConfig(com.spotify.docker.client.messages.HostConfig) Ulimit(com.spotify.docker.client.messages.HostConfig.Ulimit) Long.toHexString(java.lang.Long.toHexString) Matchers.isEmptyOrNullString(org.hamcrest.Matchers.isEmptyOrNullString) Matchers.containsString(org.hamcrest.Matchers.containsString) Test(org.junit.Test)

Example 13 with HostConfig

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

the class DefaultDockerClientTest method testContainerVolumesOldStyle.

@Test
@SuppressWarnings("deprecation")
public void testContainerVolumesOldStyle() throws Exception {
    requireDockerApiVersionLessThan("1.20", "Creating a container with volumes and inspecting volumes in old style");
    sut.pull(BUSYBOX_LATEST);
    final HostConfig hostConfig = HostConfig.builder().binds(Bind.from("/local/path").to("/remote/path").build()).build();
    final ContainerConfig volumeConfig = ContainerConfig.builder().image(BUSYBOX_LATEST).addVolume("/foo").hostConfig(hostConfig).build();
    final String id = sut.createContainer(volumeConfig, randomName()).id();
    final ContainerInfo volumeContainer = sut.inspectContainer(id);
    final List<String> expectedDestinations = newArrayList("/foo", "/remote/path");
    final Set<String> actualDestinations = volumeContainer.volumes().keySet();
    // To make sure two sets are equal, when they may be in different orders,
    // we check that each one contains all the elements of the other.
    // Equivalent to, in math, proving two sets are one-to-one by proving
    // they are injective ("into") and surjective ("onto").
    assertThat(actualDestinations, everyItem(isIn(expectedDestinations)));
    assertThat(expectedDestinations, everyItem(isIn(actualDestinations)));
    // The local paths returned from ContainerInfo.volumes() are paths in the docker
    // file system. So they are not predictable (at least by me, the test writer,
    // John Flavin.) However, the local path we asked for will always be included as part of
    // the path that is returned. So we can just check that one in the list of items
    // we got back contains our expected path.
    final String expectedLocalPath = "/local/path";
    assertThat(volumeContainer.volumes().values(), hasItem(containsString(expectedLocalPath)));
    assertThat(volumeContainer.config().volumes(), hasItem("/foo"));
}
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 14 with HostConfig

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

the class DefaultDockerClientTest method testInspectContainerWithSecurityOpts.

@Test
public void testInspectContainerWithSecurityOpts() throws Exception {
    final String userLabel = "label:user:dxia";
    final String roleLabel = "label:role:foo";
    final String typeLabel = "label:type:bar";
    final String levelLabel = "label:level:9001";
    sut.pull(MEMCACHED_LATEST);
    final HostConfig hostConfig = HostConfig.builder().securityOpt(userLabel, roleLabel, typeLabel, levelLabel).build();
    final ContainerConfig config = ContainerConfig.builder().image(MEMCACHED_LATEST).hostConfig(hostConfig).build();
    final ContainerCreation container = sut.createContainer(config, randomName());
    sut.startContainer(container.id());
    final ContainerInfo containerInfo = sut.inspectContainer(container.id());
    assertThat(containerInfo, notNullValue());
    assertThat(containerInfo.hostConfig().securityOpt(), hasItems(userLabel, roleLabel, typeLabel, levelLabel));
}
Also used : 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) Test(org.junit.Test)

Example 15 with HostConfig

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

the class DefaultDockerClientTest method testRestartPolicy.

private void testRestartPolicy(HostConfig.RestartPolicy restartPolicy) throws Exception {
    sut.pull(BUSYBOX_LATEST);
    final HostConfig hostConfig = HostConfig.builder().restartPolicy(restartPolicy).build();
    final ContainerConfig containerConfig = ContainerConfig.builder().image(BUSYBOX_LATEST).cmd("sh", "-c", "while :; do sleep 1; done").hostConfig(hostConfig).build();
    final String containerName = randomName();
    final ContainerCreation containerCreation = sut.createContainer(containerConfig, containerName);
    final String containerId = containerCreation.id();
    final ContainerInfo info = sut.inspectContainer(containerId);
    assertThat(info.hostConfig().restartPolicy().name(), is(restartPolicy.name()));
    final Integer retryCount = restartPolicy.maxRetryCount() == null ? 0 : restartPolicy.maxRetryCount();
    assertThat(info.hostConfig().restartPolicy().maxRetryCount(), is(retryCount));
}
Also used : 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)

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