Search in sources :

Example 1 with Volume

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);
}
Also used : Volume(com.spotify.docker.client.messages.Volume) Long.toHexString(java.lang.Long.toHexString) Matchers.isEmptyOrNullString(org.hamcrest.Matchers.isEmptyOrNullString) Matchers.containsString(org.hamcrest.Matchers.containsString) Test(org.junit.Test)

Example 2 with Volume

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")));
}
Also used : MockResponse(okhttp3.mockwebserver.MockResponse) Volume(com.spotify.docker.client.messages.Volume) Test(org.junit.Test)

Example 3 with Volume

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));
}
Also used : ContainerConfig(com.spotify.docker.client.messages.ContainerConfig) Bind(com.spotify.docker.client.messages.HostConfig.Bind) SecretBind(com.spotify.docker.client.messages.swarm.SecretBind) Volume(com.spotify.docker.client.messages.Volume) 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 4 with Volume

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);
}
Also used : Volume(com.spotify.docker.client.messages.Volume) Long.toHexString(java.lang.Long.toHexString) Matchers.isEmptyOrNullString(org.hamcrest.Matchers.isEmptyOrNullString) Matchers.containsString(org.hamcrest.Matchers.containsString) VolumeList(com.spotify.docker.client.messages.VolumeList) Test(org.junit.Test)

Example 5 with 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);
}
Also used : Volume(com.spotify.docker.client.messages.Volume) Long.toHexString(java.lang.Long.toHexString) Matchers.isEmptyOrNullString(org.hamcrest.Matchers.isEmptyOrNullString) Matchers.containsString(org.hamcrest.Matchers.containsString) Test(org.junit.Test)

Aggregations

Volume (com.spotify.docker.client.messages.Volume)7 Test (org.junit.Test)7 Long.toHexString (java.lang.Long.toHexString)5 Matchers.containsString (org.hamcrest.Matchers.containsString)5 Matchers.isEmptyOrNullString (org.hamcrest.Matchers.isEmptyOrNullString)5 ContainerConfig (com.spotify.docker.client.messages.ContainerConfig)3 HostConfig (com.spotify.docker.client.messages.HostConfig)3 ContainerCreation (com.spotify.docker.client.messages.ContainerCreation)2 Predicate (com.google.common.base.Predicate)1 ContainerInfo (com.spotify.docker.client.messages.ContainerInfo)1 ContainerMount (com.spotify.docker.client.messages.ContainerMount)1 Event (com.spotify.docker.client.messages.Event)1 Bind (com.spotify.docker.client.messages.HostConfig.Bind)1 VolumeList (com.spotify.docker.client.messages.VolumeList)1 SecretBind (com.spotify.docker.client.messages.swarm.SecretBind)1 Date (java.util.Date)1 MockResponse (okhttp3.mockwebserver.MockResponse)1