]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-7844 persist scanner context in DB instead of logging it
authorSébastien Lesaint <sebastien.lesaint@sonarsource.com>
Wed, 17 Aug 2016 15:39:39 +0000 (17:39 +0200)
committerSébastien Lesaint <sebastien.lesaint@sonarsource.com>
Mon, 22 Aug 2016 08:25:43 +0000 (10:25 +0200)
server/sonar-server/src/main/java/org/sonar/server/computation/task/projectanalysis/step/LogScannerContextStep.java [deleted file]
server/sonar-server/src/main/java/org/sonar/server/computation/task/projectanalysis/step/PersistScannerContextStep.java [new file with mode: 0644]
server/sonar-server/src/main/java/org/sonar/server/computation/task/projectanalysis/step/ReportComputationSteps.java
server/sonar-server/src/test/java/org/sonar/server/ce/ws/TaskActionTest.java
server/sonar-server/src/test/java/org/sonar/server/computation/task/projectanalysis/step/LogScannerContextStepTest.java [deleted file]
server/sonar-server/src/test/java/org/sonar/server/computation/task/projectanalysis/step/PersistScannerContextStepTest.java [new file with mode: 0644]
server/sonar-server/src/test/java/org/sonar/server/computation/task/projectanalysis/step/ReportComputationStepsTest.java
sonar-db/src/test/java/org/sonar/db/ce/CeActivityDaoTest.java

