Skip to content

Commit

Permalink
Wait for Active license before running CCR API tests (#53966)
Browse files Browse the repository at this point in the history
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
  • Loading branch information
tlrx committed Mar 24, 2020
1 parent 1421471 commit dea8a31
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@
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;
Expand All @@ -46,6 +45,7 @@
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;
Expand Down Expand Up @@ -102,6 +102,13 @@ protected ClientYamlTestClient initClientYamlTestClient(
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()) {
Expand Down Expand Up @@ -163,6 +170,11 @@ protected boolean isTransformTest() {
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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
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;
Expand Down Expand Up @@ -100,6 +101,7 @@
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}.
Expand Down Expand Up @@ -1279,4 +1281,35 @@ protected static Response performSyncedFlush(String indexName) throws IOExceptio
}
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"));
}
});
}
}

0 comments on commit dea8a31

Please sign in to comment.