james-server-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From btell...@apache.org
Subject [07/10] james-project git commit: JAMES-2525 enforce singleton swift container for test
Date Thu, 11 Oct 2018 02:28:45 GMT
JAMES-2525 enforce singleton swift container for test


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

Branch: refs/heads/master
Commit: 7c57ae178a75ccbb63e12df59ed431f31ee644d6
Parents: 5dff052
Author: Jean Helou <jhe@codamens.fr>
Authored: Mon Sep 10 16:08:07 2018 +0200
Committer: Benoit Tellier <btellier@linagora.com>
Committed: Thu Oct 11 09:26:49 2018 +0700

----------------------------------------------------------------------
 .../objectstorage/DockerSwiftContainer.java     | 134 +++++++++++++++++++
 .../objectstorage/DockerSwiftExtension.java     |  52 +------
 .../blob/objectstorage/DockerSwiftRule.java     |  49 +++++++
 .../objectstorage/DockerSwiftSingleton.java     |  31 +++++
 4 files changed, 217 insertions(+), 49 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/james-project/blob/7c57ae17/server/blob/blob-objectstorage/src/test/java/org/apache/james/blob/objectstorage/DockerSwiftContainer.java
----------------------------------------------------------------------
diff --git a/server/blob/blob-objectstorage/src/test/java/org/apache/james/blob/objectstorage/DockerSwiftContainer.java
b/server/blob/blob-objectstorage/src/test/java/org/apache/james/blob/objectstorage/DockerSwiftContainer.java
new file mode 100644
index 0000000..b073eed
--- /dev/null
+++ b/server/blob/blob-objectstorage/src/test/java/org/apache/james/blob/objectstorage/DockerSwiftContainer.java
@@ -0,0 +1,134 @@
+/****************************************************************
+ * Licensed to the Apache Software Foundation (ASF) under one   *
+ * or more contributor license agreements.  See the NOTICE file *
+ * distributed with this work for additional information        *
+ * regarding copyright ownership.  The ASF licenses this file   *
+ * to you under the Apache License, Version 2.0 (the            *
+ * "License"); you may not use this file except in compliance   *
+ * with the License.  You may obtain a copy of the License at   *
+ *                                                              *
+ *   http://www.apache.org/licenses/LICENSE-2.0                 *
+ *                                                              *
+ * Unless required by applicable law or agreed to in writing,   *
+ * software distributed under the License is distributed on an  *
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY       *
+ * KIND, either express or implied.  See the License for the    *
+ * specific language governing permissions and limitations      *
+ * under the License.                                           *
+ ****************************************************************/
+
+package org.apache.james.blob.objectstorage;
+
+import static org.testcontainers.containers.wait.strategy.Wait.forHttp;
+
+import java.net.URI;
+
+import org.apache.james.util.Host;
+import org.apache.james.util.docker.RateLimiters;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+import org.testcontainers.containers.Container;
+import org.testcontainers.containers.GenericContainer;
+import org.testcontainers.containers.output.OutputFrame;
+import org.testcontainers.containers.wait.strategy.WaitAllStrategy;
+
+import com.github.fge.lambdas.Throwing;
+
+public class DockerSwiftContainer {
+    private static final Logger LOGGER = LoggerFactory.getLogger(DockerSwiftContainer.class);
+    private static final String SWIFT_DOCKER_IMAGE = "jeantil/openstack-keystone-swift:pike";
+    private static final int SWIFT_PORT = 8080;
+    private static final int KEYSTONE_ADMIN_PORT = 35357;
+    private final GenericContainer<?> swiftContainer;
+    private DockerSwift dockerSwift;
+
+    public DockerSwiftContainer() {
+        this.swiftContainer = new GenericContainer<>(SWIFT_DOCKER_IMAGE)
+            .withExposedPorts(KEYSTONE_ADMIN_PORT)
+            .withExposedPorts(SWIFT_PORT)
+            .withLogConsumer(DockerSwiftContainer::displayDockerLog)
+            .waitingFor(
+                new WaitAllStrategy()
+                    .withStrategy(
+                        forHttp("/v3")
+                            .forPort(KEYSTONE_ADMIN_PORT)
+                            .forStatusCode(200)
+                            .withRateLimiter(RateLimiters.DEFAULT)
+                    ).withStrategy(
+                    forHttp("/info")
+                        .forPort(SWIFT_PORT)
+                        .forStatusCode(200)
+                        .withRateLimiter(RateLimiters.DEFAULT)
+                )
+            );
+
+    }
+
+    public void start() {
+        swiftContainer.start();
+        Integer swiftPort = swiftContainer.getMappedPort(SWIFT_PORT);
+        String containerIpAddress = swiftContainer.getContainerIpAddress();
+        Container.ExecResult execResult =
+            Throwing.supplier(() ->
+                swiftContainer.execInContainer(
+                    "/swift/bin/register-swift-endpoint.sh",
+                    "http://" + containerIpAddress + ":" + swiftPort))
+                .sneakyThrow()
+                .get();
+        if (!execResult.getStdout().isEmpty()) {
+            LOGGER.debug(execResult.getStdout());
+        }
+        if (!execResult.getStderr().isEmpty()) {
+            LOGGER.error(execResult.getStderr());
+        }
+        URI keystoneV2Endpoint =
+            URI.create("http://" + getKeystoneHost() + "/v2.0");
+        URI keystoneV3Endpoint =
+            URI.create("http://" + getKeystoneHost() + "/v3");
+        URI swiftEndpoint =
+            URI.create("http://" + getSwiftHost() + "/auth/v1.0");
+        dockerSwift = new DockerSwift(keystoneV2Endpoint, keystoneV3Endpoint, swiftEndpoint);
+
+    }
+
+    public void stop() {
+        swiftContainer.stop();
+    }
+
+    public Host getKeystoneHost() {
+        return Host.from(
+            getIp(),
+            getKeystonePort());
+    }
+
+    public Host getSwiftHost() {
+        return Host.from(
+            getIp(),
+            getSwiftPort());
+    }
+
+    public String getIp() {
+        return swiftContainer.getContainerIpAddress();
+    }
+
+    public int getKeystonePort() {
+        return swiftContainer.getMappedPort(KEYSTONE_ADMIN_PORT);
+    }
+
+    public int getSwiftPort() {
+        return swiftContainer.getMappedPort(SWIFT_PORT);
+    }
+
+    public DockerSwift dockerSwift() {
+        return dockerSwift;
+    }
+
+    public GenericContainer<?> getRawContainer() {
+        return swiftContainer;
+    }
+
+    private static void displayDockerLog(OutputFrame outputFrame) {
+        LOGGER.info(outputFrame.getUtf8String());
+    }
+
+}

