Unit test for docker local health checks.
authorDavid Bosschaert <davidb@apache.org>
Fri, 16 Jun 2017 12:10:27 +0000 (13:10 +0100)
committerDavid Bosschaert <davidb@apache.org>
Fri, 16 Jun 2017 12:10:27 +0000 (13:10 +0100)
containers-docker-local/src/main/java/org/apache/aries/containers/docker/local/impl/LocalDockerServiceManager.java
containers-docker-local/src/test/java/org/apache/aries/containers/docker/local/impl/LocalDockerServiceManagerTest.java

index 6e0f4a2..f5c0575 100644 (file)
@@ -164,6 +164,9 @@ public class LocalDockerServiceManager implements ServiceManager {
             command.add(hc.getInterval() + "s");
             command.add("--health-retries");
             command.add("" + hc.getMaxFailures());
+// TODO Add this when docker supports it in a release.
+//            command.add("--health-start-period");
+//            command.add(hc.getGracePeriod() + "s");
             command.add("--health-timeout");
             command.add(hc.getTimeout() + "s");
         }
index 934bd52..71c2ad2 100644 (file)
@@ -28,10 +28,13 @@ import java.util.Map;
 import java.util.Set;
 
 import org.apache.aries.containers.Container;
+import org.apache.aries.containers.HealthCheck;
 import org.apache.aries.containers.Service;
 import org.apache.aries.containers.ServiceConfig;
 import org.junit.Test;
 import org.mockito.Mockito;
+import org.mockito.invocation.InvocationOnMock;
+import org.mockito.stubbing.Answer;
 
 import static org.junit.Assert.assertEquals;
 
@@ -125,4 +128,42 @@ public class LocalDockerServiceManagerTest {
 
         assertEquals(new HashSet<>(Arrays.asList("svc1", "svc2", "svc3")), sm.listServices());
     }
+
+    @Test
+    public void testCreateDockerContainerWithHealthCheck() throws Exception {
+        Map<String, String> expectedArgs = new HashMap<>();
+        expectedArgs.put("--health-cmd", "curl --fail http://localhost:8080 || exit 1");
+        expectedArgs.put("--health-interval", "71s");
+        expectedArgs.put("--health-retries", "11");
+        expectedArgs.put("--health-timeout", "90s");
+        LocalDockerController dc = Mockito.mock(LocalDockerController.class);
+        Mockito.when(dc.run(Mockito.anyListOf(String.class))).then(new Answer<DockerContainerInfo>() {
+            @Override
+            public DockerContainerInfo answer(InvocationOnMock invocation) throws Throwable {
+                @SuppressWarnings("unchecked")
+                List<String> sl = (List<String>) invocation.getArguments()[0];
+
+                Map<String,String> ea = new HashMap<>(expectedArgs);
+                for (int i=0; i < sl.size() - 1; i++) {
+                    ea.remove(sl.get(i), sl.get(i+1));
+                }
+                if (ea.size() > 0) {
+                    // not all expected args found
+                    return null;
+                }
+                return new DockerContainerInfo("anid", "99.99.99.99");
+            }
+        });
+
+        LocalDockerServiceManager sm = new LocalDockerServiceManager(dc);
+
+        HealthCheck hc = HealthCheck.builder(HealthCheck.Type.COMMAND).
+                parameters("curl --fail http://localhost:8080 || exit 1").
+                interval(71).maxFailures(11).timeout(90).build();
+        ServiceConfig sc = ServiceConfig.builder("MySvc", "img1").healthCheck(hc).build();
+        ContainerImpl ctr = sm.createDockerContainer(sc);
+
+        assertEquals("anid", ctr.getID());
+        assertEquals("99.99.99.99", ctr.getHostName());
+    }
 }