-
Notifications
You must be signed in to change notification settings - Fork 245
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 4 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
79 changes: 79 additions & 0 deletions
79
...t/src/test/java11/org/opensearch/client/opensearch/integTest/OpenSearchTestContainer.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,79 @@ | ||
| /* | ||
| * 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 org.opensearch.testcontainers.OpenSearchContainer; | ||
|
|
||
| final class OpenSearchTestContainer { | ||
|
lsh1215 marked this conversation as resolved.
Outdated
|
||
| static final String ENABLED_PROPERTY = "tests.opensearch.testcontainers.enabled"; | ||
| static final String VERSION_PROPERTY = "tests.opensearch.version"; | ||
| static final String IMAGE_PROPERTY = "tests.opensearch.image"; | ||
| static final String CLUSTER_PROPERTY = "tests.rest.cluster"; | ||
| static final String HTTPS_PROPERTY = "https"; | ||
| static final String USER_PROPERTY = "user"; | ||
| static final String PASSWORD_PROPERTY = "password"; | ||
|
|
||
| private static final String DEFAULT_IMAGE = "opensearchproject/opensearch"; | ||
| private static final String DEFAULT_ADMIN_PASSWORD = "admin"; | ||
| private static final int HTTP_PORT = 9200; | ||
|
|
||
| private static OpenSearchContainer<?> container; | ||
|
|
||
| private OpenSearchTestContainer() {} | ||
|
|
||
| static synchronized void startIfNeeded() { | ||
| if (hasText(System.getProperty(CLUSTER_PROPERTY)) || !testcontainersEnabled()) { | ||
| return; | ||
| } | ||
|
|
||
| if (container == null) { | ||
| String version = System.getProperty(VERSION_PROPERTY); | ||
| String imageName = resolveImageName(version, System.getProperty(IMAGE_PROPERTY)); | ||
| OpenSearchContainer<?> openSearch = createContainer(imageName); | ||
| openSearch.start(); | ||
| container = openSearch; | ||
| } | ||
|
|
||
| System.setProperty(CLUSTER_PROPERTY, container.getHost() + ":" + container.getMappedPort(HTTP_PORT)); | ||
| 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 imageName) { | ||
| OpenSearchContainer<?> openSearch = new OpenSearchContainer<>(imageName).withSecurityEnabled() | ||
| .withEnv("bootstrap.memory_lock", "true") | ||
| .withEnv("cluster.routing.allocation.disk.threshold_enabled", "false"); | ||
|
|
||
| String configuredPassword = System.getProperty(PASSWORD_PROPERTY); | ||
| if (hasText(configuredPassword) && !DEFAULT_ADMIN_PASSWORD.equals(configuredPassword)) { | ||
| openSearch.withEnv("OPENSEARCH_INITIAL_ADMIN_PASSWORD", configuredPassword); | ||
| } | ||
|
|
||
| return openSearch; | ||
| } | ||
|
|
||
| static String resolveImageName(String version, String image) { | ||
| if (hasText(image)) { | ||
| return image; | ||
| } | ||
| if (!hasText(version)) { | ||
| throw new IllegalStateException("Missing " + VERSION_PROPERTY + " for OpenSearch Testcontainers image"); | ||
| } | ||
| return DEFAULT_IMAGE + ":" + version; | ||
| } | ||
|
|
||
| private static boolean testcontainersEnabled() { | ||
| return Boolean.parseBoolean(System.getProperty(ENABLED_PROPERTY, "true")); | ||
| } | ||
|
|
||
| private static boolean hasText(String value) { | ||
| return value != null && !value.isBlank(); | ||
| } | ||
| } | ||
41 changes: 41 additions & 0 deletions
41
.../test/java11/org/opensearch/client/opensearch/integTest/OpenSearchTestContainerTests.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,41 @@ | ||
| /* | ||
| * 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 static org.junit.Assert.assertEquals; | ||
|
|
||
| import org.junit.Test; | ||
|
|
||
| public class OpenSearchTestContainerTests { | ||
|
lsh1215 marked this conversation as resolved.
Outdated
|
||
|
|
||
| @Test | ||
| public void defaultImageUsesOfficialOpenSearchImage() { | ||
| assertEquals("opensearchproject/opensearch:3.2.0", OpenSearchTestContainer.resolveImageName("3.2.0", null)); | ||
| } | ||
|
|
||
| @Test | ||
| public void configuredImageTakesPrecedenceOverVersion() { | ||
| assertEquals( | ||
| "opensearchproject/opensearch:2.19.2", | ||
| OpenSearchTestContainer.resolveImageName("3.2.0", "opensearchproject/opensearch:2.19.2") | ||
| ); | ||
| } | ||
|
|
||
| @Test | ||
| public void missingVersionWithoutCustomImageFailsFast() { | ||
| try { | ||
| OpenSearchTestContainer.resolveImageName(null, null); | ||
| } catch (IllegalStateException e) { | ||
| assertEquals("Missing tests.opensearch.version for OpenSearch Testcontainers image", e.getMessage()); | ||
| return; | ||
| } | ||
|
|
||
| throw new AssertionError("Expected missing OpenSearch version to fail"); | ||
| } | ||
| } | ||
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 { | ||
|
lsh1215 marked this conversation as resolved.
|
||
| @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-"); | ||
| } | ||
| } | ||
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.