|
| 1 | +/* |
| 2 | + * SPDX-License-Identifier: Apache-2.0 |
| 3 | + * |
| 4 | + * The OpenSearch Contributors require contributions made to |
| 5 | + * this file be licensed under the Apache-2.0 license or a |
| 6 | + * compatible open source license. |
| 7 | + */ |
| 8 | + |
| 9 | +package org.opensearch.client.opensearch.integTest; |
| 10 | + |
| 11 | +import java.net.URI; |
| 12 | +import org.junit.rules.ExternalResource; |
| 13 | +import org.opensearch.testcontainers.OpenSearchContainer; |
| 14 | +import org.opensearch.testcontainers.OpenSearchDockerImage; |
| 15 | + |
| 16 | +/** |
| 17 | + * Starts one OpenSearch test container for the whole test JVM and points the integration tests at |
| 18 | + * it through system properties. The container is shared by every test class, so it is left running |
| 19 | + * after each class; Testcontainers' Ryuk reaper removes it once the JVM exits. |
| 20 | + */ |
| 21 | +final class OpenSearchTestContainerRule extends ExternalResource { |
| 22 | + private static final String ENABLED_PROPERTY = "tests.opensearch.testcontainers.enabled"; |
| 23 | + private static final String VERSION_PROPERTY = "tests.opensearch.version"; |
| 24 | + private static final String IMAGE_PROPERTY = "tests.opensearch.image"; |
| 25 | + private static final String CLUSTER_PROPERTY = "tests.rest.cluster"; |
| 26 | + private static final String HTTPS_PROPERTY = "https"; |
| 27 | + private static final String USER_PROPERTY = "user"; |
| 28 | + private static final String PASSWORD_PROPERTY = "password"; |
| 29 | + |
| 30 | + private static final String DEFAULT_ADMIN_PASSWORD = "admin"; |
| 31 | + |
| 32 | + private static OpenSearchContainer<?> container; |
| 33 | + |
| 34 | + @Override |
| 35 | + protected void before() { |
| 36 | + startIfNeeded(); |
| 37 | + } |
| 38 | + |
| 39 | + private static void startIfNeeded() { |
| 40 | + if (hasText(System.getProperty(CLUSTER_PROPERTY)) || !testcontainersEnabled()) { |
| 41 | + return; |
| 42 | + } |
| 43 | + |
| 44 | + if (container == null) { |
| 45 | + OpenSearchContainer<?> openSearch = createContainer(); |
| 46 | + openSearch.start(); |
| 47 | + container = openSearch; |
| 48 | + } |
| 49 | + |
| 50 | + // getHttpHostAddress() is scheme-prefixed, but tests.rest.cluster expects host:port. |
| 51 | + URI httpHostAddress = URI.create(container.getHttpHostAddress()); |
| 52 | + System.setProperty(CLUSTER_PROPERTY, httpHostAddress.getHost() + ":" + httpHostAddress.getPort()); |
| 53 | + System.setProperty(HTTPS_PROPERTY, Boolean.toString(container.isSecurityEnabled())); |
| 54 | + System.setProperty(USER_PROPERTY, container.getUsername()); |
| 55 | + System.setProperty(PASSWORD_PROPERTY, container.getPassword()); |
| 56 | + } |
| 57 | + |
| 58 | + private static OpenSearchContainer<?> createContainer() { |
| 59 | + String image = System.getProperty(IMAGE_PROPERTY); |
| 60 | + OpenSearchContainer<?> openSearch = hasText(image) |
| 61 | + ? new OpenSearchContainer<>(image) |
| 62 | + : new OpenSearchContainer<>(OpenSearchDockerImage.ofVersion(requiredVersion())); |
| 63 | + |
| 64 | + // Disk watermarks must stay disabled; constrained disks otherwise trip index_create_block_exception. |
| 65 | + openSearch.withSecurityEnabled().withEnv("cluster.routing.allocation.disk.threshold_enabled", "false"); |
| 66 | + |
| 67 | + // The container has no password setter; OPENSEARCH_INITIAL_ADMIN_PASSWORD is its supported |
| 68 | + // input and getPassword() reflects it. Only forward a non-default override: when the env is |
| 69 | + // unset, the container substitutes its own strong default on images >= 2.12. |
| 70 | + String configuredPassword = System.getProperty(PASSWORD_PROPERTY); |
| 71 | + if (hasText(configuredPassword) && !DEFAULT_ADMIN_PASSWORD.equals(configuredPassword)) { |
| 72 | + openSearch.withEnv("OPENSEARCH_INITIAL_ADMIN_PASSWORD", configuredPassword); |
| 73 | + } |
| 74 | + |
| 75 | + return openSearch; |
| 76 | + } |
| 77 | + |
| 78 | + private static String requiredVersion() { |
| 79 | + String version = System.getProperty(VERSION_PROPERTY); |
| 80 | + if (!hasText(version)) { |
| 81 | + throw new IllegalStateException("Missing " + VERSION_PROPERTY + " for OpenSearch Testcontainers image"); |
| 82 | + } |
| 83 | + return version; |
| 84 | + } |
| 85 | + |
| 86 | + private static boolean testcontainersEnabled() { |
| 87 | + return Boolean.parseBoolean(System.getProperty(ENABLED_PROPERTY, "true")); |
| 88 | + } |
| 89 | + |
| 90 | + private static boolean hasText(String value) { |
| 91 | + return value != null && !value.isBlank(); |
| 92 | + } |
| 93 | +} |
0 commit comments