com.github.dockerjava.api.model.Event Java Examples

The following examples show how to use com.github.dockerjava.api.model.Event. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: EventsCmdIT.java    From docker-java with Apache License 2.0 6 votes vote down vote up
@Test
public void testEventStreamTimeBound() throws Exception {
    //since until and filtering events is broken in swarm
    //https://github.com/docker/swarm/issues/1203
    assumeNotSwarm("", dockerRule);

    String startTime = getEpochTime();
    int expectedEvents = generateEvents();
    String endTime = getEpochTime();

    EventsTestCallback eventCallback = new EventsTestCallback(expectedEvents);

    dockerRule.getClient().eventsCmd()
            .withSince(startTime)
            .withUntil(endTime)
            .exec(eventCallback);

    List<Event> events = eventCallback.awaitExpectedEvents(30, TimeUnit.SECONDS);

    // we may receive more events as expected
    assertTrue("Received events: " + events, events.size() >= expectedEvents);
}
 
Example #2
Source File: EventsCmdIT.java    From docker-java with Apache License 2.0 6 votes vote down vote up
@Test
public void testEventStreamingWithFilter() throws Exception {
    //since until and filtering events is broken in swarm
    //https://github.com/docker/swarm/issues/1203
    assumeNotSwarm("", dockerRule);

    String startTime = getEpochTime();
    int expectedEvents = 1;

    EventsTestCallback eventCallback = new EventsTestCallback(expectedEvents);

    dockerRule.getClient().eventsCmd()
            .withSince(startTime)
            .withEventFilter("start")
            .exec(eventCallback);

    generateEvents();

    List<Event> events = eventCallback.awaitExpectedEvents(30, TimeUnit.SECONDS);

    // we should only get "start" events here
    for (Event event : events) {
        assertThat("Received event: " + event, event.getAction(), is("start"));
    }
}
 
Example #3
Source File: EventsCmdExec.java    From docker-java with Apache License 2.0 6 votes vote down vote up
@Override
protected Void execute0(EventsCmd command, ResultCallback<Event> resultCallback) {

    WebTarget webTarget = getBaseResource().path("/events").queryParam("since", command.getSince())
            .queryParam("until", command.getUntil());

    if (command.getFilters() != null && !command.getFilters().isEmpty()) {
        webTarget = webTarget
                .queryParam("filters", FiltersEncoder.jsonEncode(command.getFilters()));
    }

    LOGGER.trace("GET: {}", webTarget);

    webTarget.request().get(new TypeReference<Event>() {
    }, resultCallback);

    return null;
}
 
Example #4
Source File: ResultCallback.java    From Core with MIT License 5 votes vote down vote up
@Override
public void onNext(Event event) {
    if (event.getType() == null || !event.getType().getValue().equals("container")) {
        super.onNext(event);
        return;
    }

    ContainerEvent containerEvent = new ContainerEvent(event.getId(), event.getAction());

    // Not lets inspect the container, we won't fire any events without network information
    try {
        InspectContainerResponse info = this.dockerClient.inspectContainerCmd(event.getId()).exec();

        containerEvent.setName(info.getName());
        containerEvent.setEnvironmentVariables(this.getEnvironmentVariables(info));
        containerEvent.setPort(this.getPort(info));
        containerEvent.setIp(this.getIp(info, this.network));

    } catch (NotFoundException e) {
        super.onNext(event);
        return;
    }

    super.onNext(event);
    this.proxyServer.getPluginManager().callEvent(containerEvent);

}
 
Example #5
Source File: DockerContainerInspector.java    From Core with MIT License 5 votes vote down vote up
public void runContainerInspection() {
    this.logger.info("[Docker Container Inspector] Running initial inspection.");

    EventsResultCallback callback = this.getEventResultCallback();
    List<Container> containers = this.dockerClient.listContainersCmd().exec();

    // Trigger fake Event to use same Result Callback
    for (Container container: containers) {
        Event event = new Event("start", container.getId(), container.getImage(), System.currentTimeMillis())
                .withAction("bootstrap")
                .withType(EventType.forValue("container"));

        callback.onNext(event);
    }
}
 
Example #6
Source File: EventStreamTest.java    From testcontainers-java with MIT License 5 votes vote down vote up
/**
 * Test that docker events can be streamed from the client.
 */
@Test
public void test() throws IOException, InterruptedException {
    CountDownLatch latch = new CountDownLatch(1);

    try (
        GenericContainer container = new GenericContainer<>()
            .withCommand("true")
            .withStartupCheckStrategy(new OneShotStartupCheckStrategy())
    ) {
        container.start();
        String createdAt = container.getContainerInfo().getCreated();

        // Request all events between startTime and endTime for the container
        try (
            EventsResultCallback response = DockerClientFactory.instance().client().eventsCmd()
                .withContainerFilter(container.getContainerId())
                .withEventFilter("create")
                .withSince(Instant.parse(createdAt).getEpochSecond() + "")
                .exec(new EventsResultCallback() {
                    @Override
                    public void onNext(@NotNull Event event) {
                        // Check that a create event for the container is received
                        if (event.getId().equals(container.getContainerId()) && event.getStatus().equals("create")) {
                            latch.countDown();
                        }
                    }
                })
        ) {
            response.awaitStarted(5, TimeUnit.SECONDS);
            latch.await(5, TimeUnit.SECONDS);
        }
    }
}
 
Example #7
Source File: DockerTraceabilityReport.java    From docker-traceability-plugin with MIT License 5 votes vote down vote up
public DockerTraceabilityReport(@Nonnull Event event, @Nonnull Info hostInfo, 
        @CheckForNull InspectContainerResponse container, 
        @CheckForNull String imageId, @CheckForNull String imageName, 
        @CheckForNull InspectImageResponse image,
        @Nonnull List<String> parents, @CheckForNull String environment) {
    this.event = event;
    this.hostInfo = hostInfo;
    this.container = container;
    this.imageId = imageId;
    this.image = image;
    this.parents = new ArrayList<String>(parents);
    this.imageName = imageName;
    this.environment = environment;
    
}
 
Example #8
Source File: EventsCmdIT.java    From docker-java with Apache License 2.0 5 votes vote down vote up
@Test
public void testEventStreaming() throws Exception {
    String startTime = getEpochTime();

    int expectedEvents = generateEvents();

    EventsTestCallback eventCallback = new EventsTestCallback(expectedEvents);

    dockerRule.getClient().eventsCmd()
            .withSince(startTime)
            .exec(eventCallback);

    generateEvents();

    List<Event> events = eventCallback.awaitExpectedEvents(30, TimeUnit.SECONDS);

    // we may receive more events as expected
    assertTrue("Received events: " + events, events.size() >= expectedEvents);

    for (Event event : events) {
        if (TestUtils.isSwarm(dockerRule.getClient())) {
            assertThat(event.getNode(), is(notNullValue()));
            assertThat(event.getNode().getAddr(), is(notNullValue()));
            assertThat(event.getNode().getId(), is(notNullValue()));
            assertThat(event.getNode().getIp(), is(notNullValue()));
            assertThat(event.getNode().getName(), is(notNullValue()));
        } else {
            assertThat(event.getNode(), is(nullValue()));
        }
    }
}
 
Example #9
Source File: EventsCmdIT.java    From docker-java with Apache License 2.0 5 votes vote down vote up
public List<Event> awaitExpectedEvents(long timeout, TimeUnit unit) {
    try {
        countDownLatch.await(timeout, unit);
        close();
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
    return new ArrayList<>(events);
}
 
Example #10
Source File: DockerTraceabilityReport.java    From docker-traceability-plugin with MIT License 4 votes vote down vote up
/**
 * Stub constructor for deserialization purposes.
 */
public DockerTraceabilityReport() {
    event = new Event();
    hostInfo = new Info();
    parents = new LinkedList<String>();
}
 
Example #11
Source File: DockerTraceabilityReport.java    From docker-traceability-plugin with MIT License 4 votes vote down vote up
public @Nonnull Event getEvent() {
    return event;
}
 
Example #12
Source File: EventsCmdIT.java    From docker-java with Apache License 2.0 4 votes vote down vote up
public void onNext(Event event) {
    LOG.info("Received event #{}: {}", countDownLatch.getCount(), event);
    events.add(event);
    countDownLatch.countDown();
}
 
Example #13
Source File: EventsResultCallback.java    From docker-java with Apache License 2.0 4 votes vote down vote up
@Override
public void onNext(Event item) {
    LOGGER.debug(item.toString());
}