Skip to content

Use codeinclude for Elasticsearch Module #2828

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

Merged
merged 9 commits into from
Jun 20, 2020
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 9 additions & 30 deletions docs/modules/elasticsearch.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,45 +9,24 @@ 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:

```java
// Create the elasticsearch container.
ElasticsearchContainer container = new ElasticsearchContainer("docker.elastic.co/elasticsearch/elasticsearch:6.4.1");
<!--codeinclude-->
[HttpClient](../../modules/elasticsearch/src/test/java/org/testcontainers/elasticsearch/ElasticsearchContainerTest.java) inside_block:httpClientContainer
[TransportClient](../../modules/elasticsearch/src/test/java/org/testcontainers/elasticsearch/ElasticsearchContainerTest.java) inside_block:transportClientContainer
<!--/codeinclude-->

// 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("elastic", "changeme"));
RestClient restClient = RestClient.builder(HttpHost.create(container.getHttpHostAddress()))
.setHttpClientConfigCallback(httpClientBuilder -> httpClientBuilder.setDefaultCredentialsProvider(credentialsProvider))
.build();
Response response = restClient.performRequest(new Request("GET", "/"));

// ... or the transport client
TransportAddress transportAddress = new TransportAddress(container.getTcpHost());
Settings settings = Settings.builder().put("cluster.name", "docker-cluster").build();
TransportClient transportClient = new PreBuiltTransportClient(settings)
.addTransportAddress(transportAddress);
ClusterHealthResponse healths = transportClient.admin().cluster().prepareHealth().get();

// Stop the container.
container.stop();
```

