Search in sources :

Example 26 with SonarScanner

use of com.sonar.orchestrator.build.SonarScanner in project sonarqube by SonarSource.

the class IssuesModeTest method runConcurrentIssues.

private void runConcurrentIssues(final String workDirPath) throws Exception {
    // Install sonar-runner in advance to avoid concurrent unzip issues
    FileSystem fileSystem = orchestrator.getConfiguration().fileSystem();
    new SonarScannerInstaller(fileSystem).install(Version.create(SonarScanner.DEFAULT_SCANNER_VERSION), fileSystem.workspace(), true);
    final int nThreads = 3;
    ExecutorService executorService = Executors.newFixedThreadPool(nThreads);
    List<Callable<BuildResult>> tasks = new ArrayList<>();
    final File homeDir = temp.newFolder();
    for (int i = 0; i < nThreads; i++) {
        tasks.add(new Callable<BuildResult>() {

            public BuildResult call() throws Exception {
                SonarScanner runner = configureRunnerIssues("shared/xoo-sample", homeDir, "sonar.it.enableWaitingSensor", "true", "sonar.working.directory", workDirPath);
                return orchestrator.executeBuild(runner);
            }
        });
    }
    boolean expectedError = false;
    for (Future<BuildResult> result : executorService.invokeAll(tasks)) {
        try {
            result.get();
        } catch (ExecutionException e) {
            if (e.getCause() instanceof BuildFailureException) {
                BuildFailureException bfe = (BuildFailureException) e.getCause();
                assertThat(bfe.getResult().getLogs()).contains("Another SonarQube analysis is already in progress for this project");
                expectedError = true;
            } else {
                throw e;
            }
        }
    }
    if (!expectedError) {
        fail("At least one of the threads should have failed");
    }
}
Also used : BuildFailureException(com.sonar.orchestrator.build.BuildFailureException) ArrayList(java.util.ArrayList) Callable(java.util.concurrent.Callable) ExpectedException(org.junit.rules.ExpectedException) BuildFailureException(com.sonar.orchestrator.build.BuildFailureException) IOException(java.io.IOException) ExecutionException(java.util.concurrent.ExecutionException) BuildResult(com.sonar.orchestrator.build.BuildResult) FileSystem(com.sonar.orchestrator.config.FileSystem) ExecutorService(java.util.concurrent.ExecutorService) SonarScannerInstaller(com.sonar.orchestrator.build.SonarScannerInstaller) ExecutionException(java.util.concurrent.ExecutionException) File(java.io.File) SonarScanner(com.sonar.orchestrator.build.SonarScanner)

Example 27 with SonarScanner

use of com.sonar.orchestrator.build.SonarScanner in project sonarqube by SonarSource.

the class IssuesModeTest method only_scan_changed_files_transitions.

// SONAR-6931
@Test
public void only_scan_changed_files_transitions() throws IOException {
    restoreProfile("one-issue-per-line.xml");
    orchestrator.getServer().provisionProject("sample", "xoo-sample");
    orchestrator.getServer().associateProjectToQualityProfile("sample", "xoo", "one-issue-per-line");
    SonarScanner runner = configureRunner("shared/xoo-sample", "sonar.verbose", "true");
    BuildResult result = orchestrator.executeBuild(runner);
    List<Issue> serverIssues = ItUtils.getAllServerIssues(orchestrator);
    for (Issue i : serverIssues) {
        assertThat(i.status()).isEqualTo("OPEN");
    }
    assertThat(serverIssues).hasSize(17);
    // resolve 2 issues
    IssueClient issueClient = orchestrator.getServer().wsClient("admin", "admin").issueClient();
    issueClient.doTransition(serverIssues.get(0).key(), "wontfix");
    issueClient.doTransition(serverIssues.get(1).key(), "wontfix");
    // do it again, scanning nothing (all files should be unchanged)
    runner = configureRunnerIssues("shared/xoo-sample", null, "sonar.verbose", "true");
    result = orchestrator.executeBuild(runner);
    assertThat(result.getLogs()).contains("Scanning only changed files");
    assertThat(result.getLogs()).contains("'One Issue Per Line' skipped because there is no related file in current project");
    ItUtils.assertIssuesInJsonReport(result, 0, 0, 15);
}
Also used : BuildResult(com.sonar.orchestrator.build.BuildResult) Issue(org.sonar.wsclient.issue.Issue) IssueClient(org.sonar.wsclient.issue.IssueClient) SonarScanner(com.sonar.orchestrator.build.SonarScanner) Test(org.junit.Test)

Example 28 with SonarScanner

use of com.sonar.orchestrator.build.SonarScanner in project sonarqube by SonarSource.

the class IssuesModeTest method only_scan_changed_files_qps.

// SONAR-6931
@Test
public void only_scan_changed_files_qps() throws IOException {
    restoreProfile("one-issue-per-line.xml");
    orchestrator.getServer().provisionProject("sample", "xoo-sample");
    orchestrator.getServer().associateProjectToQualityProfile("sample", "xoo", "one-issue-per-line");
    SonarScanner runner = configureRunner("shared/xoo-sample", "sonar.verbose", "true");
    BuildResult result = orchestrator.executeBuild(runner);
    List<Issue> serverIssues = ItUtils.getAllServerIssues(orchestrator);
    for (Issue i : serverIssues) {
        assertThat(i.status()).isEqualTo("OPEN");
    }
    assertThat(serverIssues).hasSize(17);
    // change quality profile
    restoreProfile("with-many-rules.xml");
    orchestrator.getServer().associateProjectToQualityProfile("sample", "xoo", "with-many-rules");
    // do it again, scanning nothing (all files should be unchanged)
    runner = configureRunnerIssues("shared/xoo-sample", null, "sonar.verbose", "true");
    result = orchestrator.executeBuild(runner);
    assertThat(result.getLogs()).contains("Scanning only changed files");
    assertThat(result.getLogs()).contains("'One Issue Per Line' skipped because there is no related file in current project");
    ItUtils.assertIssuesInJsonReport(result, 0, 0, 17);
}
Also used : BuildResult(com.sonar.orchestrator.build.BuildResult) Issue(org.sonar.wsclient.issue.Issue) SonarScanner(com.sonar.orchestrator.build.SonarScanner) Test(org.junit.Test)

