chore(es): allow optional use of Amazon open distro images
This allows users to switch between Amazon Open Distro ES and the official elastic
images for the test runs. You can use Open Distro by setting `-Dapiman.es.provider=opendistro`.
A specific version of Open Distro can be set with `-Dapiman.opendistro-version=<version>`
Closes
MODIFY
common/es/src/main/java/io/apiman/common/es/util/EsConstants.java
common/es/src/main/java/io/apiman/common/es/util/EsConstants.java
package io.apiman.common.es.util;
import java.io.IOException;
+import java.io.InputStream;
+import java.io.UncheckedIOException;
import java.util.Properties;
/**
@@ -244,30 +246,24 @@ public class EsConstants {
public static final String ES_MAPPING_TYPE_OBJECT = "object";
public static final String ES_MAPPING_TYPE_TEXT = "text";
// caches the es version read from the property file
- private static String esVersion;
+ private static Properties esVersions;
/**
* Reads the elasticsearch version from the maven-generated properties file
*
* @return version the elasticsearch version
*/
- public static String getEsVersion() {
- if (esVersion != null) {
- return esVersion;
- } else {
- java.io.InputStream is = EsConstants.class.getResourceAsStream("apiman-es.properties");
- java.util.Properties p = new Properties();
- String version;
+ public static Properties getEsVersion() {
+ if (esVersions == null) {
+ InputStream is = EsConstants.class.getResourceAsStream("apiman-es.properties");
+ Properties p = new Properties();
try {
p.load(is);
} catch (IOException e) {
+ throw new UncheckedIOException(e);
}
- version = p.getProperty("apiman.elasticsearch-version");
- if (version == null || version.isEmpty()) {
- version = "latest";
- }
- esVersion = version;
- return version;
+ esVersions = p;
}
+ return esVersions;
}
}

MODIFY
common/es/src/main/java/io/apiman/common/es/util/EsUtils.java
common/es/src/main/java/io/apiman/common/es/util/EsUtils.java
@@ -114,5 +114,4 @@ public class EsUtils {
}
return sb.toString();
}
-
}

DELETE
common/es/src/main/resources-filtered/io/apiman/common/es/util/apiman-embedded-elastic.properties
-apiman.embedded-es-version=${version.org.elasticsearch}
\ No newline at end of file

MODIFY
common/es/src/main/resources-filtered/io/apiman/common/es/util/apiman-es.properties
common/es/src/main/resources-filtered/io/apiman/common/es/util/apiman-es.properties
apiman.elasticsearch-version=${version.org.elasticsearch}
+apiman.opendistro-version=${version.com.amazon.opendistroforelasticsearch}
\ No newline at end of file

