]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-7488 add ITs for PostProjectAnalysisTask
authorSébastien Lesaint <sebastien.lesaint@sonarsource.com>
Fri, 1 Apr 2016 16:34:16 +0000 (18:34 +0200)
committerSébastien Lesaint <sebastien.lesaint@sonarsource.com>
Wed, 6 Apr 2016 13:42:53 +0000 (15:42 +0200)
it/it-plugins/pom.xml
it/it-plugins/posttask-plugin/pom.xml [new file with mode: 0644]
it/it-plugins/posttask-plugin/src/main/java/PostProjectAnalysisTaskImpl.java [new file with mode: 0644]
it/it-plugins/posttask-plugin/src/main/java/PostTaskPlugin.java [new file with mode: 0644]
it/it-tests/src/test/java/it/Category1Suite.java
it/it-tests/src/test/java/it/qualityGate/QualityGateTest.java
it/it-tests/src/test/java/util/ItUtils.java

index 71023da0185ebc8e1565198a8bf185484095f966..bee94bef359a34efdbca44f272b4a80ce64ea8d3 100644 (file)
@@ -51,5 +51,6 @@
     <module>sonar-subcategories-plugin</module>
     <module>ui-extensions-plugin</module>
     <module>issue-filter-plugin</module>
+    <module>posttask-plugin</module>
   </modules>
 </project>
diff --git a/it/it-plugins/posttask-plugin/pom.xml b/it/it-plugins/posttask-plugin/pom.xml
new file mode 100644 (file)
index 0000000..1933cb7
--- /dev/null
@@ -0,0 +1,39 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
+  <modelVersion>4.0.0</modelVersion>
+
+  <parent>
+    <groupId>org.sonarsource.sonarqube</groupId>
+    <artifactId>it-plugins</artifactId>
+    <version>5.5-SNAPSHOT</version>
+  </parent>
+
+  <artifactId>posttask-plugin</artifactId>
+  <version>1.0-SNAPSHOT</version>
+  <packaging>sonar-plugin</packaging>
+  <name>SonarQube Integration Tests :: Plugins :: PostTask</name>
+  <description>Plugin testing the Compute Engine Post Task API</description>
+
+  <dependencies>
+    <dependency>
+      <groupId>org.sonarsource.sonarqube</groupId>
+      <artifactId>sonar-plugin-api</artifactId>
+      <version>${apiVersion}</version>
+      <scope>provided</scope>
+    </dependency>
+  </dependencies>
+
+  <build>
+    <plugins>
+      <plugin>
+        <groupId>org.sonarsource.sonar-packaging-maven-plugin</groupId>
+        <artifactId>sonar-packaging-maven-plugin</artifactId>
+        <version>1.15</version>
+        <extensions>true</extensions>
+        <configuration>
+          <pluginClass>PostTaskPlugin</pluginClass>
+        </configuration>
+      </plugin>
+    </plugins>
+  </build>
+</project>
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 (file)
index 0000000..79633f6
--- /dev/null
@@ -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 (file)
index 0000000..df813bb
--- /dev/null
@@ -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);
+  }
+}
index 2257aeb1af2dc43184c1d4c7802ec3f2e7f7a0b2..db792a9a01452b7417501a005d33888350ec0ed0 100644 (file)
@@ -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();
 
index d25961a7e18162a199d4632af434cbd87b13c118..4b2f8f011511116156928ff81d56f1df54059731 100644 (file)
  */
 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<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()
@@ -262,4 +303,14 @@ public class QualityGateTest {
   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();
+  }
 }
index 7eb6464faa64439a1df32f1101d1475c05119a24..b435e1244c0d40ab17eb6a153d2376deb590c3c0 100644 (file)
@@ -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<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();
+  }
+
 }