2626import co .elastic .clients .json .jsonb .JsonbJsonpMapper ;
2727import co .elastic .clients .transport .ElasticsearchTransport ;
2828import co .elastic .clients .transport .JsonEndpoint ;
29+ import co .elastic .clients .transport .Version ;
2930import co .elastic .clients .transport .endpoints .DelegatingJsonEndpoint ;
3031import co .elastic .clients .transport .rest_client .RestClientTransport ;
3132import org .apache .http .HttpHost ;
3435import org .apache .http .impl .client .BasicCredentialsProvider ;
3536import org .elasticsearch .client .RestClient ;
3637import org .testcontainers .elasticsearch .ElasticsearchContainer ;
38+ import org .testcontainers .images .builder .ImageFromDockerfile ;
39+ import org .testcontainers .utility .DockerImageName ;
3740
41+ import javax .net .ssl .SSLContext ;
3842import java .io .IOException ;
3943import java .time .Duration ;
4044
4145public class ElasticsearchTestServer implements AutoCloseable {
4246
47+ private final String [] plugins ;
4348 private volatile ElasticsearchContainer container ;
4449 private int port ;
4550 private final JsonpMapper mapper = new JsonbJsonpMapper ();
@@ -54,7 +59,7 @@ public static synchronized ElasticsearchTestServer global() {
5459 System .out .println ("Starting global ES test server." );
5560 global = new ElasticsearchTestServer ();
5661 try {
57- global .setup ();
62+ global .start ();
5863 } catch (Exception e ) {
5964 e .printStackTrace ();
6065 throw e ;
@@ -67,24 +72,62 @@ public static synchronized ElasticsearchTestServer global() {
6772 return global ;
6873 }
6974
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 )
72104 .withEnv ("ES_JAVA_OPTS" , "-Xms256m -Xmx256m" )
73105 .withEnv ("path.repo" , "/tmp" ) // for snapshots
74106 .withStartupTimeout (Duration .ofSeconds (60 ))
75107 .withPassword ("changeme" );
76108 container .start ();
109+
77110 port = container .getMappedPort (9200 );
78111
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+
79117 BasicCredentialsProvider credsProv = new BasicCredentialsProvider ();
80118 credsProv .setCredentials (
81119 AuthScope .ANY , new UsernamePasswordCredentials ("elastic" , "changeme" )
82120 );
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+ )
85126 .build ();
86127 transport = new RestClientTransport (restClient , mapper );
87128 client = new ElasticsearchClient (transport );
129+
130+ return this ;
88131 }
89132
90133 /**
0 commit comments