From 01e5043f41214d8ca652436b47025e15588b81f0 Mon Sep 17 00:00:00 2001 From: Simon Brandhof Date: Sun, 24 Sep 2017 21:59:12 +0200 Subject: [PATCH] SONAR-9802 fix NPE in EsStatusCheck --- .../main/java/org/sonar/server/health/EsStatusCheck.java | 7 +++++-- .../org/sonar/server/health/EsStatusNodeCheckTest.java | 2 +- 2 files changed, 6 insertions(+), 3 deletions(-) diff --git a/server/sonar-server/src/main/java/org/sonar/server/health/EsStatusCheck.java b/server/sonar-server/src/main/java/org/sonar/server/health/EsStatusCheck.java index 7c4c0bb57a1..a6947fb63bf 100644 --- a/server/sonar-server/src/main/java/org/sonar/server/health/EsStatusCheck.java +++ b/server/sonar-server/src/main/java/org/sonar/server/health/EsStatusCheck.java @@ -37,7 +37,7 @@ abstract class EsStatusCheck { .setStatus(Health.Status.RED) .addCause("Elasticsearch status is RED") .build(); - private static final Health RED_HEALTH_EXCEPTION_OCCURED = newHealthCheckBuilder() + private static final Health RED_HEALTH_UNAVAILABLE = newHealthCheckBuilder() .setStatus(Health.Status.RED) .addCause("Elasticsearch status is RED (unavailable)") .build(); @@ -51,6 +51,9 @@ abstract class EsStatusCheck { Health checkEsStatus() { try { ClusterHealthStatus esStatus = esClient.prepareClusterStats().get().getStatus(); + if (esStatus == null) { + return RED_HEALTH_UNAVAILABLE; + } switch (esStatus) { case GREEN: return Health.GREEN; @@ -63,7 +66,7 @@ abstract class EsStatusCheck { } } catch (Exception e) { LOG.error("Failed to query ES status", e); - return RED_HEALTH_EXCEPTION_OCCURED; + return RED_HEALTH_UNAVAILABLE; } } } diff --git a/server/sonar-server/src/test/java/org/sonar/server/health/EsStatusNodeCheckTest.java b/server/sonar-server/src/test/java/org/sonar/server/health/EsStatusNodeCheckTest.java index 2530be657db..4c91adf083e 100644 --- a/server/sonar-server/src/test/java/org/sonar/server/health/EsStatusNodeCheckTest.java +++ b/server/sonar-server/src/test/java/org/sonar/server/health/EsStatusNodeCheckTest.java @@ -38,7 +38,7 @@ public class EsStatusNodeCheckTest { @Test public void check_ignores_NodeHealth_arg_and_returns_RED_with_cause_if_an_exception_occurs_checking_ES_cluster_status() { EsClient esClient = Mockito.mock(EsClient.class); - when(esClient.prepareClusterStats()).thenThrow(new RuntimeException("Faking an exception occuring while using the EsClient")); + when(esClient.prepareClusterStats()).thenThrow(new RuntimeException("Faking an exception occurring while using the EsClient")); Health health = new EsStatusNodeCheck(esClient).check(); -- 2.39.5