[TEST] Wait for all ML index template before ML security tests (elastic/x-pack-elasticsearch#1148)
Previously we were only waiting for the results index template Original commit: elastic/x-pack-elasticsearch@3a9e6ab82f
This commit is contained in:
parent
fc316bd947
commit
5b19b8e7f0
|
@ -9,6 +9,7 @@ import com.carrotsearch.randomizedtesting.annotations.Name;
|
|||
import com.carrotsearch.randomizedtesting.annotations.ParametersFactory;
|
||||
|
||||
import org.apache.http.HttpStatus;
|
||||
import org.elasticsearch.common.CheckedFunction;
|
||||
import org.elasticsearch.common.settings.SecureString;
|
||||
import org.elasticsearch.common.settings.Settings;
|
||||
import org.elasticsearch.common.util.concurrent.ThreadContext;
|
||||
|
@ -17,16 +18,18 @@ import org.elasticsearch.test.rest.yaml.ClientYamlTestResponse;
|
|||
import org.elasticsearch.test.rest.yaml.ESClientYamlSuiteTestCase;
|
||||
import org.elasticsearch.xpack.ml.MachineLearningTemplateRegistry;
|
||||
import org.elasticsearch.xpack.ml.integration.MlRestTestStateCleaner;
|
||||
import org.elasticsearch.xpack.ml.job.persistence.AnomalyDetectorsIndex;
|
||||
import org.elasticsearch.xpack.security.SecurityLifecycleService;
|
||||
import org.junit.After;
|
||||
import org.junit.Before;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.concurrent.atomic.AtomicReference;
|
||||
import java.util.function.Supplier;
|
||||
|
||||
import static org.elasticsearch.xpack.security.authc.support.UsernamePasswordToken.basicAuthHeaderValue;
|
||||
|
||||
|
@ -46,40 +49,60 @@ public class MlWithSecurityIT extends ESClientYamlSuiteTestCase {
|
|||
}
|
||||
|
||||
/**
|
||||
* Waits for the Security and .ml-anomalies templates to be created by the {@link SecurityLifecycleService}
|
||||
* and {@link MachineLearningTemplateRegistry}.
|
||||
* Waits for the Security template to be created by the {@link SecurityLifecycleService} and
|
||||
* the Machine Learning templates to be created by {@link MachineLearningTemplateRegistry}
|
||||
*/
|
||||
@Before
|
||||
public void waitForIndexTemplates() throws Exception {
|
||||
String templateApi = "indices.exists_template";
|
||||
Map<String, String> securityParams = Collections.singletonMap("name", SecurityLifecycleService.SECURITY_TEMPLATE_NAME);
|
||||
Map<String, String> anomaliesParams = Collections.singletonMap("name", AnomalyDetectorsIndex.jobResultsIndexPrefix());
|
||||
Map<String, String> headers = Collections.singletonMap("Authorization",
|
||||
basicAuthHeaderValue(TEST_ADMIN_USERNAME, new SecureString(TEST_ADMIN_PASSWORD.toCharArray())));
|
||||
public void waitForTemplates() throws Exception {
|
||||
List<String> templates = new ArrayList<>();
|
||||
templates.add(SecurityLifecycleService.SECURITY_TEMPLATE_NAME);
|
||||
templates.addAll(Arrays.asList(MachineLearningTemplateRegistry.TEMPLATE_NAMES));
|
||||
|
||||
for (Map<String, String> params : Arrays.asList(securityParams, anomaliesParams)) {
|
||||
AtomicReference<IOException> exceptionHolder = new AtomicReference<>();
|
||||
awaitBusy(() -> {
|
||||
try {
|
||||
ClientYamlTestResponse response = getAdminExecutionContext().callApi(templateApi, params, Collections.emptyList(),
|
||||
headers);
|
||||
if (response.getStatusCode() == HttpStatus.SC_OK) {
|
||||
exceptionHolder.set(null);
|
||||
return true;
|
||||
}
|
||||
} catch (IOException e) {
|
||||
exceptionHolder.set(e);
|
||||
for (String template : templates) {
|
||||
awaitCallApi("indices.exists_template", Collections.singletonMap("name", template), Collections.emptyList(),
|
||||
response -> true,
|
||||
() -> "Exception when waiting for [" + template + "] template to be created");
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Executes an API call using the admin context, waiting for it to succeed.
|
||||
*/
|
||||
private void awaitCallApi(String apiName,
|
||||
Map<String, String> params,
|
||||
List<Map<String, Object>> bodies,
|
||||
CheckedFunction<ClientYamlTestResponse, Boolean, IOException> success,
|
||||
Supplier<String> error) throws Exception {
|
||||
|
||||
AtomicReference<IOException> exceptionHolder = new AtomicReference<>();
|
||||
awaitBusy(() -> {
|
||||
try {
|
||||
ClientYamlTestResponse response = callApi(apiName, params, bodies);
|
||||
if (response.getStatusCode() == HttpStatus.SC_OK) {
|
||||
exceptionHolder.set(null);
|
||||
return success.apply(response);
|
||||
}
|
||||
return false;
|
||||
});
|
||||
|
||||
IOException exception = exceptionHolder.get();
|
||||
if (exception != null) {
|
||||
throw new IllegalStateException("Exception when waiting for index template to be created", exception);
|
||||
} catch (IOException e) {
|
||||
exceptionHolder.set(e);
|
||||
}
|
||||
return false;
|
||||
});
|
||||
|
||||
IOException exception = exceptionHolder.get();
|
||||
if (exception != null) {
|
||||
throw new IllegalStateException(error.get(), exception);
|
||||
}
|
||||
}
|
||||
|
||||
private ClientYamlTestResponse callApi(String apiName,
|
||||
Map<String, String> params,
|
||||
List<Map<String, Object>> bodies) throws IOException {
|
||||
Map<String, String> headers = Collections.singletonMap("Authorization",
|
||||
basicAuthHeaderValue(TEST_ADMIN_USERNAME, new SecureString(TEST_ADMIN_PASSWORD.toCharArray())));
|
||||
return getAdminExecutionContext().callApi(apiName, params, bodies, headers);
|
||||
}
|
||||
|
||||
@ParametersFactory
|
||||
public static Iterable<Object[]> parameters() throws Exception {
|
||||
return ESClientYamlSuiteTestCase.createParameters();
|
||||
|
|
Loading…
Reference in New Issue