Changed SimpleBlocksTests and CustomHighlighterSearchTests to use AbstractIntegrationTest

This commit is contained in:
Alexander Reelsen 2013-09-20 15:24:38 +02:00
parent be2acaa629
commit 1a34172a6c
2 changed files with 66 additions and 86 deletions

View File

@ -19,21 +19,16 @@
package org.elasticsearch.blocks; package org.elasticsearch.blocks;
import org.elasticsearch.action.admin.cluster.settings.ClusterUpdateSettingsRequestBuilder;
import org.elasticsearch.action.admin.cluster.settings.ClusterUpdateSettingsResponse;
import org.elasticsearch.action.admin.indices.create.CreateIndexResponse; import org.elasticsearch.action.admin.indices.create.CreateIndexResponse;
import org.elasticsearch.action.admin.indices.exists.indices.IndicesExistsResponse; import org.elasticsearch.action.admin.indices.exists.indices.IndicesExistsResponse;
import org.elasticsearch.action.admin.indices.settings.UpdateSettingsRequestBuilder; import org.elasticsearch.action.admin.indices.settings.UpdateSettingsRequestBuilder;
import org.elasticsearch.action.admin.indices.settings.UpdateSettingsResponse; import org.elasticsearch.action.admin.indices.settings.UpdateSettingsResponse;
import org.elasticsearch.action.index.IndexRequestBuilder; import org.elasticsearch.action.index.IndexRequestBuilder;
import org.elasticsearch.action.index.IndexResponse; import org.elasticsearch.action.index.IndexResponse;
import org.elasticsearch.client.Client;
import org.elasticsearch.cluster.block.ClusterBlockException; import org.elasticsearch.cluster.block.ClusterBlockException;
import org.elasticsearch.cluster.metadata.IndexMetaData; import org.elasticsearch.cluster.metadata.IndexMetaData;
import org.elasticsearch.cluster.metadata.MetaData; import org.elasticsearch.cluster.metadata.MetaData;
import org.elasticsearch.node.Node; import org.elasticsearch.test.AbstractIntegrationTest;
import org.elasticsearch.test.AbstractNodesTests;
import org.junit.After;
import org.junit.Test; import org.junit.Test;
import java.util.HashMap; import java.util.HashMap;
@ -41,98 +36,87 @@ import java.util.HashMap;
import static org.elasticsearch.common.settings.ImmutableSettings.settingsBuilder; import static org.elasticsearch.common.settings.ImmutableSettings.settingsBuilder;
import static org.hamcrest.Matchers.notNullValue; import static org.hamcrest.Matchers.notNullValue;
public class SimpleBlocksTests extends AbstractNodesTests { public class SimpleBlocksTests extends AbstractIntegrationTest {
@After
public void closeNodes() {
closeAllNodes();
}
@Test @Test
public void verifyIndexAndClusterReadOnly() throws Exception { public void verifyIndexAndClusterReadOnly() throws Exception {
Node node1 = startNode("node1");
Client client = node1.client();
// cluster.read_only = null: write and metadata not blocked // cluster.read_only = null: write and metadata not blocked
canCreateIndex(client, "test1"); canCreateIndex("test1");
canIndexDocument(client, "test1"); canIndexDocument("test1");
setIndexReadOnly(client, "test1", "false"); setIndexReadOnly("test1", "false");
canIndexExists(client, "test1"); canIndexExists("test1");
// cluster.read_only = true: block write and metadata // cluster.read_only = true: block write and metadata
setClusterReadOnly(client, "true"); setClusterReadOnly("true");
canNotCreateIndex(client, "test2"); canNotCreateIndex("test2");
// even if index has index.read_only = false // even if index has index.read_only = false
canNotIndexDocument(client, "test1"); canNotIndexDocument("test1");
canNotIndexExists(client, "test1"); canNotIndexExists("test1");
// cluster.read_only = false: removes the block // cluster.read_only = false: removes the block
setClusterReadOnly(client, "false"); setClusterReadOnly("false");
canCreateIndex(client, "test2"); canCreateIndex("test2");
canIndexDocument(client, "test2"); canIndexDocument("test2");
canIndexDocument(client, "test1"); canIndexDocument("test1");
canIndexExists(client, "test1"); canIndexExists("test1");
// newly created an index has no blocks // newly created an index has no blocks
canCreateIndex(client, "ro"); canCreateIndex("ro");
canIndexDocument(client, "ro"); canIndexDocument("ro");
canIndexExists(client, "ro"); canIndexExists("ro");
// adds index write and metadata block // adds index write and metadata block
setIndexReadOnly(client, "ro", "true"); setIndexReadOnly( "ro", "true");
canNotIndexDocument(client, "ro"); canNotIndexDocument("ro");
canNotIndexExists(client, "ro"); canNotIndexExists("ro");
// other indices not blocked // other indices not blocked
canCreateIndex(client, "rw"); canCreateIndex("rw");
canIndexDocument(client, "rw"); canIndexDocument("rw");
canIndexExists(client, "rw"); canIndexExists("rw");
// blocks can be removed // blocks can be removed
setIndexReadOnly(client, "ro", "false"); setIndexReadOnly("ro", "false");
canIndexDocument(client, "ro"); canIndexDocument("ro");
canIndexExists(client, "ro"); canIndexExists("ro");
} }
@Test @Test
public void testIndexReadWriteMetaDataBlocks() { public void testIndexReadWriteMetaDataBlocks() {
Node node1 = startNode("node1"); canCreateIndex("test1");
Client client = node1.client(); canIndexDocument("test1");
client().admin().indices().prepareUpdateSettings("test1")
canCreateIndex(client, "test1");
canIndexDocument(client, "test1");
client.admin().indices().prepareUpdateSettings("test1")
.setSettings(settingsBuilder().put(IndexMetaData.SETTING_BLOCKS_WRITE, true)) .setSettings(settingsBuilder().put(IndexMetaData.SETTING_BLOCKS_WRITE, true))
.execute().actionGet(); .execute().actionGet();
canNotIndexDocument(client, "test1"); canNotIndexDocument("test1");
client.admin().indices().prepareUpdateSettings("test1") client().admin().indices().prepareUpdateSettings("test1")
.setSettings(settingsBuilder().put(IndexMetaData.SETTING_BLOCKS_WRITE, false)) .setSettings(settingsBuilder().put(IndexMetaData.SETTING_BLOCKS_WRITE, false))
.execute().actionGet(); .execute().actionGet();
canIndexDocument(client, "test1"); canIndexDocument("test1");
} }
private void canCreateIndex(Client client, String index) { private void canCreateIndex(String index) {
try { try {
CreateIndexResponse r = client.admin().indices().prepareCreate(index).execute().actionGet(); CreateIndexResponse r = client().admin().indices().prepareCreate(index).execute().actionGet();
assertThat(r, notNullValue()); assertThat(r, notNullValue());
} catch (ClusterBlockException e) { } catch (ClusterBlockException e) {
assert false; assert false;
} }
} }
private void canNotCreateIndex(Client client, String index) { private void canNotCreateIndex(String index) {
try { try {
client.admin().indices().prepareCreate(index).execute().actionGet(); client().admin().indices().prepareCreate(index).execute().actionGet();
assert false; assert false;
} catch (ClusterBlockException e) { } catch (ClusterBlockException e) {
// all is well // all is well
} }
} }
private void canIndexDocument(Client client, String index) { private void canIndexDocument(String index) {
try { try {
IndexRequestBuilder builder = client.prepareIndex(index, "zzz"); IndexRequestBuilder builder = client().prepareIndex(index, "zzz");
builder.setSource("foo", "bar"); builder.setSource("foo", "bar");
IndexResponse r = builder.execute().actionGet(); IndexResponse r = builder.execute().actionGet();
assertThat(r, notNullValue()); assertThat(r, notNullValue());
@ -141,9 +125,9 @@ public class SimpleBlocksTests extends AbstractNodesTests {
} }
} }
private void canNotIndexDocument(Client client, String index) { private void canNotIndexDocument(String index) {
try { try {
IndexRequestBuilder builder = client.prepareIndex(index, "zzz"); IndexRequestBuilder builder = client().prepareIndex(index, "zzz");
builder.setSource("foo", "bar"); builder.setSource("foo", "bar");
builder.execute().actionGet(); builder.execute().actionGet();
assert false; assert false;
@ -152,39 +136,33 @@ public class SimpleBlocksTests extends AbstractNodesTests {
} }
} }
private void canIndexExists(Client client, String index) { private void canIndexExists(String index) {
try { try {
IndicesExistsResponse r = client.admin().indices().prepareExists(index).execute().actionGet(); IndicesExistsResponse r = client().admin().indices().prepareExists(index).execute().actionGet();
assertThat(r, notNullValue()); assertThat(r, notNullValue());
} catch (ClusterBlockException e) { } catch (ClusterBlockException e) {
assert false; assert false;
} }
} }
private void canNotIndexExists(Client client, String index) { private void canNotIndexExists(String index) {
try { try {
IndicesExistsResponse r = client.admin().indices().prepareExists(index).execute().actionGet(); IndicesExistsResponse r = client().admin().indices().prepareExists(index).execute().actionGet();
assert false; assert false;
} catch (ClusterBlockException e) { } catch (ClusterBlockException e) {
// all is well // all is well
} }
} }
private void setClusterReadOnly(Client client, String value) { private void setClusterReadOnly(String value) {
HashMap<String, Object> newSettings = new HashMap<String, Object>(); updateClusterSettings(settingsBuilder().put(MetaData.SETTING_READ_ONLY, value).build());
newSettings.put(MetaData.SETTING_READ_ONLY, value);
ClusterUpdateSettingsRequestBuilder settingsRequest = client.admin().cluster().prepareUpdateSettings();
settingsRequest.setTransientSettings(newSettings);
ClusterUpdateSettingsResponse settingsResponse = settingsRequest.execute().actionGet();
assertThat(settingsResponse, notNullValue());
} }
private void setIndexReadOnly(Client client, String index, Object value) { private void setIndexReadOnly(String index, Object value) {
HashMap<String, Object> newSettings = new HashMap<String, Object>(); HashMap<String, Object> newSettings = new HashMap<String, Object>();
newSettings.put(IndexMetaData.SETTING_READ_ONLY, value); newSettings.put(IndexMetaData.SETTING_READ_ONLY, value);
UpdateSettingsRequestBuilder settingsRequest = client.admin().indices().prepareUpdateSettings(index); UpdateSettingsRequestBuilder settingsRequest = client().admin().indices().prepareUpdateSettings(index);
settingsRequest.setSettings(newSettings); settingsRequest.setSettings(newSettings);
UpdateSettingsResponse settingsResponse = settingsRequest.execute().actionGet(); UpdateSettingsResponse settingsResponse = settingsRequest.execute().actionGet();
assertThat(settingsResponse, notNullValue()); assertThat(settingsResponse, notNullValue());

View File

@ -20,43 +20,45 @@ package org.elasticsearch.search.highlight;
import com.google.common.collect.Maps; import com.google.common.collect.Maps;
import org.elasticsearch.action.search.SearchResponse; import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.client.Client;
import org.elasticsearch.common.Priority; import org.elasticsearch.common.Priority;
import org.elasticsearch.common.settings.ImmutableSettings; import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.xcontent.XContentFactory; import org.elasticsearch.common.xcontent.XContentFactory;
import org.elasticsearch.index.query.QueryBuilders; import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.test.AbstractNodesTests; import org.elasticsearch.test.AbstractIntegrationTest;
import org.junit.Before;
import org.junit.Test; import org.junit.Test;
import java.io.IOException; import java.io.IOException;
import java.util.Map; import java.util.Map;
import static org.elasticsearch.common.settings.ImmutableSettings.settingsBuilder; import static org.elasticsearch.common.settings.ImmutableSettings.settingsBuilder;
import static org.elasticsearch.test.AbstractIntegrationTest.ClusterScope;
import static org.elasticsearch.test.AbstractIntegrationTest.Scope;
import static org.elasticsearch.test.hamcrest.ElasticSearchAssertions.assertHighlight; import static org.elasticsearch.test.hamcrest.ElasticSearchAssertions.assertHighlight;
import static org.hamcrest.Matchers.equalTo; import static org.hamcrest.Matchers.equalTo;
/** /**
* *
*/ */
public class CustomHighlighterSearchTests extends AbstractNodesTests { @ClusterScope(scope = Scope.SUITE, numNodes = 1)
public class CustomHighlighterSearchTests extends AbstractIntegrationTest {
@Override @Override
protected void beforeClass() throws Exception{ protected Settings nodeSettings(int nodeOrdinal) {
ImmutableSettings.Builder settings = settingsBuilder().put("plugin.types", CustomHighlighterPlugin.class.getName()); return settingsBuilder()
startNode("server1", settings); .put("plugin.types", CustomHighlighterPlugin.class.getName())
Client client = client(); .put(super.nodeSettings(nodeOrdinal))
.build();
}
client.prepareIndex("test", "test", "1").setSource(XContentFactory.jsonBuilder() @Before
protected void setup() throws Exception{
client().prepareIndex("test", "test", "1").setSource(XContentFactory.jsonBuilder()
.startObject() .startObject()
.field("name", "arbitrary content") .field("name", "arbitrary content")
.endObject()) .endObject())
.setRefresh(true).execute().actionGet(); .setRefresh(true).execute().actionGet();
client.admin().cluster().prepareHealth().setWaitForEvents(Priority.LANGUID).setWaitForYellowStatus().execute().actionGet(); client().admin().cluster().prepareHealth().setWaitForEvents(Priority.LANGUID).setWaitForYellowStatus().execute().actionGet();
}
@Override
public Client client() {
return client("server1");
} }
@Test @Test