Skip to content

Commit 48f2af7

Browse files
reda-alaouirnorth
authored andcommitted
Add TcpHost to ElasticsearchContainer (#956)
Applications have 2 ways to communicate with ElasticSearch: - via REST client - via transport client ElasticsearchContainer currently only allows the usage of the REST client. This allows the application to connect to the container via transport client.
1 parent e823ca2 commit 48f2af7

File tree

4 files changed

+43
-7
lines changed

4 files changed

+43
-7
lines changed

docs/usage/elasticsearch_container.md

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,20 @@ ElasticsearchContainer container = new ElasticsearchContainer("docker.elastic.co
1616
// Start the container. This step might take some time...
1717
container.start();
1818

19-
// Do whatever you want here.
19+
// Do whatever you want with the rest client ...
2020
final CredentialsProvider credentialsProvider = new BasicCredentialsProvider();
2121
credentialsProvider.setCredentials(AuthScope.ANY, new UsernamePasswordCredentials("elastic", "changeme"));
22-
RestClient client = RestClient.builder(HttpHost.create(container.getHttpHostAddress()))
22+
RestClient restClient = RestClient.builder(HttpHost.create(container.getHttpHostAddress()))
2323
.setHttpClientConfigCallback(httpClientBuilder -> httpClientBuilder.setDefaultCredentialsProvider(credentialsProvider))
2424
.build();
25-
Response response = client.performRequest("GET", "/");
25+
Response response = restClient.performRequest("GET", "/");
26+
27+
// ... or the transport client
28+
TransportAddress transportAddress = new TransportAddress(container.getTcpHost());
29+
Settings settings = Settings.builder().put("cluster.name", "docker-cluster").build();
30+
TransportClient transportClient = new PreBuiltTransportClient(settings)
31+
.addTransportAddress(transportAddress);
32+
ClusterHealthResponse healths = transportClient.admin().cluster().prepareHealth().get();
2633

2734
// Stop the container.
2835
container.stop();

modules/elasticsearch/build.gradle

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,5 @@ description = "TestContainers :: elasticsearch"
33
dependencies {
44
compile project(':testcontainers')
55
testCompile "org.elasticsearch.client:elasticsearch-rest-client:6.4.1"
6+
testCompile "org.elasticsearch.client:transport:6.4.1"
67
}

modules/elasticsearch/src/main/java/org/testcontainers/elasticsearch/ElasticsearchContainer.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import org.testcontainers.containers.wait.strategy.HttpWaitStrategy;
55
import org.testcontainers.utility.Base58;
66

7+
import java.net.InetSocketAddress;
78
import java.time.Duration;
89

910
import static java.net.HttpURLConnection.HTTP_OK;
@@ -58,4 +59,8 @@ public ElasticsearchContainer(String dockerImageName) {
5859
public String getHttpHostAddress() {
5960
return getContainerIpAddress() + ":" + getMappedPort(ELASTICSEARCH_DEFAULT_PORT);
6061
}
62+
63+
public InetSocketAddress getTcpHost() {
64+
return new InetSocketAddress(getContainerIpAddress(), getMappedPort(ELASTICSEARCH_DEFAULT_TCP_PORT));
65+
}
6166
}

modules/elasticsearch/src/test/java/org/testcontainers/elasticsearch/ElasticsearchContainerTest.java

Lines changed: 27 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,15 @@
77
import org.apache.http.client.CredentialsProvider;
88
import org.apache.http.impl.client.BasicCredentialsProvider;
99
import org.apache.http.util.EntityUtils;
10+
import org.elasticsearch.action.admin.cluster.health.ClusterHealthResponse;
1011
import org.elasticsearch.client.Request;
1112
import org.elasticsearch.client.Response;
1213
import org.elasticsearch.client.ResponseException;
1314
import org.elasticsearch.client.RestClient;
15+
import org.elasticsearch.client.transport.TransportClient;
16+
import org.elasticsearch.common.settings.Settings;
17+
import org.elasticsearch.common.transport.TransportAddress;
18+
import org.elasticsearch.transport.client.PreBuiltTransportClient;
1419
import org.junit.After;
1520
import org.junit.Test;
1621

@@ -48,7 +53,7 @@ public void stopRestClient() throws IOException {
4853
public void elasticsearchDefaultTest() throws IOException {
4954
try (ElasticsearchContainer container = new ElasticsearchContainer()
5055
.withEnv("foo", "bar") // dummy env for compiler checking correct generics usage
51-
){
56+
) {
5257
container.start();
5358
Response response = getClient(container).performRequest(new Request("GET", "/"));
5459
assertThat(response.getStatusLine().getStatusCode(), is(200));
@@ -87,17 +92,35 @@ public void elasticsearchOssImage() throws IOException {
8792
}
8893
}
8994

95+
@Test
96+
public void transportClientClusterHealth() {
97+
try (ElasticsearchContainer container = new ElasticsearchContainer()) {
98+
container.start();
99+
100+
TransportAddress transportAddress = new TransportAddress(container.getTcpHost());
101+
String expectedClusterName = "docker-cluster";
102+
Settings settings = Settings.builder().put("cluster.name", expectedClusterName).build();
103+
try (TransportClient transportClient = new PreBuiltTransportClient(settings)
104+
.addTransportAddress(transportAddress)) {
105+
ClusterHealthResponse healths = transportClient.admin().cluster().prepareHealth().get();
106+
String clusterName = healths.getClusterName();
107+
assertThat(clusterName, is(expectedClusterName));
108+
}
109+
}
110+
}
111+
90112
private RestClient getClient(ElasticsearchContainer container) {
91113
if (client == null) {
92114
final CredentialsProvider credentialsProvider = new BasicCredentialsProvider();
93115
credentialsProvider.setCredentials(AuthScope.ANY,
94-
new UsernamePasswordCredentials(ELASTICSEARCH_USERNAME, ELASTICSEARCH_PASSWORD));
116+
new UsernamePasswordCredentials(ELASTICSEARCH_USERNAME, ELASTICSEARCH_PASSWORD));
95117

96118
client = RestClient.builder(HttpHost.create(container.getHttpHostAddress()))
97-
.setHttpClientConfigCallback(httpClientBuilder -> httpClientBuilder.setDefaultCredentialsProvider(credentialsProvider))
98-
.build();
119+
.setHttpClientConfigCallback(httpClientBuilder -> httpClientBuilder.setDefaultCredentialsProvider(credentialsProvider))
120+
.build();
99121
}
100122

101123
return client;
102124
}
125+
103126
}

0 commit comments

Comments
 (0)