diff options
8 files changed, 70 insertions, 69 deletions
diff --git a/server/sonar-server/src/main/java/org/sonar/server/platform/ws/StatusAction.java b/server/sonar-server/src/main/java/org/sonar/server/platform/ws/StatusAction.java index 2a067f49de7..8b29e653191 100644 --- a/server/sonar-server/src/main/java/org/sonar/server/platform/ws/StatusAction.java +++ b/server/sonar-server/src/main/java/org/sonar/server/platform/ws/StatusAction.java @@ -24,10 +24,12 @@ import org.sonar.api.platform.Server; import org.sonar.api.server.ws.Request; import org.sonar.api.server.ws.Response; import org.sonar.api.server.ws.WebService; -import org.sonar.api.utils.text.JsonWriter; +import org.sonar.core.util.Protobuf; import org.sonar.server.app.RestartFlagHolder; import org.sonar.server.platform.Platform; import org.sonar.server.platform.db.migration.DatabaseMigrationState; +import org.sonar.server.ws.WsUtils; +import org.sonarqube.ws.WsSystem; /** * Implementation of the {@code status} action for the System WebService. @@ -40,7 +42,7 @@ public class StatusAction implements SystemWsAction { private final RestartFlagHolder restartFlagHolder; public StatusAction(Server server, DatabaseMigrationState migrationState, - Platform platform, RestartFlagHolder restartFlagHolder) { + Platform platform, RestartFlagHolder restartFlagHolder) { this.server = server; this.migrationState = migrationState; this.platform = platform; @@ -71,30 +73,22 @@ public class StatusAction implements SystemWsAction { @Override public void handle(Request request, Response response) throws Exception { - JsonWriter json = response.newJsonWriter(); - writeJson(json); - json.close(); + WsSystem.StatusResponse.Builder protobuf = WsSystem.StatusResponse.newBuilder(); + Protobuf.setNullable(server.getId(), protobuf::setId); + Protobuf.setNullable(server.getVersion(), protobuf::setVersion); + protobuf.setStatus(computeStatus()); + WsUtils.writeProtobuf(protobuf.build(), request, response); } - private void writeJson(JsonWriter json) { - Status status = computeStatus(); - - json.beginObject(); - json.prop("id", server.getId()); - json.prop("version", server.getVersion()); - json.prop("status", status.toString()); - json.endObject(); - } - - private Status computeStatus() { + private WsSystem.Status computeStatus() { Platform.Status platformStatus = platform.status(); switch (platformStatus) { case BOOTING: // can not happen since there can not even exist an instance of the current class // unless the Platform's status is UP/SAFEMODE/STARTING - return Status.DOWN; + return WsSystem.Status.DOWN; case UP: - return restartFlagHolder.isRestarting() ? Status.RESTARTING : Status.UP; + return restartFlagHolder.isRestarting() ? WsSystem.Status.RESTARTING : WsSystem.Status.UP; case STARTING: return computeStatusInStarting(); case SAFEMODE: @@ -104,41 +98,37 @@ public class StatusAction implements SystemWsAction { } } - private Status computeStatusInStarting() { + private WsSystem.Status computeStatusInStarting() { DatabaseMigrationState.Status databaseMigrationStatus = migrationState.getStatus(); switch (databaseMigrationStatus) { case NONE: - return Status.STARTING; + return WsSystem.Status.STARTING; case RUNNING: - return Status.DB_MIGRATION_RUNNING; + return WsSystem.Status.DB_MIGRATION_RUNNING; case FAILED: - return Status.DOWN; + return WsSystem.Status.DOWN; case SUCCEEDED: // DB migration can be finished while we haven't yet finished SQ's initialization - return Status.STARTING; + return WsSystem.Status.STARTING; default: throw new IllegalArgumentException("Unsupported DatabaseMigration.Status " + databaseMigrationStatus); } } - private Status computeStatusInSafemode() { + private WsSystem.Status computeStatusInSafemode() { DatabaseMigrationState.Status databaseMigrationStatus = migrationState.getStatus(); switch (databaseMigrationStatus) { case NONE: - return Status.DB_MIGRATION_NEEDED; + return WsSystem.Status.DB_MIGRATION_NEEDED; case RUNNING: - return Status.DB_MIGRATION_RUNNING; + return WsSystem.Status.DB_MIGRATION_RUNNING; case FAILED: - return Status.DOWN; + return WsSystem.Status.DOWN; case SUCCEEDED: - return Status.STARTING; + return WsSystem.Status.STARTING; default: throw new IllegalArgumentException("Unsupported DatabaseMigration.Status " + databaseMigrationStatus); } } - private enum Status { - UP, DOWN, DB_MIGRATION_NEEDED, DB_MIGRATION_RUNNING, STARTING, RESTARTING - } - } diff --git a/sonar-ws/src/main/java/org/sonarqube/ws/client/system/SystemService.java b/sonar-ws/src/main/java/org/sonarqube/ws/client/system/SystemService.java index ef31bfaed45..cb8c55aec61 100644 --- a/sonar-ws/src/main/java/org/sonarqube/ws/client/system/SystemService.java +++ b/sonar-ws/src/main/java/org/sonarqube/ws/client/system/SystemService.java @@ -37,4 +37,8 @@ public class SystemService extends BaseService { public void restart() { call(new PostRequest(path("restart"))); } + + public WsSystem.StatusResponse status() { + return call(new GetRequest(path("status")), WsSystem.StatusResponse.parser()); + } } diff --git a/sonar-ws/src/main/protobuf/ws-system.proto b/sonar-ws/src/main/protobuf/ws-system.proto index 4f0fd9af5fc..39ad9210eb7 100644 --- a/sonar-ws/src/main/protobuf/ws-system.proto +++ b/sonar-ws/src/main/protobuf/ws-system.proto @@ -30,6 +30,13 @@ message HealthResponse { repeated Cause causes = 2; } +// GET api/system/status +message StatusResponse { + optional string id = 1; + optional string version = 2; + optional Status status = 3; +} + message Cause { optional string message = 1; } @@ -39,3 +46,12 @@ enum Health { YELLOW = 1; RED = 2; } + +enum Status { + STARTING = 0; + UP = 1; + DOWN = 2; + RESTARTING = 3; + DB_MIGRATION_NEEDED = 4; + DB_MIGRATION_RUNNING = 5; +} diff --git a/sonar-ws/src/test/java/org/sonarqube/ws/client/system/SystemServiceTest.java b/sonar-ws/src/test/java/org/sonarqube/ws/client/system/SystemServiceTest.java index d9fe6011c4f..a473bda635e 100644 --- a/sonar-ws/src/test/java/org/sonarqube/ws/client/system/SystemServiceTest.java +++ b/sonar-ws/src/test/java/org/sonarqube/ws/client/system/SystemServiceTest.java @@ -46,6 +46,16 @@ public class SystemServiceTest { } @Test + public void test_status() throws Exception { + underTest.status(); + + GetRequest getRequest = serviceTester.getGetRequest(); + serviceTester.assertThat(getRequest) + .hasPath("status") + .andNoOtherParam(); + } + + @Test public void test_restart() throws Exception { underTest.restart(); diff --git a/tests/src/test/java/org/sonarqube/tests/serverSystem/RestartTest.java b/tests/src/test/java/org/sonarqube/tests/serverSystem/RestartTest.java index ee69d37febb..eac784ff61e 100644 --- a/tests/src/test/java/org/sonarqube/tests/serverSystem/RestartTest.java +++ b/tests/src/test/java/org/sonarqube/tests/serverSystem/RestartTest.java @@ -29,10 +29,9 @@ import org.junit.rules.DisableOnDebug; import org.junit.rules.ExpectedException; import org.junit.rules.TestRule; import org.junit.rules.Timeout; -import org.sonarqube.ws.client.GetRequest; +import org.sonarqube.ws.WsSystem; import org.sonarqube.ws.client.PostRequest; import org.sonarqube.ws.client.WsClient; -import org.sonarqube.ws.client.WsResponse; import org.sonarqube.ws.client.permission.AddUserWsRequest; import util.ItUtils; @@ -76,8 +75,7 @@ public class RestartTest { createSystemAdministrator("big", "boss"); ItUtils.newUserWsClient(orchestrator, "big", "boss").system().restart(); - WsResponse wsResponse = newAdminWsClient(orchestrator).wsConnector().call(new GetRequest("/api/system/status")).failIfNotSuccessful(); - assertThat(wsResponse.content()).contains("RESTARTING"); + assertThat(newAdminWsClient(orchestrator).system().status().getStatus()).isEqualTo(WsSystem.Status.RESTARTING); // we just wait five seconds, for a lack of a better approach to waiting for the restart process to start in SQ Thread.sleep(5000); diff --git a/tests/src/test/java/org/sonarqube/tests/serverSystem/ServerSystemRestartingOrchestrator.java b/tests/src/test/java/org/sonarqube/tests/serverSystem/ServerSystemRestartingOrchestrator.java index 02e7887b6cf..cd4e2c1ac78 100644 --- a/tests/src/test/java/org/sonarqube/tests/serverSystem/ServerSystemRestartingOrchestrator.java +++ b/tests/src/test/java/org/sonarqube/tests/serverSystem/ServerSystemRestartingOrchestrator.java @@ -22,19 +22,17 @@ package org.sonarqube.tests.serverSystem; import com.sonar.orchestrator.Orchestrator; import com.sonar.orchestrator.locator.FileLocation; import java.io.File; -import java.util.Map; import org.apache.commons.io.FileUtils; import org.junit.After; import org.junit.Rule; import org.junit.Test; import org.junit.rules.ExpectedException; -import org.sonarqube.ws.client.GetRequest; -import org.sonarqube.ws.client.WsResponse; +import org.sonarqube.ws.WsSystem; import util.ItUtils; import static org.assertj.core.api.Assertions.assertThat; import static org.junit.Assert.fail; -import static util.ItUtils.newWsClient; +import static util.ItUtils.newAdminWsClient; /** * This class start a new orchestrator on each test case @@ -79,9 +77,7 @@ public class ServerSystemRestartingOrchestrator { .build(); orchestrator.start(); - WsResponse statusResponse = newWsClient(orchestrator).wsConnector().call(new GetRequest("api/system/status")); - Map<String, Object> json = ItUtils.jsonToMap(statusResponse.content()); - assertThat(json.get("status")).isEqualTo("UP"); + assertThat(newAdminWsClient(orchestrator).system().status().getStatus()).isEqualTo(WsSystem.Status.UP); } // SONAR-4748 diff --git a/tests/src/test/java/org/sonarqube/tests/serverSystem/ServerSystemTest.java b/tests/src/test/java/org/sonarqube/tests/serverSystem/ServerSystemTest.java index 5be7753116e..ac888b10998 100644 --- a/tests/src/test/java/org/sonarqube/tests/serverSystem/ServerSystemTest.java +++ b/tests/src/test/java/org/sonarqube/tests/serverSystem/ServerSystemTest.java @@ -36,8 +36,8 @@ import org.sonarqube.tests.Category4Suite; import org.sonarqube.tests.Tester; import org.sonarqube.ws.MediaTypes; import org.sonarqube.ws.ServerId.ShowWsResponse; +import org.sonarqube.ws.WsSystem; import org.sonarqube.ws.client.GetRequest; -import org.sonarqube.ws.client.WsResponse; import util.ItUtils; import static org.apache.commons.lang.StringUtils.startsWithAny; @@ -62,10 +62,10 @@ public class ServerSystemTest { @Test public void get_sonarqube_version() { - Map<String, Object> json = callStatus(); + WsSystem.StatusResponse response = tester.wsClient().system().status(); - String version = (String) json.get("version"); - if (!startsWithAny(version, new String[]{"6."})) { + String version = response.getVersion(); + if (!startsWithAny(version, new String[]{"6.", "7.", "8."})) { fail("Bad version: " + version); } } @@ -96,11 +96,6 @@ public class ServerSystemTest { assertThat(serverId).isNotEmpty(); } - private Map<String, Object> callStatus() { - WsResponse statusResponse = tester.wsClient().wsConnector().call(new GetRequest("api/system/status")); - return ItUtils.jsonToMap(statusResponse.content()); - } - /** * See http://jira.codehaus.org/browse/SONAR-2727 */ diff --git a/tests/src/test/java/org/sonarqube/tests/serverSystem/SystemStateTest.java b/tests/src/test/java/org/sonarqube/tests/serverSystem/SystemStateTest.java index 9559380e2b6..e942a432e96 100644 --- a/tests/src/test/java/org/sonarqube/tests/serverSystem/SystemStateTest.java +++ b/tests/src/test/java/org/sonarqube/tests/serverSystem/SystemStateTest.java @@ -24,7 +24,6 @@ import com.sonar.orchestrator.util.NetworkUtils; import java.io.File; import java.io.IOException; import java.net.InetAddress; -import java.util.Map; import java.util.Optional; import java.util.function.Supplier; import org.apache.commons.io.FileUtils; @@ -36,10 +35,7 @@ import org.junit.rules.TestRule; import org.junit.rules.Timeout; import org.sonarqube.tests.Elasticsearch; import org.sonarqube.ws.WsSystem; -import org.sonarqube.ws.client.GetRequest; import org.sonarqube.ws.client.WsClient; -import org.sonarqube.ws.client.WsResponse; -import util.ItUtils; import static com.google.common.base.Preconditions.checkState; import static org.assertj.core.api.Assertions.assertThat; @@ -64,17 +60,17 @@ public class SystemStateTest { commander.start(lock); commander.waitFor(() -> commander.webLogsContain("ServerStartupLock - Waiting for file to be deleted")); - commander.verifyStatus("STARTING"); + commander.verifyStatus(WsSystem.Status.STARTING); commander.verifyHealth(WsSystem.Health.RED, "SonarQube webserver is not up"); lock.unlockWeb(); // status is UP as soon as web server is up, whatever the status of Compute Engine - commander.waitFor(() -> "UP".equals(commander.status().orElse(null))); + commander.waitFor(() -> WsSystem.Status.UP == commander.status().orElse(null)); commander.verifyHealth(WsSystem.Health.RED, "Compute Engine is not operational"); lock.unlockCe(); commander.waitForHealth(WsSystem.Health.GREEN); - commander.verifyStatus("UP"); + commander.verifyStatus(WsSystem.Status.UP); } } @@ -86,12 +82,12 @@ public class SystemStateTest { commander.makeElasticsearchYellow(); commander.waitForHealth(WsSystem.Health.YELLOW, "Elasticsearch status is YELLOW"); - commander.verifyStatus("UP"); + commander.verifyStatus(WsSystem.Status.UP); commander.makeElasticsearchGreen(); commander.waitForHealth(WsSystem.Health.GREEN); // status does not change after being UP - commander.verifyStatus("UP"); + commander.verifyStatus(WsSystem.Status.UP); } } @@ -165,15 +161,11 @@ public class SystemStateTest { } } - Optional<String> status() { + Optional<WsSystem.Status> status() { if (orchestrator.getServer() != null) { WsClient wsClient = newWsClient(orchestrator); try { - WsResponse statusResponse = wsClient.wsConnector().call(new GetRequest("api/system/status")); - if (statusResponse.isSuccessful()) { - Map<String, Object> json = ItUtils.jsonToMap(statusResponse.content()); - return Optional.ofNullable((String) json.get("status")); - } + return Optional.of(wsClient.system().status().getStatus()); } catch (Exception e) { // server does not accept connections } @@ -181,7 +173,7 @@ public class SystemStateTest { return Optional.empty(); } - void verifyStatus(String expectedStatus) { + void verifyStatus(WsSystem.Status expectedStatus) { assertThat(status()).hasValue(expectedStatus); } @@ -209,7 +201,7 @@ public class SystemStateTest { void verifyHealth(WsSystem.Health expectedHealth, String... expectedMessages) { WsSystem.HealthResponse response = healthResponse().get(); - assertThat(response.getHealth()).isEqualTo(expectedHealth); + assertThat(response.getHealth()).isEqualTo(expectedHealth); assertThat(response.getCausesList()) .extracting(WsSystem.Cause::getMessage) .containsExactlyInAnyOrder(expectedMessages); |