use of com.spotify.docker.client.messages.Volume in project docker-client by spotify.
the class DefaultDockerClientTest method testCreateVolume.
@Test
public void testCreateVolume() throws Exception {
requireDockerApiVersionAtLeast("1.21", "volumes");
// Create bare volume
final Volume blankVolume = sut.createVolume();
assertThat(blankVolume, not(nullValue()));
sut.removeVolume(blankVolume);
// Create volume with attributes
final ImmutableMap<String, String> labels = ImmutableMap.of("foo", "bar");
final String volName = randomName();
final Volume toCreate;
if (dockerApiVersionLessThan("1.23")) {
toCreate = Volume.builder().name(volName).driver("local").build();
} else {
toCreate = Volume.builder().name(volName).driver("local").labels(labels).build();
}
final Volume created = sut.createVolume(toCreate);
assertEquals(toCreate.name(), created.name());
assertEquals(toCreate.driver(), created.driver());
assertEquals(toCreate.driverOpts(), created.driverOpts());
// mountpoint gets set by server regardless of whatever we ask for
assertNotEquals(toCreate.mountpoint(), created.mountpoint());
if (dockerApiVersionAtLeast("1.23")) {
assertEquals(labels, created.labels());
}
if (dockerApiVersionAtLeast("1.24")) {
assertEquals("local", created.scope());
}
sut.removeVolume(created);
}
use of com.spotify.docker.client.messages.Volume in project docker-client by spotify.
the class DefaultDockerClientUnitTest method testInspectVolume.
@Test
public void testInspectVolume() throws Exception {
final DefaultDockerClient dockerClient = new DefaultDockerClient(builder);
server.enqueue(new MockResponse().setResponseCode(200).addHeader("Content-Type", "application/json").setBody(fixture("fixtures/1.33/inspectVolume.json")));
final Volume volume = dockerClient.inspectVolume("my-volume");
assertThat(volume.name(), is("tardis"));
assertThat(volume.driver(), is("custom"));
assertThat(volume.mountpoint(), is("/var/lib/docker/volumes/tardis"));
assertThat(volume.status(), is(ImmutableMap.of("hello", "world")));
assertThat(volume.labels(), is(ImmutableMap.of("com.example.some-label", "some-value", "com.example.some-other-label", "some-other-value")));
assertThat(volume.scope(), is("local"));
assertThat(volume.options(), is(ImmutableMap.of("foo", "bar", "baz", "qux")));
}
use of com.spotify.docker.client.messages.Volume 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.Volume in project docker-client by spotify.
the class DefaultDockerClientTest method testListVolumes.
@Test
public void testListVolumes() throws Exception {
requireDockerApiVersionAtLeast("1.21", "volumes");
final Volume volume = sut.createVolume();
final String volumeName = volume.name();
final String volumeDriver = volume.driver();
final VolumeList volumeList = sut.listVolumes();
if (volumeList.warnings() != null && volumeList.warnings().isEmpty()) {
for (final String warning : volumeList.warnings()) {
log.warn(warning);
}
}
assertThat(volume, isIn(volumeList.volumes()));
final VolumeList volumeListWithDangling = sut.listVolumes(dangling());
if (volumeListWithDangling.warnings() != null && !volumeListWithDangling.warnings().isEmpty()) {
for (final String warning : volumeListWithDangling.warnings()) {
log.warn(warning);
}
}
assertThat(volume, isIn(volumeListWithDangling.volumes()));
if (dockerApiVersionAtLeast("1.24")) {
final VolumeList volumeListByName = sut.listVolumes(name(volumeName));
if (volumeListByName.warnings() != null && !volumeListByName.warnings().isEmpty()) {
for (final String warning : volumeListByName.warnings()) {
log.warn(warning);
}
}
assertThat(volume, isIn(volumeListByName.volumes()));
final VolumeList volumeListByDriver = sut.listVolumes(driver(volumeDriver));
if (volumeListByDriver.warnings() != null && !volumeListByDriver.warnings().isEmpty()) {
for (final String warning : volumeListByDriver.warnings()) {
log.warn(warning);
}
}
assertThat(volume, isIn(volumeListByDriver.volumes()));
}
if (dockerApiVersionAtLeast("1.24")) {
assertEquals("local", volume.scope());
// I don't know what is in the status object - JF
assertThat(volume.status(), is(anything()));
}
sut.removeVolume(volume);
}
use of com.spotify.docker.client.messages.Volume in project docker-client by spotify.
the class DefaultDockerClientTest method testInspectVolume.
@Test
public void testInspectVolume() throws Exception {
requireDockerApiVersionAtLeast("1.21", "volumes");
final Volume volume = sut.createVolume();
assertEquals(volume, sut.inspectVolume(volume.name()));
sut.removeVolume(volume);
final String badVolumeName = "this-is-a-very-unlikely-volume-name";
exception.expect(VolumeNotFoundException.class);
exception.expect(volumeNotFoundExceptionWithName(badVolumeName));
sut.inspectVolume(badVolumeName);
}
Aggregations