Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,6 @@
.classpath
.project
.gradle
.idea/
build/
bin/

13 changes: 7 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ The [opensearch-testcontainers](https://github.com/opensearch-project/opensearch

|opensearch-testcontainers|OpenSearch|testcontainers|JDK|
|---|---|---|---|
|3.0.0-SNAPSHOT|2.0.0+|1.20.0+|21+|
|2.1.4-SNAPSHOT|2.0.0+|1.20.0+|11+|
|2.1.3|2.0.0+|1.20.0+|11+|
|2.1.2|2.0.0+|1.20.0+|11+|
Expand Down Expand Up @@ -41,7 +42,7 @@ Follow the [JUnit 4 Quickstart](https://www.testcontainers.org/quickstart/junit_

```java
@Rule
public OpensearchContainer<?> opensearch = new OpensearchContainer<>(DockerImageName.parse("opensearchproject/opensearch:2.11.0"));
public OpenSearchContainer<?> opensearch = new OpenSearchContainer<>(DockerImageName.parse("opensearchproject/opensearch:2.11.0"));

```

Expand All @@ -51,15 +52,15 @@ Follow the [JUnit 5 Quickstart](https://www.testcontainers.org/quickstart/junit_

```java
@Container
public OpensearchContainer<?> opensearch = new OpensearchContainer<>(DockerImageName.parse("opensearchproject/opensearch:2.11.0"));
public OpenSearchContainer<?> opensearch = new OpenSearchContainer<>(DockerImageName.parse("opensearchproject/opensearch:2.11.0"));

```

Please note that at the moment [testcontainers](https://www.testcontainers.org/) brings in [JUnit 4.x dependencies](https://github.com/testcontainers/testcontainers-java/issues/970). If it conflicts with your project configuration, please follow linked Github issue for available mitigation strategies.

## Usage Examples

By default, [OpenSearch](https://opensearch.org/) Docker containers run with [security plugin](https://github.com/opensearch-project/security) activated, however `OpensearchContainer` deactivates it. Use `withSecurityEnabled()` to enable security, please notice that in this case in order to connect to the running container the HTTPS protocol should be used along with username / password credentials.
By default, [OpenSearch](https://opensearch.org/) Docker containers run with [security plugin](https://github.com/opensearch-project/security) activated, however `OpenSearchContainer` deactivates it. Use `withSecurityEnabled()` to enable security, please notice that in this case in order to connect to the running container the HTTPS protocol should be used along with username / password credentials.

```java

Expand All @@ -78,7 +79,7 @@ import org.opensearch.client.RestClient;
private static final DockerImageName OPENSEARCH_IMAGE = DockerImageName.parse("opensearchproject/opensearch:2.11.0");

// Create the Opensearch container.
try (OpensearchContainer<?> container = new OpensearchContainer<>(OPENSEARCH_IMAGE).withSecurityEnabled()) {
try (OpenSearchContainer<?> container = new OpenSearchContainer<>(OPENSEARCH_IMAGE).withSecurityEnabled()) {
// Start the container. This step might take some time...
container.start();

Expand Down Expand Up @@ -109,7 +110,7 @@ try (OpensearchContainer<?> container = new OpensearchContainer<>(OPENSEARCH_IMA
}
```

When [security plugin](https://github.com/opensearch-project/security) is not required (not recommended for production), just use `OpensearchContainer` default constructor.
When [security plugin](https://github.com/opensearch-project/security) is not required (not recommended for production), just use `OpenSearchContainer` default constructor.

```java

Expand All @@ -121,7 +122,7 @@ import org.opensearch.client.RestClient;
private static final DockerImageName OPENSEARCH_IMAGE = DockerImageName.parse("opensearchproject/opensearch:2.11.0");

// Create the OpenSearch container.
try (OpensearchContainer<?> container = new OpensearchContainer<>(OPENSEARCH_IMAGE)) {
try (OpenSearchContainer<?> container = new OpenSearchContainer<>(OPENSEARCH_IMAGE)) {
// Start the container. This step might take some time...
container.start();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,19 +18,19 @@
import org.testcontainers.utility.DockerImageName;

/**
* The Opensearch Docker container (single node cluster) which exposes by default ports 9200
* The OpenSearch Docker container (single node cluster) which exposes by default ports 9200
* (http/https) and 9300 (tcp, deprecated).
*/
public class OpensearchContainer<SELF extends OpensearchContainer<SELF>> extends GenericContainer<SELF> {
public class OpenSearchContainer<SELF extends OpenSearchContainer<SELF>> extends GenericContainer<SELF> {
// The initial password is required starting from OpenSearch 2.12.0
private static final Pattern OPENSEARCH_INITIAL_PASSWORD_VERSION = Pattern.compile(
"^(([3-9][.]\\d+[.]\\d+|[2][.][1][2-9]+[.]\\d+|[2][.][2-9]\\d+[.]\\d+)(-SNAPSHOT)?|latest)$");

// Default username to connect to Opensearch instance
// Default username to connect to OpenSearch instance
private static final String DEFAULT_USER = "admin";
// Default password to connect to Opensearch instance
// Default password to connect to OpenSearch instance
private static final String DEFAULT_PASSWORD = "admin";
// Default initial password to connect to Opensearch instance
// Default initial password to connect to OpenSearch instance
private static final String DEFAULT_INITIAL_PASSWORD = "_ad0m#Ns_";

// Default HTTP port.
Expand All @@ -39,7 +39,7 @@ public class OpensearchContainer<SELF extends OpensearchContainer<SELF>> extends
// Default TCP port (deprecated and may be removed in future versions).
private static final int DEFAULT_TCP_PORT = 9300;

// Opensearch Docker base image.
// OpenSearch Docker base image.
private static final DockerImageName DEFAULT_IMAGE_NAME = DockerImageName.parse("opensearchproject/opensearch");

// Disables (or enables) security plugin. If security is enabled, the communication protocol switches from HTTP to
Expand All @@ -50,18 +50,18 @@ public class OpensearchContainer<SELF extends OpensearchContainer<SELF>> extends
private String password = DEFAULT_PASSWORD;

/**
* Create an Opensearch Container by passing the full docker image name.
* Create an OpenSearch Container by passing the full docker image name.
*
* @param dockerImageName Full docker image name as a {@link String}, like:
* opensearchproject/opensearch:1.2.4 opensearchproject/opensearch:1.3.1
* opensearchproject/opensearch:2.0.0
*/
public OpensearchContainer(String dockerImageName) {
public OpenSearchContainer(String dockerImageName) {
this(DockerImageName.parse(dockerImageName));
}

/**
* Create an Opensearch Container (with security plugin enabled) by passing the full docker image
* Create an OpenSearch Container (with security plugin enabled) by passing the full docker image
* name.
*
* @param dockerImageName Full docker image name as a {@link DockerImageName}, like:
Expand All @@ -71,7 +71,7 @@ public OpensearchContainer(String dockerImageName) {
* DockerImageName.parse("opensearchproject/opensearch:2.0.0")
*
*/
public OpensearchContainer(final DockerImageName dockerImageName) {
public OpenSearchContainer(final DockerImageName dockerImageName) {
super(dockerImageName);
dockerImageName.assertCompatibleWith(DEFAULT_IMAGE_NAME);

Expand Down Expand Up @@ -114,7 +114,7 @@ protected void configure() {

final WaitStrategy waitStrategy;
if (!disableSecurity) {
// By default, Opensearch uses self-signed certificates for HTTPS, allowing insecure
// By default, OpenSearch uses self-signed certificates for HTTPS, allowing insecure
// connection in order to skip the certificate validation checks.
waitStrategy = new HttpWaitStrategy()
.usingTls()
Expand All @@ -136,7 +136,7 @@ protected void configure() {
}

/**
* Return HTTP(s) host and port to connect to Opensearch container.
* Return HTTP(s) host and port to connect to OpenSearch container.
*
* @return HTTP(s) host and port (in a form of "host:port")
*/
Expand All @@ -154,7 +154,7 @@ public boolean isSecurityEnabled() {
}

/**
* Return socket address to connect to Opensearch over TCP. The TransportClient will is deprecated
* Return socket address to connect to OpenSearch over TCP. The TransportClient will is deprecated
* and may be removed in future versions.
*
* @return TCP socket address
Expand All @@ -165,16 +165,16 @@ public InetSocketAddress getTcpHost() {
}

/**
* Return user name to connect to Opensearch container (if security plugin is enabled)
* @return user name to connect to Opensearch container
* Return user name to connect to OpenSearch container (if security plugin is enabled)
* @return user name to connect to OpenSearch container
*/
public String getUsername() {
return DEFAULT_USER;
}

/**
* Return password to connect to Opensearch container (if security plugin is enabled)
* @return password to connect to Opensearch container
* Return password to connect to OpenSearch container (if security plugin is enabled)
* @return password to connect to OpenSearch container
*/
public String getPassword() {
return password;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,14 +35,14 @@
import org.opensearch.client.RestClient;
import org.testcontainers.utility.DockerImageName;

class OpensearchContainerTest {
@DisplayName("Create default OpensearchContainer with security enabled")
@ParameterizedTest(name = "Running Opensearch version={0} (security enabled)")
class OpenSearchContainerTest {
@DisplayName("Create default OpenSearchContainer with security enabled")
@ParameterizedTest(name = "Running OpenSearch version={0} (security enabled)")
@MethodSource("containers")
public void defaultWithSecurity(final String version, final Map<String, String> env, final DockerImageName image)
throws Exception {
try (OpensearchContainer<?> container =
new OpensearchContainer<>(image).withEnv(env).withSecurityEnabled()) {
try (OpenSearchContainer<?> container =
new OpenSearchContainer<>(image).withEnv(env).withSecurityEnabled()) {
container.start();

try (RestClient client = getClient(container)) {
Expand All @@ -59,12 +59,12 @@ public void defaultWithSecurity(final String version, final Map<String, String>
}
}

@DisplayName("Create OpensearchContainer with security disabled")
@ParameterizedTest(name = "Running Opensearch version={0} (security disabled)")
@DisplayName("Create OpenSearchContainer with security disabled")
@ParameterizedTest(name = "Running OpenSearch version={0} (security disabled)")
@MethodSource("containers")
public void defaultNoSecurity(final String version, final Map<String, String> env, final DockerImageName image)
throws Exception {
try (OpensearchContainer<?> container = new OpensearchContainer<>(image).withEnv(env)) {
try (OpenSearchContainer<?> container = new OpenSearchContainer<>(image).withEnv(env)) {
container.start();

try (RestClient client = getClient(container)) {
Expand Down Expand Up @@ -106,10 +106,14 @@ private static Stream<Arguments> containers() {
Arguments.of(
"2.17.0",
Map.of(), /* empty env */
DockerImageName.parse("opensearchproject/opensearch").withTag("2.17.0")));
DockerImageName.parse("opensearchproject/opensearch").withTag("2.17.0")),
Arguments.of(
"2.19.1",
Map.of(), /* empty env */
DockerImageName.parse("opensearchproject/opensearch").withTag("2.19.1")));
}

private RestClient getClient(OpensearchContainer<?> container)
private RestClient getClient(OpenSearchContainer<?> container)
throws KeyManagementException, NoSuchAlgorithmException, KeyStoreException {
final CredentialsProvider credentialsProvider =
getCredentialsProvider(container).orElse(null);
Expand All @@ -127,7 +131,7 @@ private RestClient getClient(OpensearchContainer<?> container)
.build();
}

private Optional<CredentialsProvider> getCredentialsProvider(OpensearchContainer<?> container) {
private Optional<CredentialsProvider> getCredentialsProvider(OpenSearchContainer<?> container) {
if (!container.isSecurityEnabled()) {
return Optional.empty();
}
Expand Down
2 changes: 1 addition & 1 deletion version.properties
Original file line number Diff line number Diff line change
@@ -1 +1 @@
version=2.1.4
version=3.0.0
Loading