package org.apache.mesos.logstash.systemtest;

import com.github.dockerjava.api.DockerClient;
import com.github.dockerjava.api.command.InspectContainerResponse;
import com.github.dockerjava.core.DockerClientBuilder;
import com.github.dockerjava.core.DockerClientConfig;
import org.apache.log4j.Logger;
import org.junit.Test;

import java.util.concurrent.Callable;
import java.util.concurrent.TimeUnit;

import static com.jayway.awaitility.Awaitility.await;
import static org.hamcrest.core.IsNull.notNullValue;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.fail;

/**
 * Tests REST node discovery
 */
public class DiscoverySystemTest {

    public static final Logger LOGGER = Logger.getLogger(DiscoverySystemTest.class);

    @Test
    public void testNodeDiscoveryRest() throws InterruptedException {

        DockerClient outerDockerClient = createDockerClient("unix:///var/run/docker.sock");
        InspectContainerResponse response = outerDockerClient.inspectContainerCmd("mesosls_slave1_1").exec();

        String ipAddress = response.getNetworkSettings().getIpAddress();

        DockerClient innerDockerClient = createDockerClient(String.format("http://%s:2376", ipAddress));

        LOGGER.info(String.format("Asking for containers at %s", ipAddress));

        int numberOfContainersRunning = innerDockerClient.listContainersCmd().exec().size();

        assertEquals(1, numberOfContainersRunning);
    }

    private static DockerClient createDockerClient(String hostAddress) {
        DockerClientConfig config = DockerClientConfig.createDefaultConfigBuilder()
                .withVersion("1.18")
                .withUri(hostAddress)
                .build();

        return DockerClientBuilder.getInstance(config).build();
    }

}
@@ -154,16 +154,76 @@
 sponse;%0A
+import com.github.dockerjava.api.command.ListContainersCmd;%0A
 import c
@@ -265,24 +265,24 @@
 entBuilder;%0A
-
 import com.g
@@ -563,16 +563,55 @@
 lValue;%0A
+import static org.hamcrest.Matchers.*;%0A
 import s
@@ -878,25 +878,33 @@
 test
-NodeDiscoveryRest
+ThatOneContainerIsRunning
 () t
@@ -1435,16 +1435,45 @@
 nning = 
+await().until(containersSize(
 innerDoc
@@ -1505,21 +1505,21 @@
 md()
-.exec().size(
+), equalTo(1)
 );%0A%0A
@@ -1832,24 +1832,24 @@
  .build();%0A%0A
-
         retu
@@ -1907,9 +1907,312 @@
 %0A    %7D%0A%0A
+    private Callable%3CInteger%3E containersSize(final ListContainersCmd listContainersCmd) %7B%0A        return new Callable%3CInteger%3E() %7B%0A            public Integer call() throws Exception %7B%0A                return listContainersCmd.exec().size(); // The condition supplier part%0A            %7D%0A        %7D;%0A    %7D%0A%0A
 %7D

Test that checks for a running container on the slave