From dea8a31480b3a5edfc2bff07775fd2bfe3637a80 Mon Sep 17 00:00:00 2001 From: Tanguy Leroux Date: Tue, 24 Mar 2020 14:21:00 +0100 Subject: [PATCH] Wait for Active license before running CCR API tests (#53966) DocsClientYamlTestSuiteIT sometimes fails for CCR related tests because tests are started before the license is fully applied and active within the cluster. The first tests to be executed then fails with the error noticed in #53430. This can be easily reproduced locally by only running CCR docs tests. This commit adds some @Before logic in DocsClientYamlTestSuiteIT so that it waits for the license to be active before running CCR tests. Closes #53430 --- .../smoketest/DocsClientYamlTestSuiteIT.java | 14 +++++++- .../test/rest/ESRestTestCase.java | 33 +++++++++++++++++++ 2 files changed, 46 insertions(+), 1 deletion(-) diff --git a/docs/src/test/java/org/elasticsearch/smoketest/DocsClientYamlTestSuiteIT.java b/docs/src/test/java/org/elasticsearch/smoketest/DocsClientYamlTestSuiteIT.java index 249101cfc54..069945f1b74 100644 --- a/docs/src/test/java/org/elasticsearch/smoketest/DocsClientYamlTestSuiteIT.java +++ b/docs/src/test/java/org/elasticsearch/smoketest/DocsClientYamlTestSuiteIT.java @@ -22,7 +22,6 @@ package org.elasticsearch.smoketest; import com.carrotsearch.randomizedtesting.annotations.Name; import com.carrotsearch.randomizedtesting.annotations.ParametersFactory; import com.carrotsearch.randomizedtesting.annotations.TimeoutSuite; - import org.apache.http.HttpHost; import org.apache.http.util.EntityUtils; import org.apache.lucene.util.BytesRef; @@ -46,6 +45,7 @@ import org.elasticsearch.test.rest.yaml.ESClientYamlSuiteTestCase; import org.elasticsearch.test.rest.yaml.restspec.ClientYamlSuiteRestSpec; import org.elasticsearch.test.rest.yaml.section.ExecutableSection; import org.junit.After; +import org.junit.Before; import java.io.IOException; import java.util.ArrayList; @@ -102,6 +102,13 @@ public class DocsClientYamlTestSuiteIT extends ESClientYamlSuiteTestCase { return new ClientYamlDocsTestClient(restSpec, restClient, hosts, esVersion, masterVersion, this::getClientBuilderWithSniffedHosts); } + @Before + public void waitForRequirements() throws Exception { + if (isCcrTest()) { + ESRestTestCase.waitForActiveLicense(adminClient()); + } + } + @After public void cleanup() throws Exception { if (isMachineLearningTest() || isTransformTest()) { @@ -163,6 +170,11 @@ public class DocsClientYamlTestSuiteIT extends ESClientYamlSuiteTestCase { return testName != null && (testName.contains("/transform/") || testName.contains("\\transform\\")); } + protected boolean isCcrTest() { + String testName = getTestName(); + return testName != null && testName.contains("/ccr/"); + } + /** * Compares the results of running two analyzers against many random * strings. The goal is to figure out if two anlayzers are "the same" by diff --git a/test/framework/src/main/java/org/elasticsearch/test/rest/ESRestTestCase.java b/test/framework/src/main/java/org/elasticsearch/test/rest/ESRestTestCase.java index f391592aaac..87a91fca4b7 100644 --- a/test/framework/src/main/java/org/elasticsearch/test/rest/ESRestTestCase.java +++ b/test/framework/src/main/java/org/elasticsearch/test/rest/ESRestTestCase.java @@ -22,6 +22,7 @@ package org.elasticsearch.test.rest; import org.apache.http.Header; import org.apache.http.HttpHost; import org.apache.http.HttpStatus; +import org.apache.http.client.methods.HttpGet; import org.apache.http.message.BasicHeader; import org.apache.http.nio.conn.ssl.SSLIOSessionStrategy; import org.apache.http.ssl.SSLContexts; @@ -100,6 +101,7 @@ import static org.hamcrest.Matchers.anyOf; import static org.hamcrest.Matchers.equalTo; import static org.hamcrest.Matchers.everyItem; import static org.hamcrest.Matchers.in; +import static org.hamcrest.Matchers.notNullValue; /** * Superclass for tests that interact with an external test cluster using Elasticsearch's {@link RestClient}. @@ -1279,4 +1281,35 @@ public abstract class ESRestTestCase extends ESTestCase { } return client().performRequest(request); } + + /** + * Wait for the license to be applied and active. The specified admin client is used to check the license and this is done using + * {@link ESTestCase#assertBusy(CheckedRunnable)} to give some time to the License to be applied on nodes. + * + * @param restClient the client to use + * @throws Exception if an exception is thrown while checking the status of the license + */ + protected static void waitForActiveLicense(final RestClient restClient) throws Exception { + assertBusy(() -> { + final Request request = new Request(HttpGet.METHOD_NAME, "/_xpack"); + request.setOptions(RequestOptions.DEFAULT.toBuilder()); + + final Response response = restClient.performRequest(request); + assertOK(response); + + try (InputStream is = response.getEntity().getContent()) { + XContentType xContentType = XContentType.fromMediaTypeOrFormat(response.getEntity().getContentType().getValue()); + final Map map = XContentHelper.convertToMap(xContentType.xContent(), is, true); + assertThat(map, notNullValue()); + assertThat("License must exist", map.containsKey("license"), equalTo(true)); + @SuppressWarnings("unchecked") + final Map license = (Map) map.get("license"); + assertThat("Expecting non-null license", license, notNullValue()); + assertThat("License status must exist", license.containsKey("status"), equalTo(true)); + final String status = (String) license.get("status"); + assertThat("Expecting non-null license status", status, notNullValue()); + assertThat("Expecting active license", status, equalTo("active")); + } + }); + } }