Skip to content

Commit

Permalink
More test cleanups
Browse files Browse the repository at this point in the history
  • Loading branch information
s1monw committed Jun 5, 2013
1 parent dd0fb81 commit aa1a52d
Show file tree
Hide file tree
Showing 6 changed files with 40 additions and 74 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
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;
Expand Down Expand Up @@ -64,6 +65,10 @@ public static void assertSearchHit(SearchResponse searchResponse, int number, Ma
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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -30,6 +31,9 @@
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;
Expand Down Expand Up @@ -234,6 +238,7 @@ public ClusterHealthStatus ensureGreen() {
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();
}

Expand All @@ -257,13 +262,17 @@ protected void index(String index, String type, XContentBuilder source) {
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) {
Expand All @@ -286,5 +295,10 @@ protected int numAssignedShards(String... indices) {
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();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,12 @@
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());

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -45,9 +42,4 @@ protected void createIndex() {
protected String getConcreteIndexName() {
return "test1";
}

@Override
protected Settings nodeSettings() {
return ImmutableSettings.settingsBuilder().put("action.auto_create_index", false).build();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
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;
Expand All @@ -48,6 +49,7 @@

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;
Expand All @@ -73,61 +75,42 @@ public static void beforeClass() throws Exception {
}

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());
}


protected String getConcreteIndexName() {
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;

Expand Down

This file was deleted.

0 comments on commit aa1a52d

Please sign in to comment.