@Test public void verify_getters() { Health health = randomHealth(); Set<NodeHealth> nodeHealths = randomNodeHealths(); ClusterHealth underTest = new ClusterHealth(health, nodeHealths); assertThat(underTest.getHealth()).isSameAs(health); assertThat(underTest.getNodes()).isEqualTo(nodeHealths); }
@Test public void test_getNodeHealth() { Health health = randomHealth(); Set<NodeHealth> nodeHealths = new HashSet<>(Arrays.asList(newNodeHealth("foo"), newNodeHealth("bar"))); ClusterHealth underTest = new ClusterHealth(health, nodeHealths); assertThat(underTest.getNodeHealth("does_not_exist")).isEmpty(); assertThat(underTest.getNodeHealth("bar")).isPresent(); }
@Test public void hashcode_is_based_on_content() { Health health = randomHealth(); Set<NodeHealth> nodeHealths = randomNodeHealths(); ClusterHealth underTest = new ClusterHealth(health, nodeHealths); assertThat(underTest.hashCode()).isEqualTo(underTest.hashCode()); }
@Test public void verify_toString() { Health health = randomHealth(); Set<NodeHealth> nodeHealths = randomNodeHealths(); ClusterHealth underTest = new ClusterHealth(health, nodeHealths); assertThat(underTest.toString()).isEqualTo("ClusterHealth{health=" + health + ", nodes=" + nodeHealths + "}"); }
@Override public ClusterHealth checkCluster() { checkState(!webServer.isStandalone(), "Clustering is not enabled"); checkState(sharedHealthState != null, "HealthState instance can't be null when clustering is enabled"); Set<NodeHealth> nodeHealths = sharedHealthState.readAll(); Health health = clusterHealthChecks.stream() .map(clusterHealthCheck -> clusterHealthCheck.check(nodeHealths)) .reduce(Health.GREEN, HealthReducer.INSTANCE); return new ClusterHealth(health, nodeHealths); }
private static System.HealthResponse toResponse(ClusterHealth check) { System.HealthResponse.Builder responseBuilder = System.HealthResponse.newBuilder(); System.Node.Builder nodeBuilder = System.Node.newBuilder(); System.Cause.Builder causeBuilder = System.Cause.newBuilder(); Health health = check.getHealth(); responseBuilder.setHealth(System.Health.valueOf(health.getStatus().name())); health.getCauses().forEach(str -> responseBuilder.addCauses(toCause(str, causeBuilder))); System.Nodes.Builder nodesBuilder = System.Nodes.newBuilder(); check.getNodes().stream() .sorted(NODE_HEALTH_COMPARATOR) .map(node -> toNode(node, nodeBuilder, causeBuilder)) .forEach(nodesBuilder::addNodes); responseBuilder.setNodes(nodesBuilder.build()); return responseBuilder.build(); }
@Test public void checkCluster_returns_causes_of_all_ClusterHealthChecks_whichever_their_status() { when(webServer.isStandalone()).thenReturn(false); List<String[]> causesGroups = IntStream.range(0, 1 + random.nextInt(20)) .mapToObj(s -> IntStream.range(0, random.nextInt(3)).mapToObj(i -> randomAlphanumeric(3)).toArray(String[]::new)) .collect(Collectors.toList()); ClusterHealthCheck[] clusterHealthChecks = causesGroups.stream() .map(HardcodedHealthClusterCheck::new) .map(ClusterHealthCheck.class::cast) .toArray(ClusterHealthCheck[]::new); String[] expectedCauses = causesGroups.stream().flatMap(Arrays::stream).collect(Collectors.toSet()).stream().toArray(String[]::new); HealthCheckerImpl underTest = new HealthCheckerImpl(webServer, new NodeHealthCheck[0], clusterHealthChecks, sharedHealthState); assertThat(underTest.checkCluster().getHealth().getCauses()).containsOnly(expectedCauses); }
private void writeNodeInfo(NodeInfo nodeInfo, ClusterHealth clusterHealth, JsonWriter json) { json.beginObject(); json.prop("Name", nodeInfo.getName()); json.prop("Error", nodeInfo.getErrorMessage().orElse(null)); json.prop("Host", nodeInfo.getHost().orElse(null)); json.prop("Started At", nodeInfo.getStartedAt().orElse(null)); clusterHealth.getNodeHealth(nodeInfo.getName()).ifPresent(h -> { json.prop("Health", h.getStatus().name()); json.name("Health Causes").beginArray().values(h.getCauses()).endArray(); }); writeSections(nodeInfo.getSections(), json); json.endObject(); }
@Test public void checkCluster_returns_NodeHealths_returned_by_HealthState() { when(webServer.isStandalone()).thenReturn(false); Set<NodeHealth> nodeHealths = IntStream.range(0, 1 + random.nextInt(4)).mapToObj(i -> randomNodeHealth()).collect(Collectors.toSet()); when(sharedHealthState.readAll()).thenReturn(nodeHealths); HealthCheckerImpl underTest = new HealthCheckerImpl(webServer, new NodeHealthCheck[0], new ClusterHealthCheck[0], sharedHealthState); ClusterHealth clusterHealth = underTest.checkCluster(); assertThat(clusterHealth.getNodes()).isEqualTo(nodeHealths); }
@Before public void before() throws InterruptedException { when(globalInfoLoader.load()).thenReturn(Collections.singletonList(createSection("globalInfo"))); when(appNodesInfoLoader.load()).thenReturn(Collections.singletonList(createNodeInfo("appNodes"))); when(searchNodesInfoLoader.load()).thenReturn(Collections.singletonList(createNodeInfo("searchNodes"))); Health health = Health.newHealthCheckBuilder().setStatus(Health.Status.GREEN).build(); when(healthChecker.checkCluster()).thenReturn(new ClusterHealth(health, Collections.emptySet())); }
private static System.HealthResponse toResponse(ClusterHealth check) { System.HealthResponse.Builder responseBuilder = System.HealthResponse.newBuilder(); System.Node.Builder nodeBuilder = System.Node.newBuilder(); System.Cause.Builder causeBuilder = System.Cause.newBuilder(); Health health = check.getHealth(); responseBuilder.setHealth(System.Health.valueOf(health.getStatus().name())); health.getCauses().forEach(str -> responseBuilder.addCauses(toCause(str, causeBuilder))); System.Nodes.Builder nodesBuilder = System.Nodes.newBuilder(); check.getNodes().stream() .sorted(NODE_HEALTH_COMPARATOR) .map(node -> toNode(node, nodeBuilder, causeBuilder)) .forEach(nodesBuilder::addNodes); responseBuilder.setNodes(nodesBuilder.build()); return responseBuilder.build(); }
@Test public void checkCluster_returns_YELLOW_status_if_only_GREEN_and_at_least_one_YELLOW_statuses_returned_by_ClusterHealthChecks() { when(webServer.isStandalone()).thenReturn(false); List<Health.Status> statuses = new ArrayList<>(); Stream.concat( IntStream.range(0, 1 + random.nextInt(20)).mapToObj(i -> YELLOW), // at least 1 YELLOW IntStream.range(0, random.nextInt(20)).mapToObj(i -> GREEN)).forEach(statuses::add); // between 0 and 19 GREEN Collections.shuffle(statuses); HealthCheckerImpl underTest = newClusterHealthCheckerImpl(statuses.stream()); assertThat(underTest.checkCluster().getHealth().getStatus()) .describedAs("%s should have been computed from %s statuses", YELLOW, statuses) .isEqualTo(YELLOW); }
private void writeNodeInfo(NodeInfo nodeInfo, ClusterHealth clusterHealth, JsonWriter json) { json.beginObject(); json.prop("Name", nodeInfo.getName()); json.prop("Error", nodeInfo.getErrorMessage().orElse(null)); json.prop("Host", nodeInfo.getHost().orElse(null)); json.prop("Started At", nodeInfo.getStartedAt().orElse(null)); clusterHealth.getNodeHealth(nodeInfo.getName()).ifPresent(h -> { json.prop("Health", h.getStatus().name()); json.name("Health Causes").beginArray().values(h.getCauses()).endArray(); }); writeSections(nodeInfo.getSections(), json); json.endObject(); }
@Test public void constructor_fails_with_NPE_if_Health_is_null() { expectedException.expect(NullPointerException.class); expectedException.expectMessage("health can't be null"); new ClusterHealth(null, Collections.emptySet()); }
@Test public void checkCluster_returns_GREEN_status_if_only_GREEN_statuses_returned_by_ClusterHealthChecks() { when(webServer.isStandalone()).thenReturn(false); List<Health.Status> statuses = IntStream.range(1, 1 + random.nextInt(20)).mapToObj(i -> GREEN).collect(Collectors.toList()); HealthCheckerImpl underTest = newClusterHealthCheckerImpl(statuses.stream()); assertThat(underTest.checkCluster().getHealth().getStatus()) .describedAs("%s should have been computed from %s statuses", GREEN, statuses) .isEqualTo(GREEN); }
private ClusterHealth randomStatusMinimalClusterHealth() { return new ClusterHealth(newHealthCheckBuilder() .setStatus(Health.Status.values()[random.nextInt(Health.Status.values().length)]) .build(), emptySet()); }
@Test public void checkCluster_returns_RED_status_if_at_least_one_RED_status_returned_by_ClusterHealthChecks() { when(webServer.isStandalone()).thenReturn(false); List<Health.Status> statuses = new ArrayList<>(); Stream.of( IntStream.range(0, 1 + random.nextInt(20)).mapToObj(i -> RED), // at least 1 RED IntStream.range(0, random.nextInt(20)).mapToObj(i -> YELLOW), // between 0 and 19 YELLOW IntStream.range(0, random.nextInt(20)).mapToObj(i -> GREEN) // between 0 and 19 GREEN ).flatMap(s -> s) .forEach(statuses::add); Collections.shuffle(statuses); HealthCheckerImpl underTest = newClusterHealthCheckerImpl(statuses.stream()); assertThat(underTest.checkCluster().getHealth().getStatus()) .describedAs("%s should have been computed from %s statuses", RED, statuses) .isEqualTo(RED); }
@Test public void equals_is_based_on_content() { Health health = randomHealth(); Set<NodeHealth> nodeHealths = randomNodeHealths(); ClusterHealth underTest = new ClusterHealth(health, nodeHealths); assertThat(underTest) .isEqualTo(underTest) .isEqualTo(new ClusterHealth(health, nodeHealths)) .isNotEqualTo(new Object()) .isNotEqualTo(null) .isNotEqualTo(new ClusterHealth( newHealthCheckBuilder() .setStatus(health.getStatus()) .addCause("foo_bar") .build(), randomNodeHealths())) .isNotEqualTo(new ClusterHealth( health, concat(nodeHealths.stream(), Stream.of(randomNodeHealth())).collect(toSet()))); }
@Test public void checkCluster_returns_GREEN_when_there_is_no_ClusterHealthCheck() { when(webServer.isStandalone()).thenReturn(false); HealthCheckerImpl underTest = new HealthCheckerImpl(webServer, new NodeHealthCheck[0], new ClusterHealthCheck[0], sharedHealthState); assertThat(underTest.checkCluster().getHealth()).isEqualTo(Health.GREEN); }
@Test public void constructor_fails_with_NPE_if_NodeHealth_is_null() { expectedException.expect(NullPointerException.class); expectedException.expectMessage("nodes can't be null"); new ClusterHealth(Health.GREEN, null); }