Cut over NoMasterNodeTests to AbstractIntegrationTest

This commit is contained in:
Simon Willnauer 2013-09-20 16:04:23 +02:00
parent 1a34172a6c
commit e97aa9918a
2 changed files with 38 additions and 42 deletions

View File

@ -26,10 +26,10 @@ import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.unit.TimeValue;
import org.elasticsearch.common.xcontent.XContentFactory;
import org.elasticsearch.discovery.Discovery;
import org.elasticsearch.node.Node;
import org.elasticsearch.rest.RestStatus;
import org.elasticsearch.test.AbstractNodesTests;
import org.junit.After;
import org.elasticsearch.test.AbstractIntegrationTest;
import org.elasticsearch.test.AbstractIntegrationTest.ClusterScope;
import org.elasticsearch.test.AbstractIntegrationTest.Scope;
import org.junit.Test;
import java.util.HashMap;
@ -40,12 +40,8 @@ import static org.hamcrest.Matchers.greaterThan;
/**
*/
public class NoMasterNodeTests extends AbstractNodesTests {
@After
public void cleanAndCloseNodes() throws Exception {
closeAllNodes();
}
@ClusterScope(scope=Scope.TEST, numNodes=0)
public class NoMasterNodeTests extends AbstractIntegrationTest {
@Test
public void testNoMasterActions() throws Exception {
@ -60,32 +56,30 @@ public class NoMasterNodeTests extends AbstractNodesTests {
TimeValue timeout = TimeValue.timeValueMillis(200);
final Node node = startNode("node1", settings);
cluster().startNode(settings);
// start a second node, create an index, and then shut it down so we have no master block
Node node2 = startNode("node2", settings);
node.client().admin().indices().prepareCreate("test").execute().actionGet();
node.client().admin().cluster().prepareHealth("test").setWaitForGreenStatus().execute().actionGet();
node2.close();
awaitBusy(new Predicate<Object>() {
cluster().startNode(settings);
client().admin().indices().prepareCreate("test").execute().actionGet();
client().admin().cluster().prepareHealth("test").setWaitForGreenStatus().execute().actionGet();
cluster().stopRandomNode();
assertThat(awaitBusy(new Predicate<Object>() {
public boolean apply(Object o) {
ClusterState state = node.client().admin().cluster().prepareState().setLocal(true).execute().actionGet().getState();
ClusterState state = client().admin().cluster().prepareState().setLocal(true).execute().actionGet().getState();
return state.blocks().hasGlobalBlock(Discovery.NO_MASTER_BLOCK);
}
});
}), equalTo(true));
ClusterState state = node.client().admin().cluster().prepareState().setLocal(true).execute().actionGet().getState();
assertThat(state.blocks().hasGlobalBlock(Discovery.NO_MASTER_BLOCK), equalTo(true));
try {
node.client().prepareGet("test", "type1", "1").execute().actionGet();
assert false;
client().prepareGet("test", "type1", "1").execute().actionGet();
fail("Expected ClusterBlockException");
} catch (ClusterBlockException e) {
assertThat(e.status(), equalTo(RestStatus.SERVICE_UNAVAILABLE));
}
try {
node.client().prepareMultiGet().add("test", "type1", "1").execute().actionGet();
assert false;
client().prepareMultiGet().add("test", "type1", "1").execute().actionGet();
fail("Expected ClusterBlockException");
} catch (ClusterBlockException e) {
assertThat(e.status(), equalTo(RestStatus.SERVICE_UNAVAILABLE));
}
@ -93,41 +87,41 @@ public class NoMasterNodeTests extends AbstractNodesTests {
try {
PercolateSourceBuilder percolateSource = new PercolateSourceBuilder();
percolateSource.percolateDocument().setDoc(new HashMap());
node.client().preparePercolate()
client().preparePercolate()
.setIndices("test").setDocumentType("type1")
.setSource(percolateSource).execute().actionGet();
assert false;
fail("Expected ClusterBlockException");
} catch (ClusterBlockException e) {
assertThat(e.status(), equalTo(RestStatus.SERVICE_UNAVAILABLE));
}
long now = System.currentTimeMillis();
try {
node.client().prepareUpdate("test", "type1", "1").setScript("test script").setTimeout(timeout).execute().actionGet();
assert false;
client().prepareUpdate("test", "type1", "1").setScript("test script").setTimeout(timeout).execute().actionGet();
fail("Expected ClusterBlockException");
} catch (ClusterBlockException e) {
assertThat(System.currentTimeMillis() - now, greaterThan(timeout.millis() - 50));
assertThat(e.status(), equalTo(RestStatus.SERVICE_UNAVAILABLE));
}
try {
node.client().admin().indices().prepareAnalyze("test", "this is a test").execute().actionGet();
assert false;
client().admin().indices().prepareAnalyze("test", "this is a test").execute().actionGet();
fail("Expected ClusterBlockException");
} catch (ClusterBlockException e) {
assertThat(e.status(), equalTo(RestStatus.SERVICE_UNAVAILABLE));
}
try {
node.client().prepareCount("test").execute().actionGet();
assert false;
client().prepareCount("test").execute().actionGet();
fail("Expected ClusterBlockException");
} catch (ClusterBlockException e) {
assertThat(e.status(), equalTo(RestStatus.SERVICE_UNAVAILABLE));
}
now = System.currentTimeMillis();
try {
node.client().prepareIndex("test", "type1", "1").setSource(XContentFactory.jsonBuilder().startObject().endObject()).setTimeout(timeout).execute().actionGet();
assert false;
client().prepareIndex("test", "type1", "1").setSource(XContentFactory.jsonBuilder().startObject().endObject()).setTimeout(timeout).execute().actionGet();
fail("Expected ClusterBlockException");
} catch (ClusterBlockException e) {
assertThat(System.currentTimeMillis() - now, greaterThan(timeout.millis() - 50));
assertThat(e.status(), equalTo(RestStatus.SERVICE_UNAVAILABLE));

View File

@ -155,18 +155,20 @@ public abstract class AbstractIntegrationTest extends ElasticSearchTestCase {
public void after() throws IOException {
try {
logger.info("[{}#{}]: cleaning up after test", getTestClass().getSimpleName(), getTestName());
Scope currentClusterScope = getCurrentClusterScope();
if (currentClusterScope == Scope.TEST) {
clearClusters(); // it is ok to leave persistent / transient cluster state behind if scope is TEST
} else {
MetaData metaData = client().admin().cluster().prepareState().execute().actionGet().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(), metaData
.persistentSettings().getAsMap().size(), equalTo(0));
}
wipeIndices(); // wipe after to make sure we fail in the test that
// didn't ack the delete
wipeTemplates();
Scope currentClusterScope = getCurrentClusterScope();
if (currentClusterScope == Scope.TEST) {
clearClusters();
}
ensureAllSearchersClosed();
ensureAllFilesClosed();
logger.info("[{}#{}]: cleaned up after test", getTestClass().getSimpleName(), getTestName());