2626import org .apache .http .nio .conn .ssl .SSLIOSessionStrategy ;
2727import org .apache .http .ssl .SSLContexts ;
2828import org .apache .http .util .EntityUtils ;
29+ import org .elasticsearch .Version ;
2930import org .elasticsearch .action .admin .cluster .node .tasks .list .ListTasksAction ;
3031import org .elasticsearch .client .Request ;
3132import org .elasticsearch .client .Response ;
7374import java .util .List ;
7475import java .util .Map ;
7576import java .util .Set ;
77+ import java .util .TreeSet ;
7678import java .util .concurrent .TimeUnit ;
7779import java .util .function .Predicate ;
7880
@@ -105,25 +107,13 @@ public static Map<String, Object> entityAsMap(Response response) throws IOExcept
105107 }
106108
107109 /**
108- * Does the cluster being tested have xpack installed?
110+ * Does any node in the cluster being tested have x-pack installed?
109111 */
110112 public static boolean hasXPack () throws IOException {
111- RestClient client = adminClient ();
112- if (client == null ) {
113+ if (hasXPack == null ) {
113114 throw new IllegalStateException ("must be called inside of a rest test case test" );
114115 }
115- Map <?, ?> response = entityAsMap (client .performRequest (new Request ("GET" , "_nodes/plugins" )));
116- Map <?, ?> nodes = (Map <?, ?>) response .get ("nodes" );
117- for (Map .Entry <?, ?> node : nodes .entrySet ()) {
118- Map <?, ?> nodeInfo = (Map <?, ?>) node .getValue ();
119- for (Object module : (List <?>) nodeInfo .get ("modules" )) {
120- Map <?, ?> moduleInfo = (Map <?, ?>) module ;
121- if (moduleInfo .get ("name" ).toString ().startsWith ("x-pack-" )) {
122- return true ;
123- }
124- }
125- }
126- return false ;
116+ return hasXPack ;
127117 }
128118
129119 private static List <HttpHost > clusterHosts ;
@@ -136,12 +126,16 @@ public static boolean hasXPack() throws IOException {
136126 * completes
137127 */
138128 private static RestClient adminClient ;
129+ private static Boolean hasXPack ;
130+ private static TreeSet <Version > nodeVersions ;
139131
140132 @ Before
141133 public void initClient () throws IOException {
142134 if (client == null ) {
143135 assert adminClient == null ;
144136 assert clusterHosts == null ;
137+ assert hasXPack == null ;
138+ assert nodeVersions == null ;
145139 String cluster = System .getProperty ("tests.rest.cluster" );
146140 if (cluster == null ) {
147141 throw new RuntimeException ("Must specify [tests.rest.cluster] system property with a comma delimited list of [host:port] "
@@ -162,10 +156,27 @@ public void initClient() throws IOException {
162156 logger .info ("initializing REST clients against {}" , clusterHosts );
163157 client = buildClient (restClientSettings (), clusterHosts .toArray (new HttpHost [clusterHosts .size ()]));
164158 adminClient = buildClient (restAdminSettings (), clusterHosts .toArray (new HttpHost [clusterHosts .size ()]));
159+
160+ hasXPack = false ;
161+ nodeVersions = new TreeSet <>();
162+ Map <?, ?> response = entityAsMap (adminClient .performRequest (new Request ("GET" , "_nodes/plugins" )));
163+ Map <?, ?> nodes = (Map <?, ?>) response .get ("nodes" );
164+ for (Map .Entry <?, ?> node : nodes .entrySet ()) {
165+ Map <?, ?> nodeInfo = (Map <?, ?>) node .getValue ();
166+ nodeVersions .add (Version .fromString (nodeInfo .get ("version" ).toString ()));
167+ for (Object module : (List <?>) nodeInfo .get ("modules" )) {
168+ Map <?, ?> moduleInfo = (Map <?, ?>) module ;
169+ if (moduleInfo .get ("name" ).toString ().startsWith ("x-pack-" )) {
170+ hasXPack = true ;
171+ }
172+ }
173+ }
165174 }
166175 assert client != null ;
167176 assert adminClient != null ;
168177 assert clusterHosts != null ;
178+ assert hasXPack != null ;
179+ assert nodeVersions != null ;
169180 }
170181
171182 /**
@@ -195,6 +206,8 @@ public static void closeClients() throws IOException {
195206 clusterHosts = null ;
196207 client = null ;
197208 adminClient = null ;
209+ hasXPack = null ;
210+ nodeVersions = null ;
198211 }
199212 }
200213
@@ -335,8 +348,6 @@ protected boolean preserveRollupJobsUponCompletion() {
335348 }
336349
337350 private void wipeCluster () throws Exception {
338- boolean hasXPack = hasXPack ();
339-
340351 if (preserveIndicesUponCompletion () == false ) {
341352 // wipe indices
342353 try {
0 commit comments