Parcourir la source

Wait for license generation before REST test (#83563)

The license related rest tests assume that the cluster has a valid,
self-generated trial license at the start of the test execution.

However, Elasticsearch can have a healthy (green) cluster state but not
yet have a valid license (because the license generation process is a
task that runs after cluster state is ready).

This changes AbstractXPackRestTest to wait until the GET _license API
returns a valid status before it starts the test suite

Resolves: #83259
Tim Vernum il y a 3 ans
Parent
commit
7b952c28ff

+ 25 - 4
x-pack/plugin/src/test/java/org/elasticsearch/xpack/test/rest/AbstractXPackRestTest.java

@@ -18,6 +18,7 @@ import org.elasticsearch.plugins.MetadataUpgrader;
 import org.elasticsearch.test.SecuritySettingsSourceField;
 import org.elasticsearch.test.rest.yaml.ClientYamlTestCandidate;
 import org.elasticsearch.test.rest.yaml.ClientYamlTestResponse;
+import org.elasticsearch.test.rest.yaml.ClientYamlTestResponseException;
 import org.elasticsearch.test.rest.yaml.ESClientYamlSuiteTestCase;
 import org.elasticsearch.xpack.core.ml.integration.MlRestTestStateCleaner;
 import org.elasticsearch.xpack.core.ml.job.persistence.AnomalyDetectorsIndex;
@@ -89,6 +90,20 @@ public class AbstractXPackRestTest extends ESClientYamlSuiteTestCase {
         }
     }
 
+    /**
+     * Waits for the cluster's self-generated license to be created and installed
+     */
+    protected void waitForLicense() {
+        // GET _licence returns a 404 status up until the license exists
+        awaitCallApi(
+            "license.get",
+            Map.of(),
+            List.of(),
+            response -> true,
+            () -> "Exception when waiting for initial license to be generated"
+        );
+    }
+
     /**
      * Cleanup after tests.
      *
@@ -130,10 +145,16 @@ public class AbstractXPackRestTest extends ESClientYamlSuiteTestCase {
         try {
             final AtomicReference<ClientYamlTestResponse> response = new AtomicReference<>();
             assertBusy(() -> {
-                // The actual method call that sends the API requests returns a Future, but we immediately
-                // call .get() on it so there's no need for this method to do any other awaiting.
-                response.set(callApi(apiName, params, bodies, getApiCallHeaders()));
-                assertEquals(HttpStatus.SC_OK, response.get().getStatusCode());
+                try {
+                    // The actual method call that sends the API requests returns a Future, but we immediately
+                    // call .get() on it so there's no need for this method to do any other awaiting.
+                    response.set(callApi(apiName, params, bodies, getApiCallHeaders()));
+                    assertEquals(HttpStatus.SC_OK, response.get().getStatusCode());
+                } catch (ClientYamlTestResponseException e) {
+                    // Convert to an AssertionError so that "assertBusy" treats it as a failed assertion (and tries again)
+                    // rather than a runtime failure (which terminates the loop)
+                    throw new AssertionError("Failed to call API " + apiName, e);
+                }
             });
             success.apply(response.get());
         } catch (Exception e) {

+ 9 - 0
x-pack/plugin/src/yamlRestTest/java/org/elasticsearch/xpack/test/rest/XPackRestIT.java

@@ -10,6 +10,7 @@ package org.elasticsearch.xpack.test.rest;
 import com.carrotsearch.randomizedtesting.annotations.ParametersFactory;
 
 import org.elasticsearch.test.rest.yaml.ClientYamlTestCandidate;
+import org.junit.Before;
 
 public class XPackRestIT extends AbstractXPackRestTest {
 
@@ -21,4 +22,12 @@ public class XPackRestIT extends AbstractXPackRestTest {
     public static Iterable<Object[]> parameters() throws Exception {
         return createParameters();
     }
+
+    /**
+     * Some rest tests depend on the trial license being generated before they run
+     */
+    @Before
+    public void setupLicense() {
+        super.waitForLicense();
+    }
 }

+ 6 - 0
x-pack/plugin/src/yamlRestTest/resources/rest-api-spec/test/license/20_put_license.yml

@@ -127,6 +127,12 @@ teardown:
 ---
 "Current license is trial means not eligle to start trial":
 
+  # Check the current state of the license. If this is not a trail, then the rest of the test will fail (but the failure is hard to diagnose without this assertion)
+  - do:
+      license.get: {}
+
+  - match: { license.type: "trial" }
+
   - do:
       license.get_trial_status: {}