Search in sources :

Example 16 with NodeHealth

use of org.sonar.process.cluster.health.NodeHealth in project sonarqube by SonarSource.

the class HealthActionTest method response_sort_nodes_by_type_name_host_then_port_when_clustered.

@Test
public void response_sort_nodes_by_type_name_host_then_port_when_clustered() {
    authenticateWithRandomMethod();
    // using created field as a unique identifier. pseudo random value to ensure sorting is not based on created field
    List<NodeHealth> nodeHealths = new ArrayList<>(Arrays.asList(randomNodeHealth(NodeDetails.Type.APPLICATION, "1_name", "1_host", 1, 99), randomNodeHealth(NodeDetails.Type.APPLICATION, "1_name", "2_host", 1, 85), randomNodeHealth(NodeDetails.Type.APPLICATION, "1_name", "2_host", 2, 12), randomNodeHealth(NodeDetails.Type.APPLICATION, "2_name", "1_host", 1, 6), randomNodeHealth(NodeDetails.Type.APPLICATION, "2_name", "1_host", 2, 30), randomNodeHealth(NodeDetails.Type.APPLICATION, "2_name", "2_host", 1, 75), randomNodeHealth(NodeDetails.Type.APPLICATION, "2_name", "2_host", 2, 258), randomNodeHealth(NodeDetails.Type.SEARCH, "1_name", "1_host", 1, 963), randomNodeHealth(NodeDetails.Type.SEARCH, "1_name", "1_host", 2, 1), randomNodeHealth(NodeDetails.Type.SEARCH, "1_name", "2_host", 1, 35), randomNodeHealth(NodeDetails.Type.SEARCH, "1_name", "2_host", 2, 45), randomNodeHealth(NodeDetails.Type.SEARCH, "2_name", "1_host", 1, 39), randomNodeHealth(NodeDetails.Type.SEARCH, "2_name", "1_host", 2, 28), randomNodeHealth(NodeDetails.Type.SEARCH, "2_name", "2_host", 1, 66), randomNodeHealth(NodeDetails.Type.SEARCH, "2_name", "2_host", 2, 77)));
    String[] expected = nodeHealths.stream().map(s -> formatDateTime(new Date(s.getDetails().getStartedAt()))).toArray(String[]::new);
    Collections.shuffle(nodeHealths);
    when(webServer.isStandalone()).thenReturn(false);
    when(healthChecker.checkCluster()).thenReturn(new ClusterHealth(GREEN, new HashSet<>(nodeHealths)));
    System.HealthResponse response = underTest.newRequest().executeProtobuf(System.HealthResponse.class);
    assertThat(response.getNodes().getNodesList()).extracting(System.Node::getStartedAt).containsExactly(expected);
}
Also used : IntStream(java.util.stream.IntStream) ArgumentMatchers.any(org.mockito.ArgumentMatchers.any) Arrays(java.util.Arrays) RandomStringUtils.randomAlphanumeric(org.apache.commons.lang.RandomStringUtils.randomAlphanumeric) Date(java.util.Date) WebServer(org.sonar.server.platform.WebServer) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) GREEN(org.sonar.server.health.Health.GREEN) Random(java.util.Random) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) NodeHealth.newNodeHealthBuilder(org.sonar.process.cluster.health.NodeHealth.newNodeHealthBuilder) Health.newHealthCheckBuilder(org.sonar.server.health.Health.newHealthCheckBuilder) ThrowingCallable(org.assertj.core.api.ThrowableAssert.ThrowingCallable) WebService(org.sonar.api.server.ws.WebService) Collections.singleton(java.util.Collections.singleton) Assertions.assertThatThrownBy(org.assertj.core.api.Assertions.assertThatThrownBy) NodeDetails(org.sonar.process.cluster.health.NodeDetails) HealthChecker(org.sonar.server.health.HealthChecker) Health(org.sonar.server.health.Health) DateUtils.formatDateTime(org.sonar.api.utils.DateUtils.formatDateTime) RandomStringUtils(org.apache.commons.lang.RandomStringUtils) UserSessionRule(org.sonar.server.tester.UserSessionRule) ImmutableSet(com.google.common.collect.ImmutableSet) Collections.emptySet(java.util.Collections.emptySet) RandomStringUtils.randomAlphabetic(org.apache.commons.lang.RandomStringUtils.randomAlphabetic) TestRequest(org.sonar.server.ws.TestRequest) JsonAssert.assertJson(org.sonar.test.JsonAssert.assertJson) Test(org.junit.Test) Mockito.when(org.mockito.Mockito.when) WsActionTester(org.sonar.server.ws.WsActionTester) System(org.sonarqube.ws.System) ClusterHealth(org.sonar.server.health.ClusterHealth) NodeDetails.newNodeDetailsBuilder(org.sonar.process.cluster.health.NodeDetails.newNodeDetailsBuilder) List(java.util.List) Rule(org.junit.Rule) SystemPasscode(org.sonar.server.user.SystemPasscode) DateUtils.parseDateTime(org.sonar.api.utils.DateUtils.parseDateTime) ForbiddenException(org.sonar.server.exceptions.ForbiddenException) NodeHealth(org.sonar.process.cluster.health.NodeHealth) Collections(java.util.Collections) TestResponse(org.sonar.server.ws.TestResponse) Mockito.mock(org.mockito.Mockito.mock) ArrayList(java.util.ArrayList) Date(java.util.Date) System(org.sonarqube.ws.System) NodeHealth(org.sonar.process.cluster.health.NodeHealth) ClusterHealth(org.sonar.server.health.ClusterHealth) HashSet(java.util.HashSet) Test(org.junit.Test)

