1
0
Эх сурвалжийг харах

tests: Add to ability for a integration test to prevent specific templates from being wiped between tests.

Martijn van Groningen 10 жил өмнө
parent
commit
1a8495d1d6

+ 9 - 2
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<String> excludeTemplates() {
+        return Collections.emptySet();
+    }
+
     protected void beforeIndexDeletion() {
         cluster().beforeIndexDeletion();
     }

+ 25 - 2
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<Client>, 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<String> excludeTemplates) {
         wipeIndices("_all");
         wipeTemplates();
+        wipeAllTemplates(excludeTemplates);
         wipeRepositories();
     }
 
@@ -160,6 +164,25 @@ public abstract class TestCluster implements Iterable<Client>, Closeable {
         }
     }
 
+    /**
+     * Removes all templates, except the templates defined in the exclude
+     */
+    public void wipeAllTemplates(Set<String> 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.

+ 2 - 1
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.<String>emptySet());
             } finally {
                 cluster2.afterTest();
             }