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
This commit is contained in:
Tanguy Leroux 2020-03-24 14:21:00 +01:00
parent 1421471556
commit dea8a31480
2 changed files with 46 additions and 1 deletions

View File

@ -22,7 +22,6 @@ package org.elasticsearch.smoketest;
import com.carrotsearch.randomizedtesting.annotations.Name; import com.carrotsearch.randomizedtesting.annotations.Name;
import com.carrotsearch.randomizedtesting.annotations.ParametersFactory; import com.carrotsearch.randomizedtesting.annotations.ParametersFactory;
import com.carrotsearch.randomizedtesting.annotations.TimeoutSuite; import com.carrotsearch.randomizedtesting.annotations.TimeoutSuite;
import org.apache.http.HttpHost; import org.apache.http.HttpHost;
import org.apache.http.util.EntityUtils; import org.apache.http.util.EntityUtils;
import org.apache.lucene.util.BytesRef; 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.restspec.ClientYamlSuiteRestSpec;
import org.elasticsearch.test.rest.yaml.section.ExecutableSection; import org.elasticsearch.test.rest.yaml.section.ExecutableSection;
import org.junit.After; import org.junit.After;
import org.junit.Before;
import java.io.IOException; import java.io.IOException;
import java.util.ArrayList; import java.util.ArrayList;
@ -102,6 +102,13 @@ public class DocsClientYamlTestSuiteIT extends ESClientYamlSuiteTestCase {
return new ClientYamlDocsTestClient(restSpec, restClient, hosts, esVersion, masterVersion, this::getClientBuilderWithSniffedHosts); return new ClientYamlDocsTestClient(restSpec, restClient, hosts, esVersion, masterVersion, this::getClientBuilderWithSniffedHosts);
} }
@Before
public void waitForRequirements() throws Exception {
if (isCcrTest()) {
ESRestTestCase.waitForActiveLicense(adminClient());
}
}
@After @After
public void cleanup() throws Exception { public void cleanup() throws Exception {
if (isMachineLearningTest() || isTransformTest()) { if (isMachineLearningTest() || isTransformTest()) {
@ -163,6 +170,11 @@ public class DocsClientYamlTestSuiteIT extends ESClientYamlSuiteTestCase {
return testName != null && (testName.contains("/transform/") || testName.contains("\\transform\\")); 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 * Compares the results of running two analyzers against many random
* strings. The goal is to figure out if two anlayzers are "the same" by * strings. The goal is to figure out if two anlayzers are "the same" by

View File

@ -22,6 +22,7 @@ package org.elasticsearch.test.rest;
import org.apache.http.Header; import org.apache.http.Header;
import org.apache.http.HttpHost; import org.apache.http.HttpHost;
import org.apache.http.HttpStatus; import org.apache.http.HttpStatus;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.message.BasicHeader; import org.apache.http.message.BasicHeader;
import org.apache.http.nio.conn.ssl.SSLIOSessionStrategy; import org.apache.http.nio.conn.ssl.SSLIOSessionStrategy;
import org.apache.http.ssl.SSLContexts; 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.equalTo;
import static org.hamcrest.Matchers.everyItem; import static org.hamcrest.Matchers.everyItem;
import static org.hamcrest.Matchers.in; 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}. * 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); 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<String, ?> map = XContentHelper.convertToMap(xContentType.xContent(), is, true);
assertThat(map, notNullValue());
assertThat("License must exist", map.containsKey("license"), equalTo(true));
@SuppressWarnings("unchecked")
final Map<String, ?> license = (Map<String, ?>) 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"));
}
});
}
} }