diff --git a/server/sonar-server/src/main/java/org/sonar/server/computation/task/projectanalysis/step/LogScannerContextStep.java b/server/sonar-server/src/main/java/org/sonar/server/computation/task/projectanalysis/step/LogScannerContextStep.java
deleted file mode 100644 (file)
index 868107f..0000000
+++ /dev/null
@@ -1,54 +0,0 @@
-/*
- * 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.
- */
-package org.sonar.server.computation.task.projectanalysis.step;
-
-import org.sonar.api.utils.log.Logger;
-import org.sonar.api.utils.log.Loggers;
-import org.sonar.core.util.CloseableIterator;
-import org.sonar.server.computation.task.projectanalysis.batch.BatchReportReader;
-import org.sonar.server.computation.task.step.ComputationStep;
-
-public class LogScannerContextStep implements ComputationStep {
-
-  private static final Logger LOGGER = Loggers.get(LogScannerContextStep.class);
-
-  private final BatchReportReader reportReader;
-
-  public LogScannerContextStep(BatchReportReader reportReader) {
-    this.reportReader = reportReader;
-  }
-
-  @Override
-  public void execute() {
-    CloseableIterator<String> logs = reportReader.readScannerLogs();
-    try {
-      while (logs.hasNext()) {
-        LOGGER.debug(logs.next());
-      }
-    } finally {
-      logs.close();
-    }
-  }
-
-  @Override
-  public String getDescription() {
-    return "Log scanner context";
-  }
-}
diff --git a/server/sonar-server/src/main/java/org/sonar/server/computation/task/projectanalysis/step/PersistScannerContextStep.java b/server/sonar-server/src/main/java/org/sonar/server/computation/task/projectanalysis/step/PersistScannerContextStep.java
new file mode 100644 (file)
index 0000000..5a49cff
--- /dev/null
@@ -0,0 +1,56 @@
+/*
+ * 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.
+ */
+package org.sonar.server.computation.task.projectanalysis.step;
+
+import org.sonar.core.util.CloseableIterator;
+import org.sonar.db.DbClient;
+import org.sonar.db.DbSession;
+import org.sonar.server.computation.task.projectanalysis.analysis.AnalysisMetadataHolder;
+import org.sonar.server.computation.task.projectanalysis.batch.BatchReportReader;
+import org.sonar.server.computation.task.step.ComputationStep;
+
+public class PersistScannerContextStep implements ComputationStep {
+  private final BatchReportReader reportReader;
+  private final AnalysisMetadataHolder analysisMetadataHolder;
+  private final DbClient dbClient;
+
+  public PersistScannerContextStep(BatchReportReader reportReader, AnalysisMetadataHolder analysisMetadataHolder, DbClient dbClient) {
+    this.reportReader = reportReader;
+    this.analysisMetadataHolder = analysisMetadataHolder;
+    this.dbClient = dbClient;
+  }
+
+  @Override
+  public void execute() {
+    try (CloseableIterator<String> logsIterator = reportReader.readScannerLogs()) {
+      if (logsIterator.hasNext()) {
+        try (DbSession dbSession = dbClient.openSession(false)) {
+          dbClient.scannerContextDao().insert(dbSession, analysisMetadataHolder.getUuid(), logsIterator);
+          dbSession.commit();
+        }
+      }
+    }
+  }
+
+  @Override
+  public String getDescription() {
+    return "Persist scanner context";
+  }
+}
index 6c244f5d007bc627aca07a10b1cdd730226de340..217d4cc45058289cae8a2ab42ff555cd13b00428 100644 (file)
@@ -38,7 +38,6 @@ public class ReportComputationSteps extends AbstractComputationSteps {
 
   private static final List<Class<? extends ComputationStep>> STEPS = Arrays.asList(
     ExtractReportStep.class,
-    LogScannerContextStep.class,
     GenerateAnalysisUuid.class,
 
     // Builds Component tree
@@ -97,6 +96,7 @@ public class ReportComputationSteps extends AbstractComputationSteps {
     PersistFileSourcesStep.class,
     PersistTestsStep.class,
     PersistCrossProjectDuplicationIndexStep.class,
+    PersistScannerContextStep.class,
     EnableAnalysisStep.class,
 
     UpdateQualityProfilesLastUsedDateStep.class,
index 7ed877581dcd84da6b2f3bfef2ddd88d6e5304b0..5a9e1893a73912ce56e96405e16890b9d8b9f664 100644 (file)
@@ -24,6 +24,7 @@ import org.junit.Rule;
 import org.junit.Test;
 import org.junit.rules.ExpectedException;
 import org.sonar.api.utils.System2;
+import org.sonar.core.util.CloseableIterator;
 import org.sonar.core.util.Protobuf;
 import org.sonar.db.DbTester;
 import org.sonar.db.ce.CeActivityDto;
@@ -39,6 +40,7 @@ import org.sonar.server.ws.WsActionTester;
 import org.sonarqube.ws.MediaTypes;
 import org.sonarqube.ws.WsCe;
 
+import static java.util.Collections.singleton;
 import static org.assertj.core.api.Assertions.assertThat;
 import static org.sonar.core.permission.GlobalPermissions.PROVISIONING;
 import static org.sonar.core.permission.GlobalPermissions.SCAN_EXECUTION;
@@ -293,9 +295,15 @@ public class TaskActionTest {
     dbTester.commit();
   }
 
-  private void persist(CeActivityDto activityDto) {
+  private CeActivityDto persist(CeActivityDto activityDto) {
     dbTester.getDbClient().ceActivityDao().insert(dbTester.getSession(), activityDto);
     dbTester.commit();
+    return activityDto;
+  }
+
+  private void persistScannerContext(String analysisUuid, String scannerContext) {
+    dbTester.getDbClient().scannerContextDao().insert(dbTester.getSession(), analysisUuid, CloseableIterator.from(singleton(scannerContext).iterator()));
+    dbTester.commit();
   }
 
 }
diff --git a/server/sonar-server/src/test/java/org/sonar/server/computation/task/projectanalysis/step/LogScannerContextStepTest.java b/server/sonar-server/src/test/java/org/sonar/server/computation/task/projectanalysis/step/LogScannerContextStepTest.java
deleted file mode 100644 (file)
index 9627e66..0000000
+++ /dev/null
@@ -1,50 +0,0 @@
-/*
- * 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.
- */
-package org.sonar.server.computation.task.projectanalysis.step;
-
-import org.junit.Rule;
-import org.junit.Test;
-import org.sonar.api.utils.log.LogTester;
-import org.sonar.api.utils.log.LoggerLevel;
-import org.sonar.server.computation.task.projectanalysis.batch.BatchReportReaderRule;
-
-import static java.util.Arrays.asList;
-import static org.assertj.core.api.Assertions.assertThat;
-
-public class LogScannerContextStepTest {
-
-  @Rule
-  public BatchReportReaderRule reportReader = new BatchReportReaderRule();
-
-  @Rule
-  public LogTester logTester = new LogTester();
-
-  LogScannerContextStep underTest = new LogScannerContextStep(reportReader);
-
-  @Test
-  public void log_scanner_logs() {
-    reportReader.setScannerLogs(asList("log1", "log2"));
-
-    underTest.execute();
-
-    assertThat(logTester.logs(LoggerLevel.DEBUG)).containsExactly("log1", "log2");
-  }
-
-}
diff --git a/server/sonar-server/src/test/java/org/sonar/server/computation/task/projectanalysis/step/PersistScannerContextStepTest.java b/server/sonar-server/src/test/java/org/sonar/server/computation/task/projectanalysis/step/PersistScannerContextStepTest.java
new file mode 100644 (file)
index 0000000..0dafc13
--- /dev/null
@@ -0,0 +1,64 @@
+/*
+ * 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.
+ */
+package org.sonar.server.computation.task.projectanalysis.step;
+
+import org.junit.ClassRule;
+import org.junit.Rule;
+import org.junit.Test;
+import org.sonar.api.utils.System2;
+import org.sonar.db.DbClient;
+import org.sonar.db.DbTester;
+import org.sonar.server.computation.task.projectanalysis.analysis.AnalysisMetadataHolderRule;
+import org.sonar.server.computation.task.projectanalysis.batch.BatchReportReaderRule;
+
+import static java.util.Arrays.asList;
+import static org.assertj.core.api.Assertions.assertThat;
+
+public class PersistScannerContextStepTest {
+  private static final String ANALYSIS_UUID = "UUID";
+
+  @ClassRule
+  public static final DbTester dbTester = DbTester.create(System2.INSTANCE);
+
+  @Rule
+  public BatchReportReaderRule reportReader = new BatchReportReaderRule();
+  @Rule
+  public AnalysisMetadataHolderRule analysisMetadataHolder = new AnalysisMetadataHolderRule()
+    .setUuid(ANALYSIS_UUID);
+
+  private DbClient dbClient = dbTester.getDbClient();
+  private PersistScannerContextStep underTest = new PersistScannerContextStep(reportReader, analysisMetadataHolder, dbClient);
+
+  @Test
+  public void getDescription() {
+    assertThat(underTest.getDescription()).isEqualTo("Persist scanner context");
+  }
+
+  @Test
+  public void log_scanner_logs() {
+    reportReader.setScannerLogs(asList("log1", "log2"));
+
+    underTest.execute();
+
+    assertThat(dbClient.scannerContextDao().selectScannerContext(dbTester.getSession(), ANALYSIS_UUID))
+      .contains("log1" + '\n' + "log2");
+  }
+
+}
index da6a95418fbf21eed9fb081334d173a25aefd5b6..f8ff545f980ebef7915f7e6f054194bb5d25a1f3 100644 (file)
@@ -52,7 +52,7 @@ public class ReportComputationStepsTest {
   @Test
   public void instances_throws_ISE_if_container_does_not_have_second_step() throws Exception {
     expectedException.expect(IllegalStateException.class);
-    expectedException.expectMessage("Component not found: class org.sonar.server.computation.task.projectanalysis.step.LogScannerContextStep");
+    expectedException.expectMessage("Component not found: class org.sonar.server.computation.task.projectanalysis.step.GenerateAnalysisUuid");
 
     final ExtractReportStep reportExtractionStep = mock(ExtractReportStep.class);
     ComponentContainer componentContainer = new ComponentContainer() {
index 885d9b11ed2c9979ca672eef987cfc8962df4583..fed7575080f61002febe4c651071edb569c1b292 100644 (file)
@@ -22,13 +22,14 @@ package org.sonar.db.ce;
 import com.google.common.base.Function;
 import com.google.common.base.Optional;
 import com.google.common.base.Strings;
-import com.google.common.collect.FluentIterable;
 import java.util.Collections;
 import java.util.List;
 import javax.annotation.Nonnull;
 import org.junit.Rule;
 import org.junit.Test;
 import org.sonar.api.utils.internal.TestSystem2;
+import org.sonar.core.util.CloseableIterator;
+import org.sonar.core.util.stream.Collectors;
 import org.sonar.db.DbSession;
 import org.sonar.db.DbTester;
 
@@ -42,13 +43,13 @@ public class CeActivityDaoTest {
 
   private static final String AN_ANALYSIS_UUID = "U1";
 
-  TestSystem2 system2 = new TestSystem2().setNow(1_450_000_000_000L);
+  private TestSystem2 system2 = new TestSystem2().setNow(1_450_000_000_000L);
 
   @Rule
   public DbTester db = DbTester.create(system2);
-  DbSession dbSession = db.getSession();
 
-  CeActivityDao underTest = new CeActivityDao(system2);
+  private DbSession dbSession = db.getSession();
+  private CeActivityDao underTest = new CeActivityDao(system2);
 
   @Test
   public void test_insert() {
@@ -330,7 +331,7 @@ public class CeActivityDaoTest {
     return dto;
   }
 
-  private void insertWithCreationDate(String uuid, long date) {
+  private CeActivityDto insertWithCreationDate(String uuid, long date) {
     CeQueueDto queueDto = new CeQueueDto();
     queueDto.setUuid(uuid);
     queueDto.setTaskType("fake");
@@ -339,10 +340,18 @@ public class CeActivityDaoTest {
     dto.setStatus(CeActivityDto.Status.SUCCESS);
     system2.setNow(date);
     underTest.insert(db.getSession(), dto);
+    return dto;
+  }
+
+  private void insertScannerContext(String analysisUuid) {
+    db.getDbClient().scannerContextDao().insert(dbSession, analysisUuid, CloseableIterator.from(singletonList("scanner context of " + analysisUuid).iterator()));
+    dbSession.commit();
   }
 
   private List<String> selectPageOfUuids(int offset, int pageSize) {
-    return FluentIterable.from(underTest.selectByQuery(db.getSession(), new CeTaskQuery(), offset, pageSize)).transform(CeActivityToUuid.INSTANCE).toList();
+    return underTest.selectByQuery(db.getSession(), new CeTaskQuery(), offset, pageSize).stream()
+        .map(CeActivityToUuid.INSTANCE::apply)
+        .collect(Collectors.toList());
   }
 
   private enum CeActivityToUuid implements Function<CeActivityDto, String> {