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

This commit is contained in:
Martijn van Groningen 2015-09-17 12:34:35 +02:00
parent 5b0ad2272e
commit 1a8495d1d6
3 changed files with 36 additions and 5 deletions

View File

@ -319,7 +319,7 @@ public abstract class ESIntegTestCase extends ESTestCase {
fail("Unknown Scope: [" + currentClusterScope + "]"); fail("Unknown Scope: [" + currentClusterScope + "]");
} }
cluster().beforeTest(getRandom(), getPerTestTransportClientRatio()); cluster().beforeTest(getRandom(), getPerTestTransportClientRatio());
cluster().wipe(); cluster().wipe(excludeTemplates());
randomIndexTemplate(); randomIndexTemplate();
} catch (OutOfMemoryError e) { } catch (OutOfMemoryError e) {
if (e.getMessage().contains("unable to create new native thread")) { if (e.getMessage().contains("unable to create new native thread")) {
@ -597,7 +597,7 @@ public abstract class ESIntegTestCase extends ESTestCase {
} }
} }
beforeIndexDeletion(); 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) { if (afterClass || currentClusterScope == Scope.TEST) {
cluster().close(); 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() { protected void beforeIndexDeletion() {
cluster().beforeIndexDeletion(); cluster().beforeIndexDeletion();
} }

View File

@ -21,8 +21,10 @@ package org.elasticsearch.test;
import com.carrotsearch.hppc.ObjectArrayList; import com.carrotsearch.hppc.ObjectArrayList;
import org.elasticsearch.action.admin.cluster.state.ClusterStateResponse; 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.client.Client;
import org.elasticsearch.cluster.metadata.IndexMetaData; import org.elasticsearch.cluster.metadata.IndexMetaData;
import org.elasticsearch.cluster.metadata.IndexTemplateMetaData;
import org.elasticsearch.common.logging.ESLogger; import org.elasticsearch.common.logging.ESLogger;
import org.elasticsearch.common.logging.Loggers; import org.elasticsearch.common.logging.Loggers;
import org.elasticsearch.index.IndexNotFoundException; import org.elasticsearch.index.IndexNotFoundException;
@ -33,6 +35,7 @@ import java.io.Closeable;
import java.io.IOException; import java.io.IOException;
import java.net.InetSocketAddress; import java.net.InetSocketAddress;
import java.util.Random; import java.util.Random;
import java.util.Set;
import static org.elasticsearch.test.hamcrest.ElasticsearchAssertions.*; 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"); wipeIndices("_all");
wipeTemplates(); wipeTemplates();
wipeAllTemplates(excludeTemplates);
wipeRepositories(); 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. * Deletes index templates, support wildcard notation.
* If no template name is passed to this method all templates are removed. * If no template name is passed to this method all templates are removed.

View File

@ -49,6 +49,7 @@ import org.junit.Test;
import java.io.IOException; import java.io.IOException;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Collections;
import java.util.Map; import java.util.Map;
import static org.elasticsearch.test.hamcrest.ElasticsearchAssertions.*; import static org.elasticsearch.test.hamcrest.ElasticsearchAssertions.*;
@ -94,7 +95,7 @@ public class TribeIT extends ESIntegTestCase {
public void tearDownTribeNode() throws IOException { public void tearDownTribeNode() throws IOException {
if (cluster2 != null) { if (cluster2 != null) {
try { try {
cluster2.wipe(); cluster2.wipe(Collections.<String>emptySet());
} finally { } finally {
cluster2.afterTest(); cluster2.afterTest();
} }