-
Notifications
You must be signed in to change notification settings - Fork 239
Use Testcontainers for integration tests #2033
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
reta
merged 6 commits into
opensearch-project:main
from
lsh1215:issue-1916-testcontainers
Jul 5, 2026
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
5923c09
Use Testcontainers for integration tests
lsh1215 42fed03
Remove redundant Testcontainers startup call
lsh1215 26fdffb
Handle unknown OpenSearch versions conservatively
lsh1215 728e3fe
Use official OpenSearch Testcontainers
lsh1215 65bb540
Wire the test container through a JUnit 4 class rule
lsh1215 d460616
Make the container rule fields private and drop synchronized
lsh1215 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
20 changes: 20 additions & 0 deletions
20
...rc/test/java11/org/opensearch/client/opensearch/integTest/TestcontainersThreadFilter.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,20 @@ | ||
| /* | ||
| * SPDX-License-Identifier: Apache-2.0 | ||
| * | ||
| * The OpenSearch Contributors require contributions made to | ||
| * this file be licensed under the Apache-2.0 license or a | ||
| * compatible open source license. | ||
| */ | ||
|
|
||
| package org.opensearch.client.opensearch.integTest; | ||
|
|
||
| import com.carrotsearch.randomizedtesting.ThreadFilter; | ||
|
|
||
| public final class TestcontainersThreadFilter implements ThreadFilter { | ||
| @Override | ||
| public boolean reject(Thread thread) { | ||
| String name = thread.getName(); | ||
| // Testcontainers owns these helper threads and Ryuk cleans them up after the JVM exits. | ||
| return "testcontainers-ryuk".equals(name) || name.startsWith("testcontainers-pull-watchdog-") || name.startsWith("ducttape-"); | ||
| } | ||
| } | ||
93 changes: 93 additions & 0 deletions
93
...c/test/java21/org/opensearch/client/opensearch/integTest/OpenSearchTestContainerRule.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,93 @@ | ||
| /* | ||
| * SPDX-License-Identifier: Apache-2.0 | ||
| * | ||
| * The OpenSearch Contributors require contributions made to | ||
| * this file be licensed under the Apache-2.0 license or a | ||
| * compatible open source license. | ||
| */ | ||
|
|
||
| package org.opensearch.client.opensearch.integTest; | ||
|
|
||
| import java.net.URI; | ||
| import org.junit.rules.ExternalResource; | ||
| import org.opensearch.testcontainers.OpenSearchContainer; | ||
| import org.opensearch.testcontainers.OpenSearchDockerImage; | ||
|
|
||
| /** | ||
| * Starts one OpenSearch test container for the whole test JVM and points the integration tests at | ||
| * it through system properties. The container is shared by every test class, so it is left running | ||
| * after each class; Testcontainers' Ryuk reaper removes it once the JVM exits. | ||
| */ | ||
| final class OpenSearchTestContainerRule extends ExternalResource { | ||
| private static final String ENABLED_PROPERTY = "tests.opensearch.testcontainers.enabled"; | ||
| private static final String VERSION_PROPERTY = "tests.opensearch.version"; | ||
| private static final String IMAGE_PROPERTY = "tests.opensearch.image"; | ||
| private static final String CLUSTER_PROPERTY = "tests.rest.cluster"; | ||
| private static final String HTTPS_PROPERTY = "https"; | ||
| private static final String USER_PROPERTY = "user"; | ||
| private static final String PASSWORD_PROPERTY = "password"; | ||
|
|
||
| private static final String DEFAULT_ADMIN_PASSWORD = "admin"; | ||
|
|
||
| private static OpenSearchContainer<?> container; | ||
|
|
||
| @Override | ||
| protected void before() { | ||
| startIfNeeded(); | ||
| } | ||
|
|
||
| private static void startIfNeeded() { | ||
| if (hasText(System.getProperty(CLUSTER_PROPERTY)) || !testcontainersEnabled()) { | ||
| return; | ||
| } | ||
|
|
||
| if (container == null) { | ||
| OpenSearchContainer<?> openSearch = createContainer(); | ||
| openSearch.start(); | ||
| container = openSearch; | ||
| } | ||
|
|
||
| // getHttpHostAddress() is scheme-prefixed, but tests.rest.cluster expects host:port. | ||
| URI httpHostAddress = URI.create(container.getHttpHostAddress()); | ||
| System.setProperty(CLUSTER_PROPERTY, httpHostAddress.getHost() + ":" + httpHostAddress.getPort()); | ||
| System.setProperty(HTTPS_PROPERTY, Boolean.toString(container.isSecurityEnabled())); | ||
| System.setProperty(USER_PROPERTY, container.getUsername()); | ||
| System.setProperty(PASSWORD_PROPERTY, container.getPassword()); | ||
| } | ||
|
|
||
| private static OpenSearchContainer<?> createContainer() { | ||
| String image = System.getProperty(IMAGE_PROPERTY); | ||
| OpenSearchContainer<?> openSearch = hasText(image) | ||
| ? new OpenSearchContainer<>(image) | ||
| : new OpenSearchContainer<>(OpenSearchDockerImage.ofVersion(requiredVersion())); | ||
|
|
||
| // Disk watermarks must stay disabled; constrained disks otherwise trip index_create_block_exception. | ||
| openSearch.withSecurityEnabled().withEnv("cluster.routing.allocation.disk.threshold_enabled", "false"); | ||
|
|
||
| // The container has no password setter; OPENSEARCH_INITIAL_ADMIN_PASSWORD is its supported | ||
| // input and getPassword() reflects it. Only forward a non-default override: when the env is | ||
| // unset, the container substitutes its own strong default on images >= 2.12. | ||
| String configuredPassword = System.getProperty(PASSWORD_PROPERTY); | ||
| if (hasText(configuredPassword) && !DEFAULT_ADMIN_PASSWORD.equals(configuredPassword)) { | ||
| openSearch.withEnv("OPENSEARCH_INITIAL_ADMIN_PASSWORD", configuredPassword); | ||
| } | ||
|
|
||
| return openSearch; | ||
| } | ||
|
|
||
| private static String requiredVersion() { | ||
| String version = System.getProperty(VERSION_PROPERTY); | ||
| if (!hasText(version)) { | ||
| throw new IllegalStateException("Missing " + VERSION_PROPERTY + " for OpenSearch Testcontainers image"); | ||
| } | ||
| return version; | ||
| } | ||
|
|
||
| private static boolean testcontainersEnabled() { | ||
| return Boolean.parseBoolean(System.getProperty(ENABLED_PROPERTY, "true")); | ||
| } | ||
|
|
||
| private static boolean hasText(String value) { | ||
| return value != null && !value.isBlank(); | ||
| } | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.