Browse Source

Fix NodeInfo version parsing in integration tests (#100770)

* Compatible version parsing in YAML tests
* Compatible version parsing in various IT tests
Lorenzo Dematté 2 years ago
parent
commit
f878a8c308

+ 4 - 3
qa/rolling-upgrade/src/javaRestTest/java/org/elasticsearch/upgrades/FieldCapsIT.java

@@ -11,7 +11,7 @@ package org.elasticsearch.upgrades;
 import com.carrotsearch.randomizedtesting.annotations.Name;
 
 import org.apache.http.HttpHost;
-import org.elasticsearch.Version;
+import org.elasticsearch.Build;
 import org.elasticsearch.action.fieldcaps.FieldCapabilitiesResponse;
 import org.elasticsearch.client.Request;
 import org.elasticsearch.client.RestClient;
@@ -272,18 +272,19 @@ public class FieldCapsIT extends ParameterizedRollingUpgradeTestCase {
     @SuppressWarnings("unchecked")
     // Returns a client connected to one of the upgraded nodes.
     private RestClient getUpgradedNodeClient() throws IOException {
+        var currentVersion = Build.current().version();
         for (HttpHost host : getClusterHosts()) {
             RestClient client = RestClient.builder(host).build();
             Request nodesRequest = new Request("GET", "_nodes/_local/_none");
             Map<String, ?> nodeMap = (Map<String, ?>) entityAsMap(client.performRequest(nodesRequest)).get("nodes");
             Map<String, ?> nameMap = (Map<String, ?>) nodeMap.values().iterator().next();
             String version = (String) nameMap.get("version");
-            if (version.equals(Version.CURRENT.toString())) {
+            if (version.equals(currentVersion)) {
                 return client;
             }
             client.close();
         }
-        throw new IllegalStateException("Couldn't find node on version " + Version.CURRENT);
+        throw new IllegalStateException("Couldn't find node on version " + currentVersion);
     }
 
     // Test field type filtering on mixed cluster

+ 8 - 14
qa/rolling-upgrade/src/javaRestTest/java/org/elasticsearch/upgrades/IndexingIT.java

@@ -29,6 +29,7 @@ import java.nio.charset.StandardCharsets;
 import java.util.List;
 import java.util.Map;
 import java.util.concurrent.TimeUnit;
+import java.util.function.Predicate;
 
 import static org.elasticsearch.rest.action.search.RestSearchAction.TOTAL_HITS_AS_INT_PARAM;
 import static org.elasticsearch.test.ListMatcher.matchesList;
@@ -139,8 +140,7 @@ public class IndexingIT extends ParameterizedRollingUpgradeTestCase {
             Request waitForGreen = new Request("GET", "/_cluster/health");
             waitForGreen.addParameter("wait_for_nodes", "3");
             client().performRequest(waitForGreen);
-            Version minNodeVersion = minNodeVersion();
-            if (minNodeVersion.before(Version.V_7_5_0)) {
+            if (clusterSupportsBulkApi() == false) {
                 ResponseException e = expectThrows(ResponseException.class, () -> client().performRequest(bulk));
                 assertEquals(400, e.getResponse().getStatusLine().getStatusCode());
                 assertThat(
@@ -410,19 +410,13 @@ public class IndexingIT extends ParameterizedRollingUpgradeTestCase {
         );
     }
 
-    private Version minNodeVersion() throws IOException {
+    // TODO[lor]: replace this check with a (historical) feature check ("supports bulk requests")
+    private boolean clusterSupportsBulkApi() throws IOException {
         Map<?, ?> response = entityAsMap(client().performRequest(new Request("GET", "_nodes")));
         Map<?, ?> nodes = (Map<?, ?>) response.get("nodes");
-        Version minNodeVersion = null;
-        for (Map.Entry<?, ?> node : nodes.entrySet()) {
-            Map<?, ?> nodeInfo = (Map<?, ?>) node.getValue();
-            Version nodeVersion = Version.fromString(nodeInfo.get("version").toString());
-            if (minNodeVersion == null) {
-                minNodeVersion = nodeVersion;
-            } else if (nodeVersion.before(minNodeVersion)) {
-                minNodeVersion = nodeVersion;
-            }
-        }
-        return minNodeVersion;
+
+        Predicate<Map<?, ?>> nodeSupportsBulkApi = n -> Version.fromString(n.get("version").toString()).onOrAfter(Version.V_7_5_0);
+
+        return nodes.values().stream().map(o -> (Map<?, ?>) o).allMatch(nodeSupportsBulkApi);
     }
 }

+ 21 - 13
x-pack/qa/rolling-upgrade/src/test/java/org/elasticsearch/upgrades/ApiKeyBackwardsCompatibilityIT.java

@@ -303,11 +303,18 @@ public class ApiKeyBackwardsCompatibilityIT extends AbstractUpgradeTestCase {
         }
     }
 
+    boolean nodeSupportApiKeyRemoteIndices(Map<String, Object> nodeDetails) {
+        // TODO[lor]: the method can be kept, but we need to replace version check with features checks
+        String versionString = (String) nodeDetails.get("version");
+        Version version = Version.fromString(versionString.replace("-SNAPSHOT", ""));
+        return version.onOrAfter(API_KEY_SUPPORT_REMOTE_INDICES_VERSION);
+    }
+
     private void createClientsByVersion() throws IOException {
-        Map<Version, RestClient> clientsByVersion = getRestClientByVersion();
-        if (clientsByVersion.size() == 2) {
-            for (Map.Entry<Version, RestClient> client : clientsByVersion.entrySet()) {
-                if (client.getKey().before(API_KEY_SUPPORT_REMOTE_INDICES_VERSION)) {
+        var clientsByCapability = getRestClientByCapability();
+        if (clientsByCapability.size() == 2) {
+            for (Map.Entry<Boolean, RestClient> client : clientsByCapability.entrySet()) {
+                if (client.getKey() == false) {
                     oldVersionClient = client.getValue();
                 } else {
                     newVersionClient = client.getValue();
@@ -316,7 +323,7 @@ public class ApiKeyBackwardsCompatibilityIT extends AbstractUpgradeTestCase {
             assertThat(oldVersionClient, notNullValue());
             assertThat(newVersionClient, notNullValue());
         } else {
-            fail("expected 2 versions during rolling upgrade but got: " + clientsByVersion.size());
+            fail("expected 2 versions during rolling upgrade but got: " + clientsByCapability.size());
         }
     }
 
@@ -332,23 +339,24 @@ public class ApiKeyBackwardsCompatibilityIT extends AbstractUpgradeTestCase {
     }
 
     @SuppressWarnings("unchecked")
-    private Map<Version, RestClient> getRestClientByVersion() throws IOException {
+    private Map<Boolean, RestClient> getRestClientByCapability() throws IOException {
         Response response = client().performRequest(new Request("GET", "_nodes"));
         assertOK(response);
         ObjectPath objectPath = ObjectPath.createFromResponse(response);
         Map<String, Object> nodesAsMap = objectPath.evaluate("nodes");
-        Map<Version, List<HttpHost>> hostsByVersion = new HashMap<>();
+        Map<Boolean, List<HttpHost>> hostsByCapability = new HashMap<>();
         for (Map.Entry<String, Object> entry : nodesAsMap.entrySet()) {
             Map<String, Object> nodeDetails = (Map<String, Object>) entry.getValue();
-            Version version = Version.fromString((String) nodeDetails.get("version"));
+            var capabilitySupported = nodeSupportApiKeyRemoteIndices(nodeDetails);
             Map<String, Object> httpInfo = (Map<String, Object>) nodeDetails.get("http");
-            hostsByVersion.computeIfAbsent(version, k -> new ArrayList<>()).add(HttpHost.create((String) httpInfo.get("publish_address")));
+            hostsByCapability.computeIfAbsent(capabilitySupported, k -> new ArrayList<>())
+                .add(HttpHost.create((String) httpInfo.get("publish_address")));
         }
-        Map<Version, RestClient> clientsByVersion = new HashMap<>();
-        for (Map.Entry<Version, List<HttpHost>> entry : hostsByVersion.entrySet()) {
-            clientsByVersion.put(entry.getKey(), buildClient(restClientSettings(), entry.getValue().toArray(new HttpHost[0])));
+        Map<Boolean, RestClient> clientsByCapability = new HashMap<>();
+        for (var entry : hostsByCapability.entrySet()) {
+            clientsByCapability.put(entry.getKey(), buildClient(restClientSettings(), entry.getValue().toArray(new HttpHost[0])));
         }
-        return clientsByVersion;
+        return clientsByCapability;
     }
 
     private static RoleDescriptor randomRoleDescriptor(boolean includeRemoteIndices) {

+ 6 - 7
x-pack/qa/rolling-upgrade/src/test/java/org/elasticsearch/upgrades/TokenBackwardsCompatibilityIT.java

@@ -8,7 +8,6 @@ package org.elasticsearch.upgrades;
 
 import org.apache.http.HttpHeaders;
 import org.apache.http.HttpHost;
-import org.elasticsearch.Version;
 import org.elasticsearch.action.search.SearchResponse;
 import org.elasticsearch.client.Request;
 import org.elasticsearch.client.RequestOptions;
@@ -43,7 +42,7 @@ public class TokenBackwardsCompatibilityIT extends AbstractUpgradeTestCase {
 
     @Before
     private void collectClientsByVersion() throws IOException {
-        Map<Version, RestClient> clientsByVersion = getRestClientByVersion();
+        Map<String, RestClient> clientsByVersion = getRestClientByVersion();
         if (clientsByVersion.size() == 2) {
             // usual case, clients have different versions
             twoClients = clientsByVersion.values();
@@ -316,20 +315,20 @@ public class TokenBackwardsCompatibilityIT extends AbstractUpgradeTestCase {
     }
 
     @SuppressWarnings("unchecked")
-    private Map<Version, RestClient> getRestClientByVersion() throws IOException {
+    private Map<String, RestClient> getRestClientByVersion() throws IOException {
         Response response = client().performRequest(new Request("GET", "_nodes"));
         assertOK(response);
         ObjectPath objectPath = ObjectPath.createFromResponse(response);
         Map<String, Object> nodesAsMap = objectPath.evaluate("nodes");
-        Map<Version, List<HttpHost>> hostsByVersion = new HashMap<>();
+        Map<String, List<HttpHost>> hostsByVersion = new HashMap<>();
         for (Map.Entry<String, Object> entry : nodesAsMap.entrySet()) {
             Map<String, Object> nodeDetails = (Map<String, Object>) entry.getValue();
-            Version version = Version.fromString((String) nodeDetails.get("version"));
+            String version = (String) nodeDetails.get("version");
             Map<String, Object> httpInfo = (Map<String, Object>) nodeDetails.get("http");
             hostsByVersion.computeIfAbsent(version, k -> new ArrayList<>()).add(HttpHost.create((String) httpInfo.get("publish_address")));
         }
-        Map<Version, RestClient> clientsByVersion = new HashMap<>();
-        for (Map.Entry<Version, List<HttpHost>> entry : hostsByVersion.entrySet()) {
+        Map<String, RestClient> clientsByVersion = new HashMap<>();
+        for (Map.Entry<String, List<HttpHost>> entry : hostsByVersion.entrySet()) {
             clientsByVersion.put(entry.getKey(), buildClient(restClientSettings(), entry.getValue().toArray(new HttpHost[0])));
         }
         return clientsByVersion;