26
26
import co .elastic .clients .json .jsonb .JsonbJsonpMapper ;
27
27
import co .elastic .clients .transport .ElasticsearchTransport ;
28
28
import co .elastic .clients .transport .JsonEndpoint ;
29
+ import co .elastic .clients .transport .Version ;
29
30
import co .elastic .clients .transport .endpoints .DelegatingJsonEndpoint ;
30
31
import co .elastic .clients .transport .rest_client .RestClientTransport ;
31
32
import org .apache .http .HttpHost ;
34
35
import org .apache .http .impl .client .BasicCredentialsProvider ;
35
36
import org .elasticsearch .client .RestClient ;
36
37
import org .testcontainers .elasticsearch .ElasticsearchContainer ;
38
+ import org .testcontainers .images .builder .ImageFromDockerfile ;
39
+ import org .testcontainers .utility .DockerImageName ;
37
40
41
+ import javax .net .ssl .SSLContext ;
38
42
import java .io .IOException ;
39
43
import java .time .Duration ;
40
44
41
45
public class ElasticsearchTestServer implements AutoCloseable {
42
46
47
+ private final String [] plugins ;
43
48
private volatile ElasticsearchContainer container ;
44
49
private int port ;
45
50
private final JsonpMapper mapper = new JsonbJsonpMapper ();
@@ -54,7 +59,7 @@ public static synchronized ElasticsearchTestServer global() {
54
59
System .out .println ("Starting global ES test server." );
55
60
global = new ElasticsearchTestServer ();
56
61
try {
57
- global .setup ();
62
+ global .start ();
58
63
} catch (Exception e ) {
59
64
e .printStackTrace ();
60
65
throw e ;
@@ -67,24 +72,62 @@ public static synchronized ElasticsearchTestServer global() {
67
72
return global ;
68
73
}
69
74
70
- private synchronized void setup () {
71
- container = new ElasticsearchContainer ("docker.elastic.co/elasticsearch/elasticsearch:7.17.4" )
75
+ public ElasticsearchTestServer (String ... plugins ) {
76
+ this .plugins = plugins ;
77
+ }
78
+
79
+ public synchronized ElasticsearchTestServer start () {
80
+ Version version = Version .VERSION .major () < 8 ? new Version (7 ,17 ,5 ,false ) : new Version (8 ,3 ,3 ,false );
81
+
82
+ // Note we could use version.major() + "." + version.minor() + "-SNAPSHOT" but plugins won't install on a snapshot version
83
+ String esImage = "docker.elastic.co/elasticsearch/elasticsearch:" + version ;
84
+
85
+ DockerImageName image ;
86
+ if (plugins .length == 0 ) {
87
+ image = DockerImageName .parse (esImage );
88
+ } else {
89
+ String esWithPluginsImage = new ImageFromDockerfile ()
90
+ .withDockerfileFromBuilder (b -> {
91
+ b .from (esImage );
92
+ for (String plugin : plugins ) {
93
+ b .run ("/usr/share/elasticsearch/bin/elasticsearch-plugin" , "install" , plugin );
94
+ }
95
+ }
96
+ ).get ();
97
+
98
+ image = DockerImageName
99
+ .parse (esWithPluginsImage )
100
+ .asCompatibleSubstituteFor ("docker.elastic.co/elasticsearch/elasticsearch" );
101
+ }
102
+
103
+ container = new ElasticsearchContainer (image )
72
104
.withEnv ("ES_JAVA_OPTS" , "-Xms256m -Xmx256m" )
73
105
.withEnv ("path.repo" , "/tmp" ) // for snapshots
74
106
.withStartupTimeout (Duration .ofSeconds (60 ))
75
107
.withPassword ("changeme" );
76
108
container .start ();
109
+
77
110
port = container .getMappedPort (9200 );
78
111
112
+ boolean useTLS = version .major () >= 8 ;
113
+ HttpHost host = new HttpHost ("localhost" , port , useTLS ? "https" : "http" );
114
+
115
+ SSLContext sslContext = useTLS ? container .createSslContextFromCa () : null ;
116
+
79
117
BasicCredentialsProvider credsProv = new BasicCredentialsProvider ();
80
118
credsProv .setCredentials (
81
119
AuthScope .ANY , new UsernamePasswordCredentials ("elastic" , "changeme" )
82
120
);
83
- restClient = RestClient .builder (new HttpHost ("localhost" , port ))
84
- .setHttpClientConfigCallback (hc -> hc .setDefaultCredentialsProvider (credsProv ))
121
+ restClient = RestClient .builder (host )
122
+ .setHttpClientConfigCallback (hc -> hc
123
+ .setDefaultCredentialsProvider (credsProv )
124
+ .setSSLContext (sslContext )
125
+ )
85
126
.build ();
86
127
transport = new RestClientTransport (restClient , mapper );
87
128
client = new ElasticsearchClient (transport );
129
+
130
+ return this ;
88
131
}
89
132
90
133
/**
0 commit comments