-
Notifications
You must be signed in to change notification settings - Fork 237
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
Open
lsh1215
wants to merge
4
commits into
opensearch-project:main
Choose a base branch
from
lsh1215:issue-1916-testcontainers
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 3 commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
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
Some comments aren't visible on the classic Files Changed page.
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
181 changes: 181 additions & 0 deletions
181
...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,181 @@ | ||
| /* | ||
| * 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.time.Duration; | ||
| import org.testcontainers.containers.GenericContainer; | ||
| import org.testcontainers.containers.wait.strategy.HttpWaitStrategy; | ||
| import org.testcontainers.utility.DockerImageName; | ||
|
|
||
| final class OpenSearchTestContainer { | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please move under |
||
| 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 ADMIN_USER = "admin"; | ||
| private static final String DEFAULT_ADMIN_PASSWORD = "admin"; | ||
| // OpenSearch 2.12+ rejects the old admin/admin demo password during container startup. | ||
| private static final String DEFAULT_INITIAL_ADMIN_PASSWORD = "OSClient-Test-2026-Password!"; | ||
| private static final int HTTP_PORT = 9200; | ||
|
|
||
| private static GenericContainer<?> container; | ||
| private static String password; | ||
|
|
||
| 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); | ||
| String passwordCompatibilityVersion = passwordCompatibilityVersion(version, imageName); | ||
| password = passwordFor(passwordCompatibilityVersion); | ||
| GenericContainer<?> openSearch = createContainer(imageName, passwordCompatibilityVersion, password); | ||
| openSearch.start(); | ||
| container = openSearch; | ||
| } | ||
|
|
||
| System.setProperty(CLUSTER_PROPERTY, container.getHost() + ":" + container.getMappedPort(HTTP_PORT)); | ||
| System.setProperty(HTTPS_PROPERTY, "true"); | ||
| System.setProperty(USER_PROPERTY, ADMIN_USER); | ||
| System.setProperty(PASSWORD_PROPERTY, password); | ||
| } | ||
|
|
||
| private static GenericContainer<?> createContainer(String imageName, String passwordCompatibilityVersion, String adminPassword) { | ||
| GenericContainer<?> openSearch = new GenericContainer<>(DockerImageName.parse(imageName)).withExposedPorts(HTTP_PORT) | ||
| .withEnv("discovery.type", "single-node") | ||
| .withEnv("bootstrap.memory_lock", "true") | ||
| .withEnv("cluster.routing.allocation.disk.threshold_enabled", "false") | ||
| .waitingFor( | ||
| new HttpWaitStrategy().forPort(HTTP_PORT) | ||
| .usingTls() | ||
| .allowInsecure() | ||
| .withBasicCredentials(ADMIN_USER, adminPassword) | ||
| .forStatusCode(200) | ||
| .withReadTimeout(Duration.ofSeconds(10)) | ||
| .withStartupTimeout(Duration.ofMinutes(5)) | ||
| ); | ||
|
|
||
| if (requiresInitialAdminPassword(passwordCompatibilityVersion)) { | ||
| openSearch.withEnv("OPENSEARCH_INITIAL_ADMIN_PASSWORD", adminPassword); | ||
| } | ||
|
|
||
| return openSearch; | ||
| } | ||
|
|
||
| private static String resolveImageName(String version) { | ||
| String image = System.getProperty(IMAGE_PROPERTY); | ||
| if (hasText(image)) { | ||
| return image; | ||
| } | ||
| if (!hasText(version)) { | ||
| throw new IllegalStateException("Missing " + VERSION_PROPERTY + " for OpenSearch Testcontainers image"); | ||
| } | ||
| return DEFAULT_IMAGE + ":" + version; | ||
| } | ||
|
|
||
| private static String passwordFor(String version) { | ||
| String configuredPassword = System.getProperty(PASSWORD_PROPERTY); | ||
| if (requiresInitialAdminPassword(version)) { | ||
| if (!hasText(configuredPassword) || DEFAULT_ADMIN_PASSWORD.equals(configuredPassword)) { | ||
| return DEFAULT_INITIAL_ADMIN_PASSWORD; | ||
| } | ||
| return configuredPassword; | ||
| } | ||
| return hasText(configuredPassword) ? configuredPassword : DEFAULT_ADMIN_PASSWORD; | ||
| } | ||
|
|
||
| static boolean requiresInitialAdminPassword(String version) { | ||
| if (!hasText(version) || "latest".equalsIgnoreCase(version)) { | ||
| return true; | ||
| } | ||
|
|
||
| String[] components = version.split("[-+]", 2)[0].split("\\."); | ||
| Integer major = parseInt(components, 0); | ||
| if (major == null) { | ||
| return true; | ||
| } | ||
| if (major > 2) { | ||
| return true; | ||
| } | ||
| if (major < 2) { | ||
| return false; | ||
| } | ||
| Integer minor = parseInt(components, 1); | ||
| if (minor == null) { | ||
| return true; | ||
| } | ||
| return minor >= 12; | ||
| } | ||
|
|
||
| private static Integer parseInt(String[] components, int index) { | ||
| if (index >= components.length) { | ||
| return null; | ||
| } | ||
| return parseInt(components[index]); | ||
| } | ||
|
|
||
| private static Integer parseInt(String component) { | ||
| try { | ||
| return Integer.parseInt(component); | ||
| } catch (NumberFormatException e) { | ||
| return null; | ||
| } | ||
| } | ||
|
|
||
| static String passwordCompatibilityVersion(String version, String imageName) { | ||
| String imageTag = imageTag(imageName); | ||
| if (isCompatibilityVersion(imageTag)) { | ||
| return imageTag; | ||
| } | ||
| return hasText(version) ? version : imageTag; | ||
| } | ||
|
|
||
| private static boolean isCompatibilityVersion(String version) { | ||
| if (!hasText(version)) { | ||
| return false; | ||
| } | ||
| if ("latest".equalsIgnoreCase(version)) { | ||
| return true; | ||
| } | ||
|
|
||
| String[] components = version.split("[-+]", 2)[0].split("\\."); | ||
| for (String component : components) { | ||
| if (parseInt(component) == null) { | ||
| return false; | ||
| } | ||
| } | ||
| return true; | ||
| } | ||
|
|
||
| private static String imageTag(String imageName) { | ||
| int lastSlash = imageName.lastIndexOf('/'); | ||
| int lastColon = imageName.lastIndexOf(':'); | ||
| if (lastColon > lastSlash && lastColon < imageName.length() - 1) { | ||
| return imageName.substring(lastColon + 1); | ||
| } | ||
| return null; | ||
| } | ||
|
|
||
| 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.
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.