aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorlukasz-jarocki-sonarsource <lukasz.jarocki@sonarsource.com>2024-10-21 13:25:00 +0200
committersonartech <sonartech@sonarsource.com>2024-10-21 20:04:00 +0000
commit63eec3eb8ea425f3f4a7f4185b33a5e444a03152 (patch)
tree9444712127cc93a642d65d8b3ace8bcd392ba5eb
parentfecf25981b0b8ef000ff329b736e34dc57d3f5c3 (diff)
downloadsonarqube-63eec3eb8ea425f3f4a7f4185b33a5e444a03152.tar.gz
sonarqube-63eec3eb8ea425f3f4a7f4185b33a5e444a03152.zip
SONAR-23430 modified api/system/liveness to not pollute logs
-rw-r--r--server/sonar-webserver-webapi/src/main/java/org/sonar/server/platform/ws/LivenessActionSupport.java6
-rw-r--r--server/sonar-webserver-webapi/src/test/java/org/sonar/server/platform/ws/LivenessActionTest.java11
-rw-r--r--server/sonar-webserver-webapi/src/test/java/org/sonar/server/platform/ws/SafeModeLivenessActionTest.java10
3 files changed, 16 insertions, 11 deletions
diff --git a/server/sonar-webserver-webapi/src/main/java/org/sonar/server/platform/ws/LivenessActionSupport.java b/server/sonar-webserver-webapi/src/main/java/org/sonar/server/platform/ws/LivenessActionSupport.java
index e92c75b914f..85890a0e011 100644
--- a/server/sonar-webserver-webapi/src/main/java/org/sonar/server/platform/ws/LivenessActionSupport.java
+++ b/server/sonar-webserver-webapi/src/main/java/org/sonar/server/platform/ws/LivenessActionSupport.java
@@ -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);
}
}
diff --git a/server/sonar-webserver-webapi/src/test/java/org/sonar/server/platform/ws/LivenessActionTest.java b/server/sonar-webserver-webapi/src/test/java/org/sonar/server/platform/ws/LivenessActionTest.java
index 12b337cdc07..e1d3968ce61 100644
--- a/server/sonar-webserver-webapi/src/test/java/org/sonar/server/platform/ws/LivenessActionTest.java
+++ b/server/sonar-webserver-webapi/src/test/java/org/sonar/server/platform/ws/LivenessActionTest.java
@@ -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
diff --git a/server/sonar-webserver-webapi/src/test/java/org/sonar/server/platform/ws/SafeModeLivenessActionTest.java b/server/sonar-webserver-webapi/src/test/java/org/sonar/server/platform/ws/SafeModeLivenessActionTest.java
index 091bcd835d6..dddbcdd7d32 100644
--- a/server/sonar-webserver-webapi/src/test/java/org/sonar/server/platform/ws/SafeModeLivenessActionTest.java
+++ b/server/sonar-webserver-webapi/src/test/java/org/sonar/server/platform/ws/SafeModeLivenessActionTest.java
@@ -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