Skip to content

Add TestContainers configuration #1861

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
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,12 @@
*/
package org.springframework.data.elasticsearch.junit.jupiter;

import java.io.InputStream;
import java.util.Collections;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Properties;

import org.junit.jupiter.api.extension.ExtensionContext;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
Expand Down Expand Up @@ -77,10 +83,13 @@ private ClusterConnectionInfo startElasticsearchContainer() {
try {
String elasticsearchVersion = VersionInfo.versionProperties()
.getProperty(VersionInfo.VERSION_ELASTICSEARCH_CLIENT);
Map<String, String> elasticsearchProperties = elasticsearchProperties();

String dockerImageName = ELASTICSEARCH_DEFAULT_IMAGE + ':' + elasticsearchVersion;
LOGGER.debug("Docker image: {}", dockerImageName);

ElasticsearchContainer elasticsearchContainer = new ElasticsearchContainer(dockerImageName);
elasticsearchContainer.withEnv(elasticsearchProperties);
elasticsearchContainer.start();
return ClusterConnectionInfo.builder() //
.withHostAndPort(elasticsearchContainer.getHost(),
Expand All @@ -95,6 +104,24 @@ private ClusterConnectionInfo startElasticsearchContainer() {
return null;
}

private Map<String, String> elasticsearchProperties() {

String propertiesFile = "testcontainers-elasticsearch.properties";
try (InputStream inputStream = getClass().getClassLoader().getResourceAsStream(propertiesFile)) {
Properties props = new Properties();

if (inputStream != null) {
props.load(inputStream);
}
Map<String, String> elasticsearchProperties = new LinkedHashMap<>();
props.forEach((key, value) -> elasticsearchProperties.put(key.toString(), value.toString()));
return elasticsearchProperties;
} catch (Exception e) {
LOGGER.error("Cannot load " + propertiesFile);
}
return Collections.emptyMap();
}

@Override
public void close() {

Expand Down
2 changes: 2 additions & 0 deletions src/test/resources/testcontainers-elasticsearch.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# needed as we do a DELETE /* at the end of the tests, will be requited from 8.0 on, produces a warning since 7.13
action.destructive_requires_name=false