use of com.spotify.docker.client.messages.Event in project docker-client by spotify.
the class DefaultDockerClientTest method testEventStreamPolling.
@Test
public void testEventStreamPolling() throws Exception {
// In this test we do stuff, then open an event stream for the
// time window where we did the stuff, and make sure all the events
// we did are in there
final String containerName = randomName();
final ContainerConfig config = ContainerConfig.builder().image(BUSYBOX_LATEST).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);
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;
// By reading the event stream into a list, we can retain all the events
// but still ensure that we are able to close the stream.
// In other words, the HTTP connection has been closed.
final List<Event> eventList;
try (final EventStream stream = getImageAndContainerEventStream(since(startTime), until(endTime))) {
eventList = Lists.newArrayList(stream);
}
assertNotNull(eventList);
assertThat(eventList, hasSize(5));
imageEventAssertions(eventList.get(0), BUSYBOX_LATEST, "pull");
// create and start event assertions
containerEventAssertions(eventList.get(1), containerId, containerName, "create", BUSYBOX_LATEST);
containerEventAssertions(eventList.get(2), containerId, containerName, "start", BUSYBOX_LATEST);
containerEventAssertions(eventList.get(3), containerId, containerName, "die", BUSYBOX_LATEST);
containerEventAssertions(eventList.get(4), containerId, containerName, "destroy", BUSYBOX_LATEST);
}
use of com.spotify.docker.client.messages.Event 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