--- /dev/null
+/*
+ * SonarQube
+ * Copyright (C) 2009-2016 SonarSource SA
+ * mailto:contact 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.
+ */
+import org.sonar.api.ce.posttask.CeTask;
+import org.sonar.api.ce.posttask.PostProjectAnalysisTask;
+import org.sonar.api.ce.posttask.Project;
+import org.sonar.api.ce.posttask.QualityGate;
+import org.sonar.api.utils.log.Logger;
+import org.sonar.api.utils.log.Loggers;
+
+public class PostProjectAnalysisTaskImpl implements PostProjectAnalysisTask {
+ private static final Logger LOG = Loggers.get(PostProjectAnalysisTaskImpl.class);
+
+ @Override
+ public void finished(ProjectAnalysis analysis) {
+ CeTask ceTask = analysis.getCeTask();
+ Project project = analysis.getProject();
+ QualityGate qualityGate = analysis.getQualityGate();
+ LOG.info("POSTASKPLUGIN: finished() CeTask[{}] Project[{}] Date[{}] QualityGate[{}]",
+ ceTask.getStatus(),
+ project.getKey(),
+ analysis.getDate().getTime(),
+ qualityGate == null ? null : qualityGate.getStatus());
+ }
+}
*/
package it.qualityGate;
+import com.google.common.base.Predicate;
import com.sonar.orchestrator.Orchestrator;
-import com.sonar.orchestrator.build.SonarRunner;
+import com.sonar.orchestrator.build.BuildResult;
+import com.sonar.orchestrator.build.SonarScanner;
import it.Category1Suite;
import java.io.File;
import java.io.IOException;
import java.io.StringReader;
import java.nio.charset.StandardCharsets;
+import java.util.List;
import java.util.Properties;
import org.apache.commons.io.FileUtils;
import org.junit.AfterClass;
import org.sonarqube.ws.client.WsResponse;
import org.sonarqube.ws.client.qualitygate.ProjectStatusWsRequest;
+import static com.google.common.collect.FluentIterable.from;
import static org.assertj.core.api.Assertions.assertThat;
+import static util.ItUtils.extractCeTaskId;
import static util.ItUtils.newAdminWsClient;
import static util.ItUtils.projectDir;
+import static util.ItUtils.retrieveCeLogs;
public class QualityGateTest {
+ private static final String TASK_STATUS_SUCCESS = "SUCCESS";
+ private static final String TASK_STATUS_FAILED = "FAILED";
+ private static final String QG_STATUS_NO_QG = "null";
+ private static final String QG_STATUS_OK = "OK";
+ private static final String QG_STATUS_ERROR = "ERROR";
+ private static final String QG_STATUS_WARN = "WARN";
+
private static long DEFAULT_QUALITY_GATE;
private long provisionedProjectId = -1L;
}
@Test
- public void do_not_compute_status_if_no_gate() {
- SonarRunner build = SonarRunner.create(projectDir("qualitygate/xoo-sample"));
- orchestrator.executeBuild(build);
+ public void do_not_compute_status_if_no_gate() throws IOException {
+ SonarScanner build = SonarScanner.create(projectDir("qualitygate/xoo-sample"));
+ BuildResult buildResult = orchestrator.executeBuild(build);
+
+ verifyQGStatusInPostTask(buildResult, TASK_STATUS_SUCCESS, QG_STATUS_NO_QG);
assertThat(fetchResourceWithGateStatus()).isNull();
}
qgClient().setDefault(empty.id());
try {
- SonarRunner build = SonarRunner.create(projectDir("qualitygate/xoo-sample"));
- orchestrator.executeBuild(build);
+ SonarScanner build = SonarScanner.create(projectDir("qualitygate/xoo-sample"));
+ BuildResult buildResult = orchestrator.executeBuild(build);
+
+ verifyQGStatusInPostTask(buildResult, TASK_STATUS_SUCCESS, QG_STATUS_OK);
assertThat(fetchGateStatus().getData()).isEqualTo("OK");
} finally {
qgClient().createCondition(NewCondition.create(simple.id()).metricKey("ncloc").operator("GT").warningThreshold("40"));
try {
- SonarRunner build = SonarRunner.create(projectDir("qualitygate/xoo-sample"));
- orchestrator.executeBuild(build);
+ SonarScanner build = SonarScanner.create(projectDir("qualitygate/xoo-sample"));
+ BuildResult buildResult = orchestrator.executeBuild(build);
+
+ verifyQGStatusInPostTask(buildResult, TASK_STATUS_SUCCESS, QG_STATUS_OK);
assertThat(fetchGateStatus().getData()).isEqualTo("OK");
} finally {
qgClient().createCondition(NewCondition.create(simple.id()).metricKey("ncloc").operator("GT").warningThreshold("10"));
try {
- SonarRunner build = SonarRunner.create(projectDir("qualitygate/xoo-sample"));
- orchestrator.executeBuild(build);
+ SonarScanner build = SonarScanner.create(projectDir("qualitygate/xoo-sample"));
+ BuildResult buildResult = orchestrator.executeBuild(build);
+
+ verifyQGStatusInPostTask(buildResult, TASK_STATUS_SUCCESS, QG_STATUS_WARN);
assertThat(fetchGateStatus().getData()).isEqualTo("WARN");
} finally {
qgClient().createCondition(NewCondition.create(simple.id()).metricKey("ncloc").operator("GT").errorThreshold("10"));
try {
- SonarRunner build = SonarRunner.create(projectDir("qualitygate/xoo-sample"));
- orchestrator.executeBuild(build);
+ SonarScanner build = SonarScanner.create(projectDir("qualitygate/xoo-sample"));
+ BuildResult buildResult = orchestrator.executeBuild(build);
+
+ verifyQGStatusInPostTask(buildResult, TASK_STATUS_SUCCESS, QG_STATUS_ERROR);
assertThat(fetchGateStatus().getData()).isEqualTo("ERROR");
} finally {
qgClient().selectProject(error.id(), provisionedProjectId);
try {
- SonarRunner build = SonarRunner.create(projectDir("qualitygate/xoo-sample"));
- orchestrator.executeBuild(build);
+ SonarScanner build = SonarScanner.create(projectDir("qualitygate/xoo-sample"));
+ BuildResult buildResult = orchestrator.executeBuild(build);
+
+
+ verifyQGStatusInPostTask(buildResult, TASK_STATUS_SUCCESS, QG_STATUS_ERROR);
assertThat(fetchGateStatus().getData()).isEqualTo("ERROR");
} finally {
qgClient().setDefault(allTypes.id());
try {
- SonarRunner build = SonarRunner.create(projectDir("qualitygate/xoo-sample"))
+ SonarScanner build = SonarScanner.create(projectDir("qualitygate/xoo-sample"))
.setProperty("sonar.cpd.xoo.minimumLines", "2")
.setProperty("sonar.cpd.xoo.minimumTokens", "5");
- orchestrator.executeBuild(build);
+ BuildResult buildResult = orchestrator.executeBuild(build);
+
+
+ verifyQGStatusInPostTask(buildResult, TASK_STATUS_SUCCESS, QG_STATUS_WARN);
Measure alertStatus = fetchGateStatus();
assertThat(alertStatus.getData()).isEqualTo("WARN");
try {
File projectDir = projectDir("qualitygate/xoo-sample");
- SonarRunner build = SonarRunner.create(projectDir);
- orchestrator.executeBuild(build);
+ SonarScanner build = SonarScanner.create(projectDir);
+ BuildResult buildResult = orchestrator.executeBuild(build);
+
+ verifyQGStatusInPostTask(buildResult, TASK_STATUS_SUCCESS, QG_STATUS_ERROR);
String taskId = getTaskIdInLocalReport(projectDir);
String analysisId = getAnalysisId(taskId);
}
}
+ private void verifyQGStatusInPostTask(BuildResult buildResult, String taskStatus, String qgStatus) {
+ List<String> postTaskLogLines = extractPosttaskPluginLogs(retrieveCeLogs(orchestrator, extractCeTaskId(buildResult)));
+
+ assertThat(postTaskLogLines).hasSize(1);
+ assertThat(postTaskLogLines.iterator().next())
+ .contains("CeTask[" + taskStatus + "]")
+ .contains("Project[sample]")
+ .contains("QualityGate[" + qgStatus + "]");
+ }
+
private String getAnalysisId(String taskId) throws IOException {
WsResponse activity = wsClient
.wsConnector()
private static QualityGateClient qgClient() {
return orchestrator.getServer().adminWsClient().qualityGateClient();
}
+
+ private static List<String> extractPosttaskPluginLogs(Iterable<String> ceLogs) {
+ return from(ceLogs)
+ .filter(new Predicate<String>() {
+ @Override
+ public boolean apply(String s) {
+ return s.contains("POSTASKPLUGIN: finished()");
+ }
+ }).toList();
+ }
}
*/
package util;
+import com.google.common.base.Function;
+import com.google.common.base.Predicate;
+import com.google.common.base.Splitter;
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.Iterables;
import com.sonar.orchestrator.Orchestrator;
import org.sonarqube.ws.client.HttpWsClient;
import org.sonarqube.ws.client.WsClient;
+import static com.google.common.base.Preconditions.checkState;
import static com.google.common.collect.FluentIterable.from;
import static com.sonar.orchestrator.container.Server.ADMIN_LOGIN;
import static com.sonar.orchestrator.container.Server.ADMIN_PASSWORD;
import static org.assertj.core.api.Assertions.fail;
public class ItUtils {
+ public static final Splitter LINE_SPLITTER = Splitter.on(System.getProperty("line.separator"));
private ItUtils() {
}
return sdf.format(d);
}
+ public static Iterable<String> retrieveCeLogs(Orchestrator orchestrator, String taskId) {
+ String ceLogs = orchestrator.getServer().adminWsClient().get("/api/ce/logs?taskId=" + taskId);
+
+ return LINE_SPLITTER.split(ceLogs);
+ }
+
+ public static String extractCeTaskId(BuildResult buildResult) {
+ List<String> taskIds = extractCeTaskIds(buildResult);
+ checkState(taskIds.size() == 1, "More than one task id retrieved from logs");
+ return taskIds.iterator().next();
+ }
+
+ public static List<String> extractCeTaskIds(BuildResult buildResult) {
+ String logs = buildResult.getLogs();
+ return from(LINE_SPLITTER.split(logs))
+ .filter(new Predicate<String>() {
+ @Override
+ public boolean apply(String s) {
+ return s.contains("More about the report processing at");
+ }
+ }).transform(new Function<String, String>() {
+ @Nullable
+ @Override
+ public String apply(String s) {
+ return s.substring(s.length() - 20, s.length());
+ }
+ }).toList();
+ }
+
}