]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-7175 Keep last snapshots in KeepOneFilter 960/head
authorJulien Lancelot <julien.lancelot@sonarsource.com>
Thu, 12 May 2016 10:20:54 +0000 (12:20 +0200)
committerJulien Lancelot <julien.lancelot@sonarsource.com>
Thu, 12 May 2016 15:05:46 +0000 (17:05 +0200)
 Last snapshot is now kept Instead of first snapshot

it/it-tests/src/test/java/it/dbCleaner/PurgeTest.java
sonar-db/src/main/java/org/sonar/db/purge/period/KeepOneFilter.java
sonar-db/src/test/java/org/sonar/db/purge/period/KeepOneFilterTest.java

index 417344cd14fd48ba9e7d6ae01fef7e71be78f833..d2040c301ae847b8acc689eb48df3b600e62196f 100644 (file)
@@ -21,7 +21,7 @@ package it.dbCleaner;
 
 import com.sonar.orchestrator.Orchestrator;
 import com.sonar.orchestrator.build.BuildResult;
-import com.sonar.orchestrator.build.SonarRunner;
+import com.sonar.orchestrator.build.SonarScanner;
 import com.sonar.orchestrator.locator.FileLocation;
 import it.Category4Suite;
 import java.util.Date;
@@ -34,12 +34,17 @@ import org.junit.ClassRule;
 import org.junit.Rule;
 import org.junit.Test;
 import org.junit.rules.ErrorCollector;
-import org.sonar.wsclient.services.PropertyDeleteQuery;
-import org.sonar.wsclient.services.PropertyUpdateQuery;
+import org.sonarqube.ws.client.GetRequest;
+import org.sonarqube.ws.client.WsResponse;
 import util.ItUtils;
 
+import static org.apache.commons.lang.time.DateUtils.addDays;
 import static org.assertj.core.api.Assertions.assertThat;
 import static org.hamcrest.Matchers.equalTo;
