More test cleanups

This commit is contained in:
Simon Willnauer 2013-06-05 15:45:03 +02:00
parent 23ad8401d0
commit 56dfa96851
6 changed files with 40 additions and 74 deletions

View File

@ -26,6 +26,7 @@ import java.util.Arrays;
import org.apache.lucene.search.BooleanQuery;
import org.apache.lucene.search.Query;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.action.support.broadcast.BroadcastOperationResponse;
import org.elasticsearch.search.SearchHit;
import org.elasticsearch.search.suggest.Suggest;
import org.hamcrest.Matcher;
@ -64,6 +65,10 @@ public class ElasticsearchAssertions {
public static void assertNoFailures(SearchResponse searchResponse) {
assertThat("Unexpectd ShardFailures: " + Arrays.toString(searchResponse.getShardFailures()), searchResponse.getShardFailures().length, equalTo(0));
}
public static void assertNoFailures(BroadcastOperationResponse response) {
assertThat("Unexpectd ShardFailures: " + Arrays.toString(response.getShardFailures()), response.getFailedShards(), equalTo(0));
}
public static void assertSearchHit(SearchHit searchHit, Matcher<SearchHit> matcher) {
assertThat(searchHit, matcher);

View File

@ -19,6 +19,7 @@
package org.elasticsearch.test.integration;
import static org.elasticsearch.common.xcontent.XContentFactory.jsonBuilder;
import static org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertNoFailures;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.equalTo;
@ -30,6 +31,9 @@ import java.util.Set;
import org.elasticsearch.action.admin.cluster.health.ClusterHealthResponse;
import org.elasticsearch.action.admin.cluster.health.ClusterHealthStatus;
import org.elasticsearch.action.admin.indices.create.CreateIndexRequestBuilder;
import org.elasticsearch.action.admin.indices.exists.indices.IndicesExistsResponse;
import org.elasticsearch.action.admin.indices.optimize.OptimizeResponse;
import org.elasticsearch.action.admin.indices.refresh.RefreshResponse;
import org.elasticsearch.client.Client;
import org.elasticsearch.client.Requests;
import org.elasticsearch.cluster.ClusterService;
@ -234,6 +238,7 @@ public abstract class AbstractSharedClusterTest extends ElasticsearchTestCase {
ClusterHealthResponse actionGet = client().admin().cluster()
.health(Requests.clusterHealthRequest().waitForGreenStatus().waitForEvents(Priority.LANGUID)).actionGet();
assertThat(actionGet.isTimedOut(), equalTo(false));
assertThat(actionGet.getStatus(), equalTo(ClusterHealthStatus.GREEN));
return actionGet.getStatus();
}
@ -257,13 +262,17 @@ public abstract class AbstractSharedClusterTest extends ElasticsearchTestCase {
client().prepareIndex(index, type).setSource(source).execute().actionGet();
}
protected void refresh() {
protected RefreshResponse refresh() {
// TODO RANDOMIZE with flush?
client().admin().indices().prepareRefresh().execute().actionGet();
RefreshResponse actionGet = client().admin().indices().prepareRefresh().execute().actionGet();
assertNoFailures(actionGet);
return actionGet;
}
protected void optimize() {
client().admin().indices().prepareOptimize().execute().actionGet();
protected OptimizeResponse optimize() {
OptimizeResponse actionGet = client().admin().indices().prepareOptimize().execute().actionGet();
assertNoFailures(actionGet);
return actionGet;
}
protected Set<String> nodeIdsWithIndex(String... indices) {
@ -286,5 +295,10 @@ public abstract class AbstractSharedClusterTest extends ElasticsearchTestCase {
GroupShardsIterator allAssignedShardsGrouped = state.routingTable().allAssignedShardsGrouped(indices, true);
return allAssignedShardsGrouped.size();
}
protected boolean indexExists(String index) {
IndicesExistsResponse actionGet = client().admin().indices().prepareExists("test1234565").execute().actionGet();
return actionGet.isExists();
}
}

View File

@ -64,6 +64,12 @@ import com.google.common.collect.Iterators;
import com.google.common.collect.Sets;
public class TestCluster {
/* some random options to consider
* "action.auto_create_index"
* "node.local"
*/
protected final ESLogger logger = Loggers.getLogger(getClass());

View File

@ -19,9 +19,6 @@
package org.elasticsearch.test.integration.document;
import org.elasticsearch.common.settings.ImmutableSettings;
import org.elasticsearch.common.settings.Settings;
import static org.elasticsearch.client.Requests.createIndexRequest;
import static org.elasticsearch.common.settings.ImmutableSettings.settingsBuilder;
@ -45,9 +42,4 @@ public class AliasedIndexDocumentActionsTests extends DocumentActionsTests {
protected String getConcreteIndexName() {
return "test1";
}
@Override
protected Settings nodeSettings() {
return ImmutableSettings.settingsBuilder().put("action.auto_create_index", false).build();
}
}

View File

@ -39,6 +39,7 @@ import org.elasticsearch.common.settings.ImmutableSettings;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.xcontent.XContentBuilder;
import org.elasticsearch.common.xcontent.XContentFactory;
import org.elasticsearch.test.integration.AbstractSharedClusterTest;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Test;
@ -48,6 +49,7 @@ import java.util.Map;
import static org.elasticsearch.client.Requests.*;
import static org.elasticsearch.index.query.QueryBuilders.termQuery;
import static org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertNoFailures;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.nullValue;
@ -73,13 +75,8 @@ public class DocumentActionsTests extends AbstractSharedClusterTest {
}
protected void createIndex() {
try {
client().admin().indices().prepareDelete("test").execute().actionGet();
} catch (Exception e) {
// ignore
}
logger.info("--> creating index test");
client().admin().indices().create(createIndexRequest("test")).actionGet();
wipeIndex(getConcreteIndexName());
createIndex(getConcreteIndexName());
}
@ -87,47 +84,33 @@ public class DocumentActionsTests extends AbstractSharedClusterTest {
return "test";
}
protected Settings nodeSettings() {
return ImmutableSettings.Builder.EMPTY_SETTINGS;
}
@Test
public void testIndexActions() throws Exception {
createIndex();
logger.info("Running Cluster Health");
ClusterHealthResponse clusterHealth = client().admin().cluster().health(clusterHealthRequest().waitForGreenStatus()).actionGet();
logger.info("Done Cluster Health, status " + clusterHealth.getStatus());
assertThat(clusterHealth.isTimedOut(), equalTo(false));
assertThat(clusterHealth.getStatus(), equalTo(ClusterHealthStatus.GREEN));
ensureGreen();
logger.info("Indexing [type1/1]");
IndexResponse indexResponse = client().prepareIndex().setIndex("test").setType("type1").setId("1").setSource(source("1", "test")).setRefresh(true).execute().actionGet();
assertThat(indexResponse.getIndex(), equalTo(getConcreteIndexName()));
assertThat(indexResponse.getId(), equalTo("1"));
assertThat(indexResponse.getType(), equalTo("type1"));
logger.info("Refreshing");
RefreshResponse refreshResponse = client().admin().indices().prepareRefresh("test").execute().actionGet();
RefreshResponse refreshResponse = refresh();
assertThat(refreshResponse.getSuccessfulShards(), equalTo(10));
assertThat(refreshResponse.getFailedShards(), equalTo(0));
logger.info("--> index exists?");
IndicesExistsResponse indicesExistsResponse = client().admin().indices().prepareExists(getConcreteIndexName()).execute().actionGet();
assertThat(indicesExistsResponse.isExists(), equalTo(true));
assertThat(indexExists(getConcreteIndexName()), equalTo(false));
logger.info("--> index exists?, fake index");
indicesExistsResponse = client().admin().indices().prepareExists("test1234565").execute().actionGet();
assertThat(indicesExistsResponse.isExists(), equalTo(false));
assertThat(indexExists("test1234565"), equalTo(false));
logger.info("Clearing cache");
ClearIndicesCacheResponse clearIndicesCacheResponse = client().admin().indices().clearCache(clearIndicesCacheRequest("test").recycler(true).fieldDataCache(true).filterCache(true).idCache(true)).actionGet();
assertNoFailures(clearIndicesCacheResponse);
assertThat(clearIndicesCacheResponse.getSuccessfulShards(), equalTo(10));
assertThat(clearIndicesCacheResponse.getFailedShards(), equalTo(0));
logger.info("Optimizing");
OptimizeResponse optimizeResponse = client().admin().indices().prepareOptimize("test").execute().actionGet();
OptimizeResponse optimizeResponse = optimize();
assertThat(optimizeResponse.getSuccessfulShards(), equalTo(10));
assertThat(optimizeResponse.getFailedShards(), equalTo(0));
GetResponse getResult;

View File

@ -1,34 +0,0 @@
/*
* Licensed to ElasticSearch and Shay Banon under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. ElasticSearch licenses this
* file to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package org.elasticsearch.test.integration.document;
import org.elasticsearch.common.settings.ImmutableSettings;
import org.elasticsearch.common.settings.Settings;
/**
*
*/
public class LocalDocumentActionsTests extends DocumentActionsTests {
@Override
protected Settings nodeSettings() {
return ImmutableSettings.settingsBuilder().put("node.local", true).build();
}
}