From 8b0674c18d65479eec531bd09da22874f2d115d7 Mon Sep 17 00:00:00 2001 From: Tanguy Leroux Date: Thu, 23 Apr 2015 16:52:43 +0200 Subject: [PATCH] Tests: Add backward compatibility test for cluster state with blocks --- .../ClusterStateBackwardsCompatTests.java | 70 ++++++++++++++++--- 1 file changed, 62 insertions(+), 8 deletions(-) diff --git a/src/test/java/org/elasticsearch/bwcompat/ClusterStateBackwardsCompatTests.java b/src/test/java/org/elasticsearch/bwcompat/ClusterStateBackwardsCompatTests.java index 3eb69750894..c55681d3fd2 100644 --- a/src/test/java/org/elasticsearch/bwcompat/ClusterStateBackwardsCompatTests.java +++ b/src/test/java/org/elasticsearch/bwcompat/ClusterStateBackwardsCompatTests.java @@ -19,16 +19,25 @@ package org.elasticsearch.bwcompat; -import org.elasticsearch.action.admin.cluster.state.ClusterStateResponse; import org.elasticsearch.action.admin.cluster.node.info.NodeInfo; import org.elasticsearch.action.admin.cluster.node.info.NodesInfoResponse; +import org.elasticsearch.action.admin.cluster.state.ClusterStateResponse; import org.elasticsearch.client.transport.TransportClient; import org.elasticsearch.cluster.ClusterState; +import org.elasticsearch.cluster.block.ClusterBlock; +import org.elasticsearch.cluster.block.ClusterBlockLevel; +import org.elasticsearch.cluster.block.ClusterBlocks; +import org.elasticsearch.cluster.metadata.IndexMetaData; import org.elasticsearch.common.settings.ImmutableSettings; import org.elasticsearch.common.settings.Settings; import org.elasticsearch.test.ElasticsearchBackwardsCompatIntegrationTest; import org.junit.Test; -import static org.hamcrest.Matchers.*; + +import java.util.HashMap; +import java.util.Map; + +import static org.elasticsearch.cluster.metadata.IndexMetaData.*; +import static org.hamcrest.Matchers.equalTo; public class ClusterStateBackwardsCompatTests extends ElasticsearchBackwardsCompatIntegrationTest { @@ -36,13 +45,9 @@ public class ClusterStateBackwardsCompatTests extends ElasticsearchBackwardsComp public void testClusterState() throws Exception { createIndex("test"); - NodesInfoResponse nodesInfo = client().admin().cluster().prepareNodesInfo().execute().actionGet(); - Settings settings = ImmutableSettings.settingsBuilder().put("client.transport.ignore_cluster_name", true) - .put("node.name", "transport_client_" + getTestName()).build(); - // connect to each node with a custom TransportClient, issue a ClusterStateRequest to test serialization - for (NodeInfo n : nodesInfo.getNodes()) { - try (TransportClient tc = new TransportClient(settings)) { + for (NodeInfo n : clusterNodes()) { + try (TransportClient tc = newTransportClient()) { tc.addTransportAddress(n.getNode().address()); ClusterStateResponse response = tc.admin().cluster().prepareState().execute().actionGet(); @@ -52,4 +57,53 @@ public class ClusterStateBackwardsCompatTests extends ElasticsearchBackwardsComp } } } + + @Test + public void testClusterStateWithBlocks() { + createIndex("test-blocks"); + + Map blocks = new HashMap<>(); + blocks.put(SETTING_BLOCKS_READ, IndexMetaData.INDEX_READ_BLOCK); + blocks.put(SETTING_BLOCKS_WRITE, IndexMetaData.INDEX_WRITE_BLOCK); + blocks.put(SETTING_BLOCKS_METADATA, IndexMetaData.INDEX_METADATA_BLOCK); + + for (Map.Entry block : blocks.entrySet()) { + try { + enableIndexBlock("test-blocks", block.getKey()); + + for (NodeInfo n : clusterNodes()) { + try (TransportClient tc = newTransportClient()) { + tc.addTransportAddress(n.getNode().address()); + + ClusterStateResponse response = tc.admin().cluster().prepareState().setIndices("test-blocks") + .setBlocks(true).setNodes(false).execute().actionGet(); + + ClusterBlocks clusterBlocks = response.getState().blocks(); + assertNotNull(clusterBlocks); + assertTrue(clusterBlocks.hasIndexBlock("test-blocks", block.getValue())); + + for (ClusterBlockLevel level : block.getValue().levels()) { + assertTrue(clusterBlocks.indexBlocked(level, "test-blocks")); + } + + IndexMetaData indexMetaData = response.getState().getMetaData().getIndices().get("test-blocks"); + assertNotNull(indexMetaData); + assertTrue(indexMetaData.settings().getAsBoolean(block.getKey(), null)); + } + } + } finally { + disableIndexBlock("test-blocks", block.getKey()); + } + } + } + + private NodesInfoResponse clusterNodes() { + return client().admin().cluster().prepareNodesInfo().execute().actionGet(); + } + + private TransportClient newTransportClient() { + Settings settings = ImmutableSettings.settingsBuilder().put("client.transport.ignore_cluster_name", true) + .put("node.name", "transport_client_" + getTestName()).build(); + return new TransportClient(settings); + } }