From 1f69c8dd6cf99519d927cb8f7938d37dc3ac5b14 Mon Sep 17 00:00:00 2001 From: =?utf8?q?S=C3=A9bastien=20Lesaint?= Date: Fri, 1 Apr 2016 18:34:16 +0200 Subject: [PATCH] SONAR-7488 add ITs for PostProjectAnalysisTask --- it/it-plugins/pom.xml | 1 + it/it-plugins/posttask-plugin/pom.xml | 39 +++++++++ .../java/PostProjectAnalysisTaskImpl.java | 41 +++++++++ .../src/main/java/PostTaskPlugin.java | 28 ++++++ .../src/test/java/it/Category1Suite.java | 2 + .../java/it/qualityGate/QualityGateTest.java | 87 +++++++++++++++---- it/it-tests/src/test/java/util/ItUtils.java | 34 ++++++++ 7 files changed, 214 insertions(+), 18 deletions(-) create mode 100644 it/it-plugins/posttask-plugin/pom.xml create mode 100644 it/it-plugins/posttask-plugin/src/main/java/PostProjectAnalysisTaskImpl.java create mode 100644 it/it-plugins/posttask-plugin/src/main/java/PostTaskPlugin.java diff --git a/it/it-plugins/pom.xml b/it/it-plugins/pom.xml index 71023da0185..bee94bef359 100644 --- a/it/it-plugins/pom.xml +++ b/it/it-plugins/pom.xml @@ -51,5 +51,6 @@ sonar-subcategories-plugin ui-extensions-plugin issue-filter-plugin + posttask-plugin diff --git a/it/it-plugins/posttask-plugin/pom.xml b/it/it-plugins/posttask-plugin/pom.xml new file mode 100644 index 00000000000..1933cb710df --- /dev/null +++ b/it/it-plugins/posttask-plugin/pom.xml @@ -0,0 +1,39 @@ + + + 4.0.0 + + + org.sonarsource.sonarqube + it-plugins + 5.5-SNAPSHOT + + + posttask-plugin + 1.0-SNAPSHOT + sonar-plugin + SonarQube Integration Tests :: Plugins :: PostTask + Plugin testing the Compute Engine Post Task API + + + + org.sonarsource.sonarqube + sonar-plugin-api + ${apiVersion} + provided + + + + + + + org.sonarsource.sonar-packaging-maven-plugin + sonar-packaging-maven-plugin + 1.15 + true + + PostTaskPlugin + + + + + diff --git a/it/it-plugins/posttask-plugin/src/main/java/PostProjectAnalysisTaskImpl.java b/it/it-plugins/posttask-plugin/src/main/java/PostProjectAnalysisTaskImpl.java new file mode 100644 index 00000000000..79633f6061f --- /dev/null +++ b/it/it-plugins/posttask-plugin/src/main/java/PostProjectAnalysisTaskImpl.java @@ -0,0 +1,41 @@ +/* + * 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()); + } +} diff --git a/it/it-plugins/posttask-plugin/src/main/java/PostTaskPlugin.java b/it/it-plugins/posttask-plugin/src/main/java/PostTaskPlugin.java new file mode 100644 index 00000000000..df813bb6599 --- /dev/null +++ b/it/it-plugins/posttask-plugin/src/main/java/PostTaskPlugin.java @@ -0,0 +1,28 @@ +/* + * 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 java.util.Arrays; +import java.util.List; +import org.sonar.api.SonarPlugin; + +public class PostTaskPlugin extends SonarPlugin { + public List getExtensions() { + return Arrays.asList(PostProjectAnalysisTaskImpl.class); + } +} diff --git a/it/it-tests/src/test/java/it/Category1Suite.java b/it/it-tests/src/test/java/it/Category1Suite.java index 2257aeb1af2..db792a9a014 100644 --- a/it/it-tests/src/test/java/it/Category1Suite.java +++ b/it/it-tests/src/test/java/it/Category1Suite.java @@ -128,6 +128,8 @@ public class Category1Suite { // Used in SettingsTest.should_get_settings_default_value .addPlugin(pluginArtifact("server-plugin")) + .addPlugin(pluginArtifact("posttask-plugin")) + .addPlugin(xooPlugin()) .build(); diff --git a/it/it-tests/src/test/java/it/qualityGate/QualityGateTest.java b/it/it-tests/src/test/java/it/qualityGate/QualityGateTest.java index d25961a7e18..4b2f8f01151 100644 --- a/it/it-tests/src/test/java/it/qualityGate/QualityGateTest.java +++ b/it/it-tests/src/test/java/it/qualityGate/QualityGateTest.java @@ -19,13 +19,16 @@ */ 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; @@ -48,12 +51,22 @@ import org.sonarqube.ws.client.WsClient; 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; @@ -82,9 +95,11 @@ public class QualityGateTest { } @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(); } @@ -95,8 +110,10 @@ public class QualityGateTest { 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 { @@ -112,8 +129,10 @@ public class QualityGateTest { 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 { @@ -129,8 +148,10 @@ public class QualityGateTest { 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 { @@ -147,8 +168,10 @@ public class QualityGateTest { 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 { @@ -168,8 +191,11 @@ public class QualityGateTest { 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 { @@ -187,10 +213,13 @@ public class QualityGateTest { 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"); @@ -211,8 +240,10 @@ public class QualityGateTest { 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); @@ -230,6 +261,16 @@ public class QualityGateTest { } } + private void verifyQGStatusInPostTask(BuildResult buildResult, String taskStatus, String qgStatus) { + List 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() @@ -262,4 +303,14 @@ public class QualityGateTest { private static QualityGateClient qgClient() { return orchestrator.getServer().adminWsClient().qualityGateClient(); } + + private static List extractPosttaskPluginLogs(Iterable ceLogs) { + return from(ceLogs) + .filter(new Predicate() { + @Override + public boolean apply(String s) { + return s.contains("POSTASKPLUGIN: finished()"); + } + }).toList(); + } } diff --git a/it/it-tests/src/test/java/util/ItUtils.java b/it/it-tests/src/test/java/util/ItUtils.java index 7eb6464faa6..b435e1244c0 100644 --- a/it/it-tests/src/test/java/util/ItUtils.java +++ b/it/it-tests/src/test/java/util/ItUtils.java @@ -19,6 +19,9 @@ */ 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; @@ -49,6 +52,7 @@ import org.sonarqube.ws.client.HttpConnector; 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; @@ -57,6 +61,7 @@ import static org.assertj.core.api.Assertions.assertThat; 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() { } @@ -254,4 +259,33 @@ public class ItUtils { return sdf.format(d); } + public static Iterable 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 taskIds = extractCeTaskIds(buildResult); + checkState(taskIds.size() == 1, "More than one task id retrieved from logs"); + return taskIds.iterator().next(); + } + + public static List extractCeTaskIds(BuildResult buildResult) { + String logs = buildResult.getLogs(); + return from(LINE_SPLITTER.split(logs)) + .filter(new Predicate() { + @Override + public boolean apply(String s) { + return s.contains("More about the report processing at"); + } + }).transform(new Function() { + @Nullable + @Override + public String apply(String s) { + return s.substring(s.length() - 20, s.length()); + } + }).toList(); + } + } -- 2.39.5