From 9c2a9a75fff56e56799273e4a61364ed51870b7e Mon Sep 17 00:00:00 2001 From: Simon Brandhof Date: Tue, 19 Sep 2017 13:35:38 +0200 Subject: [PATCH] SONAR-9839 return health in api/system/info --- .../process/systeminfo/SystemInfoUtils.java | 10 +++++ .../main/protobuf/process_system_info.proto | 1 + .../platform/monitoring/SonarQubeSection.java | 9 ++++- .../sonar/server/platform/ws/InfoAction.java | 1 + .../health/TestStandaloneHealthChecker.java | 39 +++++++++++++++++++ .../monitoring/SonarQubeSectionTest.java | 19 ++++++++- .../monitoring/SystemInfoTesting.java | 9 ++++- 7 files changed, 85 insertions(+), 3 deletions(-) create mode 100644 server/sonar-server/src/test/java/org/sonar/server/health/TestStandaloneHealthChecker.java diff --git a/server/sonar-process/src/main/java/org/sonar/process/systeminfo/SystemInfoUtils.java b/server/sonar-process/src/main/java/org/sonar/process/systeminfo/SystemInfoUtils.java index 738329480ff..63dbdec055d 100644 --- a/server/sonar-process/src/main/java/org/sonar/process/systeminfo/SystemInfoUtils.java +++ b/server/sonar-process/src/main/java/org/sonar/process/systeminfo/SystemInfoUtils.java @@ -19,6 +19,7 @@ */ package org.sonar.process.systeminfo; +import java.util.Collection; import javax.annotation.CheckForNull; import javax.annotation.Nullable; import org.sonar.process.systeminfo.protobuf.ProtobufSystemInfo; @@ -38,6 +39,15 @@ public class SystemInfoUtils { } } + public static void setAttribute(ProtobufSystemInfo.Section.Builder section, String key, @Nullable Collection values) { + if (values != null) { + section.addAttributesBuilder() + .setKey(key) + .addAllStringValues(values) + .build(); + } + } + public static void setAttribute(ProtobufSystemInfo.Section.Builder section, String key, boolean value) { section.addAttributesBuilder() .setKey(key) diff --git a/server/sonar-process/src/main/protobuf/process_system_info.proto b/server/sonar-process/src/main/protobuf/process_system_info.proto index 8780b677596..e1d4daad792 100644 --- a/server/sonar-process/src/main/protobuf/process_system_info.proto +++ b/server/sonar-process/src/main/protobuf/process_system_info.proto @@ -40,4 +40,5 @@ message Attribute { double double_value = 4; string string_value = 5; } + repeated string string_values = 6; } diff --git a/server/sonar-server/src/main/java/org/sonar/server/platform/monitoring/SonarQubeSection.java b/server/sonar-server/src/main/java/org/sonar/server/platform/monitoring/SonarQubeSection.java index 66eefa13421..ead5a7475fd 100644 --- a/server/sonar-server/src/main/java/org/sonar/server/platform/monitoring/SonarQubeSection.java +++ b/server/sonar-server/src/main/java/org/sonar/server/platform/monitoring/SonarQubeSection.java @@ -33,6 +33,8 @@ import org.sonar.core.util.stream.MoreCollectors; import org.sonar.process.ProcessProperties; import org.sonar.process.systeminfo.protobuf.ProtobufSystemInfo; import org.sonar.server.authentication.IdentityProviderRepository; +import org.sonar.server.health.Health; +import org.sonar.server.health.HealthChecker; import org.sonar.server.platform.ServerIdLoader; import org.sonar.server.platform.ServerLogging; import org.sonar.server.user.SecurityRealmFactory; @@ -51,16 +53,18 @@ public class SonarQubeSection extends BaseSectionMBean implements SonarQubeSecti private final Server server; private final ServerLogging serverLogging; private final ServerIdLoader serverIdLoader; + private final HealthChecker healthChecker; public SonarQubeSection(Configuration config, SecurityRealmFactory securityRealmFactory, IdentityProviderRepository identityProviderRepository, Server server, ServerLogging serverLogging, - ServerIdLoader serverIdLoader) { + ServerIdLoader serverIdLoader, HealthChecker healthChecker) { this.config = config; this.securityRealmFactory = securityRealmFactory; this.identityProviderRepository = identityProviderRepository; this.server = server; this.serverLogging = serverLogging; this.serverIdLoader = serverIdLoader; + this.healthChecker = healthChecker; } @Override @@ -127,6 +131,9 @@ public class SonarQubeSection extends BaseSectionMBean implements SonarQubeSecti setAttribute(protobuf, "Server ID", serverId.getId()); setAttribute(protobuf, "Server ID validated", serverId.isValid()); }); + Health health = healthChecker.checkNode(); + setAttribute(protobuf, "Health", health.getStatus().name()); + setAttribute(protobuf, "Health Causes", health.getCauses()); setAttribute(protobuf, "Version", getVersion()); setAttribute(protobuf, "External User Authentication", getExternalUserAuthentication()); addIfNotEmpty(protobuf, "Accepted external identity providers", getEnabledIdentityProviders()); diff --git a/server/sonar-server/src/main/java/org/sonar/server/platform/ws/InfoAction.java b/server/sonar-server/src/main/java/org/sonar/server/platform/ws/InfoAction.java index 447ec555b28..39491bc60d9 100644 --- a/server/sonar-server/src/main/java/org/sonar/server/platform/ws/InfoAction.java +++ b/server/sonar-server/src/main/java/org/sonar/server/platform/ws/InfoAction.java @@ -113,6 +113,7 @@ public class InfoAction implements SystemWsAction { json.prop(attribute.getKey(), attribute.getStringValue()); break; case VALUE_NOT_SET: + json.name(attribute.getKey()).beginArray().values(attribute.getStringValuesList()).endArray(); break; default: throw new IllegalArgumentException("Unsupported type: " + attribute.getValueCase()); diff --git a/server/sonar-server/src/test/java/org/sonar/server/health/TestStandaloneHealthChecker.java b/server/sonar-server/src/test/java/org/sonar/server/health/TestStandaloneHealthChecker.java new file mode 100644 index 00000000000..f938ba2d247 --- /dev/null +++ b/server/sonar-server/src/test/java/org/sonar/server/health/TestStandaloneHealthChecker.java @@ -0,0 +1,39 @@ +/* + * SonarQube + * Copyright (C) 2009-2017 SonarSource SA + * mailto:info AT sonarsource DOT com + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 3 of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this program; if not, write to the Free Software Foundation, + * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + */ +package org.sonar.server.health; + +public class TestStandaloneHealthChecker implements HealthChecker { + + private Health health = Health.newHealthCheckBuilder().setStatus(Health.Status.GREEN).build(); + + public void setHealth(Health h) { + this.health = h; + } + + @Override + public Health checkNode() { + return health; + } + + @Override + public ClusterHealth checkCluster() { + throw new IllegalStateException(); + } +} diff --git a/server/sonar-server/src/test/java/org/sonar/server/platform/monitoring/SonarQubeSectionTest.java b/server/sonar-server/src/test/java/org/sonar/server/platform/monitoring/SonarQubeSectionTest.java index 9817a2ec101..fac46e81c31 100644 --- a/server/sonar-server/src/test/java/org/sonar/server/platform/monitoring/SonarQubeSectionTest.java +++ b/server/sonar-server/src/test/java/org/sonar/server/platform/monitoring/SonarQubeSectionTest.java @@ -33,11 +33,14 @@ import org.sonar.api.utils.log.LoggerLevel; import org.sonar.process.systeminfo.protobuf.ProtobufSystemInfo; import org.sonar.server.authentication.IdentityProviderRepositoryRule; import org.sonar.server.authentication.TestIdentityProvider; +import org.sonar.server.health.Health; +import org.sonar.server.health.TestStandaloneHealthChecker; import org.sonar.server.platform.ServerId; import org.sonar.server.platform.ServerIdLoader; import org.sonar.server.platform.ServerLogging; import org.sonar.server.user.SecurityRealmFactory; +import static java.util.Arrays.asList; import static org.assertj.core.api.Assertions.assertThat; import static org.mockito.Mockito.mock; import static org.mockito.Mockito.when; @@ -60,9 +63,10 @@ public class SonarQubeSectionTest { private ServerIdLoader serverIdLoader = mock(ServerIdLoader.class); private ServerLogging serverLogging = mock(ServerLogging.class); private SecurityRealmFactory securityRealmFactory = mock(SecurityRealmFactory.class); + private TestStandaloneHealthChecker healthChecker = new TestStandaloneHealthChecker(); private SonarQubeSection underTest = new SonarQubeSection(settings.asConfig(), securityRealmFactory, identityProviderRepository, server, - serverLogging, serverIdLoader); + serverLogging, serverIdLoader, healthChecker); @Before public void setUp() throws Exception { @@ -197,4 +201,17 @@ public class SonarQubeSectionTest { ProtobufSystemInfo.Section protobuf = underTest.toProtobuf(); assertThatAttributeIs(protobuf, "External identity providers whose users are allowed to sign themselves up", "GitHub"); } + + @Test + public void return_health() { + healthChecker.setHealth(Health.newHealthCheckBuilder() + .setStatus(Health.Status.YELLOW) + .addCause("foo") + .addCause("bar") + .build()); + + ProtobufSystemInfo.Section protobuf = underTest.toProtobuf(); + assertThatAttributeIs(protobuf, "Health", "YELLOW"); + SystemInfoTesting.assertThatAttributeHasOnlyValues(protobuf, "Health Causes", asList("foo", "bar")); + } } diff --git a/server/sonar-server/src/test/java/org/sonar/server/platform/monitoring/SystemInfoTesting.java b/server/sonar-server/src/test/java/org/sonar/server/platform/monitoring/SystemInfoTesting.java index bd02658896f..2a2c0d9fb9a 100644 --- a/server/sonar-server/src/test/java/org/sonar/server/platform/monitoring/SystemInfoTesting.java +++ b/server/sonar-server/src/test/java/org/sonar/server/platform/monitoring/SystemInfoTesting.java @@ -19,9 +19,10 @@ */ package org.sonar.server.platform.monitoring; +import java.util.Collection; import org.sonar.process.systeminfo.protobuf.ProtobufSystemInfo; -import static org.assertj.core.api.AssertionsForClassTypes.assertThat; +import static org.assertj.core.api.Assertions.assertThat; import static org.sonar.process.systeminfo.SystemInfoUtils.attribute; public class SystemInfoTesting { @@ -41,4 +42,10 @@ public class SystemInfoTesting { assertThat(value).as(key).isNotNull(); assertThat(value.getBooleanValue()).isEqualTo(expectedValue); } + + public static void assertThatAttributeHasOnlyValues(ProtobufSystemInfo.Section section, String key, Collection expectedValues) { + ProtobufSystemInfo.Attribute value = attribute(section, key); + assertThat(value).as(key).isNotNull(); + assertThat((Collection)value.getStringValuesList()).containsExactlyInAnyOrder(expectedValues.toArray(new String[expectedValues.size()])); + } } -- 2.39.5