Skip to content

Commit

Permalink
[#noissue] Replace embedded-elasticsearch with testcontainers-elastic…
Browse files Browse the repository at this point in the history
…search
  • Loading branch information
emeroad committed Jun 20, 2022
1 parent 6208c7c commit 0304cc8
Show file tree
Hide file tree
Showing 11 changed files with 250 additions and 98 deletions.
6 changes: 3 additions & 3 deletions plugins-it/elasticsearch-7.16-it/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,9 @@
<scope>test</scope>
</dependency>
<dependency>
<groupId>pl.allegro.tech</groupId>
<artifactId>embedded-elasticsearch</artifactId>
<version>2.10.0</version>
<groupId>org.testcontainers</groupId>
<artifactId>elasticsearch</artifactId>
<version>${testcontainers.version}</version>
<scope>test</scope>
</dependency>
<dependency>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package com.navercorp.pinpoint.plugin.elasticsearch;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.testcontainers.containers.output.OutputFrame;
import org.testcontainers.elasticsearch.ElasticsearchContainer;

import java.util.function.Consumer;

public class ESServerContainerFactory {

public static final int DEFAULT_PORT = 9200;

public static ElasticsearchContainer newESServerContainerFactory(String loggerName) {
ElasticsearchContainer elasticsearchContainer = new ElasticsearchContainer();
elasticsearchContainer.withLogConsumer(new Consumer<OutputFrame>() {
private final Logger logger = LogManager.getLogger(loggerName);
@Override
public void accept(OutputFrame outputFrame) {
logger.info(outputFrame.getUtf8String());
}
});
return elasticsearchContainer;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,60 +17,68 @@

import com.navercorp.pinpoint.test.plugin.shared.AfterSharedClass;
import com.navercorp.pinpoint.test.plugin.shared.BeforeSharedClass;

import org.apache.http.HttpHost;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestHighLevelClient;
import org.junit.AfterClass;
import org.junit.BeforeClass;
import pl.allegro.tech.embeddedelasticsearch.EmbeddedElastic;
import pl.allegro.tech.embeddedelasticsearch.PopularProperties;

import java.io.IOException;

import static java.util.concurrent.TimeUnit.MINUTES;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.junit.Assume;
import org.testcontainers.DockerClientFactory;
import org.testcontainers.elasticsearch.ElasticsearchContainer;

/**
* @author Roy Kim
*/

public abstract class ElasticsearchITBase {

public static EmbeddedElastic embeddedElastic;
public static RestHighLevelClient restHighLevelClient;
protected static final Logger logger = LogManager.getLogger(ElasticsearchITBase.class);

public static String ELASTICSEARCH_ADDRESS = "127.0.0.1:" + 9200;
public static ElasticsearchContainer elasticsearchContainer;

@BeforeSharedClass
public static void sharedSetUp() throws Exception {
embeddedElastic = EmbeddedElastic.builder()
.withElasticVersion("6.8.0")
.withSetting(PopularProperties.HTTP_PORT, 9200)
.withEsJavaOpts("-Xms128m -Xmx512m")
.withStartTimeout(2, MINUTES)
.build()
.start();
protected static int SERVER_PORT;
protected static String SERVER_HOST;
protected static String ELASTICSEARCH_ADDRESS;

public static int getServerPort() {
return SERVER_PORT;
}

@BeforeClass
public static void setup() throws IOException {
restHighLevelClient = new RestHighLevelClient(
RestClient.builder(
new HttpHost("127.0.0.1", 9200, "http")));
public static void setServerPort(int serverPort) {
SERVER_PORT = serverPort;
}

@AfterClass
public static void tearDown() throws IOException {
if (restHighLevelClient == null) {
restHighLevelClient.close();
}
public static String getServerHost() {
return SERVER_HOST;
}

public static void setServerHost(String serverHost) {
SERVER_HOST = serverHost;
}

public static String getElasticsearchAddress() {
return ELASTICSEARCH_ADDRESS;
}

public static void setElasticsearchAddress(String elasticsearchAddress) {
ELASTICSEARCH_ADDRESS = elasticsearchAddress;
}

@BeforeSharedClass
public static void sharedSetUp() {
Assume.assumeTrue("Docker not enabled", DockerClientFactory.instance().isDockerAvailable());

elasticsearchContainer = ESServerContainerFactory.newESServerContainerFactory(logger.getName());
elasticsearchContainer.start();

setServerPort(elasticsearchContainer.getMappedPort(ESServerContainerFactory.DEFAULT_PORT));
setServerHost(elasticsearchContainer.getHost());
setElasticsearchAddress(elasticsearchContainer.getHttpHostAddress());

}

@AfterSharedClass
public static void sharedTearDown() throws IOException {
if (embeddedElastic != null)
embeddedElastic.stop();
public static void sharedTearDown() {
if (elasticsearchContainer != null) {
elasticsearchContainer.stop();
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,19 @@
import com.navercorp.pinpoint.bootstrap.plugin.test.PluginTestVerifier;
import com.navercorp.pinpoint.bootstrap.plugin.test.PluginTestVerifierHolder;
import com.navercorp.pinpoint.pluginit.utils.AgentPath;
import com.navercorp.pinpoint.pluginit.utils.TestcontainersOption;
import com.navercorp.pinpoint.test.plugin.Dependency;
import com.navercorp.pinpoint.test.plugin.JvmVersion;
import com.navercorp.pinpoint.test.plugin.PinpointAgent;
import com.navercorp.pinpoint.test.plugin.PinpointPluginTestSuite;
import org.apache.http.HttpHost;
import org.elasticsearch.action.index.IndexRequest;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.xcontent.XContentType;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;

Expand All @@ -37,11 +43,27 @@

@RunWith(PinpointPluginTestSuite.class)
@PinpointAgent(AgentPath.PATH)
@Dependency({"org.elasticsearch.client:elasticsearch-rest-high-level-client:[7.16.0,]",
"pl.allegro.tech:embedded-elasticsearch:2.10.0"})
@Dependency({"org.elasticsearch.client:elasticsearch-rest-high-level-client:[7.16.0,7.max]",
TestcontainersOption.ELASTICSEARCH})
@JvmVersion(8)
public class ElasticsearchIT_7_16_x_IT extends ElasticsearchITBase {

private static RestHighLevelClient restHighLevelClient;

@Before
public void setup() {
restHighLevelClient = new RestHighLevelClient(
RestClient.builder(
new HttpHost(getServerHost(), getServerPort(), "http")));
}

@After
public void tearDown() throws IOException {
if (restHighLevelClient != null) {
restHighLevelClient.close();
}
}

@Test
public void testHighLevelClient() throws Exception {

Expand All @@ -53,8 +75,7 @@ public void testHighLevelClient() throws Exception {

private void testIndexV70UP(PluginTestVerifier verifier) throws IOException {

IndexRequest indexRequest = new IndexRequest(
"post2");
IndexRequest indexRequest = new IndexRequest("post2");
indexRequest.id("1");

String jsonString = "{" +
Expand Down
6 changes: 3 additions & 3 deletions plugins-it/elasticsearch-it/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,9 @@
<scope>test</scope>
</dependency>
<dependency>
<groupId>pl.allegro.tech</groupId>
<artifactId>embedded-elasticsearch</artifactId>
<version>2.10.0</version>
<groupId>org.testcontainers</groupId>
<artifactId>elasticsearch</artifactId>
<version>${testcontainers.version}</version>
<scope>test</scope>
</dependency>
<dependency>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package com.navercorp.pinpoint.plugin.elasticsearch;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.testcontainers.containers.output.OutputFrame;
import org.testcontainers.elasticsearch.ElasticsearchContainer;

import java.util.function.Consumer;

public class ESServerContainerFactory {

public static final int DEFAULT_PORT = 9200;

public static ElasticsearchContainer newESServerContainerFactory(String loggerName) {
ElasticsearchContainer elasticsearchContainer = new ElasticsearchContainer();
elasticsearchContainer.withLogConsumer(new Consumer<OutputFrame>() {
private final Logger logger = LogManager.getLogger(loggerName);
@Override
public void accept(OutputFrame outputFrame) {
logger.info(outputFrame.getUtf8String());
}
});
return elasticsearchContainer;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,60 +17,70 @@

import com.navercorp.pinpoint.test.plugin.shared.AfterSharedClass;
import com.navercorp.pinpoint.test.plugin.shared.BeforeSharedClass;

import org.apache.http.HttpHost;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestHighLevelClient;
import org.junit.AfterClass;
import org.junit.BeforeClass;
import pl.allegro.tech.embeddedelasticsearch.EmbeddedElastic;
import pl.allegro.tech.embeddedelasticsearch.PopularProperties;

import java.io.IOException;

import static java.util.concurrent.TimeUnit.MINUTES;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.junit.Assume;
import org.testcontainers.DockerClientFactory;
import org.testcontainers.elasticsearch.ElasticsearchContainer;

/**
* @author Roy Kim
*/

public abstract class ElasticsearchITBase {

public static EmbeddedElastic embeddedElastic;
public static RestHighLevelClient restHighLevelClient;
protected static final Logger logger = LogManager.getLogger(ElasticsearchITBase.class);

public static String ELASTICSEARCH_ADDRESS = "127.0.0.1:" + 9200;
public static ElasticsearchContainer elasticsearchContainer;

@BeforeSharedClass
public static void sharedSetUp() throws Exception {
embeddedElastic = EmbeddedElastic.builder()
.withElasticVersion("6.8.0")
.withSetting(PopularProperties.HTTP_PORT, 9200)
.withEsJavaOpts("-Xms128m -Xmx512m")
.withStartTimeout(2, MINUTES)
.build()
.start();

protected static int SERVER_PORT;
protected static String SERVER_HOST;
protected static String ELASTICSEARCH_ADDRESS;

public static int getServerPort() {
return SERVER_PORT;
}

@BeforeClass
public static void setup() throws IOException {
restHighLevelClient = new RestHighLevelClient(
RestClient.builder(
new HttpHost("127.0.0.1", 9200, "http")));
public static void setServerPort(int serverPort) {
SERVER_PORT = serverPort;
}

@AfterClass
public static void tearDown() throws IOException {
if (restHighLevelClient == null) {
restHighLevelClient.close();
}
public static String getServerHost() {
return SERVER_HOST;
}

public static void setServerHost(String serverHost) {
SERVER_HOST = serverHost;
}

public static String getElasticsearchAddress() {
return ELASTICSEARCH_ADDRESS;
}

public static void setElasticsearchAddress(String elasticsearchAddress) {
ELASTICSEARCH_ADDRESS = elasticsearchAddress;
}

@BeforeSharedClass
public static void sharedSetUp() {
Assume.assumeTrue("Docker not enabled", DockerClientFactory.instance().isDockerAvailable());

elasticsearchContainer = ESServerContainerFactory.newESServerContainerFactory(logger.getName());
elasticsearchContainer.start();

setServerPort(elasticsearchContainer.getMappedPort(ESServerContainerFactory.DEFAULT_PORT));
setServerHost(elasticsearchContainer.getHost());
setElasticsearchAddress(elasticsearchContainer.getHttpHostAddress());

}

@AfterSharedClass
public static void sharedTearDown() throws IOException {
if (embeddedElastic != null)
embeddedElastic.stop();
public static void sharedTearDown() {
if (elasticsearchContainer != null) {
elasticsearchContainer.stop();
}
}


}
Loading

0 comments on commit 0304cc8

Please # to comment.