]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-23430 modified api/system/liveness to not pollute logs
authorlukasz-jarocki-sonarsource <lukasz.jarocki@sonarsource.com>
Mon, 21 Oct 2024 11:25:00 +0000 (13:25 +0200)
committersonartech <sonartech@sonarsource.com>
Mon, 21 Oct 2024 20:04:00 +0000 (20:04 +0000)
server/sonar-webserver-webapi/src/main/java/org/sonar/server/platform/ws/LivenessActionSupport.java
server/sonar-webserver-webapi/src/test/java/org/sonar/server/platform/ws/LivenessActionTest.java
server/sonar-webserver-webapi/src/test/java/org/sonar/server/platform/ws/SafeModeLivenessActionTest.java

index e92c75b914f6baa9d91f5aeb8e9e332241cebe50..85890a0e011fd3f71bb11bb8cdd4581e7cc5a398 100644 (file)
@@ -19,6 +19,8 @@
  */
 package org.sonar.server.platform.ws;
 
+import javax.servlet.http.HttpServletResponse;
+import org.sonar.api.server.ws.Change;
 import org.sonar.api.server.ws.Response;
 import org.sonar.api.server.ws.WebService;
 import org.sonar.server.common.platform.LivenessChecker;
@@ -46,6 +48,8 @@ public class LivenessActionSupport {
         "</p>")
       .setSince("9.1")
       .setInternal(true)
+      .setChangelog(new Change("10.8", "The endpoint doesn't log an error anymore when the liveness check fails. " +
+        "The failed HTTP status code changed from 500 to 503."))
       .setContentType(Response.ContentType.NO_CONTENT)
       .setHandler(handler);
   }
@@ -54,7 +58,7 @@ public class LivenessActionSupport {
     if (livenessChecker.liveness()) {
       response.noContent();
     } else {
-      throw new IllegalStateException("Liveness check failed");
+      response.stream().setStatus(HttpServletResponse.SC_SERVICE_UNAVAILABLE);
     }
   }
 
index 12b337cdc071466883dcc4b2d8d7936946d48be0..e1d3968ce61edf468b9f5105525fdcf603aa094d 100644 (file)
@@ -27,6 +27,7 @@ import org.sonar.server.exceptions.ForbiddenException;
 import org.sonar.server.tester.UserSessionRule;
 import org.sonar.server.user.SystemPasscode;
 import org.sonar.server.ws.TestRequest;
+import org.sonar.server.ws.TestResponse;
 import org.sonar.server.ws.WsActionTester;
 
 import static org.assertj.core.api.Assertions.assertThat;
@@ -55,6 +56,7 @@ public class LivenessActionTest {
     assertThat(definition.since()).isEqualTo("9.1");
     assertThat(definition.isInternal()).isTrue();
     assertThat(definition.responseExample()).isNull();
+    assertThat(definition.changelog()).isNotEmpty();
     assertThat(definition.params()).isEmpty();
   }
 
@@ -80,14 +82,13 @@ public class LivenessActionTest {
   }
 
   @Test
-  public void liveness_check_failed_expect_500() {
+  public void liveness_check_failed_expect_503() {
     when(systemPasscode.isValid(any())).thenReturn(true);
     when(livenessChecker.liveness()).thenReturn(false);
 
-    TestRequest request = underTest.newRequest();
-    assertThatThrownBy(request::execute)
-      .isInstanceOf(IllegalStateException.class)
-      .hasMessage("Liveness check failed");
+    TestResponse response = underTest.newRequest().execute();
+
+    assertThat(response.getStatus()).isEqualTo(503);
   }
 
   @Test
index 091bcd835d60355aada21cf2ae898d2e24f0ec3a..dddbcdd7d325570bcdd9ae00b2704e4285e561d3 100644 (file)
@@ -25,6 +25,7 @@ import org.sonar.server.common.platform.LivenessChecker;
 import org.sonar.server.exceptions.ForbiddenException;
 import org.sonar.server.user.SystemPasscode;
 import org.sonar.server.ws.TestRequest;
+import org.sonar.server.ws.TestResponse;
 import org.sonar.server.ws.WsActionTester;
 
 import static org.assertj.core.api.Assertions.assertThat;
@@ -64,14 +65,13 @@ public class SafeModeLivenessActionTest {
   }
 
   @Test
-  public void liveness_check_failed_expect_500() {
+  public void liveness_check_failed_expect_503() {
     when(systemPasscode.isValid(any())).thenReturn(true);
     when(livenessChecker.liveness()).thenReturn(false);
 
-    TestRequest request = underTest.newRequest();
-    assertThatThrownBy(request::execute)
-      .isInstanceOf(IllegalStateException.class)
-      .hasMessage("Liveness check failed");
+    TestResponse response = underTest.newRequest().execute();
+
+    assertThat(response.getStatus()).isEqualTo(503);
   }
 
   @Test