diff --git a/elasticsearch/src/test/java/org/elasticsearch/xpack/test/rest/XPackRestIT.java b/elasticsearch/src/test/java/org/elasticsearch/xpack/test/rest/XPackRestIT.java index 72ccb89dfbc..ef8bb04e8f6 100644 --- a/elasticsearch/src/test/java/org/elasticsearch/xpack/test/rest/XPackRestIT.java +++ b/elasticsearch/src/test/java/org/elasticsearch/xpack/test/rest/XPackRestIT.java @@ -5,7 +5,19 @@ */ package org.elasticsearch.xpack.test.rest; +import org.apache.http.HttpStatus; import org.elasticsearch.test.rest.yaml.ClientYamlTestCandidate; +import org.elasticsearch.test.rest.yaml.ClientYamlTestResponse; +import org.elasticsearch.xpack.security.SecurityTemplateService; +import org.junit.Before; + +import java.io.IOException; +import java.util.Map; +import java.util.concurrent.atomic.AtomicReference; + +import static java.util.Collections.emptyList; +import static java.util.Collections.emptyMap; +import static java.util.Collections.singletonMap; /** Runs rest tests against external cluster */ public class XPackRestIT extends XPackRestTestCase { @@ -14,4 +26,32 @@ public class XPackRestIT extends XPackRestTestCase { super(testCandidate); } + /** + * Waits for the Security template to be created by the {@link SecurityTemplateService}. + */ + @Before + public void waitForSecurityTemplate() throws Exception { + String templateApi = "indices.exists_template"; + Map params = singletonMap("name", SecurityTemplateService.SECURITY_TEMPLATE_NAME); + + AtomicReference exceptionHolder = new AtomicReference<>(); + awaitBusy(() -> { + try { + ClientYamlTestResponse response = getAdminExecutionContext().callApi(templateApi, params, emptyList(), emptyMap()); + // We don't check the version of the template - it is the right one when testing documentation. + if (response.getStatusCode() == HttpStatus.SC_OK) { + exceptionHolder.set(null); + return true; + } + } catch (IOException e) { + exceptionHolder.set(e); + } + return false; + }); + + IOException exception = exceptionHolder.get(); + if (exception != null) { + throw new IllegalStateException("Exception when waiting for security template to be created", exception); + } + } }