Skip to content

Commit 4ec7eb7

Browse files
philipp94831fokion
authored andcommitted
Improve docs for Elasticsearch (testcontainers#8870)
1 parent 311c8c3 commit 4ec7eb7

File tree

2 files changed

+84
-17
lines changed

2 files changed

+84
-17
lines changed

docs/modules/elasticsearch.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,9 @@ Note that it's based on the [official Docker image](https://www.elastic.co/guide
1010
You can start an elasticsearch container instance from any Java application by using:
1111

1212
<!--codeinclude-->
13-
[HttpClient](../../modules/elasticsearch/src/test/java/org/testcontainers/elasticsearch/ElasticsearchContainerTest.java) inside_block:httpClientContainer
13+
[HttpClient](../../modules/elasticsearch/src/test/java/org/testcontainers/elasticsearch/ElasticsearchContainerTest.java) inside_block:httpClientContainer7
14+
[HttpClient with Elasticsearch 8](../../modules/elasticsearch/src/test/java/org/testcontainers/elasticsearch/ElasticsearchContainerTest.java) inside_block:httpClientContainer8
15+
[HttpClient with Elasticsearch 8 and SSL disabled](../../modules/elasticsearch/src/test/java/org/testcontainers/elasticsearch/ElasticsearchContainerTest.java) inside_block:httpClientContainerNoSSL8
1416
[TransportClient](../../modules/elasticsearch/src/test/java/org/testcontainers/elasticsearch/ElasticsearchContainerTest.java) inside_block:transportClientContainer
1517
<!--/codeinclude-->
1618

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

Lines changed: 81 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -183,7 +183,7 @@ public void elasticsearchOssImage() throws IOException {
183183

184184
@Test
185185
public void restClientClusterHealth() throws IOException {
186-
// httpClientContainer {
186+
// httpClientContainer7 {
187187
// Create the elasticsearch container.
188188
try (ElasticsearchContainer container = new ElasticsearchContainer(ELASTICSEARCH_IMAGE)) {
189189
// Start the container. This step might take some time...
@@ -208,7 +208,86 @@ public void restClientClusterHealth() throws IOException {
208208
// }}
209209
assertThat(response.getStatusLine().getStatusCode()).isEqualTo(200);
210210
assertThat(EntityUtils.toString(response.getEntity())).contains("cluster_name");
211-
// httpClientContainer {{
211+
// httpClientContainer7 {{
212+
}
213+
// }
214+
}
215+
216+
@Test
217+
public void restClientClusterHealthElasticsearch8() throws IOException {
218+
// httpClientContainer8 {
219+
// Create the elasticsearch container.
220+
try (
221+
ElasticsearchContainer container = new ElasticsearchContainer(
222+
"docker.elastic.co/elasticsearch/elasticsearch:8.1.2"
223+
)
224+
) {
225+
// Start the container. This step might take some time...
226+
container.start();
227+
228+
// Do whatever you want with the rest client ...
229+
final CredentialsProvider credentialsProvider = new BasicCredentialsProvider();
230+
credentialsProvider.setCredentials(
231+
AuthScope.ANY,
232+
new UsernamePasswordCredentials(ELASTICSEARCH_USERNAME, ELASTICSEARCH_PASSWORD)
233+
);
234+
235+
client =
236+
RestClient
237+
// use HTTPS for Elasticsearch 8
238+
.builder(HttpHost.create("https://" + container.getHttpHostAddress()))
239+
.setHttpClientConfigCallback(httpClientBuilder -> {
240+
httpClientBuilder.setDefaultCredentialsProvider(credentialsProvider);
241+
// SSL is activated by default in Elasticsearch 8
242+
httpClientBuilder.setSSLContext(container.createSslContextFromCa());
243+
return httpClientBuilder;
244+
})
245+
.build();
246+
247+
Response response = client.performRequest(new Request("GET", "/_cluster/health"));
248+
// }}
249+
assertThat(response.getStatusLine().getStatusCode()).isEqualTo(200);
250+
assertThat(EntityUtils.toString(response.getEntity())).contains("cluster_name");
251+
// httpClientContainer8 {{
252+
}
253+
// }
254+
}
255+
256+
@Test
257+
public void restClientClusterHealthElasticsearch8WithoutSSL() throws IOException {
258+
// httpClientContainerNoSSL8 {
259+
// Create the elasticsearch container.
260+
try (
261+
ElasticsearchContainer container = new ElasticsearchContainer(
262+
"docker.elastic.co/elasticsearch/elasticsearch:8.1.2"
263+
)
264+
// disable SSL
265+
.withEnv("xpack.security.transport.ssl.enabled", "false")
266+
.withEnv("xpack.security.http.ssl.enabled", "false")
267+
) {
268+
// Start the container. This step might take some time...
269+
container.start();
270+
271+
// Do whatever you want with the rest client ...
272+
final CredentialsProvider credentialsProvider = new BasicCredentialsProvider();
273+
credentialsProvider.setCredentials(
274+
AuthScope.ANY,
275+
new UsernamePasswordCredentials(ELASTICSEARCH_USERNAME, ELASTICSEARCH_PASSWORD)
276+
);
277+
278+
client =
279+
RestClient
280+
.builder(HttpHost.create(container.getHttpHostAddress()))
281+
.setHttpClientConfigCallback(httpClientBuilder -> {
282+
return httpClientBuilder.setDefaultCredentialsProvider(credentialsProvider);
283+
})
284+
.build();
285+
286+
Response response = client.performRequest(new Request("GET", "/_cluster/health"));
287+
// }}
288+
assertThat(response.getStatusLine().getStatusCode()).isEqualTo(200);
289+
assertThat(EntityUtils.toString(response.getEntity())).contains("cluster_name");
290+
// httpClientContainerNoSSL8 {{
212291
}
213292
// }
214293
}
@@ -289,20 +368,6 @@ public void incompatibleSettingsTest() {
289368
.isInstanceOf(IllegalArgumentException.class);
290369
}
291370

292-
@Test
293-
public void testElasticsearch8SecureByDefault() throws Exception {
294-
try (
295-
ElasticsearchContainer container = new ElasticsearchContainer(
296-
"docker.elastic.co/elasticsearch/elasticsearch:8.1.2"
297-
)
298-
) {
299-
// Start the container. This step might take some time...
300-
container.start();
301-
302-
assertClusterHealthResponse(container);
303-
}
304-
}
305-
306371
@Test
307372
public void testDockerHubElasticsearch8ImageSecureByDefault() throws Exception {
308373
try (ElasticsearchContainer container = new ElasticsearchContainer("elasticsearch:8.1.2")) {

0 commit comments

Comments
 (0)