Skip to content

Commit

Permalink
Ensuring that index templates are loaded before running YAML REST tes…
Browse files Browse the repository at this point in the history
…ts run
  • Loading branch information
masseyke committed Nov 7, 2023
1 parent 5a2b618 commit 80e3ab2
Showing 1 changed file with 24 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import org.elasticsearch.client.Request;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.Response;
import org.elasticsearch.client.ResponseException;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestClientBuilder;
import org.elasticsearch.client.WarningsHandler;
Expand Down Expand Up @@ -61,8 +62,10 @@
import java.util.Set;
import java.util.SortedSet;
import java.util.TreeSet;
import java.util.concurrent.TimeUnit;

import static org.elasticsearch.xcontent.XContentFactory.jsonBuilder;
import static org.hamcrest.Matchers.equalTo;

/**
* Runs a suite of yaml tests shared with all the official Elasticsearch
Expand Down Expand Up @@ -162,6 +165,7 @@ public void initAndResetContext() throws Exception {
for (final String entry : blacklistAdditions) {
blacklistPathMatchers.add(new BlacklistedPathPatternMatcher(entry));
}
assertThatTemplatesAreLoaded();
}
assert restTestExecutionContext != null;
assert adminExecutionContext != null;
Expand All @@ -173,6 +177,26 @@ public void initAndResetContext() throws Exception {
restTestExecutionContext.clear();
}

/**
* Many YAML REST tests depend on the templates that come with Elasticsearch. However, the server will respond to requests before the
* templates are loaded, and there is no way for the YAML tests to block until templates are loaded. So it is possible without this
* check for tests to begin executing before the templates are loaded. This method waits up to 1 minute for the logs template (which
* is among the last to be loaded and one of the more frequently-used ones) to be loaded, and fails with an AssertionError if it is not
* loaded within that time.
* @throws Exception If the logs template cannot be found within 60 seconds.
*/
private void assertThatTemplatesAreLoaded() throws Exception {
assertBusy(() -> {
int responseCode = 0;
try {
responseCode = adminClient().performRequest(new Request("GET", "_index_template/logs")).getStatusLine().getStatusCode();
} catch (ResponseException e) {
fail(e);
}
assertThat(responseCode, equalTo(200));
}, 60, TimeUnit.SECONDS);
}

/**
* Create the test execution context. Can be overwritten in sub-implementations of the test if the context needs to be modified.
*/
Expand Down

0 comments on commit 80e3ab2

Please sign in to comment.