[TESTS] Make DisabledFieldDataFormatTests less evil.

This commit is contained in:
Adrien Grand 2014-08-14 15:26:15 +02:00
parent a4f974dcaa
commit feefc41127
2 changed files with 33 additions and 44 deletions

View File

@ -23,32 +23,16 @@ import org.elasticsearch.action.search.SearchPhaseExecutionException;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.common.settings.ImmutableSettings;
import org.elasticsearch.common.xcontent.XContentFactory;
import org.elasticsearch.index.mapper.MapperService.SmartNameFieldMappers;
import org.elasticsearch.index.service.IndexService;
import org.elasticsearch.indices.IndicesService;
import org.elasticsearch.search.aggregations.AggregationBuilders;
import org.elasticsearch.search.aggregations.Aggregator.SubAggCollectionMode;
import org.elasticsearch.test.ElasticsearchIntegrationTest;
import org.elasticsearch.test.ElasticsearchIntegrationTest.ClusterScope;
import java.util.Set;
import java.util.concurrent.Callable;
import org.elasticsearch.test.ElasticsearchSingleNodeTest;
import static org.elasticsearch.test.hamcrest.ElasticsearchAssertions.*;
import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.notNullValue;
@ClusterScope(randomDynamicTemplates = false)
public class DisabledFieldDataFormatTests extends ElasticsearchIntegrationTest {
@Override
protected int numberOfReplicas() {
return 0;
}
public class DisabledFieldDataFormatTests extends ElasticsearchSingleNodeTest {
public void test() throws Exception {
prepareCreate("test").addMapping("type", "s", "type=string").execute().actionGet();
ensureGreen();
createIndex("test", ImmutableSettings.EMPTY, "type", "s", "type=string");
logger.info("indexing data start");
for (int i = 0; i < 10; ++i) {
client().prepareIndex("test", "type", Integer.toString(i)).setSource("s", "value" + i).execute().actionGet();
@ -57,7 +41,7 @@ public class DisabledFieldDataFormatTests extends ElasticsearchIntegrationTest {
final int searchCycles = 1;
refresh();
client().admin().indices().prepareRefresh().execute().actionGet();
// disable field data
updateFormat("disabled");
@ -97,7 +81,7 @@ public class DisabledFieldDataFormatTests extends ElasticsearchIntegrationTest {
// but add more docs and the new segment won't be loaded
client().prepareIndex("test", "type", "-1").setSource("s", "value").execute().actionGet();
refresh();
client().admin().indices().prepareRefresh().execute().actionGet();
for (int i = 0; i < searchCycles; i++) {
try {
resp = client().prepareSearch("test").setPreference(Integer.toString(i)).addAggregation(AggregationBuilders.terms("t").field("s")
@ -124,24 +108,6 @@ public class DisabledFieldDataFormatTests extends ElasticsearchIntegrationTest {
.endObject()
.endObject()).get());
logger.info(">> put mapping end {}", format);
assertBusy(new Callable<Object>() {
@Override
public Object call() throws Exception {
Set<String> nodes = internalCluster().nodesInclude("test");
assertFalse(nodes.isEmpty());
for (String node : nodes) {
IndicesService indicesService = internalCluster().getInstance(IndicesService.class, node);
IndexService indexService = indicesService.indexService("test");
assertThat(indexService, notNullValue());
final SmartNameFieldMappers mappers = indexService.mapperService().smartName("s");
assertThat(mappers, notNullValue());
assertTrue(mappers.hasMapper());
final String currentFormat = mappers.mapper().fieldDataType().getFormat(ImmutableSettings.EMPTY);
assertThat(currentFormat, equalTo(format));
}
return null;
}
});
}
}

View File

@ -23,6 +23,7 @@ import org.elasticsearch.action.admin.cluster.health.ClusterHealthStatus;
import org.elasticsearch.action.admin.indices.create.CreateIndexRequestBuilder;
import org.elasticsearch.cache.recycler.CacheRecycler;
import org.elasticsearch.cache.recycler.PageCacheRecycler;
import org.elasticsearch.client.Client;
import org.elasticsearch.client.Requests;
import org.elasticsearch.cluster.ClusterName;
import org.elasticsearch.cluster.metadata.IndexMetaData;
@ -60,8 +61,8 @@ public abstract class ElasticsearchSingleNodeTest extends ElasticsearchTestCase
}
static void cleanup() {
assertAcked(Holder.NODE.client().admin().indices().prepareDelete("*").get());
MetaData metaData = Holder.NODE.client().admin().cluster().prepareState().get().getState().getMetaData();
assertAcked(client().admin().indices().prepareDelete("*").get());
MetaData metaData = client().admin().cluster().prepareState().get().getState().getMetaData();
assertThat("test leaves persistent cluster metadata behind: " + metaData.persistentSettings().getAsMap(),
metaData.persistentSettings().getAsMap().size(), equalTo(0));
assertThat("test leaves transient cluster metadata behind: " + metaData.transientSettings().getAsMap(),
@ -89,6 +90,13 @@ public abstract class ElasticsearchSingleNodeTest extends ElasticsearchTestCase
return build;
}
/**
* Returns a client to the single-node cluster.
*/
public static Client client() {
return Holder.NODE.client();
}
/**
* Returns the single test nodes name.
*/
@ -128,21 +136,36 @@ public abstract class ElasticsearchSingleNodeTest extends ElasticsearchTestCase
* Create a new index on the singleton node with the provided index settings.
*/
protected static IndexService createIndex(String index, Settings settings) {
return createIndex(index, settings, null, null);
return createIndex(index, settings, null, (XContentBuilder) null);
}
/**
* Create a new index on the singleton node with the provided index settings.
*/
protected static IndexService createIndex(String index, Settings settings, String type, XContentBuilder mappings) {
CreateIndexRequestBuilder createIndexRequestBuilder = Holder.NODE.client().admin().indices().prepareCreate(index).setSettings(settings);
CreateIndexRequestBuilder createIndexRequestBuilder = client().admin().indices().prepareCreate(index).setSettings(settings);
if (type != null && mappings != null) {
createIndexRequestBuilder.addMapping(type, mappings);
}
return createIndex(index, createIndexRequestBuilder);
}
/**
* Create a new index on the singleton node with the provided index settings.
*/
protected static IndexService createIndex(String index, Settings settings, String type, Object... mappings) {
CreateIndexRequestBuilder createIndexRequestBuilder = client().admin().indices().prepareCreate(index).setSettings(settings);
if (type != null && mappings != null) {
createIndexRequestBuilder.addMapping(type, mappings);
}
return createIndex(index, createIndexRequestBuilder);
}
private static IndexService createIndex(String index, CreateIndexRequestBuilder createIndexRequestBuilder) {
assertAcked(createIndexRequestBuilder.get());
// Wait for the index to be allocated so that cluster state updates don't override
// changes that would have been done locally
ClusterHealthResponse health = Holder.NODE.client().admin().cluster()
ClusterHealthResponse health = client().admin().cluster()
.health(Requests.clusterHealthRequest(index).waitForYellowStatus().waitForEvents(Priority.LANGUID).waitForRelocatingShards(0)).actionGet();
assertThat(health.getStatus(), lessThanOrEqualTo(ClusterHealthStatus.YELLOW));
assertThat("Cluster must be a single node cluster", health.getNumberOfDataNodes(), equalTo(1));