Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve docs for Elasticsearch 8 #8870

Merged
merged 11 commits into from
Aug 7, 2024
Merged
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 {{
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
// httpClientContainer8 {{
// httpClientContainer8 {

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In line 211 it is also {{. Which one is correct?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It should be a single curly brace.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I tested it and it only works with a single curly brace here. Docs preview looks good to me now

}
// }
}

@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"));
// }}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
// }}
// }

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
Loading