@@ -35,6 +35,7 @@ public class CeWsModule extends Module { | |||
SubmitAction.class, | |||
TaskFormatter.class, | |||
TaskAction.class, | |||
TaskTypesAction.class); | |||
TaskTypesAction.class, | |||
WorkerCountAction.class); | |||
} | |||
} |
@@ -0,0 +1,80 @@ | |||
/* | |||
* 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.ce.ws; | |||
import javax.annotation.Nullable; | |||
import org.sonar.api.server.ws.Request; | |||
import org.sonar.api.server.ws.Response; | |||
import org.sonar.api.server.ws.WebService; | |||
import org.sonar.ce.configuration.WorkerCountProvider; | |||
import org.sonar.server.user.UserSession; | |||
import org.sonarqube.ws.WsCe.WorkerCountResponse; | |||
import static org.sonar.server.ws.WsUtils.writeProtobuf; | |||
import static org.sonarqube.ws.client.ce.CeWsParameters.ACTION_WORKER_COUNT; | |||
public class WorkerCountAction implements CeWsAction { | |||
private static final int DEFAULT_WORKER_COUNT = 1; | |||
private final UserSession userSession; | |||
private final WorkerCountProvider workerCountProvider; | |||
public WorkerCountAction(UserSession userSession, @Nullable WorkerCountProvider workerCountProvider) { | |||
this.userSession = userSession; | |||
this.workerCountProvider = workerCountProvider; | |||
} | |||
public WorkerCountAction(UserSession userSession) { | |||
this(userSession, null); | |||
} | |||
@Override | |||
public void define(WebService.NewController controller) { | |||
controller.createAction(ACTION_WORKER_COUNT) | |||
.setDescription("Return number of Compute Engine workers.<br/>" + | |||
"Requires the system administration permission") | |||
.setResponseExample(getClass().getResource("worker_count-example.json")) | |||
.setSince("6.5") | |||
.setInternal(true) | |||
.setHandler(this); | |||
} | |||
@Override | |||
public void handle(Request wsRequest, Response wsResponse) throws Exception { | |||
userSession.checkIsSystemAdministrator(); | |||
writeProtobuf(createResponse(), wsRequest, wsResponse); | |||
} | |||
private WorkerCountResponse createResponse(){ | |||
WorkerCountResponse.Builder builder = WorkerCountResponse.newBuilder(); | |||
if (workerCountProvider == null) { | |||
return builder | |||
.setValue(DEFAULT_WORKER_COUNT) | |||
.setCanSetWorkerCount(false) | |||
.build(); | |||
} | |||
return builder | |||
.setValue(workerCountProvider.get()) | |||
.setCanSetWorkerCount(true) | |||
.build(); | |||
} | |||
} |
@@ -0,0 +1,4 @@ | |||
{ | |||
"value": 5, | |||
"canSetWorkerCount": true | |||
} |
@@ -23,6 +23,7 @@ import org.junit.Test; | |||
import org.sonar.core.platform.ComponentContainer; | |||
import static org.assertj.core.api.Assertions.assertThat; | |||
import static org.sonar.core.platform.ComponentContainer.COMPONENTS_IN_EMPTY_COMPONENT_CONTAINER; | |||
public class CeWsModuleTest { | |||
@@ -30,6 +31,6 @@ public class CeWsModuleTest { | |||
public void verify_count_of_added_components() { | |||
ComponentContainer container = new ComponentContainer(); | |||
new CeWsModule().configure(container); | |||
assertThat(container.size()).isEqualTo(11 + 2 /* injected by ComponentContainer */); | |||
assertThat(container.size()).isEqualTo(12 + COMPONENTS_IN_EMPTY_COMPONENT_CONTAINER); | |||
} | |||
} |
@@ -0,0 +1,101 @@ | |||
/* | |||
* 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.ce.ws; | |||
import org.junit.Rule; | |||
import org.junit.Test; | |||
import org.junit.rules.ExpectedException; | |||
import org.sonar.api.server.ws.WebService; | |||
import org.sonar.ce.configuration.WorkerCountProvider; | |||
import org.sonar.server.exceptions.ForbiddenException; | |||
import org.sonar.server.tester.UserSessionRule; | |||
import org.sonar.server.ws.WsActionTester; | |||
import org.sonarqube.ws.WsCe.WorkerCountResponse; | |||
import static org.assertj.core.api.Assertions.assertThat; | |||
import static org.mockito.Mockito.mock; | |||
import static org.mockito.Mockito.when; | |||
import static org.sonar.test.JsonAssert.assertJson; | |||
public class WorkerCountActionTest { | |||
@Rule | |||
public ExpectedException expectedException = ExpectedException.none(); | |||
@Rule | |||
public UserSessionRule userSession = UserSessionRule.standalone(); | |||
private WorkerCountProvider workerCountProvider = mock(WorkerCountProvider.class); | |||
@Test | |||
public void return_value_and_can_set_worker_count_to_true_when_provider_exists() { | |||
userSession.logIn().setSystemAdministrator(); | |||
when(workerCountProvider.get()).thenReturn(5); | |||
WsActionTester ws = new WsActionTester(new WorkerCountAction(userSession, workerCountProvider)); | |||
WorkerCountResponse response = ws.newRequest().executeProtobuf(WorkerCountResponse.class); | |||
assertThat(response.getValue()).isEqualTo(5); | |||
assertThat(response.getCanSetWorkerCount()).isTrue(); | |||
} | |||
@Test | |||
public void return_1_and_can_set_worker_count_to_false_when_provider_does_not_exist() { | |||
userSession.logIn().setSystemAdministrator(); | |||
WsActionTester ws = new WsActionTester(new WorkerCountAction(userSession)); | |||
WorkerCountResponse response = ws.newRequest().executeProtobuf(WorkerCountResponse.class); | |||
assertThat(response.getValue()).isEqualTo(1); | |||
assertThat(response.getCanSetWorkerCount()).isFalse(); | |||
} | |||
@Test | |||
public void fail_when_not_system_administrator() { | |||
userSession.logIn().setNonSystemAdministrator(); | |||
WsActionTester ws = new WsActionTester(new WorkerCountAction(userSession)); | |||
expectedException.expect(ForbiddenException.class); | |||
ws.newRequest().execute(); | |||
} | |||
@Test | |||
public void test_definition() { | |||
WsActionTester ws = new WsActionTester(new WorkerCountAction(userSession, workerCountProvider)); | |||
WebService.Action action = ws.getDef(); | |||
assertThat(action.key()).isEqualTo("worker_count"); | |||
assertThat(action.since()).isEqualTo("6.5"); | |||
assertThat(action.responseExampleAsString()).isNotEmpty(); | |||
assertThat(action.params()).isEmpty(); | |||
} | |||
@Test | |||
public void test_example() { | |||
userSession.logIn().setSystemAdministrator(); | |||
when(workerCountProvider.get()).thenReturn(5); | |||
WsActionTester ws = new WsActionTester(new WorkerCountAction(userSession, workerCountProvider)); | |||
String response = ws.newRequest().execute().getInput(); | |||
assertJson(response).isSimilarTo(ws.getDef().responseExample()); | |||
} | |||
} |
@@ -22,10 +22,12 @@ package org.sonarqube.ws.client.ce; | |||
import org.sonarqube.ws.WsCe; | |||
import org.sonarqube.ws.WsCe.ActivityResponse; | |||
import org.sonarqube.ws.WsCe.TaskTypesWsResponse; | |||
import org.sonarqube.ws.WsCe.WorkerCountResponse; | |||
import org.sonarqube.ws.client.BaseService; | |||
import org.sonarqube.ws.client.GetRequest; | |||
import org.sonarqube.ws.client.WsConnector; | |||
import static org.sonarqube.ws.client.ce.CeWsParameters.ACTION_WORKER_COUNT; | |||
import static org.sonarqube.ws.client.ce.CeWsParameters.PARAM_COMPONENT_ID; | |||
import static org.sonarqube.ws.client.ce.CeWsParameters.PARAM_COMPONENT_KEY; | |||
import static org.sonarqube.ws.client.ce.CeWsParameters.PARAM_MAX_EXECUTED_AT; | |||
@@ -89,4 +91,8 @@ public class CeService extends BaseService { | |||
WsCe.ActivityStatusWsResponse.parser()); | |||
} | |||
public WorkerCountResponse workerCount() { | |||
return call(new GetRequest(path(ACTION_WORKER_COUNT)), WorkerCountResponse.parser()); | |||
} | |||
} |
@@ -20,6 +20,9 @@ | |||
package org.sonarqube.ws.client.ce; | |||
public class CeWsParameters { | |||
public static final String ACTION_WORKER_COUNT = "worker_count"; | |||
public static final String PARAM_COMPONENT_ID = "componentId"; | |||
public static final String PARAM_COMPONENT_KEY = "componentKey"; | |||
public static final String PARAM_COMPONENT_QUERY = "componentQuery"; |
@@ -61,6 +61,12 @@ message TaskTypesWsResponse { | |||
repeated string taskTypes = 1; | |||
} | |||
// GET api/ce/worker_count | |||
message WorkerCountResponse { | |||
optional int32 value = 1; | |||
optional bool canSetWorkerCount = 2; | |||
} | |||
message Task { | |||
optional string organization = 20; | |||
optional string id = 1; |
@@ -133,4 +133,12 @@ public class CeServiceTest { | |||
assertThat(serviceTester.getGetRequest().getPath()).isEqualTo("api/ce/task"); | |||
assertThat(serviceTester.getGetRequest().getParams()).containsOnly(entry("id", "task_id"), entry("additionalFields", "stacktrace")); | |||
} | |||
@Test | |||
public void worker_count() { | |||
underTest.workerCount(); | |||
assertThat(serviceTester.getGetRequest().getPath()).isEqualTo("api/ce/worker_count"); | |||
assertThat(serviceTester.getGetParser()).isSameAs(WsCe.WorkerCountResponse.parser()); | |||
} | |||
} |
@@ -27,8 +27,11 @@ import java.util.Set; | |||
import java.util.stream.Collectors; | |||
import org.junit.After; | |||
import org.junit.Test; | |||
import org.sonarqube.ws.WsCe.WorkerCountResponse; | |||
import static java.lang.Integer.parseInt; | |||
import static org.assertj.core.api.Assertions.assertThat; | |||
import static util.ItUtils.newAdminWsClient; | |||
import static util.ItUtils.pluginArtifact; | |||
public class WorkerCountTest { | |||
@@ -58,5 +61,9 @@ public class WorkerCountTest { | |||
assertThat(line) | |||
.hasSize(1); | |||
assertThat(line.iterator().next()).contains(workerCount); | |||
assertThat(newAdminWsClient(orchestrator).ce().workerCount()) | |||
.extracting(WorkerCountResponse::getValue, WorkerCountResponse::getCanSetWorkerCount) | |||
.containsOnly(parseInt(workerCount), true); | |||
} | |||
} |