Skip to content

Commit

Permalink
Improve docs for Elasticsearch (testcontainers#8870)
Browse files Browse the repository at this point in the history
  • Loading branch information
philipp94831 authored and fokion committed Jan 18, 2025
1 parent 311c8c3 commit 4ec7eb7
Show file tree
Hide file tree
Showing 2 changed files with 84 additions and 17 deletions.
4 changes: 3 additions & 1 deletion docs/modules/elasticsearch.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,9 @@ Note that it's based on the [official Docker image](https://www.elastic.co/guide
You can start an elasticsearch container instance from any Java application by using:

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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,7 @@ public void elasticsearchOssImage() throws IOException {

@Test
public void restClientClusterHealth() throws IOException {
// httpClientContainer {
// httpClientContainer7 {
// Create the elasticsearch container.
try (ElasticsearchContainer container = new ElasticsearchContainer(ELASTICSEARCH_IMAGE)) {
// Start the container. This step might take some time...
Expand All @@ -208,7 +208,86 @@ public void restClientClusterHealth() throws IOException {
// }}
assertThat(response.getStatusLine().getStatusCode()).isEqualTo(200);
assertThat(EntityUtils.toString(response.getEntity())).contains("cluster_name");
// httpClientContainer {{
// httpClientContainer7 {{
}
// }
}

@Test
public void restClientClusterHealthElasticsearch8() throws IOException {
// httpClientContainer8 {
// Create the elasticsearch container.
try (
ElasticsearchContainer container = new ElasticsearchContainer(
"docker.elastic.co/elasticsearch/elasticsearch:8.1.2"
)
) {
// Start the container. This step might take some time...
container.start();

// Do whatever you want with the rest client ...
final CredentialsProvider credentialsProvider = new BasicCredentialsProvider();
credentialsProvider.setCredentials(
AuthScope.ANY,
new UsernamePasswordCredentials(ELASTICSEARCH_USERNAME, ELASTICSEARCH_PASSWORD)
);

client =
RestClient
// use HTTPS for Elasticsearch 8
.builder(HttpHost.create("https://" + container.getHttpHostAddress()))
.setHttpClientConfigCallback(httpClientBuilder -> {
httpClientBuilder.setDefaultCredentialsProvider(credentialsProvider);
// SSL is activated by default in Elasticsearch 8
httpClientBuilder.setSSLContext(container.createSslContextFromCa());
return httpClientBuilder;
})
.build();

Response response = client.performRequest(new Request("GET", "/_cluster/health"));
// }}
assertThat(response.getStatusLine().getStatusCode()).isEqualTo(200);
assertThat(EntityUtils.toString(response.getEntity())).contains("cluster_name");
// httpClientContainer8 {{
}
// }
}

@Test
public void restClientClusterHealthElasticsearch8WithoutSSL() throws IOException {
// httpClientContainerNoSSL8 {
// Create the elasticsearch container.
try (
ElasticsearchContainer container = new ElasticsearchContainer(
"docker.elastic.co/elasticsearch/elasticsearch:8.1.2"
)
// disable SSL
.withEnv("xpack.security.transport.ssl.enabled", "false")
.withEnv("xpack.security.http.ssl.enabled", "false")
) {
// Start the container. This step might take some time...
container.start();

// Do whatever you want with the rest client ...
final CredentialsProvider credentialsProvider = new BasicCredentialsProvider();
credentialsProvider.setCredentials(
AuthScope.ANY,
new UsernamePasswordCredentials(ELASTICSEARCH_USERNAME, ELASTICSEARCH_PASSWORD)
);

client =
RestClient
.builder(HttpHost.create(container.getHttpHostAddress()))
.setHttpClientConfigCallback(httpClientBuilder -> {
return httpClientBuilder.setDefaultCredentialsProvider(credentialsProvider);
})
.build();

Response response = client.performRequest(new Request("GET", "/_cluster/health"));
// }}
assertThat(response.getStatusLine().getStatusCode()).isEqualTo(200);
assertThat(EntityUtils.toString(response.getEntity())).contains("cluster_name");
// httpClientContainerNoSSL8 {{
}
// }
}
Expand Down Expand Up @@ -289,20 +368,6 @@ public void incompatibleSettingsTest() {
.isInstanceOf(IllegalArgumentException.class);
}

@Test
public void testElasticsearch8SecureByDefault() throws Exception {
try (
ElasticsearchContainer container = new ElasticsearchContainer(
"docker.elastic.co/elasticsearch/elasticsearch:8.1.2"
)
) {
// Start the container. This step might take some time...
container.start();

assertClusterHealthResponse(container);
}
}

@Test
public void testDockerHubElasticsearch8ImageSecureByDefault() throws Exception {
try (ElasticsearchContainer container = new ElasticsearchContainer("elasticsearch:8.1.2")) {
Expand Down

0 comments on commit 4ec7eb7

Please sign in to comment.