http://git-wip-us.apache.org/repos/asf/james-project/blob/7c57ae17/server/blob/blob-objectstorage/src/test/java/org/apache/james/blob/objectstorage/DockerSwiftExtension.java
----------------------------------------------------------------------
diff --git a/server/blob/blob-objectstorage/src/test/java/org/apache/james/blob/objectstorage/DockerSwiftExtension.java
b/server/blob/blob-objectstorage/src/test/java/org/apache/james/blob/objectstorage/DockerSwiftExtension.java
index b30bd44..43155a0 100644
--- a/server/blob/blob-objectstorage/src/test/java/org/apache/james/blob/objectstorage/DockerSwiftExtension.java
+++ b/server/blob/blob-objectstorage/src/test/java/org/apache/james/blob/objectstorage/DockerSwiftExtension.java
@@ -19,73 +19,27 @@
 
 package org.apache.james.blob.objectstorage;
 
-import static org.testcontainers.containers.wait.strategy.Wait.forHttp;
-
-import java.net.URI;
-
-import org.apache.james.util.docker.RateLimiters;
 import org.junit.jupiter.api.extension.AfterAllCallback;
 import org.junit.jupiter.api.extension.BeforeAllCallback;
 import org.junit.jupiter.api.extension.ExtensionContext;
 import org.junit.jupiter.api.extension.ParameterContext;
 import org.junit.jupiter.api.extension.ParameterResolutionException;
 import org.junit.jupiter.api.extension.ParameterResolver;
