use of com.spotify.docker.client.messages.HostConfig in project docker-client by spotify.
the class DefaultDockerClientTest method testContainerVolumes.
@Test
public void testContainerVolumes() throws Exception {
requireDockerApiVersionAtLeast("1.20", "Creating a container with volumes and inspecting volumes");
sut.pull(BUSYBOX_LATEST);
final String namedVolumeName = "aVolume";
final String namedVolumeFrom = "/a/host/path";
final String namedVolumeTo = "/a/destination/path";
final String bindObjectFrom = "/some/path";
final String bindObjectTo = "/some/other/path";
final String bindStringFrom = "/local/path";
final String bindStringTo = "/remote/path";
final String anonVolumeTo = "/foo";
final Volume volume = Volume.builder().name(namedVolumeName).mountpoint(namedVolumeFrom).build();
sut.createVolume(volume);
final Bind bindUsingVolume = Bind.from(volume).to(namedVolumeTo).build();
final Bind bind = Bind.from(bindObjectFrom).to(bindObjectTo).readOnly(true).build();
final HostConfig hostConfig = HostConfig.builder().appendBinds(bind).appendBinds(bindStringFrom + ":" + bindStringTo).appendBinds(bindUsingVolume).build();
final ContainerConfig volumeConfig = ContainerConfig.builder().image(BUSYBOX_LATEST).addVolume(anonVolumeTo).hostConfig(hostConfig).build();
final String id = sut.createContainer(volumeConfig, randomName()).id();
final ContainerInfo containerInfo = sut.inspectContainer(id);
final List<ContainerMount> mounts = containerInfo.mounts();
assertThat(mounts.size(), equalTo(4));
{
final ContainerMount bindObjectMount = Iterables.find(mounts, new Predicate<ContainerMount>() {
@Override
public boolean apply(ContainerMount mount) {
return bindObjectFrom.equals(mount.source());
}
}, null);
assertThat("Did not find mount from bind object", bindObjectMount, notNullValue());
assertThat(bindObjectMount.source(), is(bindObjectFrom));
assertThat(bindObjectMount.destination(), is(bindObjectTo));
assertThat(bindObjectMount.driver(), isEmptyOrNullString());
assertThat(bindObjectMount.rw(), is(false));
assertThat(bindObjectMount.mode(), is(equalTo("ro")));
if (dockerApiVersionAtLeast("1.25") && dockerApiVersionLessThan("1.30")) {
// From version 1.25 to 1.29, the API behaved like this
assertThat(bindObjectMount.name(), isEmptyOrNullString());
assertThat(bindObjectMount.propagation(), isEmptyOrNullString());
} else if (dockerApiVersionAtLeast("1.22") || dockerApiVersionAtLeast("1.30")) {
// From version 1.22 to 1.24, and from 1.30 up, the API behaves like this
assertThat(bindObjectMount.name(), isEmptyOrNullString());
assertThat(bindObjectMount.propagation(), is(equalTo("rprivate")));
} else {
// Below version 1.22
assertThat(bindObjectMount.name(), is(nullValue()));
assertThat(bindObjectMount.propagation(), is(nullValue()));
}
if (dockerApiVersionAtLeast("1.26")) {
assertThat(bindObjectMount.type(), is(equalTo("bind")));
assertThat(bindObjectMount.driver(), isEmptyOrNullString());
} else {
assertThat(bindObjectMount.type(), is(nullValue()));
assertThat(bindObjectMount.driver(), is(nullValue()));
}
}
{
final ContainerMount bindStringMount = Iterables.find(mounts, new Predicate<ContainerMount>() {
@Override
public boolean apply(ContainerMount mount) {
return bindStringFrom.equals(mount.source());
}
}, null);
assertThat("Did not find mount from bind string", bindStringMount, notNullValue());
assertThat(bindStringMount.source(), is(equalTo(bindStringFrom)));
assertThat(bindStringMount.destination(), is(equalTo(bindStringTo)));
assertThat(bindStringMount.driver(), isEmptyOrNullString());
assertThat(bindStringMount.rw(), is(true));
assertThat(bindStringMount.mode(), is(equalTo("")));
if (dockerApiVersionAtLeast("1.25") && dockerApiVersionLessThan("1.30")) {
// From version 1.25 to 1.29, the API behaved like this
assertThat(bindStringMount.name(), isEmptyOrNullString());
assertThat(bindStringMount.propagation(), isEmptyOrNullString());
} else if (dockerApiVersionAtLeast("1.22") || dockerApiVersionAtLeast("1.30")) {
// From version 1.22 to 1.24, and from 1.30 up, the API behaves like this
assertThat(bindStringMount.name(), isEmptyOrNullString());
assertThat(bindStringMount.propagation(), is(equalTo("rprivate")));
} else {
// Below version 1.22
assertThat(bindStringMount.name(), is(nullValue()));
assertThat(bindStringMount.propagation(), is(nullValue()));
}
if (dockerApiVersionAtLeast("1.26")) {
assertThat(bindStringMount.type(), is(equalTo("bind")));
assertThat(bindStringMount.driver(), isEmptyOrNullString());
} else {
assertThat(bindStringMount.type(), is(nullValue()));
assertThat(bindStringMount.driver(), is(nullValue()));
}
}
{
final ContainerMount namedVolumeMount = Iterables.find(mounts, new Predicate<ContainerMount>() {
@Override
public boolean apply(ContainerMount mount) {
return namedVolumeTo.equals(mount.destination());
}
}, null);
assertThat("Did not find mount from named volume", namedVolumeMount, notNullValue());
assertThat(namedVolumeMount.name(), is(equalTo(namedVolumeName)));
assertThat(namedVolumeMount.source(), containsString("/" + namedVolumeName + "/"));
assertThat(namedVolumeMount.destination(), is(equalTo(namedVolumeTo)));
assertThat(namedVolumeMount.rw(), is(true));
assertThat(namedVolumeMount.mode(), is(equalTo("z")));
assertThat(namedVolumeMount.name(), is(namedVolumeName));
assertThat(namedVolumeMount.driver(), is(equalTo("local")));
if (dockerApiVersionAtLeast("1.25")) {
assertThat(namedVolumeMount.propagation(), isEmptyOrNullString());
} else if (dockerApiVersionAtLeast("1.22")) {
assertThat(namedVolumeMount.propagation(), is(equalTo("rprivate")));
} else {
assertThat(namedVolumeMount.propagation(), is(nullValue()));
}
if (dockerApiVersionAtLeast("1.26")) {
assertThat(namedVolumeMount.type(), is(equalTo("volume")));
} else {
assertThat(namedVolumeMount.type(), is(nullValue()));
}
}
{
final ContainerMount anonVolumeMount = Iterables.find(mounts, new Predicate<ContainerMount>() {
@Override
public boolean apply(ContainerMount mount) {
return anonVolumeTo.equals(mount.destination());
}
}, null);
assertThat("Did not find mount from anonymous volume", anonVolumeMount, notNullValue());
assertThat(anonVolumeMount.source(), containsString("/" + anonVolumeMount.name() + "/"));
assertThat(anonVolumeMount.destination(), is(equalTo(anonVolumeTo)));
assertThat(anonVolumeMount.mode(), isEmptyOrNullString());
assertThat(anonVolumeMount.rw(), is(true));
assertThat(anonVolumeMount.mode(), is(equalTo("")));
assertThat(anonVolumeMount.name(), not(isEmptyOrNullString()));
assertThat(anonVolumeMount.driver(), is(equalTo("local")));
if (dockerApiVersionAtLeast("1.22")) {
assertThat(anonVolumeMount.propagation(), isEmptyOrNullString());
} else {
assertThat(anonVolumeMount.propagation(), is(nullValue()));
}
if (dockerApiVersionAtLeast("1.26")) {
assertThat(anonVolumeMount.type(), is(equalTo("volume")));
} else {
assertThat(anonVolumeMount.type(), is(nullValue()));
}
}
assertThat(containerInfo.config().volumes(), hasItem(anonVolumeTo));
}
use of com.spotify.docker.client.messages.HostConfig in project docker-client by spotify.
the class DefaultDockerClientTest method testRemoveVolume.
@Test
public void testRemoveVolume() throws Exception {
requireDockerApiVersionAtLeast("1.21", "volumes");
// Create a volume and remove it
final Volume volume1 = sut.createVolume();
sut.removeVolume(volume1);
// Remove non-existent volume
exception.expect(VolumeNotFoundException.class);
exception.expect(volumeNotFoundExceptionWithName(volume1.name()));
sut.removeVolume(volume1);
// Create a volume, assign it to a container, and try to remove it.
// Should get a ConflictException.
final Volume volume2 = sut.createVolume();
final HostConfig hostConfig = HostConfig.builder().binds(Bind.from(volume2).to("/tmp").build()).build();
final ContainerConfig config = ContainerConfig.builder().image(BUSYBOX).hostConfig(hostConfig).build();
final ContainerCreation container = sut.createContainer(config, randomName());
exception.expect(ConflictException.class);
sut.removeVolume(volume2);
// Clean up
sut.removeContainer(container.id());
sut.removeVolume(volume2);
}
use of com.spotify.docker.client.messages.HostConfig in project docker-client by spotify.
the class DefaultDockerClientTest method testContainerWithAppArmorLogs.
@Test
public void testContainerWithAppArmorLogs() throws Exception {
requireDockerApiVersionAtLeast("1.21", "StopSignal and AppArmorProfile");
sut.pull(BUSYBOX_LATEST);
final boolean privileged = true;
final boolean publishAllPorts = true;
final String dns = "1.2.3.4";
final HostConfig expected = HostConfig.builder().privileged(privileged).publishAllPorts(publishAllPorts).dns(dns).cpuShares(4096L).build();
final String stopSignal = "SIGTERM";
final ContainerConfig config = ContainerConfig.builder().image(BUSYBOX_LATEST).hostConfig(expected).stopSignal(stopSignal).build();
final String name = randomName();
final ContainerCreation creation = sut.createContainer(config, name);
final String id = creation.id();
sut.startContainer(id);
final ContainerInfo inspection = sut.inspectContainer(id);
final HostConfig actual = inspection.hostConfig();
assertThat(actual.privileged(), equalTo(expected.privileged()));
assertThat(actual.publishAllPorts(), equalTo(expected.publishAllPorts()));
assertThat(actual.dns(), equalTo(expected.dns()));
assertThat(actual.cpuShares(), equalTo(expected.cpuShares()));
assertThat(sut.inspectContainer(id).config().stopSignal(), equalTo(config.stopSignal()));
assertThat(inspection.appArmorProfile(), equalTo(""));
assertThat(inspection.execIds(), equalTo(null));
assertThat(inspection.logPath(), containsString(id + "-json.log"));
assertThat(inspection.restartCount(), equalTo(0L));
assertThat(inspection.mounts().isEmpty(), equalTo(true));
// Wait for the container to exit
sut.waitContainer(id);
final List<Container> containers = sut.listContainers(allContainers(), withStatusExited());
Container targetCont = null;
for (final Container container : containers) {
if (container.id().equals(id)) {
targetCont = container;
break;
}
}
assertNotNull(targetCont);
assertThat(targetCont.imageId(), equalTo(inspection.image()));
}
use of com.spotify.docker.client.messages.HostConfig in project docker-client by spotify.
the class DefaultDockerClientTest method testContainerWithCpuQuota.
@Test
public void testContainerWithCpuQuota() throws Exception {
requireDockerApiVersionAtLeast("1.19", "Container Creation with HostConfig");
assumeFalse(CIRCLECI);
sut.pull(BUSYBOX_LATEST);
final boolean privileged = true;
final boolean publishAllPorts = true;
final String dns = "1.2.3.4";
final HostConfig expected = HostConfig.builder().privileged(privileged).publishAllPorts(publishAllPorts).dns(dns).cpuQuota(50000L).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()));
assertThat(actual.cpuQuota(), equalTo(expected.cpuQuota()));
}
use of com.spotify.docker.client.messages.HostConfig in project docker-client by spotify.
the class DefaultDockerClientTest method testEventTypes.
@Test(timeout = 10000)
public void testEventTypes() throws Exception {
requireDockerApiVersionAtLeast("1.22", "Event types");
final String volumeName = randomName();
final String containerName = randomName();
final String mountPath = "/anywhere";
final Volume volume = Volume.builder().name(volumeName).build();
final HostConfig hostConfig = HostConfig.builder().binds(Bind.from(volume).to(mountPath).build()).build();
final ContainerConfig config = ContainerConfig.builder().image(BUSYBOX_LATEST).hostConfig(hostConfig).build();
// Wait once to clean our event "palette" of events from other tests
Thread.sleep(1000);
final Date start = new Date();
final long startTime = start.getTime() / 1000;
sut.pull(BUSYBOX_LATEST);
sut.createVolume(volume);
final ContainerCreation container = sut.createContainer(config, containerName);
final String containerId = container.id();
sut.startContainer(containerId);
await().until(containerIsRunning(sut, containerId), is(false));
sut.removeContainer(containerId);
// Wait again to ensure we get back events for everything we did
Thread.sleep(1000);
final Date end = new Date();
final long endTime = end.getTime() / 1000;
// Image events
try (final EventStream stream = sut.events(since(startTime), until(endTime), type(IMAGE))) {
assertTrue("Docker did not return any image events.", stream.hasNext());
imageEventAssertions(stream.next(), BUSYBOX_LATEST, "pull");
assertFalse("Expect no more image events", stream.hasNext());
}
// Container events
try (final EventStream stream = sut.events(since(startTime), until(endTime), type(CONTAINER))) {
assertTrue("Docker did not return any container events.", stream.hasNext());
containerEventAssertions(stream.next(), containerId, containerName, "create", BUSYBOX_LATEST);
assertTrue("Docker did not return enough events. " + "Expected to see an event for starting a container.", stream.hasNext());
containerEventAssertions(stream.next(), containerId, containerName, "start", BUSYBOX_LATEST);
assertTrue("Docker did not return enough events. " + "Expected to see an event for the container finishing.", stream.hasNext());
containerEventAssertions(stream.next(), containerId, containerName, "die", BUSYBOX_LATEST);
assertTrue("Docker did not return enough events. " + "Expected to see an event for removing the container.", stream.hasNext());
containerEventAssertions(stream.next(), containerId, containerName, "destroy", BUSYBOX_LATEST);
assertFalse("Expect no more container events", stream.hasNext());
}
// Volume events
try (final EventStream stream = sut.events(since(startTime), until(endTime), type(VOLUME))) {
assertTrue("Docker did not return any volume events.", stream.hasNext());
final Event volumeCreate = stream.next();
assertEquals(VOLUME, volumeCreate.type());
assertEquals("create", volumeCreate.action());
assertEquals(volumeName, volumeCreate.actor().id());
assertThat(volumeCreate.actor().attributes(), hasEntry("driver", "local"));
assertNotNull(volumeCreate.timeNano());
assertTrue("Docker did not return enough volume events." + "Expected a volume mount event.", stream.hasNext());
final Event volumeMount = stream.next();
assertEquals(VOLUME, volumeMount.type());
assertEquals("mount", volumeMount.action());
assertEquals(volumeName, volumeMount.actor().id());
final Map<String, String> mountAttributes = volumeMount.actor().attributes();
assertThat(mountAttributes, hasEntry("driver", "local"));
assertThat(mountAttributes, hasEntry("container", containerId));
assertThat(mountAttributes, hasEntry("destination", mountPath));
assertThat(mountAttributes, hasEntry("read/write", "true"));
// Default value is system-dependent
assertThat(mountAttributes, hasKey("propagation"));
assertNotNull(volumeMount.timeNano());
assertTrue("Docker did not return enough volume events." + "Expected a volume unmount event.", stream.hasNext());
final Event volumeUnmount = stream.next();
assertEquals(VOLUME, volumeUnmount.type());
assertEquals("unmount", volumeUnmount.action());
assertEquals(volumeName, volumeUnmount.actor().id());
assertThat(volumeUnmount.actor().attributes(), hasEntry("driver", "local"));
assertThat(volumeUnmount.actor().attributes(), hasEntry("container", containerId));
assertNotNull(volumeUnmount.timeNano());
assertFalse("Expect no more volume events", stream.hasNext());
}
// Network events
try (final EventStream stream = sut.events(since(startTime), until(endTime), type(NETWORK))) {
assertTrue("Docker did not return any network events.", stream.hasNext());
final Event networkConnect = stream.next();
assertEquals(NETWORK, networkConnect.type());
assertEquals("connect", networkConnect.action());
// not sure how to get the network id
assertNotNull(networkConnect.actor().id());
assertThat(networkConnect.actor().attributes(), hasEntry("container", containerId));
assertThat(networkConnect.actor().attributes(), hasEntry("name", "bridge"));
assertThat(networkConnect.actor().attributes(), hasEntry("type", "bridge"));
assertTrue("Docker did not return enough network events." + "Expected a network disconnect event.", stream.hasNext());
final Event networkDisconnect = stream.next();
assertEquals(NETWORK, networkDisconnect.type());
assertEquals("disconnect", networkDisconnect.action());
assertEquals(networkDisconnect.actor().id(), networkDisconnect.actor().id());
assertThat(networkDisconnect.actor().attributes(), hasEntry("container", containerId));
assertThat(networkDisconnect.actor().attributes(), hasEntry("name", "bridge"));
assertThat(networkDisconnect.actor().attributes(), hasEntry("type", "bridge"));
assertFalse("Expect no more network events", stream.hasNext());
}
}
Aggregations