-
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| 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 { | ||
|
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 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(); | ||
| } | ||
| } | ||
| 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 { | ||
|
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. I don't think we need these tests - opensearch-testcontainers has bunch of them |
||
|
|
||
| @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"); | ||
| } | ||
| } | ||
| 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 { | ||
|
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 remove, we should not see any leaking threads after https://github.com/opensearch-project/opensearch-java/pull/2033/changes#r3514648791 (the extension part) |
||
| @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-"); | ||
| } | ||
| } | ||
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please just use: