Skip to content

Commit 4c24e41

Browse files
committed
Test: Do not remove xpack templates when cleaning (#31642)
At the end of every `ESRestTestCase` we clean the cluster which includes deleting all of the templates. If xpack is installed it'll automatically recreate a few templates every time they are removed. Which is slow. This change stops the cleanup from removing the xpack templates. It cuts the time to run the docs tests more than in half and it probably saves a bit more time on other tests as well.
1 parent ab534e7 commit 4c24e41

File tree

1 file changed

+51
-2
lines changed

1 file changed

+51
-2
lines changed

test/framework/src/main/java/org/elasticsearch/test/rest/ESRestTestCase.java

Lines changed: 51 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
import org.apache.http.message.BasicHeader;
3131
import org.apache.http.nio.conn.ssl.SSLIOSessionStrategy;
3232
import org.apache.http.ssl.SSLContexts;
33+
import org.apache.http.util.EntityUtils;
3334
import org.elasticsearch.action.admin.cluster.node.tasks.list.ListTasksAction;
3435
import org.elasticsearch.client.Request;
3536
import org.elasticsearch.client.Response;
@@ -259,7 +260,7 @@ private void wipeCluster() throws IOException {
259260
if (preserveIndicesUponCompletion() == false) {
260261
// wipe indices
261262
try {
262-
adminClient().performRequest("DELETE", "*");
263+
adminClient().performRequest(new Request("DELETE", "*"));
263264
} catch (ResponseException e) {
264265
// 404 here just means we had no indexes
265266
if (e.getResponse().getStatusLine().getStatusCode() != 404) {
@@ -270,7 +271,30 @@ private void wipeCluster() throws IOException {
270271

271272
// wipe index templates
272273
if (preserveTemplatesUponCompletion() == false) {
273-
adminClient().performRequest("DELETE", "_template/*");
274+
if (hasXPack()) {
275+
/*
276+
* Delete only templates that xpack doesn't automatically
277+
* recreate. Deleting them doesn't hurt anything, but it
278+
* slows down the test because xpack will just recreate
279+
* them.
280+
*/
281+
Request request = new Request("GET", "_cat/templates");
282+
request.addParameter("h", "name");
283+
String templates = EntityUtils.toString(adminClient().performRequest(request).getEntity());
284+
if (false == "".equals(templates)) {
285+
for (String template : templates.split("\n")) {
286+
if (isXPackTemplate(template)) continue;
287+
if ("".equals(template)) {
288+
throw new IllegalStateException("empty template in templates list:\n" + templates);
289+
}
290+
logger.debug("Clearing template [{}]", template);
291+
adminClient().performRequest(new Request("DELETE", "_template/" + template));
292+
}
293+
}
294+
} else {
295+
logger.debug("Clearing all templates");
296+
adminClient().performRequest(new Request("DELETE", "_template/*"));
297+
}
274298
}
275299

276300
wipeSnapshots();
@@ -585,4 +609,29 @@ protected static Map<String, Object> getAsMap(final String endpoint) throws IOEx
585609
assertNotNull(responseEntity);
586610
return responseEntity;
587611
}
612+
613+
/**
614+
* Is this template one that is automatically created by xpack?
615+
*/
616+
private static boolean isXPackTemplate(String name) {
617+
if (name.startsWith(".monitoring-")) {
618+
return true;
619+
}
620+
if (name.startsWith(".watch-history-")) {
621+
return true;
622+
}
623+
if (name.startsWith(".ml-")) {
624+
return true;
625+
}
626+
switch (name) {
627+
case ".triggered_watches":
628+
case ".watches":
629+
case "logstash-index-template":
630+
case "security_audit_log":
631+
return true;
632+
default:
633+
return false;
634+
}
635+
}
636+
588637
}

0 commit comments

Comments
 (0)