Example 17 with NodeHealth

use of org.sonar.process.cluster.health.NodeHealth in project sonarqube by SonarSource.

the class SearchNodeHealthProviderTest method getReturnsHostFromNetworkUtils.

private void getReturnsHostFromNetworkUtils(@Nullable String hostPropertyValue) {
    String host = randomAlphanumeric(34);
    Properties properties = new Properties();
    properties.setProperty(CLUSTER_NODE_NAME.getKey(), randomAlphanumeric(3));
    properties.setProperty(CLUSTER_NODE_HZ_PORT.getKey(), valueOf(1 + random.nextInt(4)));
    if (hostPropertyValue != null) {
        properties.setProperty(CLUSTER_NODE_HOST.getKey(), hostPropertyValue);
    }
    when(clock.now()).thenReturn(1L + random.nextInt(87));
    when(networkUtils.getHostname()).thenReturn(host);
    SearchNodeHealthProvider underTest = new SearchNodeHealthProvider(new Props(properties), clusterAppState, networkUtils, clock);
    NodeHealth nodeHealth = underTest.get();
    assertThat(nodeHealth.getDetails().getHost()).isEqualTo(host);
    // change now
    when(networkUtils.getHostname()).thenReturn(randomAlphanumeric(96));
    NodeHealth newNodeHealth = underTest.get();
    assertThat(newNodeHealth.getDetails().getHost()).isEqualTo(host);
}
Also used : Properties(java.util.Properties) Props(org.sonar.process.Props) NodeHealth(org.sonar.process.cluster.health.NodeHealth)

Example 18 with NodeHealth

use of org.sonar.process.cluster.health.NodeHealth in project sonarqube by SonarSource.

the class SearchNodeHealthProviderTest method get_returns_started_from_System2_now_at_constructor_time.

@Test
public void get_returns_started_from_System2_now_at_constructor_time() {
    Properties properties = new Properties();
    long now = setRequiredPropertiesAndMocks(properties);
    SearchNodeHealthProvider underTest = new SearchNodeHealthProvider(new Props(properties), clusterAppState, networkUtils, clock);
    NodeHealth nodeHealth = underTest.get();
    assertThat(nodeHealth.getDetails().getStartedAt()).isEqualTo(now);
    // change now
    when(clock.now()).thenReturn(now);
    NodeHealth newNodeHealth = underTest.get();
    assertThat(newNodeHealth.getDetails().getStartedAt()).isEqualTo(now);
}
Also used : Properties(java.util.Properties) Props(org.sonar.process.Props) NodeHealth(org.sonar.process.cluster.health.NodeHealth) Test(org.junit.Test)

Example 19 with NodeHealth

use of org.sonar.process.cluster.health.NodeHealth in project sonarqube by SonarSource.

the class EsStatusClusterCheckTest method check_ignores_NodeHealth_arg_and_returns_GREEN_without_cause_if_ES_cluster_status_is_GREEN.

@Test
public void check_ignores_NodeHealth_arg_and_returns_GREEN_without_cause_if_ES_cluster_status_is_GREEN() {
    Set<NodeHealth> nodeHealths = ImmutableSet.of(newNodeHealth(NodeHealth.Status.YELLOW));
    when(esClient.clusterHealth(any()).getStatus()).thenReturn(ClusterHealthStatus.GREEN);
    Health health = underTest.check(nodeHealths);
    assertThat(health).isEqualTo(Health.GREEN);
}
Also used : NodeHealth(org.sonar.process.cluster.health.NodeHealth) NodeHealth(org.sonar.process.cluster.health.NodeHealth) Test(org.junit.Test)

Example 20 with NodeHealth

use of org.sonar.process.cluster.health.NodeHealth in project sonarqube by SonarSource.

the class NodeHealthProviderImplTest method getReturnsHostnameFromNetworkUtils.

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);
}
Also used : NodeHealth(org.sonar.process.cluster.health.NodeHealth)

Aggregations

NodeHealth (org.sonar.process.cluster.health.NodeHealth)37 Test (org.junit.Test)33 Arrays (java.util.Arrays)6 Properties (java.util.Properties)6 Random (java.util.Random)6 IntStream (java.util.stream.IntStream)6 RandomStringUtils.randomAlphanumeric (org.apache.commons.lang.RandomStringUtils.randomAlphanumeric)6 Props (org.sonar.process.Props)6 NodeDetails (org.sonar.process.cluster.health.NodeDetails)6 Set (java.util.Set)5 Collectors.toSet (java.util.stream.Collectors.toSet)4 Stream (java.util.stream.Stream)4 Stream.of (java.util.stream.Stream.of)4 GREEN (org.sonar.process.cluster.health.NodeHealth.Status.GREEN)4 RED (org.sonar.process.cluster.health.NodeHealth.Status.RED)4 YELLOW (org.sonar.process.cluster.health.NodeHealth.Status.YELLOW)4 HealthAssert.assertThat (org.sonar.server.health.HealthAssert.assertThat)4 Date (java.util.Date)3 HashSet (java.util.HashSet)2 List (java.util.List)2