-import org.slf4j.Logger;
-import org.slf4j.LoggerFactory;
-import org.testcontainers.containers.Container;
-import org.testcontainers.containers.GenericContainer;
-import org.testcontainers.containers.wait.strategy.WaitAllStrategy;
 
 public class DockerSwiftExtension implements ParameterResolver, BeforeAllCallback,
     AfterAllCallback {
-    private static final Logger LOGGER = LoggerFactory.getLogger(DockerSwiftExtension.class);
-    private static final String SWIFT_DOCKER_IMAGE = "jeantil/openstack-keystone-swift:pike";
-    private static final int SWIFT_PORT = 8080;
-    private static final int KEYSTONE_ADMIN_PORT = 35357;
-
-    private final GenericContainer<?> swiftContainer;
 
+    private final DockerSwiftRule swiftContainer;
     private DockerSwift dockerSwift;
 
     public DockerSwiftExtension() {
-        swiftContainer = new GenericContainer<>(SWIFT_DOCKER_IMAGE)
-            .withExposedPorts(KEYSTONE_ADMIN_PORT)
-            .withExposedPorts(SWIFT_PORT)
-            .waitingFor(
-                new WaitAllStrategy().withStrategy(
-                    forHttp("/v3")
-                        .forPort(KEYSTONE_ADMIN_PORT)
-                        .forStatusCode(200)
-                        .withRateLimiter(RateLimiters.DEFAULT)
-                ).withStrategy(
-                    forHttp("/info")
-                        .forPort(SWIFT_PORT)
-                        .forStatusCode(200)
-                        .withRateLimiter(RateLimiters.DEFAULT)
-                )
-            );
+        swiftContainer = new DockerSwiftRule();
     }
 
     @Override
     public void beforeAll(ExtensionContext context) throws Exception {
         swiftContainer.start();
-        Integer keystonePort = swiftContainer.getMappedPort(KEYSTONE_ADMIN_PORT);
-        Integer swiftPort = swiftContainer.getMappedPort(SWIFT_PORT);
-        String containerIpAddress = swiftContainer.getContainerIpAddress();
-        Container.ExecResult execResult = swiftContainer.execInContainer("/swift/bin/register-swift-endpoint.sh",
"http://" + containerIpAddress + ":" + swiftPort);
-        if (!execResult.getStdout().isEmpty()) {
-            LOGGER.debug(execResult.getStdout());
-        }
-        if (!execResult.getStderr().isEmpty()) {
-            LOGGER.error(execResult.getStderr());
-        }
-        URI keystoneV2Endpoint =
-            URI.create("http://" + containerIpAddress + ":" + keystonePort + "/v2.0");
-        URI keystoneV3Endpoint =
-            URI.create("http://" + containerIpAddress + ":" + keystonePort + "/v3");
-        URI swiftEndpoint =
-            URI.create("http://" + containerIpAddress + ":" + swiftPort + "/auth/v1.0");
-        dockerSwift = new DockerSwift(keystoneV2Endpoint, keystoneV3Endpoint, swiftEndpoint);
+        dockerSwift = swiftContainer.dockerSwift();
     }
 
     @Override

http://git-wip-us.apache.org/repos/asf/james-project/blob/7c57ae17/server/blob/blob-objectstorage/src/test/java/org/apache/james/blob/objectstorage/DockerSwiftRule.java
----------------------------------------------------------------------
diff --git a/server/blob/blob-objectstorage/src/test/java/org/apache/james/blob/objectstorage/DockerSwiftRule.java
b/server/blob/blob-objectstorage/src/test/java/org/apache/james/blob/objectstorage/DockerSwiftRule.java
new file mode 100644
index 0000000..c46c519
--- /dev/null
+++ b/server/blob/blob-objectstorage/src/test/java/org/apache/james/blob/objectstorage/DockerSwiftRule.java
@@ -0,0 +1,49 @@
+/****************************************************************
+ * Licensed to the Apache Software Foundation (ASF) under one   *
+ * or more contributor license agreements.  See the NOTICE file *
+ * distributed with this work for additional information        *
+ * regarding copyright ownership.  The ASF licenses this file   *
+ * to you under the Apache License, Version 2.0 (the            *
+ * "License"); you may not use this file except in compliance   *
+ * with the License.  You may obtain a copy of the License at   *
+ *                                                              *
+ *   http://www.apache.org/licenses/LICENSE-2.0                 *
+ *                                                              *
+ * Unless required by applicable law or agreed to in writing,   *
+ * software distributed under the License is distributed on an  *
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY       *
+ * KIND, either express or implied.  See the License for the    *
+ * specific language governing permissions and limitations      *
+ * under the License.                                           *
+ ****************************************************************/
+
+package org.apache.james.blob.objectstorage;
+
+import org.junit.rules.TestRule;
+import org.junit.runner.Description;
+import org.junit.runners.model.Statement;
+import org.testcontainers.containers.GenericContainer;
+
+public class DockerSwiftRule implements TestRule {
+
+    @Override
+    public Statement apply(Statement base, Description description) {
+        return base;
+    }
+
+    public void start() {
+
+    }
+
+    public void stop() {
+
+    }
+
+    public GenericContainer<?> getRawContainer() {
+        return DockerSwiftSingleton.singleton.getRawContainer();
+    }
+
+    public DockerSwift dockerSwift() {
+        return DockerSwiftSingleton.singleton.dockerSwift();
+    }
+}

http://git-wip-us.apache.org/repos/asf/james-project/blob/7c57ae17/server/blob/blob-objectstorage/src/test/java/org/apache/james/blob/objectstorage/DockerSwiftSingleton.java
----------------------------------------------------------------------
diff --git a/server/blob/blob-objectstorage/src/test/java/org/apache/james/blob/objectstorage/DockerSwiftSingleton.java
b/server/blob/blob-objectstorage/src/test/java/org/apache/james/blob/objectstorage/DockerSwiftSingleton.java
new file mode 100644
index 0000000..3137797
--- /dev/null
+++ b/server/blob/blob-objectstorage/src/test/java/org/apache/james/blob/objectstorage/DockerSwiftSingleton.java
@@ -0,0 +1,31 @@
+/****************************************************************
+ * Licensed to the Apache Software Foundation (ASF) under one   *
+ * or more contributor license agreements.  See the NOTICE file *
+ * distributed with this work for additional information        *
+ * regarding copyright ownership.  The ASF licenses this file   *
+ * to you under the Apache License, Version 2.0 (the            *
+ * "License"); you may not use this file except in compliance   *
+ * with the License.  You may obtain a copy of the License at   *
+ *                                                              *
+ *   http://www.apache.org/licenses/LICENSE-2.0                 *
+ *                                                              *
+ * Unless required by applicable law or agreed to in writing,   *
+ * software distributed under the License is distributed on an  *
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY       *
+ * KIND, either express or implied.  See the License for the    *
+ * specific language governing permissions and limitations      *
+ * under the License.                                           *
+ ****************************************************************/
+
+package org.apache.james.blob.objectstorage;
+
+public class DockerSwiftSingleton {
+
+    public static final DockerSwiftContainer singleton = new DockerSwiftContainer();
+
+    static {
+        singleton.start();
+    }
+
+    // Cleanup will be performed by test container resource reaper
+}


---------------------------------------------------------------------
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