+import static util.ItUtils.formatDate;
+import static util.ItUtils.newAdminWsClient;
+import static util.ItUtils.runProjectAnalysis;
+import static util.ItUtils.setServerProperty;
 
 public class PurgeTest {
 
@@ -59,8 +64,10 @@ public class PurgeTest {
     orchestrator.getServer().provisionProject(PROJECT_KEY, PROJECT_KEY);
 
     orchestrator.getServer().restoreProfile(FileLocation.ofClasspath("/dbCleaner/one-issue-per-line-profile.xml"));
-    orchestrator.getServer().getAdminWsClient().delete(new PropertyDeleteQuery("sonar.dbcleaner.hoursBeforeKeepingOnlyOneSnapshotByDay"));
-    orchestrator.getServer().getAdminWsClient().delete(new PropertyDeleteQuery("sonar.dbcleaner.cleanDirectory"));
+
+    setServerProperty(orchestrator, "sonar.dbcleaner.cleanDirectory", null);
+    setServerProperty(orchestrator, "sonar.dbcleaner.hoursBeforeKeepingOnlyOneSnapshotByDay", null);
+    setServerProperty(orchestrator, "sonar.dbcleaner.weeksBeforeKeepingOnlyOneSnapshotByWeek", null);
   }
 
   @Test
@@ -152,7 +159,7 @@ public class PurgeTest {
     int measuresCount = count("project_measures");
     // Using the "sonar.dbcleaner.hoursBeforeKeepingOnlyOneSnapshotByDay" property set to '0' is the way
     // to keep only 1 snapshot per day
-    orchestrator.getServer().getAdminWsClient().update(new PropertyUpdateQuery("sonar.dbcleaner.hoursBeforeKeepingOnlyOneSnapshotByDay", "0"));
+    setServerProperty(orchestrator, "sonar.dbcleaner.hoursBeforeKeepingOnlyOneSnapshotByDay", "0");
     scan(PROJECT_SAMPLE_PATH);
     assertThat(count("snapshots where qualifier<>'LIB'")).as("Different number of snapshots").isEqualTo(snapshotsCount);
 
@@ -161,6 +168,39 @@ public class PurgeTest {
     assertThat(count("project_measures")).as("Different number of measures").isEqualTo(measuresCount + measureOnNewMetrics);
   }
 
+  /**
+   * SONAR-7175
+   */
+  @Test
+  public void keep_latest_snapshot() {
+    // Keep all snapshots from last 4 weeks
+    setServerProperty(orchestrator, "sonar.dbcleaner.weeksBeforeKeepingOnlyOneSnapshotByWeek", "4");
+
+    // Execute an analysis 10 days ago
+    String tenDaysAgo = formatDate(addDays(new Date(), -10));
+    runProjectAnalysis(orchestrator, PROJECT_SAMPLE_PATH, "sonar.projectDate", tenDaysAgo);
+
+    // Execute an analysis 8 days ago
+    String eightDaysAgo = formatDate(addDays(new Date(), -8));
+    runProjectAnalysis(orchestrator, PROJECT_SAMPLE_PATH, "sonar.projectDate", eightDaysAgo);
+
+    // Now only keep 1 snapshot per week
+    setServerProperty(orchestrator, "sonar.dbcleaner.weeksBeforeKeepingOnlyOneSnapshotByWeek", "0");
+
+    // Execute an analysis today to execute the purge of previous weeks snapshots
+    runProjectAnalysis(orchestrator, PROJECT_SAMPLE_PATH);
+
+    // Check that only analysis from 8 days ago is kept (as it's the last one from previous week)
+    WsResponse response = newAdminWsClient(orchestrator).wsConnector().call(
+      new GetRequest("/api/timemachine/index")
+        .setParam("resource", PROJECT_KEY)
+        .setParam("metrics", "ncloc"))
+      .failIfNotSuccessful();
+    String content = response.content();
+    assertThat(content).contains(eightDaysAgo);
+    assertThat(content).doesNotContain(tenDaysAgo);
+  }
+
   /**
    * SONAR-3120
    */
@@ -171,7 +211,8 @@ public class PurgeTest {
     assertSingleSnapshot("com.sonarsource.it.samples:multi-modules-sample:module_b:module_b1");
 
     // we want the previous snapshot to be purged
-    orchestrator.getServer().getAdminWsClient().update(new PropertyUpdateQuery("sonar.dbcleaner.hoursBeforeKeepingOnlyOneSnapshotByDay", "0"));
+    setServerProperty(orchestrator, "sonar.dbcleaner.hoursBeforeKeepingOnlyOneSnapshotByDay", "0");
+
     scan("dbCleaner/modules/after");
     assertDeleted("com.sonarsource.it.samples:multi-modules-sample:module_b");
     assertDeleted("com.sonarsource.it.samples:multi-modules-sample:module_b:module_b1");
@@ -214,7 +255,8 @@ public class PurgeTest {
     String select = "snapshots where scope='DIR'";
     int directorySnapshots = count(select);
 
-    orchestrator.getServer().getAdminWsClient().update(new PropertyUpdateQuery("sonar.dbcleaner.cleanDirectory", "false"));
+    setServerProperty(orchestrator, "sonar.dbcleaner.cleanDirectory", "false");
+
     scan(PROJECT_SAMPLE_PATH, "2012-02-02");
 
     assertThat(count(select)).isEqualTo(2 * directorySnapshots);
@@ -253,13 +295,13 @@ public class PurgeTest {
   }
 
   private BuildResult scan(String path, String... extraProperties) {
-    SonarRunner runner = configureRunner(path, extraProperties);
+    SonarScanner runner = configureRunner(path, extraProperties);
     return orchestrator.executeBuild(runner);
   }
 
-  private SonarRunner configureRunner(String projectPath, String... props) {
+  private SonarScanner configureRunner(String projectPath, String... props) {
     orchestrator.getServer().associateProjectToQualityProfile(PROJECT_KEY, "xoo", "one-issue-per-line-profile");
-    return SonarRunner.create(ItUtils.projectDir(projectPath)).setProperties(props);
+    return SonarScanner.create(ItUtils.projectDir(projectPath)).setProperties(props);
   }
 
   private int count(String condition) {
@@ -292,4 +334,5 @@ public class PurgeTest {
       System.out.println("  " + row);
     }
   }
+
 }
index 72d4123a59bb063dc0dc94a13a28d8c015477ca3..7bb554ffab2388812b2a2cda3419f9688da822b6 100644 (file)
@@ -73,8 +73,8 @@ class KeepOneFilter implements Filter {
         toDelete.addAll(deletables);
 
       } else if (deletables.size() > 1) {
-        // keep one snapshot
-        toDelete.addAll(deletables.subList(1, deletables.size()));
+        // keep last snapshot
+        toDelete.addAll(deletables.subList(0, deletables.size() - 1));
       }
     }
   }
index cdfe5b88993c64b0179db4c237f44343335c658d..b7f20b5f86612468af5bae57f41811df7b6e8627 100644 (file)
@@ -60,8 +60,7 @@ public class KeepOneFilterTest {
     assertThat(toDelete).hasSize(2);
 
     List<Long> snapshotIds = snapshotIds(toDelete);
-    assertThat(snapshotIds).contains(3L);
-    assertThat(snapshotIds).contains(4L);
+    assertThat(snapshotIds).containsOnly(2L, 3L);
   }
 
   @Test