2929import org .elasticsearch .common .settings .Settings ;
3030import org .elasticsearch .common .xcontent .support .XContentMapValues ;
3131import org .elasticsearch .core .IOUtils ;
32- import org .elasticsearch .core .Tuple ;
3332import org .elasticsearch .test .ClasspathUtils ;
3433import org .elasticsearch .test .rest .ESRestTestCase ;
34+ import org .elasticsearch .test .rest .TestFeatureService ;
3535import org .elasticsearch .test .rest .yaml .restspec .ClientYamlSuiteRestApi ;
3636import org .elasticsearch .test .rest .yaml .restspec .ClientYamlSuiteRestSpec ;
3737import org .elasticsearch .test .rest .yaml .section .ClientYamlTestSection ;
6262import java .util .SortedSet ;
6363import java .util .TreeSet ;
6464import java .util .function .Predicate ;
65+ import java .util .stream .Collectors ;
6566
67+ import static org .elasticsearch .test .rest .yaml .ClientYamlTestExecutionContext .getEsVersion ;
6668import static org .elasticsearch .xcontent .XContentFactory .jsonBuilder ;
6769
6870/**
@@ -139,33 +141,35 @@ public void initAndResetContext() throws Exception {
139141 validateSpec (restSpec );
140142 restSpecification = restSpec ;
141143 final List <HttpHost > hosts = getClusterHosts ();
142- Tuple <Version , Version > versionVersionTuple = readVersionsFromCatNodes (adminClient ());
143- final Version esVersion = versionVersionTuple .v1 ();
144- final Version masterVersion = versionVersionTuple .v2 ();
144+ final Set <String > nodesVersions = getCachedNodesVersions ();
145145 final String os = readOsFromNodesInfo (adminClient ());
146146
147- logger .info (
148- "initializing client, minimum es version [{}], master version, [{}], hosts {}, os [{}]" ,
149- esVersion ,
150- masterVersion ,
151- hosts ,
152- os
147+ var semanticNodeVersions = nodesVersions .stream ()
148+ .map (ESRestTestCase ::parseLegacyVersion )
149+ .flatMap (Optional ::stream )
150+ .collect (Collectors .toSet ());
151+ final TestFeatureService testFeatureService = createTestFeatureService (
152+ getClusterStateFeatures (adminClient ()),
153+ semanticNodeVersions
153154 );
155+
156+ logger .info ("initializing client, node versions [{}], hosts {}, os [{}]" , nodesVersions , hosts , os );
157+
154158 clientYamlTestClient = initClientYamlTestClient (restSpec , client (), hosts );
155159 restTestExecutionContext = createRestTestExecutionContext (
156160 testCandidate ,
157161 clientYamlTestClient ,
158- esVersion ,
159- ESRestTestCase :: clusterHasFeature ,
160- os
162+ nodesVersions ,
163+ testFeatureService ,
164+ Set . of ( os )
161165 );
162166 adminExecutionContext = new ClientYamlTestExecutionContext (
163167 testCandidate ,
164168 clientYamlTestClient ,
165169 false ,
166- esVersion ,
167- ESRestTestCase :: clusterHasFeature ,
168- os
170+ nodesVersions ,
171+ testFeatureService ,
172+ Set . of ( os )
169173 );
170174 final String [] blacklist = resolvePathsProperty (REST_TESTS_BLACKLIST , null );
171175 blacklistPathMatchers = new ArrayList <>();
@@ -190,6 +194,23 @@ public void initAndResetContext() throws Exception {
190194 /**
191195 * Create the test execution context. Can be overwritten in sub-implementations of the test if the context needs to be modified.
192196 */
197+ protected ClientYamlTestExecutionContext createRestTestExecutionContext (
198+ ClientYamlTestCandidate clientYamlTestCandidate ,
199+ ClientYamlTestClient clientYamlTestClient ,
200+ final Set <String > nodesVersions ,
201+ final TestFeatureService testFeatureService ,
202+ final Set <String > osList
203+ ) {
204+ return createRestTestExecutionContext (
205+ clientYamlTestCandidate ,
206+ clientYamlTestClient ,
207+ getEsVersion (nodesVersions ),
208+ testFeatureService ::clusterHasFeature ,
209+ osList .iterator ().next ()
210+ );
211+ }
212+
213+ @ Deprecated
193214 protected ClientYamlTestExecutionContext createRestTestExecutionContext (
194215 ClientYamlTestCandidate clientYamlTestCandidate ,
195216 ClientYamlTestClient clientYamlTestClient ,
@@ -319,13 +340,15 @@ static Map<String, Set<Path>> loadSuites(String... paths) throws Exception {
319340 for (String strPath : paths ) {
320341 Path path = root .resolve (strPath );
321342 if (Files .isDirectory (path )) {
322- Files .walk (path ).forEach (file -> {
323- if (file .toString ().endsWith (".yml" )) {
324- addSuite (root , file , files );
325- } else if (file .toString ().endsWith (".yaml" )) {
326- throw new IllegalArgumentException ("yaml files are no longer supported: " + file );
327- }
328- });
343+ try (var filesStream = Files .walk (path )) {
344+ filesStream .forEach (file -> {
345+ if (file .toString ().endsWith (".yml" )) {
346+ addSuite (root , file , files );
347+ } else if (file .toString ().endsWith (".yaml" )) {
348+ throw new IllegalArgumentException ("yaml files are no longer supported: " + file );
349+ }
350+ });
351+ }
329352 } else {
330353 path = root .resolve (strPath + ".yml" );
331354 assert Files .exists (path ) : "Path " + path + " does not exist in YAML test root" ;
@@ -402,35 +425,6 @@ private static void validateSpec(ClientYamlSuiteRestSpec restSpec) {
402425 }
403426 }
404427
405- Tuple <Version , Version > readVersionsFromCatNodes (RestClient restClient ) throws IOException {
406- // we simply go to the _cat/nodes API and parse all versions in the cluster
407- final Request request = new Request ("GET" , "/_cat/nodes" );
408- request .addParameter ("h" , "version,master" );
409- request .setOptions (getCatNodesVersionMasterRequestOptions ());
410- Response response = restClient .performRequest (request );
411- ClientYamlTestResponse restTestResponse = new ClientYamlTestResponse (response );
412- String nodesCatResponse = restTestResponse .getBodyAsString ();
413- String [] split = nodesCatResponse .split ("\n " );
414- Version version = null ;
415- Version masterVersion = null ;
416- for (String perNode : split ) {
417- final String [] versionAndMaster = perNode .split ("\\ s+" );
418- assert versionAndMaster .length == 2 : "invalid line: " + perNode + " length: " + versionAndMaster .length ;
419- final Version currentVersion = Version .fromString (versionAndMaster [0 ]);
420- final boolean master = versionAndMaster [1 ].trim ().equals ("*" );
421- if (master ) {
422- assert masterVersion == null ;
423- masterVersion = currentVersion ;
424- }
425- if (version == null ) {
426- version = currentVersion ;
427- } else if (version .onOrAfter (currentVersion )) {
428- version = currentVersion ;
429- }
430- }
431- return new Tuple <>(version , masterVersion );
432- }
433-
434428 static String readOsFromNodesInfo (RestClient restClient ) throws IOException {
435429 final Request request = new Request ("GET" , "/_nodes/os" );
436430 Response response = restClient .performRequest (request );
@@ -459,10 +453,6 @@ static String readOsFromNodesInfo(RestClient restClient) throws IOException {
459453 return osPrettyNames .last ();
460454 }
461455
462- protected RequestOptions getCatNodesVersionMasterRequestOptions () {
463- return RequestOptions .DEFAULT ;
464- }
465-
466456 public void test () throws IOException {
467457 // skip test if it matches one of the blacklist globs
468458 for (BlacklistedPathPatternMatcher blacklistedPathMatcher : blacklistPathMatchers ) {
0 commit comments