Skip to content

Commit

Permalink
Wait for Active license before running CCR API tests (#53966) (#54094)
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 eece651 commit 958c7cd
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
import org.elasticsearch.common.xcontent.XContentLocation;
import org.elasticsearch.common.xcontent.XContentParser;
import org.elasticsearch.common.xcontent.XContentParser.Token;
import org.elasticsearch.test.rest.ESRestTestCase;
import org.elasticsearch.test.rest.yaml.ClientYamlDocsTestClient;
import org.elasticsearch.test.rest.yaml.ClientYamlTestCandidate;
import org.elasticsearch.test.rest.yaml.ClientYamlTestClient;
Expand All @@ -41,6 +42,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.Before;

import java.io.IOException;
import java.util.ArrayList;
Expand Down Expand Up @@ -95,6 +97,18 @@ protected ClientYamlTestClient initClientYamlTestClient(
return new ClientYamlDocsTestClient(restSpec, restClient, hosts, esVersion, this::getClientBuilderWithSniffedHosts);
}

@Before
public void waitForRequirements() throws Exception {
if (isCcrTest()) {
ESRestTestCase.waitForActiveLicense(adminClient());
}
}

protected boolean isCcrTest() {
String testName = getTestName();
return testName != null && testName.contains("/ccr/");
}

/**
* Compares the 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 @@ -938,4 +938,35 @@ private static boolean isXPackTemplate(String name) {
}
}


/**
* 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("GET", "/_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);
assertNotNull(map);
assertThat("License must exist", map.containsKey("license"), equalTo(true));
@SuppressWarnings("unchecked")
final Map<String, ?> license = (Map<String, ?>) map.get("license");
assertNotNull("Expecting non-null license", license);
assertThat("License status must exist", license.containsKey("status"), equalTo(true));
final String status = (String) license.get("status");
assertNotNull("Expecting non-null license status", status);
assertThat("Expecting active license", status, equalTo("active"));
}
});
}
}

0 comments on commit 958c7cd

Please sign in to comment.