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()));
}
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));
}
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"));
}
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));
}
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));
}
Aggregations