james-server-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From btell...@apache.org
Subject [2/4] james-project git commit: JAMES-1912 Somes docker tests fail when no docker
Date Wed, 03 May 2017 03:41:07 GMT
JAMES-1912 Somes docker tests fail when no docker


Project: http://git-wip-us.apache.org/repos/asf/james-project/repo
Commit: http://git-wip-us.apache.org/repos/asf/james-project/commit/0f7ee8cf
Tree: http://git-wip-us.apache.org/repos/asf/james-project/tree/0f7ee8cf
Diff: http://git-wip-us.apache.org/repos/asf/james-project/diff/0f7ee8cf

Branch: refs/heads/master
Commit: 0f7ee8cf1fbc1e4ea93bc6e4acfb9ed3594722fa
Parents: f4b51ba
Author: quynhn <qnguyen@linagora.com>
Authored: Fri Apr 21 17:07:50 2017 +0700
Committer: benwa <btellier@linagora.com>
Committed: Wed May 3 10:40:21 2017 +0700

----------------------------------------------------------------------
 .../util/streams/SwarmGenericContainer.java     | 76 ++++++++++++++++----
 1 file changed, 64 insertions(+), 12 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/james-project/blob/0f7ee8cf/server/container/util-java8/src/test/java/org/apache/james/util/streams/SwarmGenericContainer.java
----------------------------------------------------------------------
diff --git a/server/container/util-java8/src/test/java/org/apache/james/util/streams/SwarmGenericContainer.java
b/server/container/util-java8/src/test/java/org/apache/james/util/streams/SwarmGenericContainer.java
index 2c70aa1..fb743a5 100644
--- a/server/container/util-java8/src/test/java/org/apache/james/util/streams/SwarmGenericContainer.java
+++ b/server/container/util-java8/src/test/java/org/apache/james/util/streams/SwarmGenericContainer.java
@@ -21,40 +21,92 @@ package org.apache.james.util.streams;
 
 import java.util.List;
 
+import com.google.common.base.Strings;
+import org.junit.Assume;
+import org.junit.rules.TestRule;
+import org.junit.runner.Description;
+import org.junit.runners.model.Statement;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 import org.testcontainers.containers.GenericContainer;
 import org.testcontainers.images.builder.ImageFromDockerfile;
+import org.testcontainers.shaded.com.github.dockerjava.api.command.InspectContainerResponse;
 
-import com.google.common.base.Strings;
-
-public class SwarmGenericContainer extends GenericContainer<SwarmGenericContainer>
{
-
+public class SwarmGenericContainer implements TestRule {
     private static final Logger LOGGER = LoggerFactory.getLogger(SwarmGenericContainer.class);
     private static final String DOCKER_CONTAINER = "DOCKER_CONTAINER";
+    private static final String NO_DOCKER_ENVIRONMENT = "Could not find a valid Docker environment.";
+    private static final String SKIPPING_TEST_CAUTION = "Skipping all docker tests as no
Docker environment was found";
+
+    private GenericContainer container;
 
     public SwarmGenericContainer(String dockerImageName) {
-        super(dockerImageName);
+        try {
+            this.container = new GenericContainer(dockerImageName);
+        } catch (IllegalStateException e) {
+            logAndCheckSkipTest(e);
+        }
     }
 
     public SwarmGenericContainer(ImageFromDockerfile imageFromDockerfile) {
-        super(imageFromDockerfile);
+        try {
+            this.container = new GenericContainer(imageFromDockerfile);
+        } catch (IllegalStateException e) {
+            logAndCheckSkipTest(e);
+        }
+    }
+
+    private void logAndCheckSkipTest(IllegalStateException e) {
+        LOGGER.error("Cannot initial a docker container because: " + e);
+        if (e.getMessage().startsWith(NO_DOCKER_ENVIRONMENT)) {
+            Assume.assumeTrue(SKIPPING_TEST_CAUTION, false);
+        }
     }
 
     public SwarmGenericContainer withAffinityToContainer() {
-        String container = System.getenv(DOCKER_CONTAINER);
-        if (Strings.isNullOrEmpty(container)) {
+        String containerEnv = System.getenv(DOCKER_CONTAINER);
+        if (Strings.isNullOrEmpty(containerEnv)) {
             LOGGER.warn("'DOCKER_CONTAINER' environment variable not found, dockering without
affinity");
-            return self();
+            return this;
         }
-        List<String> envVariables = getEnv();
+        List<String> envVariables = container.getEnv();
         envVariables.add("affinity:container==" + container);
-        setEnv(envVariables);
-        return self();
+        container.setEnv(envVariables);
+        return this;
+    }
+
+    public SwarmGenericContainer withEnv(String key, String value) {
+        container.addEnv(key, value);
+        return this;
+    }
+
+    public void start() {
+        container.start();
+    }
+
+    public void stop() {
+        container.stop();
+    }
+
+    public String getContainerIpAddress() {
+        return container.getContainerIpAddress();
+    }
+
+    public Integer getMappedPort(int originalPort) {
+        return container.getMappedPort(originalPort);
     }
 
     @SuppressWarnings("deprecation")
     public String getIp() {
         return getContainerInfo().getNetworkSettings().getIpAddress();
     }
+
+    public InspectContainerResponse getContainerInfo() {
+        return container.getContainerInfo();
+    }
+    @Override
+    public Statement apply(Statement statement, Description description) {
+        return container.apply(statement, description);
+    }
+
 }


---------------------------------------------------------------------
To unsubscribe, e-mail: server-dev-unsubscribe@james.apache.org
For additional commands, e-mail: server-dev-help@james.apache.org


Mime
View raw message