if (redNodesCount == appNodeCount) { return builder .setStatus(Health.Status.RED) .addCause("Status of all application nodes is RED") .build(); } else if (redNodesCount > 0) { builder.addCause("At least one application node is RED"); .setStatus(Health.Status.YELLOW) .addCause("Status of all application nodes is YELLOW") .build(); } else if (yellowNodesCount > 0) { builder.addCause("At least one application node is YELLOW"); builder.setStatus(greenNodesCount > 0 || yellowNodesCount > 0 ? Health.Status.YELLOW : Health.Status.RED); return builder.build();
if (redNodesCount == appNodeCount) { return builder .setStatus(Health.Status.RED) .addCause("Status of all application nodes is RED") .build(); } else if (redNodesCount > 0) { builder.addCause("At least one application node is RED"); .setStatus(Health.Status.YELLOW) .addCause("Status of all application nodes is YELLOW") .build(); } else if (yellowNodesCount > 0) { builder.addCause("At least one application node is YELLOW"); builder.setStatus(greenNodesCount > 0 || yellowNodesCount > 0 ? Health.Status.YELLOW : Health.Status.RED); return builder.build();
@Override public Health check() { Platform.Status platformStatus = platform.status(); if (platformStatus == Platform.Status.UP && VALID_DATABASEMIGRATION_STATUSES.contains(migrationState.getStatus()) && !restartFlagHolder.isRestarting()) { return Health.GREEN; } return newHealthCheckBuilder() .setStatus(Health.Status.RED) .addCause("SonarQube webserver is not up") .build(); } }
public static Builder newHealthCheckBuilder() { return new Builder(); }
public Builder setStatus(Status status) { this.status = checkStatus(status); return this; }
public Health build() { checkStatus(this.status); return new Health(this); }
@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 get_returns_name_and_port_from_properties_at_constructor_time() { String name = randomAlphanumeric(3); int port = 1 + random.nextInt(4); mapSettings.setProperty(CLUSTER_NODE_NAME.getKey(), name); mapSettings.setProperty(CLUSTER_NODE_HZ_PORT.getKey(), port); setStartedAt(); when(healthChecker.checkNode()).thenReturn(Health.newHealthCheckBuilder() .setStatus(Health.Status.values()[random.nextInt(Health.Status.values().length)]) .build()); when(networkUtils.getHostname()).thenReturn(randomAlphanumeric(3)); NodeHealthProviderImpl underTest = new NodeHealthProviderImpl(mapSettings.asConfig(), healthChecker, server, networkUtils); NodeHealth nodeHealth = underTest.get(); assertThat(nodeHealth.getDetails().getName()).isEqualTo(name); assertThat(nodeHealth.getDetails().getPort()).isEqualTo(port); // change values in properties setRequiredPropertiesForConstructor(); NodeHealth newNodeHealth = underTest.get(); assertThat(newNodeHealth.getDetails().getName()).isEqualTo(name); assertThat(newNodeHealth.getDetails().getPort()).isEqualTo(port); }
private void getReturnsHostnameFromNetworkUtils(String hostPropertyValue) { String host = randomAlphanumeric(3); setRequiredPropertiesForConstructor(); if (hostPropertyValue != null) { mapSettings.setProperty(CLUSTER_NODE_HOST.getKey(), hostPropertyValue); } setStartedAt(); when(healthChecker.checkNode()).thenReturn(Health.newHealthCheckBuilder() .setStatus(Health.Status.values()[random.nextInt(Health.Status.values().length)]) .build()); when(networkUtils.getHostname()).thenReturn(host); NodeHealthProviderImpl underTest = new NodeHealthProviderImpl(mapSettings.asConfig(), healthChecker, server, networkUtils); NodeHealth nodeHealth = underTest.get(); assertThat(nodeHealth.getDetails().getHost()).isEqualTo(host); // change hostname when(networkUtils.getHostname()).thenReturn(randomAlphanumeric(4)); NodeHealth newNodeHealth = underTest.get(); assertThat(newNodeHealth.getDetails().getHost()).isEqualTo(host); }
@Test public void verify_toString() { assertThat(Health.GREEN.toString()).isEqualTo("Health{GREEN, causes=[]}"); Health.Builder builder = newHealthCheckBuilder().setStatus(anyStatus); randomCauses.forEach(builder::addCause); String underTest = builder.build().toString(); AbstractCharSequenceAssert<?, String> a = assertThat(underTest) .describedAs("toString for status %s and causes %s", anyStatus, randomCauses); if (randomCauses.isEmpty()) { a.isEqualTo("Health{" + anyStatus + ", causes=[]}"); } else if (randomCauses.size() == 1) { a.isEqualTo("Health{" + anyStatus + ", causes=[" + randomCauses.iterator().next() + "]}"); } else { a.startsWith("Health{" + anyStatus + ", causes=[") .endsWith("]}") .contains(randomCauses); } }
@Test public void get_returns_host_from_property_if_set_at_constructor_time() { String host = randomAlphanumeric(4); mapSettings.setProperty(CLUSTER_NODE_NAME.getKey(), randomAlphanumeric(3)); mapSettings.setProperty(CLUSTER_NODE_HZ_PORT.getKey(), 1 + random.nextInt(4)); mapSettings.setProperty(CLUSTER_NODE_HOST.getKey(), host); setStartedAt(); when(healthChecker.checkNode()).thenReturn(Health.newHealthCheckBuilder() .setStatus(Health.Status.values()[random.nextInt(Health.Status.values().length)]) .build()); NodeHealthProviderImpl underTest = new NodeHealthProviderImpl(mapSettings.asConfig(), healthChecker, server, networkUtils); NodeHealth nodeHealth = underTest.get(); assertThat(nodeHealth.getDetails().getHost()).isEqualTo(host); // change values in properties mapSettings.setProperty(CLUSTER_NODE_HOST.getKey(), randomAlphanumeric(66)); NodeHealth newNodeHealth = underTest.get(); assertThat(newNodeHealth.getDetails().getHost()).isEqualTo(host); }
@Test public void get_returns_started_from_server_startedAt_at_constructor_time() { setRequiredPropertiesForConstructor(); when(networkUtils.getHostname()).thenReturn(randomAlphanumeric(4)); Date date = new Date(); when(server.getStartedAt()).thenReturn(date); when(healthChecker.checkNode()).thenReturn(Health.newHealthCheckBuilder() .setStatus(Health.Status.values()[random.nextInt(Health.Status.values().length)]) .build()); NodeHealthProviderImpl underTest = new NodeHealthProviderImpl(mapSettings.asConfig(), healthChecker, server, networkUtils); NodeHealth nodeHealth = underTest.get(); assertThat(nodeHealth.getDetails().getStartedAt()).isEqualTo(date.getTime()); // change startedAt value setStartedAt(); NodeHealth newNodeHealth = underTest.get(); assertThat(newNodeHealth.getDetails().getStartedAt()).isEqualTo(date.getTime()); }
@Test public void response_contains_status_and_causes_from_HealthChecker_checkCluster_when_standalone() { authenticateWithRandomMethod(); Health.Status randomStatus = Health.Status.values()[random.nextInt(Health.Status.values().length)]; String[] causes = IntStream.range(0, random.nextInt(33)).mapToObj(i -> randomAlphanumeric(4)).toArray(String[]::new); Health.Builder healthBuilder = newHealthCheckBuilder() .setStatus(randomStatus); Arrays.stream(causes).forEach(healthBuilder::addCause); when(webServer.isStandalone()).thenReturn(false); when(healthChecker.checkCluster()).thenReturn(new ClusterHealth(healthBuilder.build(), emptySet())); System.HealthResponse clusterHealthResponse = underTest.newRequest().executeProtobuf(System.HealthResponse.class); assertThat(clusterHealthResponse.getHealth().name()).isEqualTo(randomStatus.name()); assertThat(clusterHealthResponse.getCausesList()) .extracting(System.Cause::getMessage) .containsOnly(causes); }
@Test public void get_returns_HEALTH_status_and_causes_from_HealthChecker_checkNode() { setRequiredPropertiesForConstructor(); setStartedAt(); when(networkUtils.getHostname()).thenReturn(randomAlphanumeric(4)); Health.Status randomStatus = Health.Status.values()[random.nextInt(Health.Status.values().length)]; String[] expected = IntStream.range(0, random.nextInt(4)).mapToObj(s -> randomAlphabetic(55)).toArray(String[]::new); Health.Builder healthBuilder = Health.newHealthCheckBuilder() .setStatus(randomStatus); Arrays.stream(expected).forEach(healthBuilder::addCause); when(healthChecker.checkNode()).thenReturn(healthBuilder.build()); NodeHealthProviderImpl underTest = new NodeHealthProviderImpl(mapSettings.asConfig(), healthChecker, server, networkUtils); NodeHealth nodeHealth = underTest.get(); assertThat(nodeHealth.getStatus().name()).isEqualTo(randomStatus.name()); assertThat(nodeHealth.getCauses()).containsOnly(expected); }