JAMES-1901 Add integration test from publishing metrics on ElasticSearch
Project: http://git-wip-us.apache.org/repos/asf/james-project/repo Commit: http://git-wip-us.apache.org/repos/asf/james-project/commit/649b62d2 Tree: http://git-wip-us.apache.org/repos/asf/james-project/tree/649b62d2 Diff: http://git-wip-us.apache.org/repos/asf/james-project/diff/649b62d2 Branch: refs/heads/master Commit: 649b62d2cd6a2509d1f28d9f1bf87d4000538237 Parents: 1688bc6 Author: Benoit Tellier <[email protected]> Authored: Mon Jan 23 18:46:31 2017 +0700 Committer: Benoit Tellier <[email protected]> Committed: Fri Feb 3 16:43:43 2017 +0700 ---------------------------------------------------------------------- .../metrics/metrics-es-reporter/pom.xml | 25 ++++ .../apache/james/metric/es/ESReporterTest.java | 120 +++++++++++++++++++ 2 files changed, 145 insertions(+) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/james-project/blob/649b62d2/server/container/metrics/metrics-es-reporter/pom.xml ---------------------------------------------------------------------- diff --git a/server/container/metrics/metrics-es-reporter/pom.xml b/server/container/metrics/metrics-es-reporter/pom.xml index 5373502..97f2500 100644 --- a/server/container/metrics/metrics-es-reporter/pom.xml +++ b/server/container/metrics/metrics-es-reporter/pom.xml @@ -127,6 +127,21 @@ </activation> <dependencies> <dependency> + <groupId>org.apache.james</groupId> + <artifactId>apache-james-backends-es</artifactId> + </dependency> + <dependency> + <groupId>org.apache.james</groupId> + <artifactId>james-server-metrics-dropwizard</artifactId> + <scope>test</scope> + </dependency> + <dependency> + <groupId>org.apache.james</groupId> + <artifactId>james-server-util-java8</artifactId> + <type>test-jar</type> + <scope>test</scope> + </dependency> + <dependency> <groupId>com.google.guava</groupId> <artifactId>guava</artifactId> </dependency> @@ -147,6 +162,16 @@ <groupId>org.elasticsearch</groupId> <artifactId>metrics-elasticsearch-reporter</artifactId> </dependency> + <dependency> + <groupId>org.elasticsearch</groupId> + <artifactId>elasticsearch</artifactId> + <version>2.2.1</version> + </dependency> + <dependency> + <groupId>org.testcontainers</groupId> + <artifactId>testcontainers</artifactId> + <scope>test</scope> + </dependency> </dependencies> <build> <plugins> http://git-wip-us.apache.org/repos/asf/james-project/blob/649b62d2/server/container/metrics/metrics-es-reporter/src/test/java/org/apache/james/metric/es/ESReporterTest.java ---------------------------------------------------------------------- diff --git a/server/container/metrics/metrics-es-reporter/src/test/java/org/apache/james/metric/es/ESReporterTest.java b/server/container/metrics/metrics-es-reporter/src/test/java/org/apache/james/metric/es/ESReporterTest.java new file mode 100644 index 0000000..633a62c --- /dev/null +++ b/server/container/metrics/metrics-es-reporter/src/test/java/org/apache/james/metric/es/ESReporterTest.java @@ -0,0 +1,120 @@ +/**************************************************************** + * 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.metric.es; + +import static com.jayway.awaitility.Awaitility.await; + +import java.util.Optional; +import java.util.Timer; +import java.util.TimerTask; + +import org.apache.james.backends.es.ClientProvider; +import org.apache.james.backends.es.ClientProviderImpl; +import org.apache.james.metrics.api.Metric; +import org.apache.james.metrics.dropwizard.DropWizardMetricFactory; +import org.apache.james.metrics.es.ESMetricReporter; +import org.apache.james.metrics.es.ESReporterConfiguration; +import org.apache.james.util.streams.SwarmGenericContainer; +import org.elasticsearch.client.Client; +import org.elasticsearch.index.query.QueryBuilders; +import org.junit.After; +import org.junit.Before; +import org.junit.Rule; +import org.junit.Test; + +import com.codahale.metrics.MetricRegistry; +import com.jayway.awaitility.Duration; + +public class ESReporterTest { + + public static final Optional<String> INDEX = Optional.of("index_name"); + public static final Optional<Long> PERIOD_IN_SECOND = Optional.of(1L); + public static final int DELAY_IN_MS = 100; + public static final int PERIOD_IN_MS = 100; + public static final int ES_APPLICATIVE_PORT = 9300; + public static final int ES_HTTP_PORT = 9200; + + @Rule + public SwarmGenericContainer esContainer = new SwarmGenericContainer("elasticsearch:2.2.2") + .withAffinityToContainer(); + + private ClientProvider clientProvider; + private ESMetricReporter esMetricReporter; + private MetricRegistry registry; + private Timer timer; + + @Before + public void setUp() { + clientProvider = new ClientProviderImpl(getContainerIp(), ES_APPLICATIVE_PORT); + await().atMost(Duration.ONE_MINUTE) + .until(() -> elasticSearchStarted(clientProvider)); + + registry = new MetricRegistry(); + timer = new Timer(); + esMetricReporter = new ESMetricReporter( + ESReporterConfiguration.enabled(getContainerIp(), ES_HTTP_PORT, INDEX, PERIOD_IN_SECOND), + registry); + } + + @After + public void tearDown() { + timer.cancel(); + esMetricReporter.stop(); + } + + @Test + public void esMetricReporterShouldProduceDocumentsOnAnElasticsearchContainer() { + esMetricReporter.start(); + + Metric metric = new DropWizardMetricFactory(registry).generate("probe"); + TimerTask timerTask = new TimerTask() { + @Override + public void run() { + metric.increment(); + } + }; + timer.schedule(timerTask, DELAY_IN_MS, PERIOD_IN_MS); + + await().atMost(Duration.TEN_MINUTES) + .until(() -> done(clientProvider)); + } + + private boolean elasticSearchStarted(ClientProvider clientProvider) { + try (Client client = clientProvider.get()) { + return true; + } catch (Exception e) { + return false; + } + } + + private boolean done(ClientProvider clientProvider) { + try (Client client = clientProvider.get()) { + return client.prepareSearch().setQuery(QueryBuilders.matchAllQuery()).get().getHits().totalHits() > 0; + } catch (Exception e) { + return false; + } + } + + @SuppressWarnings("deprecation") + private String getContainerIp() { + return esContainer.getContainerInfo().getNetworkSettings().getIpAddress(); + } + +} --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
