From 79c13d62d3d4b23d5df9a224056952ab11df6f56 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jan=20H=C3=B8ydahl?= Date: Tue, 6 Aug 2019 15:33:08 +0200 Subject: [PATCH] Added check for explicit empty line response from Zookeeper --- .../handler/admin/ZookeeperStatusHandler.java | 2 +- .../admin/ZookeeperStatusHandlerTest.java | 19 +++++++++++++++++++ 2 files changed, 20 insertions(+), 1 deletion(-) diff --git a/solr/core/src/java/org/apache/solr/handler/admin/ZookeeperStatusHandler.java b/solr/core/src/java/org/apache/solr/handler/admin/ZookeeperStatusHandler.java index 7a36889a5f8..04caefb1022 100644 --- a/solr/core/src/java/org/apache/solr/handler/admin/ZookeeperStatusHandler.java +++ b/solr/core/src/java/org/apache/solr/handler/admin/ZookeeperStatusHandler.java @@ -255,7 +255,7 @@ public class ZookeeperStatusHandler extends RequestHandlerBase { * @throws SolrException if validation fails */ protected boolean validateZkRawResponse(List response, String zkHostPort, String fourLetterWordCommand) { - if (response == null || response.isEmpty()) { + if (response == null || response.isEmpty() || (response.size() == 1 && response.get(0).isBlank())) { throw new SolrException(SolrException.ErrorCode.SERVER_ERROR, "Empty response from Zookeeper " + zkHostPort); } if (response.size() == 1 && response.get(0).contains("not in the whitelist")) { diff --git a/solr/core/src/test/org/apache/solr/handler/admin/ZookeeperStatusHandlerTest.java b/solr/core/src/test/org/apache/solr/handler/admin/ZookeeperStatusHandlerTest.java index f66db4b41d8..ad2f22e40e0 100644 --- a/solr/core/src/test/org/apache/solr/handler/admin/ZookeeperStatusHandlerTest.java +++ b/solr/core/src/test/org/apache/solr/handler/admin/ZookeeperStatusHandlerTest.java @@ -21,6 +21,7 @@ import java.io.IOException; import java.lang.invoke.MethodHandles; import java.net.URL; import java.util.Arrays; +import java.util.Collections; import java.util.List; import java.util.Map; import java.util.concurrent.ExecutionException; @@ -33,6 +34,7 @@ import org.apache.solr.client.solrj.impl.HttpSolrClient; import org.apache.solr.client.solrj.request.GenericSolrRequest; import org.apache.solr.client.solrj.response.DelegationTokenResponse; import org.apache.solr.cloud.SolrCloudTestCase; +import org.apache.solr.common.SolrException; import org.apache.solr.common.params.ModifiableSolrParams; import org.apache.solr.common.util.NamedList; import org.junit.After; @@ -41,6 +43,7 @@ import org.junit.BeforeClass; import org.junit.Test; import org.mockito.Answers; import org.mockito.ArgumentMatchers; +import org.mockito.invocation.InvocationOnMock; import org.noggit.JSONUtil; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -49,6 +52,7 @@ import static org.mockito.ArgumentMatchers.any; import static org.mockito.ArgumentMatchers.anyString; import static org.mockito.Mockito.mock; import static org.mockito.Mockito.when; +import static org.mockito.Mockito.withSettings; public class ZookeeperStatusHandlerTest extends SolrCloudTestCase { private static final Logger log = LoggerFactory.getLogger(MethodHandles.lookup().lookupClass()); @@ -148,4 +152,19 @@ public class ZookeeperStatusHandlerTest extends SolrCloudTestCase { " \"status\":\"yellow\"}"; assertEquals(expected, JSONUtil.toJSON(mockStatus)); } + + @Test(expected = SolrException.class) + public void validateNotWhitelisted() { + assumeWorkingMockito(); + ZookeeperStatusHandler zkStatusHandler = mock(ZookeeperStatusHandler.class, withSettings().defaultAnswer(InvocationOnMock::callRealMethod)); + zkStatusHandler.validateZkRawResponse(Collections.singletonList("mntr is not executed because it is not in the whitelist."), + "zoo1:2181", "mntr"); + } + + @Test(expected = SolrException.class) + public void validateEmptyResponse() { + assumeWorkingMockito(); + ZookeeperStatusHandler zkStatusHandler = mock(ZookeeperStatusHandler.class, withSettings().defaultAnswer(InvocationOnMock::callRealMethod)); + zkStatusHandler.validateZkRawResponse(Collections.emptyList(), "zoo1:2181", "mntr"); + } } \ No newline at end of file