You are viewing a plain text version of this content. The canonical link for it is here.
Posted to server-dev@james.apache.org by bt...@apache.org on 2019/05/09 10:28:26 UTC

[james-project] 01/14: JAMES-2717 DockerElasticSearch impl

This is an automated email from the ASF dual-hosted git repository.

btellier pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/james-project.git

commit 66987165ec5e4b8957c61417e060281b5968e33c
Author: Tran Tien Duc <dt...@linagora.com>
AuthorDate: Tue May 7 18:18:26 2019 +0700

    JAMES-2717 DockerElasticSearch impl
---
 backends-common/elasticsearch/pom.xml              |   8 ++
 .../james/backends/es/DockerElasticSearch.java     | 126 +++++++++++++++++++++
 .../backends/es/DockerElasticSearchSingleton.java  |  28 +++++
 .../james/util/docker/DockerGenericContainer.java  |   4 +
 4 files changed, 166 insertions(+)

diff --git a/backends-common/elasticsearch/pom.xml b/backends-common/elasticsearch/pom.xml
index 2732012..1df2811 100644
--- a/backends-common/elasticsearch/pom.xml
+++ b/backends-common/elasticsearch/pom.xml
@@ -56,6 +56,14 @@
             <scope>test</scope>
         </dependency>
         <dependency>
+            <groupId>io.github.openfeign</groupId>
+            <artifactId>feign-core</artifactId>
+        </dependency>
+        <dependency>
+            <groupId>io.github.openfeign</groupId>
+            <artifactId>feign-slf4j</artifactId>
+        </dependency>
+        <dependency>
             <groupId>org.assertj</groupId>
             <artifactId>assertj-core</artifactId>
             <scope>test</scope>
diff --git a/backends-common/elasticsearch/src/test/java/org/apache/james/backends/es/DockerElasticSearch.java b/backends-common/elasticsearch/src/test/java/org/apache/james/backends/es/DockerElasticSearch.java
new file mode 100644
index 0000000..10e6912
--- /dev/null
+++ b/backends-common/elasticsearch/src/test/java/org/apache/james/backends/es/DockerElasticSearch.java
@@ -0,0 +1,126 @@
+/****************************************************************
+ * 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.backends.es;
+
+import static org.assertj.core.api.Assertions.assertThat;
+
+import java.util.Optional;
+
+import org.apache.http.HttpStatus;
+import org.apache.james.util.Host;
+import org.apache.james.util.docker.DockerGenericContainer;
+import org.apache.james.util.docker.Images;
+import org.apache.james.util.docker.RateLimiters;
+import org.testcontainers.containers.wait.strategy.HostPortWaitStrategy;
+
+import com.google.common.collect.ImmutableList;
+
+import feign.Feign;
+import feign.Logger;
+import feign.RequestLine;
+import feign.Response;
+import feign.slf4j.Slf4jLogger;
+
+public class DockerElasticSearch {
+
+    interface ElasticSearchAPI {
+
+        static ElasticSearchAPI from(Host esHttpHost) {
+            return Feign.builder()
+                .logger(new Slf4jLogger(ElasticSearchAPI.class))
+                .logLevel(Logger.Level.FULL)
+                .target(ElasticSearchAPI.class, "http://" + esHttpHost.getHostName() + ":" + esHttpHost.getPort());
+        }
+
+        @RequestLine("DELETE /_all")
+        Response deleteAllIndexes();
+
+        @RequestLine("POST /_flush?force&wait_if_ongoing=true")
+        Response flush();
+    }
+
+    private static final int ES_HTTP_PORT = 9200;
+    private static final int ES_TCP_PORT = 9300;
+
+    private final DockerGenericContainer eSContainer;
+
+    public DockerElasticSearch() {
+        this.eSContainer = new DockerGenericContainer(Images.ELASTICSEARCH_2)
+            .withExposedPorts(ES_HTTP_PORT, ES_TCP_PORT)
+            .waitingFor(new HostPortWaitStrategy().withRateLimiter(RateLimiters.TWENTIES_PER_SECOND));
+    }
+
+    public void start() {
+        if (!eSContainer.isRunning()) {
+            eSContainer.start();
+        }
+    }
+
+    public void stop() {
+        eSContainer.stop();
+    }
+
+    public int getHttpPort() {
+        return eSContainer.getMappedPort(ES_HTTP_PORT);
+    }
+
+    public int getTcpPort() {
+        return eSContainer.getMappedPort(ES_TCP_PORT);
+    }
+
+    public String getIp() {
+        return eSContainer.getHostIp();
+    }
+
+    public Host getTcpHost() {
+        return Host.from(getIp(), getTcpPort());
+    }
+
+    public Host getHttpHost() {
+        return Host.from(getIp(), getHttpPort());
+    }
+
+    public void pause() {
+        eSContainer.pause();
+    }
+
+    public void unpause() {
+        eSContainer.unpause();
+    }
+
+    public void cleanUpData() {
+        assertThat(esAPI().deleteAllIndexes().status())
+            .isEqualTo(HttpStatus.SC_OK);
+    }
+
+    public void awaitForElasticSearch() {
+        assertThat(esAPI().flush().status())
+            .isEqualTo(HttpStatus.SC_OK);
+    }
+
+    public ClientProvider clientProvider() {
+        Optional<String> noClusterName = Optional.empty();
+        return ClientProviderImpl.fromHosts(ImmutableList.of(getTcpHost()), noClusterName);
+    }
+
+    private ElasticSearchAPI esAPI() {
+        return ElasticSearchAPI.from(getHttpHost());
+    }
+}
diff --git a/backends-common/elasticsearch/src/test/java/org/apache/james/backends/es/DockerElasticSearchSingleton.java b/backends-common/elasticsearch/src/test/java/org/apache/james/backends/es/DockerElasticSearchSingleton.java
new file mode 100644
index 0000000..e3d409a
--- /dev/null
+++ b/backends-common/elasticsearch/src/test/java/org/apache/james/backends/es/DockerElasticSearchSingleton.java
@@ -0,0 +1,28 @@
+/****************************************************************
+ * 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.backends.es;
+
+public class DockerElasticSearchSingleton {
+    public static DockerElasticSearch INSTANCE = new DockerElasticSearch();
+
+    static {
+        INSTANCE.start();
+    }
+}
diff --git a/server/testing/src/main/java/org/apache/james/util/docker/DockerGenericContainer.java b/server/testing/src/main/java/org/apache/james/util/docker/DockerGenericContainer.java
index 62401fb..a833f58 100644
--- a/server/testing/src/main/java/org/apache/james/util/docker/DockerGenericContainer.java
+++ b/server/testing/src/main/java/org/apache/james/util/docker/DockerGenericContainer.java
@@ -125,6 +125,10 @@ public class DockerGenericContainer implements TestRule {
         DockerClientFactory.instance().client().pauseContainerCmd(container.getContainerInfo().getId()).exec();
     }
 
+    public boolean isRunning() {
+        return container.isRunning();
+    }
+
     public void unpause() {
         DockerClientFactory.instance().client().unpauseContainerCmd(container.getContainerInfo().getId()).exec();
     }


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