]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-9839 return health in api/system/info
authorSimon Brandhof <simon.brandhof@sonarsource.com>
Tue, 19 Sep 2017 11:35:38 +0000 (13:35 +0200)
committerSimon Brandhof <simon.brandhof@sonarsource.com>
Tue, 26 Sep 2017 21:49:37 +0000 (23:49 +0200)
server/sonar-process/src/main/java/org/sonar/process/systeminfo/SystemInfoUtils.java
server/sonar-process/src/main/protobuf/process_system_info.proto
server/sonar-server/src/main/java/org/sonar/server/platform/monitoring/SonarQubeSection.java
server/sonar-server/src/main/java/org/sonar/server/platform/ws/InfoAction.java
server/sonar-server/src/test/java/org/sonar/server/health/TestStandaloneHealthChecker.java [new file with mode: 0644]
server/sonar-server/src/test/java/org/sonar/server/platform/monitoring/SonarQubeSectionTest.java
server/sonar-server/src/test/java/org/sonar/server/platform/monitoring/SystemInfoTesting.java

index 738329480ff3f354e32a6169af5f8cf6072b482d..63dbdec055d93c767a0bc92c9f248e3432927f79 100644 (file)
@@ -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<String> 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)
index 8780b677596c7984e99f26c0cd8488988b27d44c..e1d4daad792ec6b04ee00583b73e7e9d86071190 100644 (file)
@@ -40,4 +40,5 @@ message Attribute {
     double double_value = 4;
     string string_value = 5;
   }
+  repeated string string_values = 6;
 }
index 66eefa1342111704280d3faf4ed482e2d532c49b..ead5a7475fda9373193b62ccbdae2dc88cc11373 100644 (file)
@@ -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());
index 447ec555b2823b6ecaf6a93ce2f998eefdf0919b..39491bc60d91e17d69cf09bb9e7b3cb138894a91 100644 (file)
@@ -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 (file)
index 0000000..f938ba2
--- /dev/null
@@ -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();
+  }
+}
index 9817a2ec1010c0d7c7ffc303f845539700661d7f..fac46e81c310f2ffa23f0070904bdd971ae13d00 100644 (file)
@@ -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"));
+  }
 }
index bd02658896f537d910785017495eee8e719925d7..2a2c0d9fb9a6e80b7032a333f54ddfd6b618fbb6 100644 (file)
  */
 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<String> expectedValues) {
+    ProtobufSystemInfo.Attribute value = attribute(section, key);
+    assertThat(value).as(key).isNotNull();
+    assertThat((Collection<String>)value.getStringValuesList()).containsExactlyInAnyOrder(expectedValues.toArray(new String[expectedValues.size()]));
+  }
 }