ElasticsearchClientHealthCheck extends HealthCheck { @Override protected Result check() throws Exception { final Result result; try { ElasticsearchClusterStats clusterStats = client.getClusterStats(); String message = String.format(CLUSTER_HEALTH_MSG, clusterStats.getStatus()); switch (clusterStats.getStatus()) { case "red": result = Result.unhealthy(message); break; case "yellow": case "green": result = Result.healthy(message); break; default: result = Result.unhealthy("Unrecognized cluster status: " + clusterStats.getStatus()); } } catch (DiskSpaceCheckerException e) { LOGGER.error("Exception caught checking ES cluster health: {}", e.getMessage()); return Result.unhealthy(e); } return result; } ElasticsearchClientHealthCheck(ElasticsearchClient client);   }
@Test public void returnsUnhealthyWhenClientThrowsException() throws Exception { when(client.getClusterStats()).thenThrow(new DiskSpaceCheckerException("Error")); HealthCheck.Result result = healthCheck.check(); assertThat(result.isHealthy()).isFalse(); verify(client).getClusterStats(); }
@Test public void returnsUnhealthyWhenClusterStateRed() throws Exception { ElasticsearchClusterStats stats = mock(ElasticsearchClusterStats.class); when(stats.getStatus()).thenReturn("red"); when(client.getClusterStats()).thenReturn(stats); HealthCheck.Result result = healthCheck.check(); assertThat(result.isHealthy()).isFalse(); verify(client).getClusterStats(); }
@Test public void returnsHealthyWhenClusterStateYellow() throws Exception { ElasticsearchClusterStats stats = mock(ElasticsearchClusterStats.class); when(stats.getStatus()).thenReturn("yellow"); when(client.getClusterStats()).thenReturn(stats); HealthCheck.Result result = healthCheck.check(); assertThat(result.isHealthy()).isTrue(); verify(client).getClusterStats(); }
@Test public void returnsHealthyWhenClusterStateGreen() throws Exception { ElasticsearchClusterStats stats = mock(ElasticsearchClusterStats.class); when(stats.getStatus()).thenReturn("yellow"); when(client.getClusterStats()).thenReturn(stats); HealthCheck.Result result = healthCheck.check(); assertThat(result.isHealthy()).isTrue(); verify(client).getClusterStats(); }
@Test public void returnsUnhealthyWhenClusterStateUnknown() throws Exception { ElasticsearchClusterStats stats = mock(ElasticsearchClusterStats.class); when(stats.getStatus()).thenReturn("blah"); when(client.getClusterStats()).thenReturn(stats); HealthCheck.Result result = healthCheck.check(); assertThat(result.isHealthy()).isFalse(); verify(client).getClusterStats(); }