Search in sources :

Example 6 with Volume

use of com.spotify.docker.client.messages.Volume 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);
}
Also used : ContainerConfig(com.spotify.docker.client.messages.ContainerConfig) ContainerCreation(com.spotify.docker.client.messages.ContainerCreation) Volume(com.spotify.docker.client.messages.Volume) HostConfig(com.spotify.docker.client.messages.HostConfig) Test(org.junit.Test)

Example 7 with Volume

use of com.spotify.docker.client.messages.Volume 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());
    }
}
Also used : ContainerConfig(com.spotify.docker.client.messages.ContainerConfig) ContainerCreation(com.spotify.docker.client.messages.ContainerCreation) Volume(com.spotify.docker.client.messages.Volume) HostConfig(com.spotify.docker.client.messages.HostConfig) Event(com.spotify.docker.client.messages.Event) Long.toHexString(java.lang.Long.toHexString) Matchers.isEmptyOrNullString(org.hamcrest.Matchers.isEmptyOrNullString) Matchers.containsString(org.hamcrest.Matchers.containsString) Date(java.util.Date) 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