From 80e3ab23eead553b2d67dd526160572550490e01 Mon Sep 17 00:00:00 2001 From: Keith Massey Date: Tue, 7 Nov 2023 17:07:04 -0600 Subject: [PATCH] Ensuring that index templates are loaded before running YAML REST tests run --- .../rest/yaml/ESClientYamlSuiteTestCase.java | 24 +++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/test/yaml-rest-runner/src/main/java/org/elasticsearch/test/rest/yaml/ESClientYamlSuiteTestCase.java b/test/yaml-rest-runner/src/main/java/org/elasticsearch/test/rest/yaml/ESClientYamlSuiteTestCase.java index da6786c8341cc..dd20efe23c536 100644 --- a/test/yaml-rest-runner/src/main/java/org/elasticsearch/test/rest/yaml/ESClientYamlSuiteTestCase.java +++ b/test/yaml-rest-runner/src/main/java/org/elasticsearch/test/rest/yaml/ESClientYamlSuiteTestCase.java @@ -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; @@ -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 @@ -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; @@ -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. */