24
24
import org .junit .jupiter .api .extension .ExtensionContext ;
25
25
import org .slf4j .Logger ;
26
26
import org .slf4j .LoggerFactory ;
27
- import org .springframework .data .elasticsearch .support .VersionInfo ;
28
27
import org .springframework .lang .Nullable ;
29
28
import org .testcontainers .elasticsearch .ElasticsearchContainer ;
29
+ import org .testcontainers .utility .DockerImageName ;
30
30
31
31
/**
32
32
* This class manages the connection to an Elasticsearch Cluster, starting a containerized one if necessary. The
@@ -40,9 +40,10 @@ public class ClusterConnection implements ExtensionContext.Store.CloseableResour
40
40
41
41
private static final Logger LOGGER = LoggerFactory .getLogger (ClusterConnection .class );
42
42
43
+ private static final String SDE_TESTCONTAINER_IMAGE_NAME = "sde.testcontainers.image-name" ;
44
+ private static final String SDE_TESTCONTAINER_IMAGE_VERSION = "sde.testcontainers.image-version" ;
43
45
private static final int ELASTICSEARCH_DEFAULT_PORT = 9200 ;
44
46
private static final int ELASTICSEARCH_DEFAULT_TRANSPORT_PORT = 9300 ;
45
- private static final String ELASTICSEARCH_DEFAULT_IMAGE = "docker.elastic.co/elasticsearch/elasticsearch" ;
46
47
47
48
private static final ThreadLocal <ClusterConnectionInfo > clusterConnectionInfoThreadLocal = new ThreadLocal <>();
48
49
@@ -78,20 +79,27 @@ public ClusterConnectionInfo getClusterConnectionInfo() {
78
79
@ Nullable
79
80
private ClusterConnectionInfo startElasticsearchContainer () {
80
81
81
- LOGGER .debug ("Starting Elasticsearch Container" );
82
+ LOGGER .info ("Starting Elasticsearch Container... " );
82
83
83
84
try {
84
- String elasticsearchVersion = VersionInfo .versionProperties ()
85
- .getProperty (VersionInfo .VERSION_ELASTICSEARCH_CLIENT );
86
- Map <String , String > elasticsearchProperties = elasticsearchProperties ();
85
+ IntegrationtestEnvironment integrationtestEnvironment = IntegrationtestEnvironment .get ();
86
+ LOGGER .info ("Integration test environment: {}" , integrationtestEnvironment );
87
+ if (integrationtestEnvironment == IntegrationtestEnvironment .UNDEFINED ) {
88
+ throw new IllegalArgumentException (IntegrationtestEnvironment .SYSTEM_PROPERTY + " property not set" );
89
+ }
90
+
91
+ String testcontainersConfiguration = integrationtestEnvironment .name ().toLowerCase ();
92
+ Map <String , String > testcontainersProperties = testcontainersProperties (
93
+ "testcontainers-" + testcontainersConfiguration + ".properties" );
87
94
88
- String dockerImageName = ELASTICSEARCH_DEFAULT_IMAGE + ':' + elasticsearchVersion ;
89
- LOGGER .debug ("Docker image: {}" , dockerImageName );
95
+ DockerImageName dockerImageName = getDockerImageName (testcontainersProperties );
90
96
91
- ElasticsearchContainer elasticsearchContainer = new ElasticsearchContainer (dockerImageName );
92
- elasticsearchContainer .withEnv (elasticsearchProperties );
97
+ ElasticsearchContainer elasticsearchContainer = new ElasticsearchContainer (dockerImageName )
98
+ .withEnv (testcontainersProperties );
93
99
elasticsearchContainer .start ();
100
+
94
101
return ClusterConnectionInfo .builder () //
102
+ .withIntegrationtestEnvironment (integrationtestEnvironment )
95
103
.withHostAndPort (elasticsearchContainer .getHost (),
96
104
elasticsearchContainer .getMappedPort (ELASTICSEARCH_DEFAULT_PORT )) //
97
105
.withTransportPort (elasticsearchContainer .getMappedPort (ELASTICSEARCH_DEFAULT_TRANSPORT_PORT )) //
@@ -104,9 +112,32 @@ private ClusterConnectionInfo startElasticsearchContainer() {
104
112
return null ;
105
113
}
106
114
107
- private Map <String , String > elasticsearchProperties () {
115
+ private DockerImageName getDockerImageName (Map <String , String > testcontainersProperties ) {
116
+
117
+ String imageName = testcontainersProperties .get (SDE_TESTCONTAINER_IMAGE_NAME );
118
+ String imageVersion = testcontainersProperties .get (SDE_TESTCONTAINER_IMAGE_VERSION );
119
+
120
+ if (imageName == null ) {
121
+ throw new IllegalArgumentException ("property " + SDE_TESTCONTAINER_IMAGE_NAME + " not configured" );
122
+ }
123
+ testcontainersProperties .remove (SDE_TESTCONTAINER_IMAGE_NAME );
124
+
125
+ if (imageVersion == null ) {
126
+ throw new IllegalArgumentException ("property " + SDE_TESTCONTAINER_IMAGE_VERSION + " not configured" );
127
+ }
128
+ testcontainersProperties .remove (SDE_TESTCONTAINER_IMAGE_VERSION );
129
+
130
+ String configuredImageName = imageName + ':' + imageVersion ;
131
+ DockerImageName dockerImageName = DockerImageName .parse (configuredImageName )
132
+ .asCompatibleSubstituteFor ("docker.elastic.co/elasticsearch/elasticsearch" );
133
+ LOGGER .info ("Docker image: {}" , dockerImageName );
134
+ return dockerImageName ;
135
+ }
136
+
137
+ private Map <String , String > testcontainersProperties (String propertiesFile ) {
138
+
139
+ LOGGER .info ("load configuration from {}" , propertiesFile );
108
140
109
- String propertiesFile = "testcontainers-elasticsearch.properties" ;
110
141
try (InputStream inputStream = getClass ().getClassLoader ().getResourceAsStream (propertiesFile )) {
111
142
Properties props = new Properties ();
112
143
0 commit comments