Note that if you are still using the [TransportClient](https://www.elastic.co/guide/en/elasticsearch/client/java-api/6.3/transport-client.html)
(not recommended as it is deprecated), the default cluster name is set to `docker-cluster` so you need to change `cluster.name` setting
or set `client.transport.ignore_cluster_name` to `true`.

## Choose your Elasticsearch license

If you prefer to start a Docker image with the pure OSS version (which means with no security or
other advanced features), you can use this instead:
If you prefer to start a Docker image with the pure OSS version (which means with no security in older versions or
other new and advanced features), you can use this instead:

```java
// Create the elasticsearch container.
ElasticsearchContainer container = new ElasticsearchContainer("docker.elastic.co/elasticsearch/elasticsearch-oss:6.4.1");
```
<!--codeinclude-->
[Elasticsearch OSS](../../modules/elasticsearch/src/test/java/org/testcontainers/elasticsearch/ElasticsearchContainerTest.java) inside_block:oosContainer
<!--/codeinclude-->

## Adding this module to your project dependencies

Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,20 @@
package org.testcontainers.elasticsearch;

import static org.hamcrest.CoreMatchers.containsString;
import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.rnorth.visibleassertions.VisibleAssertions.assertThrows;
import static org.testcontainers.elasticsearch.ElasticsearchContainer.ELASTICSEARCH_DEFAULT_VERSION;

import java.io.IOException;

import org.apache.http.HttpHost;
import org.apache.http.auth.AuthScope;
import org.apache.http.auth.UsernamePasswordCredentials;
import org.apache.http.client.CredentialsProvider;
import org.apache.http.impl.client.BasicCredentialsProvider;
import org.apache.http.util.EntityUtils;
import org.elasticsearch.Version;
import org.elasticsearch.action.admin.cluster.health.ClusterHealthResponse;
import org.elasticsearch.client.Request;
import org.elasticsearch.client.Response;
Expand All @@ -19,16 +27,13 @@
import org.junit.After;
import org.junit.Test;

import java.io.IOException;

import static org.hamcrest.CoreMatchers.containsString;
import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.rnorth.visibleassertions.VisibleAssertions.assertThrows;
import static org.testcontainers.elasticsearch.ElasticsearchContainer.ELASTICSEARCH_DEFAULT_VERSION;

public class ElasticsearchContainerTest {

/**
* Elasticsearch version which should be used for the Tests
*/
private static final String ELASTICSEARCH_VERSION = Version.CURRENT.toString();

/**
* Elasticsearch default username, when secured with a license > basic
*/
Expand All @@ -51,10 +56,14 @@ public void stopRestClient() throws IOException {

@Test
public void elasticsearchDefaultTest() throws IOException {
// Create the elasticsearch container.
try (ElasticsearchContainer container = new ElasticsearchContainer()
.withEnv("foo", "bar") // dummy env for compiler checking correct generics usage
) {
// Start the container. This step might take some time...
container.start();

// Do whatever you want with the rest client ...
Response response = getClient(container).performRequest(new Request("GET", "/"));
assertThat(response.getStatusLine().getStatusCode(), is(200));
assertThat(EntityUtils.toString(response.getEntity()), containsString(ELASTICSEARCH_DEFAULT_VERSION));
Expand All @@ -69,19 +78,22 @@ public void elasticsearchDefaultTest() throws IOException {

@Test
public void elasticsearchVersion() throws IOException {
try (ElasticsearchContainer container = new ElasticsearchContainer("docker.elastic.co/elasticsearch/elasticsearch:5.6.12")) {
try (ElasticsearchContainer container = new ElasticsearchContainer("docker.elastic.co/elasticsearch/elasticsearch:" + ELASTICSEARCH_VERSION)) {
container.start();
Response response = getClient(container).performRequest(new Request("GET", "/"));
assertThat(response.getStatusLine().getStatusCode(), is(200));
String responseAsString = EntityUtils.toString(response.getEntity());
assertThat(responseAsString, containsString("5.6.12"));
assertThat(responseAsString, containsString(ELASTICSEARCH_VERSION));
}
}

@Test
public void elasticsearchOssImage() throws IOException {
try (ElasticsearchContainer container =
new ElasticsearchContainer("docker.elastic.co/elasticsearch/elasticsearch-oss:" + ELASTICSEARCH_DEFAULT_VERSION)) {
try (
// oosContainer {
ElasticsearchContainer container = new ElasticsearchContainer("docker.elastic.co/elasticsearch/elasticsearch-oss:" + ELASTICSEARCH_VERSION)
// }
) {
container.start();
Response response = getClient(container).performRequest(new Request("GET", "/"));
assertThat(response.getStatusLine().getStatusCode(), is(200));
Expand All @@ -92,11 +104,40 @@ public void elasticsearchOssImage() throws IOException {
}
}

@Test
public void restClientClusterHealth() throws IOException {
// httpClientContainer {
// Create the elasticsearch container.
try (ElasticsearchContainer container = new ElasticsearchContainer()) {
// 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 -> httpClientBuilder.setDefaultCredentialsProvider(credentialsProvider))
.build();

Response response = client.performRequest(new Request("GET", "/_cluster/health"));
assertThat(response.getStatusLine().getStatusCode(), is(200));
assertThat(EntityUtils.toString(response.getEntity()), containsString("cluster_name"));
Copy link
Member

Choose a reason for hiding this comment

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

We should probably omit these assertions from the docs...

Copy link
Contributor Author

Choose a reason for hiding this comment

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

is it possible to exclude lines from a code block with the codeinclude macro?
This would be really helpful feature to decrease the amount of new code in the tests

Copy link
Member

Choose a reason for hiding this comment

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

Not directly to exclude, but you can use the same block ID more than once and every block should be included. e.g. given:

// someBlock {
A
B
// }
C
// someBlock {
D
// }

When rendered via codeinclude, it should appear like:

A
B
D

I'm partly tempted to add explicit excludes support to the codeinclude macro, but I think I'd prefer to try the above approach first.

Copy link
Contributor Author

@raynigon raynigon Jun 4, 2020

Choose a reason for hiding this comment

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

thanks that is exactly what i need

Copy link
Contributor Author

Choose a reason for hiding this comment

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

This leads to a wrong indentation:
Bildschirmfoto 2020-06-06 um 11 10 04

Copy link
Contributor Author

Choose a reason for hiding this comment

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

This PR could help to solve the problem: rnorth/mkdocs-codeinclude-plugin#14

Copy link
Member

Choose a reason for hiding this comment

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

I've been trying to untangle the PRs on the codeinclude plugin, because I think you're right. Unfortunately they're not independent, and that PR is built upon other PRs that have issues.

I think it'll be a bit of time before that gets merged.

Shall we just go ahead and merge? The indentation issue should get sorted out separately later 😄

Copy link
Contributor Author

Choose a reason for hiding this comment

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

sounds like a plan :)

}
// }
}

@Test
public void transportClientClusterHealth() {
// transportClientContainer {
// Create the elasticsearch container.
try (ElasticsearchContainer container = new ElasticsearchContainer()) {

// Start the container. This step might take some time...
container.start();

// Do whatever you want with the transport client
TransportAddress transportAddress = new TransportAddress(container.getTcpHost());
String expectedClusterName = "docker-cluster";
Settings settings = Settings.builder().put("cluster.name", expectedClusterName).build();
Expand All @@ -107,6 +148,7 @@ public void transportClientClusterHealth() {
assertThat(clusterName, is(expectedClusterName));
}
}
// }
}

private RestClient getClient(ElasticsearchContainer container) {
Expand Down