From 1a8495d1d66df0b53e7aaa001f6236a46473dfd9 Mon Sep 17 00:00:00 2001 From: Martijn van Groningen Date: Thu, 17 Sep 2015 12:34:35 +0200 Subject: [PATCH] tests: Add to ability for a integration test to prevent specific templates from being wiped between tests. --- .../elasticsearch/test/ESIntegTestCase.java | 11 ++++++-- .../org/elasticsearch/test/TestCluster.java | 27 +++++++++++++++++-- .../java/org/elasticsearch/tribe/TribeIT.java | 3 ++- 3 files changed, 36 insertions(+), 5 deletions(-) diff --git a/core/src/test/java/org/elasticsearch/test/ESIntegTestCase.java b/core/src/test/java/org/elasticsearch/test/ESIntegTestCase.java index f561dd6df64..4d14f49cb73 100644 --- a/core/src/test/java/org/elasticsearch/test/ESIntegTestCase.java +++ b/core/src/test/java/org/elasticsearch/test/ESIntegTestCase.java @@ -319,7 +319,7 @@ public abstract class ESIntegTestCase extends ESTestCase { fail("Unknown Scope: [" + currentClusterScope + "]"); } cluster().beforeTest(getRandom(), getPerTestTransportClientRatio()); - cluster().wipe(); + cluster().wipe(excludeTemplates()); randomIndexTemplate(); } catch (OutOfMemoryError e) { if (e.getMessage().contains("unable to create new native thread")) { @@ -597,7 +597,7 @@ public abstract class ESIntegTestCase extends ESTestCase { } } beforeIndexDeletion(); - cluster().wipe(); // wipe after to make sure we fail in the test that didn't ack the delete + cluster().wipe(excludeTemplates()); // wipe after to make sure we fail in the test that didn't ack the delete if (afterClass || currentClusterScope == Scope.TEST) { cluster().close(); } @@ -618,6 +618,13 @@ public abstract class ESIntegTestCase extends ESTestCase { } } + /** + * @return An exclude set of index templates that will not be removed in between tests. + */ + protected Set excludeTemplates() { + return Collections.emptySet(); + } + protected void beforeIndexDeletion() { cluster().beforeIndexDeletion(); } diff --git a/core/src/test/java/org/elasticsearch/test/TestCluster.java b/core/src/test/java/org/elasticsearch/test/TestCluster.java index 4f95fc99447..f998dbc2a5f 100644 --- a/core/src/test/java/org/elasticsearch/test/TestCluster.java +++ b/core/src/test/java/org/elasticsearch/test/TestCluster.java @@ -21,8 +21,10 @@ package org.elasticsearch.test; import com.carrotsearch.hppc.ObjectArrayList; import org.elasticsearch.action.admin.cluster.state.ClusterStateResponse; +import org.elasticsearch.action.admin.indices.template.get.GetIndexTemplatesResponse; import org.elasticsearch.client.Client; import org.elasticsearch.cluster.metadata.IndexMetaData; +import org.elasticsearch.cluster.metadata.IndexTemplateMetaData; import org.elasticsearch.common.logging.ESLogger; import org.elasticsearch.common.logging.Loggers; import org.elasticsearch.index.IndexNotFoundException; @@ -33,6 +35,7 @@ import java.io.Closeable; import java.io.IOException; import java.net.InetSocketAddress; import java.util.Random; +import java.util.Set; import static org.elasticsearch.test.hamcrest.ElasticsearchAssertions.*; @@ -68,11 +71,12 @@ public abstract class TestCluster implements Iterable, Closeable { } /** - * Wipes any data that a test can leave behind: indices, templates and repositories + * Wipes any data that a test can leave behind: indices, templates (except exclude templates) and repositories */ - public void wipe() { + public void wipe(Set excludeTemplates) { wipeIndices("_all"); wipeTemplates(); + wipeAllTemplates(excludeTemplates); wipeRepositories(); } @@ -160,6 +164,25 @@ public abstract class TestCluster implements Iterable, Closeable { } } + /** + * Removes all templates, except the templates defined in the exclude + */ + public void wipeAllTemplates(Set exclude) { + if (size() > 0) { + GetIndexTemplatesResponse response = client().admin().indices().prepareGetTemplates().get(); + for (IndexTemplateMetaData indexTemplate : response.getIndexTemplates()) { + if (exclude.contains(indexTemplate.getName())) { + continue; + } + try { + client().admin().indices().prepareDeleteTemplate(indexTemplate.getName()).execute().actionGet(); + } catch (IndexTemplateMissingException e) { + // ignore + } + } + } + } + /** * Deletes index templates, support wildcard notation. * If no template name is passed to this method all templates are removed. diff --git a/core/src/test/java/org/elasticsearch/tribe/TribeIT.java b/core/src/test/java/org/elasticsearch/tribe/TribeIT.java index eb48d65fed4..25befab1ff6 100644 --- a/core/src/test/java/org/elasticsearch/tribe/TribeIT.java +++ b/core/src/test/java/org/elasticsearch/tribe/TribeIT.java @@ -49,6 +49,7 @@ import org.junit.Test; import java.io.IOException; import java.util.ArrayList; +import java.util.Collections; import java.util.Map; import static org.elasticsearch.test.hamcrest.ElasticsearchAssertions.*; @@ -94,7 +95,7 @@ public class TribeIT extends ESIntegTestCase { public void tearDownTribeNode() throws IOException { if (cluster2 != null) { try { - cluster2.wipe(); + cluster2.wipe(Collections.emptySet()); } finally { cluster2.afterTest(); }