MODIFY
gateway/test/src/test/java/io/apiman/gateway/test/junit/servlet/ServletGatewayTestServer.java
gateway/test/src/test/java/io/apiman/gateway/test/junit/servlet/ServletGatewayTestServer.java
@@ -17,7 +17,6 @@ package io.apiman.gateway.test.junit.servlet;
import com.fasterxml.jackson.databind.JsonNode;
import io.apiman.common.es.util.DefaultEsClientFactory;
-import io.apiman.common.es.util.EsConstants;
import io.apiman.common.util.ddl.DdlParser;
import io.apiman.gateway.engine.GatewayConfigProperties;
import io.apiman.gateway.engine.components.IBufferFactoryComponent;
@@ -32,6 +31,7 @@ import io.apiman.gateway.platforms.servlet.connectors.HttpConnectorFactory;
import io.apiman.gateway.test.server.GatewayServer;
import io.apiman.gateway.test.server.TestMetrics;
import io.apiman.test.common.echo.EchoServer;
+import io.apiman.test.common.es.EsTestUtil;
import io.apiman.test.common.resttest.IGatewayTestServer;
import io.apiman.test.common.util.TestUtil;
import org.apache.commons.dbcp.BasicDataSource;
@@ -256,7 +256,7 @@ public class ServletGatewayTestServer implements IGatewayTestServer {
private void preStart() throws IOException {
if (withES) {
if (node == null) {
- node = new ElasticsearchContainer("docker.elastic.co/elasticsearch/elasticsearch-oss:" + EsConstants.getEsVersion());
+ node = EsTestUtil.provideElasticsearchContainer();
}
if (!node.isRunning()) {
// We have to start the test container here manually but it is stopped automatically from the test container library

MODIFY
gateway/test/src/test/java/io/apiman/gateway/test/junit/vertx3/Vertx3GatewayEsServerFactory.java
gateway/test/src/test/java/io/apiman/gateway/test/junit/vertx3/Vertx3GatewayEsServerFactory.java
package io.apiman.gateway.test.junit.vertx3;
import com.fasterxml.jackson.databind.JsonNode;
-import io.apiman.common.es.util.EsConstants;
+import io.apiman.test.common.es.EsTestUtil;
import io.apiman.test.common.resttest.IGatewayTestServer;
import io.apiman.test.common.resttest.IGatewayTestServerFactory;
import org.slf4j.Logger;
@@ -21,9 +21,7 @@ public class Vertx3GatewayEsServerFactory implements IGatewayTestServerFactory {
}
private static final class Vertx3EsServer extends Vertx3GatewayTestServer {
- private final ElasticsearchContainer testContainer = new ElasticsearchContainer(
- "docker.elastic.co/elasticsearch/elasticsearch-oss:" + EsConstants.getEsVersion()
- );
+ private final ElasticsearchContainer testContainer = EsTestUtil.provideElasticsearchContainer();
static final String TEST_CONTAINERS_PORT_ENV_VAR = "test.TEST_CONTAINERS_ES_PORT";
static final String TEST_CONTAINERS_HOST_ENV_VAR = "test.TEST_CONTAINERS_ES_HOST";

MODIFY
manager/test/api/src/main/java/io/apiman/manager/test/server/ManagerApiTestServer.java
manager/test/api/src/main/java/io/apiman/manager/test/server/ManagerApiTestServer.java
@@ -26,6 +26,7 @@ import io.apiman.manager.api.security.impl.DefaultSecurityContextFilter;
import io.apiman.manager.api.war.TransactionWatchdogFilter;
import io.apiman.manager.test.util.ManagerTestUtils;
import io.apiman.manager.test.util.ManagerTestUtils.TestType;
+import io.apiman.test.common.es.EsTestUtil;
import io.apiman.test.common.util.TestUtil;
import org.apache.commons.dbcp.BasicDataSource;
import org.eclipse.jetty.security.ConstraintSecurityHandler;
@@ -168,7 +169,7 @@ public class ManagerApiTestServer {
}
if (ManagerTestUtils.getTestType() == TestType.es) {
if (node == null) {
- node = new ElasticsearchContainer("docker.elastic.co/elasticsearch/elasticsearch-oss:" + EsConstants.getEsVersion());
+ node = EsTestUtil.provideElasticsearchContainer();
}
if (!node.isRunning()) {
// We have to start the test container here manually but it is stopped automatically from the test container library

MODIFY
manager/test/api/src/test/java/io/apiman/manager/test/es/EsMetricsAccessorTest.java
manager/test/api/src/test/java/io/apiman/manager/test/es/EsMetricsAccessorTest.java
@@ -18,11 +18,11 @@ package io.apiman.manager.test.es;
import io.apiman.common.es.util.AbstractClientFactory;
import io.apiman.common.es.util.DefaultEsClientFactory;
-import io.apiman.common.es.util.EsConstants;
import io.apiman.gateway.engine.es.EsMetrics;
import io.apiman.manager.api.beans.metrics.*;
import io.apiman.manager.api.core.metrics.MetricsAccessorHelper;
import io.apiman.manager.api.es.EsMetricsAccessor;
+import io.apiman.test.common.es.EsTestUtil;
import org.apache.http.entity.ContentType;
import org.apache.http.entity.InputStreamEntity;
import org.elasticsearch.action.admin.indices.flush.FlushRequest;
@@ -55,7 +55,7 @@ public class EsMetricsAccessorTest {
public static final String APIMAN_METRICS_INDEX_NAME = "apiman_metrics";
@Container
- private static ElasticsearchContainer node = new ElasticsearchContainer("docker.elastic.co/elasticsearch/elasticsearch-oss:" + EsConstants.getEsVersion());
+ private static ElasticsearchContainer node = EsTestUtil.provideElasticsearchContainer();
private static RestHighLevelClient client;
private static Locale locale;

MODIFY
pom.xml
pom.xml
<version.commons-lang>2.6</version.commons-lang>
<version.commons-logging>1.2</version.commons-logging>
<version.commons-pool>1.6</version.commons-pool>
+ <version.com.amazon.opendistroforelasticsearch>1.13.1</version.com.amazon.opendistroforelasticsearch>
<version.com.zaxxer>2.4.0</version.com.zaxxer>
<version.com.google.guava>30.1-jre</version.com.google.guava>
<version.io.prometheus>0.0.13</version.io.prometheus>
<version.org.picketbox>4.9.6.Final</version.org.picketbox>
<version.org.reflections>0.9.11</version.org.reflections>
<version.org.slf4j>1.7.30</version.org.slf4j>
- <version.org.testcontainers>1.15.2</version.org.testcontainers>
+ <version.org.testcontainers>1.15.3</version.org.testcontainers>
<version.io.netty>4.1.49.Final</version.io.netty>
<version.io.swagger>1.6.0</version.io.swagger>
<version.xmlunit>1.6</version.xmlunit>

MODIFY
test/common/pom.xml
test/common/pom.xml
<groupId>io.apiman</groupId>
<artifactId>apiman-gateway-engine-core</artifactId>
</dependency>
+ <dependency>
+ <groupId>io.apiman</groupId>
+ <artifactId>apiman-common-es</artifactId>
+ </dependency>
<dependency>
<groupId>org.assertj</groupId>
<artifactId>assertj-core</artifactId>
<groupId>org.skyscreamer</groupId>
<artifactId>jsonassert</artifactId>
</dependency>
+ <dependency>
+ <groupId>org.testcontainers</groupId>
+ <artifactId>testcontainers</artifactId>
+ </dependency>
+ <dependency>
+ <groupId>org.testcontainers</groupId>
+ <artifactId>elasticsearch</artifactId>
+ </dependency>
+ <dependency>
+ <groupId>org.testcontainers</groupId>
+ <artifactId>elasticsearch</artifactId>
+ <scope>compile</scope>
+ </dependency>
+ <dependency>
+ <groupId>org.testcontainers</groupId>
+ <artifactId>testcontainers</artifactId>
+ <scope>compile</scope>
+ </dependency>
</dependencies>
</project>

ADD
test/common/src/main/java/io/apiman/test/common/es/EsTestUtil.java
+/*
+ * Copyright 2021 Scheer PAS Schweiz AG
+ *
+ * Licensed 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 io.apiman.test.common.es;
+
+
+import io.apiman.common.es.util.EsConstants;
+import org.testcontainers.elasticsearch.ElasticsearchContainer;
+import org.testcontainers.utility.DockerImageName;
+
+import java.util.Optional;
+
+public final class EsTestUtil {
+
+ private EsTestUtil() {}
+
+ /**
+ * Provide a testcontainer for elasticsearch based on a system property or environment variable.
+ * Use apiman.es.provider=opendistro or APIMAN_ES_PROVIDER=opendistro
+ *
+ * @return a testcontainer for elasticsearch tests
+ */
+ public static ElasticsearchContainer provideElasticsearchContainer() {
+ String esImageType = "docker.elastic.co/elasticsearch/elasticsearch";
+ String esProvider = Optional.ofNullable(System.getenv("APIMAN_ES_PROVIDER"))
+ .orElseGet(() -> System.getProperty("apiman.es.provider"));
+
+ if ("opendistro".equalsIgnoreCase(esProvider)) {
+ return new ElasticsearchContainer(DockerImageName.parse("amazon/opendistro-for-elasticsearch")
+ .withTag(EsConstants.getEsVersion().getProperty("apiman.opendistro-version"))
+ .asCompatibleSubstituteFor(esImageType))
+ .withEnv("opendistro_security.disabled", "true");
+ }
+ return new ElasticsearchContainer(DockerImageName.parse(esImageType)
+ .withTag(EsConstants.getEsVersion().getProperty("apiman.elasticsearch-version")));
+ }
+}

chore(es): allow optional use of Amazon open distro images

This allows users to switch between Amazon Open Distro ES and the official elastic 
images for the test runs. You can use Open Distro by setting `-Dapiman.es.provider=opendistro`. 

A specific version of Open Distro can be set with `-Dapiman.opendistro-version=<version>`

Closes #1077