From a893f0d646e43253ed4babaa56240ed08fe1cf80 Mon Sep 17 00:00:00 2001 From: jaymode Date: Mon, 14 Nov 2016 14:16:11 -0500 Subject: [PATCH] test: mock clusterservice to prevent NPE when remote indexing This change mocks the ClusterService and sets it up so that when remote indexing, we see the current node as the master. Original commit: elastic/x-pack-elasticsearch@1f26b3df1f707727f6af4efb2cc9dfdef66d0d0d --- .../xpack/security/audit/index/IndexAuditTrailTests.java | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/elasticsearch/src/test/java/org/elasticsearch/xpack/security/audit/index/IndexAuditTrailTests.java b/elasticsearch/src/test/java/org/elasticsearch/xpack/security/audit/index/IndexAuditTrailTests.java index 790936daac4..69e25eb1fd4 100644 --- a/elasticsearch/src/test/java/org/elasticsearch/xpack/security/audit/index/IndexAuditTrailTests.java +++ b/elasticsearch/src/test/java/org/elasticsearch/xpack/security/audit/index/IndexAuditTrailTests.java @@ -14,8 +14,10 @@ import org.elasticsearch.action.search.SearchResponse; import org.elasticsearch.action.support.IndicesOptions; import org.elasticsearch.client.Client; import org.elasticsearch.client.Requests; +import org.elasticsearch.cluster.ClusterState; import org.elasticsearch.cluster.health.ClusterHealthStatus; import org.elasticsearch.cluster.node.DiscoveryNode; +import org.elasticsearch.cluster.node.DiscoveryNodes; import org.elasticsearch.cluster.service.ClusterService; import org.elasticsearch.common.Priority; import org.elasticsearch.common.network.NetworkAddress; @@ -267,7 +269,12 @@ public class IndexAuditTrailTests extends SecurityIntegTestCase { when(localNode.getHostAddress()).thenReturn(remoteAddress.getAddress()); when(localNode.getHostName()).thenReturn(remoteAddress.getAddress()); ClusterService clusterService = mock(ClusterService.class); + ClusterState state = mock(ClusterState.class); + DiscoveryNodes nodes = mock(DiscoveryNodes.class); when(clusterService.localNode()).thenReturn(localNode); + when(clusterService.state()).thenReturn(state); + when(state.getNodes()).thenReturn(nodes); + when(nodes.isLocalNodeElectedMaster()).thenReturn(true); threadPool = new TestThreadPool("index audit trail tests"); enqueuedMessage = new SetOnce<>(); auditor = new IndexAuditTrail(settings, internalClient(), threadPool, clusterService) {