Example 29 with SonarScanner

use of com.sonar.orchestrator.build.SonarScanner in project sonarqube by SonarSource.

the class IssuesModeTest method only_scan_changed_files_on_change.

// SONAR-6931
@Test
public void only_scan_changed_files_on_change() throws IOException {
    restoreProfile("one-issue-per-line.xml");
    orchestrator.getServer().provisionProject("sample", "xoo-sample");
    orchestrator.getServer().associateProjectToQualityProfile("sample", "xoo", "one-issue-per-line");
    SonarScanner runner = configureRunner("shared/xoo-sample", "sonar.verbose", "true");
    BuildResult result = orchestrator.executeBuild(runner);
    // change QP
    restoreProfile("with-many-rules.xml");
    orchestrator.getServer().associateProjectToQualityProfile("sample", "xoo", "with-many-rules");
    // now change file hash in a temporary location
    File tmpProjectDir = temp.newFolder();
    FileUtils.copyDirectory(ItUtils.projectDir("shared/xoo-sample"), tmpProjectDir);
    File srcFile = new File(tmpProjectDir, "src/main/xoo/sample/Sample.xoo");
    FileUtils.write(srcFile, "\n", StandardCharsets.UTF_8, true);
    // scan again, with different QP
    runner = SonarScanner.create(tmpProjectDir, "sonar.working.directory", temp.newFolder().getAbsolutePath(), "sonar.analysis.mode", "issues", "sonar.report.export.path", "sonar-report.json", "sonar.userHome", temp.newFolder().getAbsolutePath(), "sonar.verbose", "true", "sonar.scanChangedFilesOnly", "true");
    result = orchestrator.executeBuild(runner);
    assertThat(result.getLogs()).contains("Scanning only changed files");
    assertThat(result.getLogs()).doesNotContain("'One Issue Per Line' skipped because there is no related file in current project");
    ItUtils.assertIssuesInJsonReport(result, 3, 0, 17);
}
Also used : BuildResult(com.sonar.orchestrator.build.BuildResult) File(java.io.File) SonarScanner(com.sonar.orchestrator.build.SonarScanner) Test(org.junit.Test)

Example 30 with SonarScanner

use of com.sonar.orchestrator.build.SonarScanner in project sonarqube by SonarSource.

the class IssuesModeTest method should_not_fail_on_resources_that_have_existed_before.

@Test
public void should_not_fail_on_resources_that_have_existed_before() throws IOException {
    restoreProfile("with-many-rules.xml");
    orchestrator.getServer().provisionProject("sample", "xoo-history");
    orchestrator.getServer().associateProjectToQualityProfile("sample", "xoo", "with-many-rules");
    // First real scan with source
    SonarScanner runner = configureRunner("shared/xoo-history-v2");
    orchestrator.executeBuild(runner);
    assertThat(getComponent(orchestrator, "sample:src/main/xoo/sample/ClassAdded.xoo")).isNotNull();
    // Second scan should remove ClassAdded.xoo
    runner = configureRunner("shared/xoo-history-v1");
    orchestrator.executeBuild(runner);
    assertThat(getMeasureAsDouble(orchestrator, "sample:src/main/xoo/sample/ClassAdded.xoo", "ncloc")).isNull();
    // Re-add ClassAdded.xoo in local workspace
    runner = configureRunnerIssues("shared/xoo-history-v2", null);
    BuildResult result = orchestrator.executeBuild(runner);
    assertThat(getMeasureAsDouble(orchestrator, "sample:src/main/xoo/sample/ClassAdded.xoo", "ncloc")).isNull();
    assertThat(result.getLogs()).contains("Issues");
    assertThat(result.getLogs()).contains("ANALYSIS SUCCESSFUL");
}
Also used : BuildResult(com.sonar.orchestrator.build.BuildResult) SonarScanner(com.sonar.orchestrator.build.SonarScanner) Test(org.junit.Test)

Aggregations

SonarScanner (com.sonar.orchestrator.build.SonarScanner)84 Test (org.junit.Test)65 BuildResult (com.sonar.orchestrator.build.BuildResult)38 File (java.io.File)21 QualityGate (org.sonar.wsclient.qualitygate.QualityGate)8 JSONArray (org.json.simple.JSONArray)4 JSONObject (org.json.simple.JSONObject)4 Issue (org.sonar.wsclient.issue.Issue)4 Favorites (org.sonarqube.ws.Favorites)3 SearchRequest (org.sonarqube.ws.client.favorite.SearchRequest)3 Path (java.nio.file.Path)2 ArrayList (java.util.ArrayList)2 BeforeClass (org.junit.BeforeClass)2 SonarClient (org.sonar.wsclient.SonarClient)2 IssueClient (org.sonar.wsclient.issue.IssueClient)2 ImmutableMap (com.google.common.collect.ImmutableMap)1 WebTestSuite.createSonarScanner (com.sonar.it.web.WebTestSuite.createSonarScanner)1 BuildFailureException (com.sonar.orchestrator.build.BuildFailureException)1 SonarScannerInstaller (com.sonar.orchestrator.build.SonarScannerInstaller)1 FileSystem (com.sonar.orchestrator.config.FileSystem)1