From d9ede4c2124e2e89f265a8ae75387bf8ece7e588 Mon Sep 17 00:00:00 2001 From: Tran Tien Duc Date: Tue, 7 May 2019 18:18:26 +0700 Subject: [PATCH] DONT REVIEW --- backends-common/elasticsearch/pom.xml | 8 ++ .../backends/es/DockerElasticSearch.java | 126 ++++++++++++++++++ .../es/DockerElasticSearchSingleton.java | 28 ++++ .../apache/james/CassandraJmapTestRule.java | 9 +- .../james/DockerElasticSearchExtension.java | 21 ++- ...Rule.java => DockerElasticSearchRule.java} | 28 ++-- .../java/org/apache/james/ESReporterTest.java | 6 +- .../james/EmbeddedElasticSearchExtension.java | 4 +- .../JamesServerWithRetryConnectionTest.java | 13 +- .../TestDockerESMetricReporterModule.java | 54 ++++++++ .../TestDockerElasticSearchModule.java | 73 ++++++++++ ...> TestEmbeddedESMetricReporterModule.java} | 2 +- ...a => TestEmbeddedElasticSearchModule.java} | 4 +- .../CassandraRabbitMQAwsS3JmapTestRule.java | 10 +- .../CassandraRabbitMQSwiftJmapTestRule.java | 9 +- .../cassandra/cucumber/CassandraStepdefs.java | 8 +- .../ReindexingWithEventDeadLettersTest.java | 13 +- .../cucumber/awss3/RabbitMQAwsS3Stepdefs.java | 11 +- .../integration/CassandraJmapExtension.java | 8 +- .../util/docker/DockerGenericContainer.java | 4 + 20 files changed, 359 insertions(+), 80 deletions(-) create mode 100644 backends-common/elasticsearch/src/test/java/org/apache/james/backends/es/DockerElasticSearch.java create mode 100644 backends-common/elasticsearch/src/test/java/org/apache/james/backends/es/DockerElasticSearchSingleton.java rename server/container/guice/cassandra-guice/src/test/java/org/apache/james/{EmbeddedElasticSearchRule.java => DockerElasticSearchRule.java} (64%) create mode 100644 server/container/guice/cassandra-guice/src/test/java/org/apache/james/modules/TestDockerESMetricReporterModule.java create mode 100644 server/container/guice/cassandra-guice/src/test/java/org/apache/james/modules/TestDockerElasticSearchModule.java rename server/container/guice/cassandra-guice/src/test/java/org/apache/james/modules/{TestESMetricReporterModule.java => TestEmbeddedESMetricReporterModule.java} (96%) rename server/container/guice/cassandra-guice/src/test/java/org/apache/james/modules/{TestElasticSearchModule.java => TestEmbeddedElasticSearchModule.java} (92%) diff --git a/backends-common/elasticsearch/pom.xml b/backends-common/elasticsearch/pom.xml index 2732012e9d35..1df2811357c2 100644 --- a/backends-common/elasticsearch/pom.xml +++ b/backends-common/elasticsearch/pom.xml @@ -55,6 +55,14 @@ junit test + + io.github.openfeign + feign-core + + + io.github.openfeign + feign-slf4j + org.assertj assertj-core 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 000000000000..10e691292e67 --- /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 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 000000000000..e3d409a724ba --- /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/container/guice/cassandra-guice/src/test/java/org/apache/james/CassandraJmapTestRule.java b/server/container/guice/cassandra-guice/src/test/java/org/apache/james/CassandraJmapTestRule.java index b0a3ef561d72..ac072e2d54ae 100644 --- a/server/container/guice/cassandra-guice/src/test/java/org/apache/james/CassandraJmapTestRule.java +++ b/server/container/guice/cassandra-guice/src/test/java/org/apache/james/CassandraJmapTestRule.java @@ -25,7 +25,7 @@ import org.apache.james.mailbox.extractor.TextExtractor; import org.apache.james.mailbox.store.search.PDFTextExtractor; -import org.apache.james.modules.TestESMetricReporterModule; +import org.apache.james.modules.TestDockerESMetricReporterModule; import org.apache.james.modules.TestJMAPServerModule; import org.apache.james.server.core.configuration.Configuration; import org.apache.james.webadmin.WebAdminConfiguration; @@ -42,17 +42,20 @@ public class CassandraJmapTestRule implements TestRule { private final TemporaryFolder temporaryFolder; public static CassandraJmapTestRule defaultTestRule() { - return new CassandraJmapTestRule(new EmbeddedElasticSearchRule()); + return new CassandraJmapTestRule(); } private final GuiceModuleTestRule guiceModuleTestRule; + private final DockerElasticSearchRule dockerElasticSearchRule; public CassandraJmapTestRule(GuiceModuleTestRule... guiceModuleTestRule) { TempFilesystemTestRule tempFilesystemTestRule = new TempFilesystemTestRule(); + this.dockerElasticSearchRule = new DockerElasticSearchRule(); this.temporaryFolder = tempFilesystemTestRule.getTemporaryFolder(); this.guiceModuleTestRule = AggregateGuiceModuleTestRule .of(guiceModuleTestRule) + .aggregate(dockerElasticSearchRule) .aggregate(tempFilesystemTestRule); } @@ -66,7 +69,7 @@ public GuiceJamesServer jmapServer(Module... additionals) throws IOException { .combineWith(ALL_BUT_JMX_CASSANDRA_MODULE) .overrideWith(binder -> binder.bind(TextExtractor.class).to(PDFTextExtractor.class)) .overrideWith(new TestJMAPServerModule(LIMIT_TO_10_MESSAGES)) - .overrideWith(new TestESMetricReporterModule()) + .overrideWith(new TestDockerESMetricReporterModule(dockerElasticSearchRule.getDockerEs().getHttpHost())) .overrideWith(guiceModuleTestRule.getModule()) .overrideWith((binder -> binder.bind(CleanupTasksPerformer.class).asEagerSingleton())) .overrideWith(binder -> binder.bind(WebAdminConfiguration.class).toInstance(WebAdminConfiguration.TEST_CONFIGURATION)) diff --git a/server/container/guice/cassandra-guice/src/test/java/org/apache/james/DockerElasticSearchExtension.java b/server/container/guice/cassandra-guice/src/test/java/org/apache/james/DockerElasticSearchExtension.java index bfc755e8152e..20f27473bba8 100644 --- a/server/container/guice/cassandra-guice/src/test/java/org/apache/james/DockerElasticSearchExtension.java +++ b/server/container/guice/cassandra-guice/src/test/java/org/apache/james/DockerElasticSearchExtension.java @@ -19,31 +19,22 @@ package org.apache.james; +import org.apache.james.backends.es.DockerElasticSearch; +import org.apache.james.backends.es.DockerElasticSearchSingleton; import org.apache.james.backends.es.ElasticSearchConfiguration; -import org.apache.james.util.Host; -import org.apache.james.util.docker.DockerGenericContainer; import org.junit.jupiter.api.extension.ExtensionContext; import com.google.inject.Module; public class DockerElasticSearchExtension implements GuiceModuleTestExtension { - public static final int ELASTIC_SEARCH_PORT = 9300; - public static final int ELASTIC_SEARCH_HTTP_PORT = 9200; - - private final DockerGenericContainer elasticSearchContainer; - - public DockerElasticSearchExtension(DockerGenericContainer elasticSearchContainer) { - this.elasticSearchContainer = elasticSearchContainer; - } @Override public void beforeEach(ExtensionContext extensionContext) { - elasticSearchContainer.start(); + getDockerES().start(); } @Override public void afterEach(ExtensionContext extensionContext) { - elasticSearchContainer.stop(); } @Override @@ -54,7 +45,11 @@ public Module getModule() { private ElasticSearchConfiguration getElasticSearchConfigurationForDocker() { return ElasticSearchConfiguration.builder() - .addHost(Host.from(elasticSearchContainer.getHostIp(), elasticSearchContainer.getMappedPort(ELASTIC_SEARCH_PORT))) + .addHost(getDockerES().getTcpHost()) .build(); } + + public DockerElasticSearch getDockerES() { + return DockerElasticSearchSingleton.INSTANCE; + } } diff --git a/server/container/guice/cassandra-guice/src/test/java/org/apache/james/EmbeddedElasticSearchRule.java b/server/container/guice/cassandra-guice/src/test/java/org/apache/james/DockerElasticSearchRule.java similarity index 64% rename from server/container/guice/cassandra-guice/src/test/java/org/apache/james/EmbeddedElasticSearchRule.java rename to server/container/guice/cassandra-guice/src/test/java/org/apache/james/DockerElasticSearchRule.java index 3c6848ab07dc..628700925b73 100644 --- a/server/container/guice/cassandra-guice/src/test/java/org/apache/james/EmbeddedElasticSearchRule.java +++ b/server/container/guice/cassandra-guice/src/test/java/org/apache/james/DockerElasticSearchRule.java @@ -19,43 +19,35 @@ package org.apache.james; -import org.apache.james.backends.es.EmbeddedElasticSearch; -import org.apache.james.modules.TestElasticSearchModule; -import org.elasticsearch.node.Node; -import org.junit.rules.RuleChain; -import org.junit.rules.TemporaryFolder; +import org.apache.james.backends.es.DockerElasticSearch; +import org.apache.james.backends.es.DockerElasticSearchSingleton; +import org.apache.james.modules.TestDockerElasticSearchModule; import org.junit.runner.Description; import org.junit.runners.model.Statement; import com.google.inject.Module; -public class EmbeddedElasticSearchRule implements GuiceModuleTestRule { +public class DockerElasticSearchRule implements GuiceModuleTestRule { - private final TemporaryFolder temporaryFolder = new TemporaryFolder(); - private final EmbeddedElasticSearch embeddedElasticSearch = new EmbeddedElasticSearch(temporaryFolder); - - private final RuleChain chain = RuleChain - .outerRule(temporaryFolder) - .around(embeddedElasticSearch); + private final DockerElasticSearch elasticSearch = DockerElasticSearchSingleton.INSTANCE; @Override public Statement apply(Statement base, Description description) { - return chain.apply(base, description); + return base; } @Override public void await() { - embeddedElasticSearch.awaitForElasticSearch(); + elasticSearch.awaitForElasticSearch(); } - @Override public Module getModule() { - return new TestElasticSearchModule(embeddedElasticSearch); + return new TestDockerElasticSearchModule(elasticSearch); } - public Node getNode() { - return embeddedElasticSearch.getNode(); + public DockerElasticSearch getDockerEs() { + return elasticSearch; } } diff --git a/server/container/guice/cassandra-guice/src/test/java/org/apache/james/ESReporterTest.java b/server/container/guice/cassandra-guice/src/test/java/org/apache/james/ESReporterTest.java index 65392fd150be..f896e28fc305 100644 --- a/server/container/guice/cassandra-guice/src/test/java/org/apache/james/ESReporterTest.java +++ b/server/container/guice/cassandra-guice/src/test/java/org/apache/james/ESReporterTest.java @@ -37,7 +37,7 @@ import org.apache.james.jmap.api.access.AccessToken; import org.apache.james.mailbox.extractor.TextExtractor; import org.apache.james.mailbox.store.search.PDFTextExtractor; -import org.apache.james.modules.TestESMetricReporterModule; +import org.apache.james.modules.TestEmbeddedESMetricReporterModule; import org.apache.james.modules.TestJMAPServerModule; import org.apache.james.modules.protocols.ImapGuiceProbe; import org.apache.james.utils.DataProbeImpl; @@ -71,7 +71,7 @@ class ESReporterTest { .combineWith(ALL_BUT_JMX_CASSANDRA_MODULE) .overrideWith(binder -> binder.bind(TextExtractor.class).to(PDFTextExtractor.class)) .overrideWith(new TestJMAPServerModule(LIMIT_TO_10_MESSAGES)) - .overrideWith(new TestESMetricReporterModule())) + .overrideWith(new TestEmbeddedESMetricReporterModule())) .build(); private static final int DELAY_IN_MS = 100; @@ -156,7 +156,7 @@ private boolean checkMetricRecordedInElasticSearch() { return !Arrays.stream(client.prepareSearch() .setQuery(QueryBuilders.matchAllQuery()) .get().getHits().getHits()) - .filter(searchHit -> searchHit.getIndex().startsWith(TestESMetricReporterModule.METRICS_INDEX)) + .filter(searchHit -> searchHit.getIndex().startsWith(TestEmbeddedESMetricReporterModule.METRICS_INDEX)) .collect(Collectors.toList()) .isEmpty(); } catch (Exception e) { diff --git a/server/container/guice/cassandra-guice/src/test/java/org/apache/james/EmbeddedElasticSearchExtension.java b/server/container/guice/cassandra-guice/src/test/java/org/apache/james/EmbeddedElasticSearchExtension.java index 743dbc3eeab9..a724c3589ae2 100644 --- a/server/container/guice/cassandra-guice/src/test/java/org/apache/james/EmbeddedElasticSearchExtension.java +++ b/server/container/guice/cassandra-guice/src/test/java/org/apache/james/EmbeddedElasticSearchExtension.java @@ -21,7 +21,7 @@ import org.apache.james.backends.es.EmbeddedElasticSearch; import org.apache.james.junit.TemporaryFolderExtension; -import org.apache.james.modules.TestElasticSearchModule; +import org.apache.james.modules.TestEmbeddedElasticSearchModule; import org.junit.jupiter.api.extension.ExtensionContext; import com.google.inject.Module; @@ -49,7 +49,7 @@ public void afterEach(ExtensionContext extensionContext) throws Exception { @Override public Module getModule() { - return new TestElasticSearchModule(embeddedElasticSearch); + return new TestEmbeddedElasticSearchModule(embeddedElasticSearch); } @Override diff --git a/server/container/guice/cassandra-guice/src/test/java/org/apache/james/JamesServerWithRetryConnectionTest.java b/server/container/guice/cassandra-guice/src/test/java/org/apache/james/JamesServerWithRetryConnectionTest.java index 85263de7e415..4f6745b1caab 100644 --- a/server/container/guice/cassandra-guice/src/test/java/org/apache/james/JamesServerWithRetryConnectionTest.java +++ b/server/container/guice/cassandra-guice/src/test/java/org/apache/james/JamesServerWithRetryConnectionTest.java @@ -20,8 +20,6 @@ package org.apache.james; import static org.apache.james.CassandraJamesServerMain.ALL_BUT_JMX_CASSANDRA_MODULE; -import static org.apache.james.DockerElasticSearchExtension.ELASTIC_SEARCH_HTTP_PORT; -import static org.apache.james.DockerElasticSearchExtension.ELASTIC_SEARCH_PORT; import static org.assertj.core.api.Assertions.assertThat; import java.io.IOException; @@ -39,8 +37,6 @@ import org.apache.james.modules.TestJMAPServerModule; import org.apache.james.modules.protocols.ImapGuiceProbe; import org.apache.james.util.concurrent.NamedThreadFactory; -import org.apache.james.util.docker.DockerGenericContainer; -import org.apache.james.util.docker.Images; import org.junit.jupiter.api.AfterEach; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; @@ -50,13 +46,12 @@ class JamesServerWithRetryConnectionTest { private static final int LIMIT_TO_10_MESSAGES = 10; private static final long WAITING_TIME = TimeUnit.MILLISECONDS.convert(10, TimeUnit.SECONDS); - private static DockerGenericContainer elasticSearchContainer = new DockerGenericContainer(Images.ELASTICSEARCH_2) - .withExposedPorts(ELASTIC_SEARCH_HTTP_PORT, ELASTIC_SEARCH_PORT); private static final DockerCassandraRule cassandraRule = new DockerCassandraRule(); + private static final DockerElasticSearchExtension dockerElasticSearch = new DockerElasticSearchExtension(); @RegisterExtension static JamesServerExtension testExtension = new JamesServerBuilder() - .extension(new DockerElasticSearchExtension(elasticSearchContainer)) + .extension(dockerElasticSearch) .extension(new CassandraExtension(cassandraRule)) .server(configuration -> GuiceJamesServer.forConfiguration(configuration) .combineWith(ALL_BUT_JMX_CASSANDRA_MODULE) @@ -97,9 +92,9 @@ void serverShouldRetryToConnectToCassandraWhenStartService(GuiceJamesServer serv @Test void serverShouldRetryToConnectToElasticSearchWhenStartService(GuiceJamesServer server) throws Exception { - elasticSearchContainer.pause(); + dockerElasticSearch.getDockerES().pause(); - waitToStartContainer(WAITING_TIME, elasticSearchContainer::unpause); + waitToStartContainer(WAITING_TIME, dockerElasticSearch.getDockerES()::unpause); assertThatServerStartCorrectly(server); } diff --git a/server/container/guice/cassandra-guice/src/test/java/org/apache/james/modules/TestDockerESMetricReporterModule.java b/server/container/guice/cassandra-guice/src/test/java/org/apache/james/modules/TestDockerESMetricReporterModule.java new file mode 100644 index 000000000000..3b9e4388f92c --- /dev/null +++ b/server/container/guice/cassandra-guice/src/test/java/org/apache/james/modules/TestDockerESMetricReporterModule.java @@ -0,0 +1,54 @@ +/**************************************************************** + * 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.modules; + +import javax.inject.Singleton; + +import org.apache.james.metrics.es.ESReporterConfiguration; +import org.apache.james.util.Host; + +import com.google.inject.AbstractModule; +import com.google.inject.Provides; + +public class TestDockerESMetricReporterModule extends AbstractModule { + + public static final String METRICS_INDEX = "metrics"; + + private final Host esHttpHost; + + public TestDockerESMetricReporterModule(Host esHttpHost) { + this.esHttpHost = esHttpHost; + } + + @Override + protected void configure() { + } + + @Provides + @Singleton + public ESReporterConfiguration provideConfiguration() { + return ESReporterConfiguration.builder() + .enabled() + .onHost(esHttpHost.getHostName(), esHttpHost.getPort()) + .onIndex(METRICS_INDEX) + .periodInSecond(1L) + .build(); + } +} diff --git a/server/container/guice/cassandra-guice/src/test/java/org/apache/james/modules/TestDockerElasticSearchModule.java b/server/container/guice/cassandra-guice/src/test/java/org/apache/james/modules/TestDockerElasticSearchModule.java new file mode 100644 index 000000000000..10797a2bfc7a --- /dev/null +++ b/server/container/guice/cassandra-guice/src/test/java/org/apache/james/modules/TestDockerElasticSearchModule.java @@ -0,0 +1,73 @@ +/**************************************************************** + * 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.modules; + +import javax.inject.Singleton; + +import org.apache.james.CleanupTasksPerformer; +import org.apache.james.backends.es.DockerElasticSearch; +import org.apache.james.backends.es.ElasticSearchConfiguration; +import org.apache.james.mailbox.elasticsearch.MailboxIndexCreationUtil; +import org.elasticsearch.client.Client; + +import com.google.inject.AbstractModule; +import com.google.inject.Provides; +import com.google.inject.multibindings.Multibinder; + +public class TestDockerElasticSearchModule extends AbstractModule { + + private static class ESContainerCleanUp implements CleanupTasksPerformer.CleanupTask { + + private final DockerElasticSearch elasticSearch; + + private ESContainerCleanUp(DockerElasticSearch elasticSearch) { + this.elasticSearch = elasticSearch; + } + + @Override + public Result run() { + elasticSearch.cleanUpData(); + + return Result.COMPLETED; + } + } + + private final DockerElasticSearch elasticSearch; + + public TestDockerElasticSearchModule(DockerElasticSearch elasticSearch) { + this.elasticSearch = elasticSearch; + } + + @Override + protected void configure() { + Multibinder.newSetBinder(binder(), CleanupTasksPerformer.CleanupTask.class) + .addBinding() + .toInstance(new ESContainerCleanUp(elasticSearch)); + } + + @Provides + @Singleton + protected Client provideClientProvider() { + Client client = elasticSearch.clientProvider().get(); + return MailboxIndexCreationUtil.prepareDefaultClient(client, ElasticSearchConfiguration.builder() + .addHost(elasticSearch.getTcpHost()) + .build()); + } +} diff --git a/server/container/guice/cassandra-guice/src/test/java/org/apache/james/modules/TestESMetricReporterModule.java b/server/container/guice/cassandra-guice/src/test/java/org/apache/james/modules/TestEmbeddedESMetricReporterModule.java similarity index 96% rename from server/container/guice/cassandra-guice/src/test/java/org/apache/james/modules/TestESMetricReporterModule.java rename to server/container/guice/cassandra-guice/src/test/java/org/apache/james/modules/TestEmbeddedESMetricReporterModule.java index 85808e00d105..13284fa1ae0e 100644 --- a/server/container/guice/cassandra-guice/src/test/java/org/apache/james/modules/TestESMetricReporterModule.java +++ b/server/container/guice/cassandra-guice/src/test/java/org/apache/james/modules/TestEmbeddedESMetricReporterModule.java @@ -26,7 +26,7 @@ import com.google.inject.AbstractModule; import com.google.inject.Provides; -public class TestESMetricReporterModule extends AbstractModule { +public class TestEmbeddedESMetricReporterModule extends AbstractModule { private static final String LOCALHOST = "localhost"; private static final int DEFAULT_ES_HTTP_PORT = 9200; diff --git a/server/container/guice/cassandra-guice/src/test/java/org/apache/james/modules/TestElasticSearchModule.java b/server/container/guice/cassandra-guice/src/test/java/org/apache/james/modules/TestEmbeddedElasticSearchModule.java similarity index 92% rename from server/container/guice/cassandra-guice/src/test/java/org/apache/james/modules/TestElasticSearchModule.java rename to server/container/guice/cassandra-guice/src/test/java/org/apache/james/modules/TestEmbeddedElasticSearchModule.java index 0ada1f641f57..a0d474866420 100644 --- a/server/container/guice/cassandra-guice/src/test/java/org/apache/james/modules/TestElasticSearchModule.java +++ b/server/container/guice/cassandra-guice/src/test/java/org/apache/james/modules/TestEmbeddedElasticSearchModule.java @@ -30,11 +30,11 @@ import com.google.inject.AbstractModule; import com.google.inject.Provides; -public class TestElasticSearchModule extends AbstractModule { +public class TestEmbeddedElasticSearchModule extends AbstractModule { private final EmbeddedElasticSearch embeddedElasticSearch; - public TestElasticSearchModule(EmbeddedElasticSearch embeddedElasticSearch) { + public TestEmbeddedElasticSearchModule(EmbeddedElasticSearch embeddedElasticSearch) { this.embeddedElasticSearch = embeddedElasticSearch; } diff --git a/server/container/guice/cassandra-rabbitmq-guice/src/test/java/org/apache/james/CassandraRabbitMQAwsS3JmapTestRule.java b/server/container/guice/cassandra-rabbitmq-guice/src/test/java/org/apache/james/CassandraRabbitMQAwsS3JmapTestRule.java index 85dd81bf6ed7..e6c295b3bfc1 100644 --- a/server/container/guice/cassandra-rabbitmq-guice/src/test/java/org/apache/james/CassandraRabbitMQAwsS3JmapTestRule.java +++ b/server/container/guice/cassandra-rabbitmq-guice/src/test/java/org/apache/james/CassandraRabbitMQAwsS3JmapTestRule.java @@ -24,8 +24,7 @@ import org.apache.james.backend.rabbitmq.DockerRabbitMQSingleton; import org.apache.james.mailbox.extractor.TextExtractor; import org.apache.james.mailbox.store.search.PDFTextExtractor; -import org.apache.james.modules.TestAwsS3BlobStoreModule; -import org.apache.james.modules.TestESMetricReporterModule; +import org.apache.james.modules.TestDockerESMetricReporterModule; import org.apache.james.modules.TestJMAPServerModule; import org.apache.james.modules.TestRabbitMQModule; import org.apache.james.modules.blobstore.BlobStoreChoosingConfiguration; @@ -45,17 +44,20 @@ public class CassandraRabbitMQAwsS3JmapTestRule implements TestRule { private final TemporaryFolder temporaryFolder; public static CassandraRabbitMQAwsS3JmapTestRule defaultTestRule() { - return new CassandraRabbitMQAwsS3JmapTestRule(new EmbeddedElasticSearchRule(), new DockerAwsS3TestRule()); + return new CassandraRabbitMQAwsS3JmapTestRule(new DockerAwsS3TestRule()); } private final GuiceModuleTestRule guiceModuleTestRule; + private final DockerElasticSearchRule dockerElasticSearchRule; public CassandraRabbitMQAwsS3JmapTestRule(GuiceModuleTestRule... guiceModuleTestRule) { TempFilesystemTestRule tempFilesystemTestRule = new TempFilesystemTestRule(); + this.dockerElasticSearchRule = new DockerElasticSearchRule(); this.temporaryFolder = tempFilesystemTestRule.getTemporaryFolder(); this.guiceModuleTestRule = AggregateGuiceModuleTestRule .of(guiceModuleTestRule) + .aggregate(dockerElasticSearchRule) .aggregate(tempFilesystemTestRule); } @@ -72,7 +74,7 @@ public GuiceJamesServer jmapServer(Module... additionals) throws IOException { .overrideWith(binder -> binder.bind(BlobStoreChoosingConfiguration.class) .toInstance(BlobStoreChoosingConfiguration.objectStorage())) .overrideWith(new TestJMAPServerModule(LIMIT_TO_10_MESSAGES)) - .overrideWith(new TestESMetricReporterModule()) + .overrideWith(new TestDockerESMetricReporterModule(dockerElasticSearchRule.getDockerEs().getHttpHost())) .overrideWith(guiceModuleTestRule.getModule()) .overrideWith((binder -> binder.bind(CleanupTasksPerformer.class).asEagerSingleton())) .overrideWith(additionals); diff --git a/server/container/guice/cassandra-rabbitmq-guice/src/test/java/org/apache/james/CassandraRabbitMQSwiftJmapTestRule.java b/server/container/guice/cassandra-rabbitmq-guice/src/test/java/org/apache/james/CassandraRabbitMQSwiftJmapTestRule.java index 8c7d7a184329..f9e7918e9140 100644 --- a/server/container/guice/cassandra-rabbitmq-guice/src/test/java/org/apache/james/CassandraRabbitMQSwiftJmapTestRule.java +++ b/server/container/guice/cassandra-rabbitmq-guice/src/test/java/org/apache/james/CassandraRabbitMQSwiftJmapTestRule.java @@ -24,7 +24,7 @@ import org.apache.james.backend.rabbitmq.DockerRabbitMQSingleton; import org.apache.james.mailbox.extractor.TextExtractor; import org.apache.james.mailbox.store.search.PDFTextExtractor; -import org.apache.james.modules.TestESMetricReporterModule; +import org.apache.james.modules.TestDockerESMetricReporterModule; import org.apache.james.modules.TestJMAPServerModule; import org.apache.james.modules.TestRabbitMQModule; import org.apache.james.modules.TestSwiftBlobStoreModule; @@ -43,17 +43,20 @@ public class CassandraRabbitMQSwiftJmapTestRule implements TestRule { private final TemporaryFolder temporaryFolder; public static CassandraRabbitMQSwiftJmapTestRule defaultTestRule() { - return new CassandraRabbitMQSwiftJmapTestRule(new EmbeddedElasticSearchRule()); + return new CassandraRabbitMQSwiftJmapTestRule(); } private final GuiceModuleTestRule guiceModuleTestRule; + private final DockerElasticSearchRule dockerElasticSearchRule; public CassandraRabbitMQSwiftJmapTestRule(GuiceModuleTestRule... guiceModuleTestRule) { TempFilesystemTestRule tempFilesystemTestRule = new TempFilesystemTestRule(); + this.dockerElasticSearchRule = new DockerElasticSearchRule(); this.temporaryFolder = tempFilesystemTestRule.getTemporaryFolder(); this.guiceModuleTestRule = AggregateGuiceModuleTestRule .of(guiceModuleTestRule) + .aggregate(dockerElasticSearchRule) .aggregate(tempFilesystemTestRule); } @@ -69,7 +72,7 @@ public GuiceJamesServer jmapServer(Module... additionals) throws IOException { .overrideWith(new TestRabbitMQModule(DockerRabbitMQSingleton.SINGLETON)) .overrideWith(new TestSwiftBlobStoreModule()) .overrideWith(new TestJMAPServerModule(LIMIT_TO_10_MESSAGES)) - .overrideWith(new TestESMetricReporterModule()) + .overrideWith(new TestDockerESMetricReporterModule(dockerElasticSearchRule.getDockerEs().getHttpHost())) .overrideWith(guiceModuleTestRule.getModule()) .overrideWith((binder -> binder.bind(CleanupTasksPerformer.class).asEagerSingleton())) .overrideWith(additionals); diff --git a/server/protocols/jmap-integration-testing/cassandra-jmap-integration-testing/src/test/java/org/apache/james/jmap/cassandra/cucumber/CassandraStepdefs.java b/server/protocols/jmap-integration-testing/cassandra-jmap-integration-testing/src/test/java/org/apache/james/jmap/cassandra/cucumber/CassandraStepdefs.java index 4cb6304636f3..7f770d6e1715 100644 --- a/server/protocols/jmap-integration-testing/cassandra-jmap-integration-testing/src/test/java/org/apache/james/jmap/cassandra/cucumber/CassandraStepdefs.java +++ b/server/protocols/jmap-integration-testing/cassandra-jmap-integration-testing/src/test/java/org/apache/james/jmap/cassandra/cucumber/CassandraStepdefs.java @@ -36,8 +36,8 @@ import org.apache.james.mailbox.cassandra.ids.CassandraMessageId; import org.apache.james.mailbox.extractor.TextExtractor; import org.apache.james.mailbox.store.extractor.DefaultTextExtractor; -import org.apache.james.modules.TestESMetricReporterModule; -import org.apache.james.modules.TestElasticSearchModule; +import org.apache.james.modules.TestEmbeddedESMetricReporterModule; +import org.apache.james.modules.TestEmbeddedElasticSearchModule; import org.apache.james.modules.TestJMAPServerModule; import org.apache.james.server.CassandraTruncateTableTask; import org.apache.james.server.core.configuration.Configuration; @@ -79,8 +79,8 @@ public void init() throws Exception { mainStepdefs.jmapServer = GuiceJamesServer.forConfiguration(configuration) .combineWith(ALL_BUT_JMX_CASSANDRA_MODULE) .overrideWith(new TestJMAPServerModule(10)) - .overrideWith(new TestESMetricReporterModule()) - .overrideWith(new TestElasticSearchModule(embeddedElasticSearch)) + .overrideWith(new TestEmbeddedESMetricReporterModule()) + .overrideWith(new TestEmbeddedElasticSearchModule(embeddedElasticSearch)) .overrideWith(cassandraServer.getModule()) .overrideWith(binder -> binder.bind(TextExtractor.class).to(DefaultTextExtractor.class)) .overrideWith((binder) -> binder.bind(PersistenceAdapter.class).to(MemoryPersistenceAdapter.class)) diff --git a/server/protocols/jmap-integration-testing/rabbitmq-jmap-integration-testing/src/test/java/org/apache/james/jmap/rabbitmq/ReindexingWithEventDeadLettersTest.java b/server/protocols/jmap-integration-testing/rabbitmq-jmap-integration-testing/src/test/java/org/apache/james/jmap/rabbitmq/ReindexingWithEventDeadLettersTest.java index b1e2c85db91d..c4385af13880 100644 --- a/server/protocols/jmap-integration-testing/rabbitmq-jmap-integration-testing/src/test/java/org/apache/james/jmap/rabbitmq/ReindexingWithEventDeadLettersTest.java +++ b/server/protocols/jmap-integration-testing/rabbitmq-jmap-integration-testing/src/test/java/org/apache/james/jmap/rabbitmq/ReindexingWithEventDeadLettersTest.java @@ -20,8 +20,6 @@ package org.apache.james.jmap.rabbitmq; import static io.restassured.RestAssured.with; -import static org.apache.james.DockerElasticSearchExtension.ELASTIC_SEARCH_HTTP_PORT; -import static org.apache.james.DockerElasticSearchExtension.ELASTIC_SEARCH_PORT; import static org.apache.james.jmap.HttpJmapAuthentication.authenticateJamesUser; import static org.apache.james.jmap.JmapCommonRequests.getDraftId; import static org.apache.james.jmap.JmapCommonRequests.listMessageIdsForAccount; @@ -47,8 +45,6 @@ import org.apache.james.modules.RabbitMQExtension; import org.apache.james.modules.TestJMAPServerModule; import org.apache.james.modules.objectstorage.PayloadCodecFactory; -import org.apache.james.util.docker.DockerGenericContainer; -import org.apache.james.util.docker.Images; import org.apache.james.utils.DataProbeImpl; import org.apache.james.utils.JmapGuiceProbe; import org.apache.james.utils.WebAdminGuiceProbe; @@ -75,8 +71,7 @@ class ReindexingWithEventDeadLettersTest { .atMost(Duration.ONE_MINUTE) .await(); - private static DockerGenericContainer elasticSearchContainer = new DockerGenericContainer(Images.ELASTICSEARCH_2) - .withExposedPorts(ELASTIC_SEARCH_HTTP_PORT, ELASTIC_SEARCH_PORT); + private static final DockerElasticSearchExtension dockerElasticSearch = new DockerElasticSearchExtension(); private static final JamesServerBuilder.ServerProvider CONFIGURATION_BUILDER = configuration -> GuiceJamesServer .forConfiguration(configuration) @@ -88,7 +83,7 @@ class ReindexingWithEventDeadLettersTest { @RegisterExtension JamesServerExtension testExtension = new JamesServerBuilder() - .extension(new DockerElasticSearchExtension(elasticSearchContainer)) + .extension(dockerElasticSearch) .extension(new CassandraExtension()) .extension(new RabbitMQExtension()) .extension(new AwsS3BlobStoreExtension(PayloadCodecFactory.AES256)) @@ -114,7 +109,7 @@ void setUp(GuiceJamesServer jamesServer) throws Exception { aliceAccessToken = authenticateJamesUser(baseUri(jamesServer), ALICE, ALICE_PASSWORD); - elasticSearchContainer.pause(); + dockerElasticSearch.getDockerES().pause(); Thread.sleep(Duration.TEN_SECONDS.getValueInMS()); // Docker pause is asynchronous and we found no way to poll for it } @@ -152,7 +147,7 @@ void redeliverShouldCleanEventDeadLetter() throws Exception { } private void unpauseElasticSearch() throws Exception { - elasticSearchContainer.unpause(); + dockerElasticSearch.getDockerES().unpause(); Thread.sleep(Duration.FIVE_SECONDS.getValueInMS()); // Docker unpause is asynchronous and we found no way to poll for it } diff --git a/server/protocols/jmap-integration-testing/rabbitmq-jmap-integration-testing/src/test/java/org/apache/james/jmap/rabbitmq/cucumber/awss3/RabbitMQAwsS3Stepdefs.java b/server/protocols/jmap-integration-testing/rabbitmq-jmap-integration-testing/src/test/java/org/apache/james/jmap/rabbitmq/cucumber/awss3/RabbitMQAwsS3Stepdefs.java index baf2f58fd87d..01d6747966c7 100644 --- a/server/protocols/jmap-integration-testing/rabbitmq-jmap-integration-testing/src/test/java/org/apache/james/jmap/rabbitmq/cucumber/awss3/RabbitMQAwsS3Stepdefs.java +++ b/server/protocols/jmap-integration-testing/rabbitmq-jmap-integration-testing/src/test/java/org/apache/james/jmap/rabbitmq/cucumber/awss3/RabbitMQAwsS3Stepdefs.java @@ -20,6 +20,7 @@ package org.apache.james.jmap.rabbitmq.cucumber.awss3; import java.util.Arrays; + import javax.inject.Inject; import org.apache.activemq.store.PersistenceAdapter; @@ -35,18 +36,18 @@ import org.apache.james.mailbox.extractor.TextExtractor; import org.apache.james.mailbox.store.extractor.DefaultTextExtractor; import org.apache.james.modules.DockerRabbitMQRule; -import org.apache.james.modules.TestESMetricReporterModule; -import org.apache.james.modules.TestElasticSearchModule; +import org.apache.james.modules.TestEmbeddedESMetricReporterModule; +import org.apache.james.modules.TestEmbeddedElasticSearchModule; import org.apache.james.modules.TestJMAPServerModule; import org.apache.james.modules.TestRabbitMQModule; import org.apache.james.modules.objectstorage.aws.s3.DockerAwsS3TestRule; import org.apache.james.server.CassandraTruncateTableTask; import org.apache.james.server.core.configuration.Configuration; - import org.junit.rules.TemporaryFolder; import com.github.fge.lambdas.runnable.ThrowingRunnable; import com.google.inject.multibindings.Multibinder; + import cucumber.api.java.After; import cucumber.api.java.Before; import cucumber.runtime.java.guice.ScenarioScoped; @@ -86,10 +87,10 @@ public void init() throws Exception { mainStepdefs.jmapServer = GuiceJamesServer.forConfiguration(configuration) .combineWith(CassandraRabbitMQJamesServerMain.MODULES) .overrideWith(new TestJMAPServerModule(JMAP_GET_MESSAGE_LIST_MAXIMUM_LIMIT)) - .overrideWith(new TestESMetricReporterModule()) + .overrideWith(new TestEmbeddedESMetricReporterModule()) .overrideWith(new TestRabbitMQModule(rabbitMQServer.dockerRabbitMQ())) .overrideWith(swiftServer.getModule()) - .overrideWith(new TestElasticSearchModule(embeddedElasticSearch)) + .overrideWith(new TestEmbeddedElasticSearchModule(embeddedElasticSearch)) .overrideWith(cassandraServer.getModule()) .overrideWith(binder -> binder.bind(TextExtractor.class).to(DefaultTextExtractor.class)) .overrideWith((binder) -> binder.bind(PersistenceAdapter.class).to(MemoryPersistenceAdapter.class)) diff --git a/server/protocols/webadmin-integration-test/src/test/java/org/apache/james/webadmin/integration/CassandraJmapExtension.java b/server/protocols/webadmin-integration-test/src/test/java/org/apache/james/webadmin/integration/CassandraJmapExtension.java index 3451c21de1b9..c89aea7921b2 100644 --- a/server/protocols/webadmin-integration-test/src/test/java/org/apache/james/webadmin/integration/CassandraJmapExtension.java +++ b/server/protocols/webadmin-integration-test/src/test/java/org/apache/james/webadmin/integration/CassandraJmapExtension.java @@ -28,8 +28,8 @@ import org.apache.james.backends.es.EmbeddedElasticSearch; import org.apache.james.mailbox.extractor.TextExtractor; import org.apache.james.mailbox.store.search.PDFTextExtractor; -import org.apache.james.modules.TestESMetricReporterModule; -import org.apache.james.modules.TestElasticSearchModule; +import org.apache.james.modules.TestEmbeddedESMetricReporterModule; +import org.apache.james.modules.TestEmbeddedElasticSearchModule; import org.apache.james.modules.TestJMAPServerModule; import org.apache.james.server.core.configuration.Configuration; import org.apache.james.util.Runnables; @@ -68,9 +68,9 @@ private GuiceJamesServer james() throws IOException { return GuiceJamesServer.forConfiguration(configuration) .combineWith(ALL_BUT_JMX_CASSANDRA_MODULE).overrideWith(binder -> binder.bind(TextExtractor.class).to(PDFTextExtractor.class)) .overrideWith(new TestJMAPServerModule(LIMIT_TO_20_MESSAGES)) - .overrideWith(new TestESMetricReporterModule()) + .overrideWith(new TestEmbeddedESMetricReporterModule()) .overrideWith(cassandra.getModule()) - .overrideWith(new TestElasticSearchModule(elasticSearch)) + .overrideWith(new TestEmbeddedElasticSearchModule(elasticSearch)) .overrideWith(binder -> binder.bind(WebAdminConfiguration.class).toInstance(WebAdminConfiguration.TEST_CONFIGURATION)) .overrideWith(new UnauthorizedModule()) .overrideWith((binder -> binder.bind(CleanupTasksPerformer.class).asEagerSingleton())); 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 62401fb92944..a833f581aba8 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 void pause() { 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(); }