Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

SONARPY-2462: Parallelize the qa_plugin tests #2248

Merged
merged 5 commits into from
Dec 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion its/plugin/it-python-plugin-test/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@
<artifactId>maven-surefire-plugin</artifactId>
<configuration>
<includes>
<include>com/sonar/python/it/plugin/TestSuite.java</include>
<include>com/sonar/python/it/plugin/*.java</include>
</includes>
</configuration>
</plugin>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,6 @@
*/
package com.sonar.python.it.plugin;

import com.sonar.orchestrator.build.SonarScanner;
import com.sonar.orchestrator.junit5.OrchestratorExtension;
import java.io.File;
import java.util.List;
import org.junit.jupiter.api.Test;
Expand All @@ -33,14 +31,14 @@ class BanditReportTest {
private static final String PROJECT = "bandit_project";

@RegisterExtension
public static final OrchestratorExtension ORCHESTRATOR = TestsUtils.ORCHESTRATOR;
public static final ConcurrentOrchestratorExtension ORCHESTRATOR = TestsUtils.ORCHESTRATOR;

@Test
void import_report() {
ORCHESTRATOR.getServer().provisionProject(PROJECT, PROJECT);
ORCHESTRATOR.getServer().associateProjectToQualityProfile(PROJECT, "py", "no_rule");
ORCHESTRATOR.executeBuild(
SonarScanner.create()
ORCHESTRATOR.createSonarScanner()
.setProjectDir(new File("projects/bandit_project")));

List<Issues.Issue> issues = issues(PROJECT);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
package com.sonar.python.it.plugin;

import com.sonar.orchestrator.build.SonarScanner;
import com.sonar.orchestrator.junit5.OrchestratorExtension;
import java.io.File;
import org.assertj.core.data.Offset;
import org.junit.jupiter.api.BeforeAll;
Expand All @@ -41,13 +40,13 @@ class CPDTest {
private static final String DUPLICATED_LINES_DENSITY = "duplicated_lines_density";

@RegisterExtension
public static final OrchestratorExtension ORCHESTRATOR = TestsUtils.ORCHESTRATOR;
public static final ConcurrentOrchestratorExtension ORCHESTRATOR = TestsUtils.ORCHESTRATOR;

@BeforeAll
static void startServer() {
ORCHESTRATOR.getServer().provisionProject(PROJECT_KEY, PROJECT_KEY);
ORCHESTRATOR.getServer().associateProjectToQualityProfile(PROJECT_KEY, "py", "no_rule");
SonarScanner build = SonarScanner.create()
SonarScanner build = ORCHESTRATOR.createSonarScanner()
.setProjectDir(new File("projects/cpd"))
.setProjectKey(PROJECT_KEY)
.setProjectName(PROJECT_KEY)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
/*
* SonarQube Python Plugin
* Copyright (C) 2012-2024 SonarSource SA
* mailto:info AT sonarsource DOT com
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the Sonar Source-Available License Version 1, as published by SonarSource SA.
*
* 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 Sonar Source-Available License for more details.
*
* You should have received a copy of the Sonar Source-Available License
* along with this program; if not, see https://sonarsource.com/license/ssal/
*/
package com.sonar.python.it.plugin;

import com.sonar.orchestrator.Orchestrator;
import com.sonar.orchestrator.OrchestratorBuilder;
import com.sonar.orchestrator.build.SonarScanner;
import com.sonar.orchestrator.build.SonarScannerInstaller;
import com.sonar.orchestrator.config.Configuration;
import com.sonar.orchestrator.container.SonarDistribution;
import com.sonar.orchestrator.server.StartupLogWatcher;
import com.sonar.orchestrator.util.System2;
import com.sonar.orchestrator.version.Version;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.atomic.AtomicInteger;
import org.jetbrains.annotations.Nullable;
import org.junit.jupiter.api.extension.BeforeAllCallback;
import org.junit.jupiter.api.extension.ExtensionContext;

public class ConcurrentOrchestratorExtension extends Orchestrator implements BeforeAllCallback {
private static final AtomicInteger REQUESTED_ORCHESTRATORS_KEY = new AtomicInteger();
private static final CountDownLatch IS_ORCHESTRATOR_READY = new CountDownLatch(1);

ConcurrentOrchestratorExtension(Configuration config, SonarDistribution distribution, @Nullable StartupLogWatcher startupLogWatcher) {
super(config, distribution, startupLogWatcher);
}

@Override
public void beforeAll(ExtensionContext context) throws InterruptedException {
if (REQUESTED_ORCHESTRATORS_KEY.getAndIncrement() == 0) {
start();

new SonarScannerInstaller(getConfiguration().locators()).install(Version.create(SonarScanner.DEFAULT_SCANNER_VERSION), getConfiguration().fileSystem().workspace());
IS_ORCHESTRATOR_READY.countDown();

} else {
IS_ORCHESTRATOR_READY.await();
}
}


public SonarScanner createSonarScanner() {
return SonarScanner.create();
}

public static ConcurrentOrchestratorExtensionBuilder builderEnv() {
return new ConcurrentOrchestratorExtensionBuilder(Configuration.createEnv());
}

public static class ConcurrentOrchestratorExtensionBuilder extends OrchestratorBuilder<ConcurrentOrchestratorExtensionBuilder, ConcurrentOrchestratorExtension> {
ConcurrentOrchestratorExtensionBuilder(Configuration initialConfig) {
this(initialConfig, System2.INSTANCE);
}

ConcurrentOrchestratorExtensionBuilder(Configuration initialConfig, System2 system2) {
super(initialConfig, system2);
}

@Override
protected ConcurrentOrchestratorExtension build(Configuration finalConfig, SonarDistribution distribution, StartupLogWatcher startupLogWatcher) {
return new ConcurrentOrchestratorExtension(finalConfig, distribution, startupLogWatcher);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
import com.google.common.collect.ImmutableMap.Builder;
import com.sonar.orchestrator.build.BuildResult;
import com.sonar.orchestrator.build.SonarScanner;
import com.sonar.orchestrator.junit5.OrchestratorExtension;
import java.io.File;
import java.util.HashMap;
import java.util.Map;
Expand All @@ -33,7 +32,7 @@ class CoverageTest {

private static final String COVERAGE_PROJECT = "projects/coverage_project";
@RegisterExtension
public static final OrchestratorExtension ORCHESTRATOR = TestsUtils.ORCHESTRATOR;
public static final ConcurrentOrchestratorExtension ORCHESTRATOR = TestsUtils.ORCHESTRATOR;

private static final String PROJECT_KEY = "coverage_project";
private static final String LINES_TO_COVER = "lines_to_cover";
Expand All @@ -55,7 +54,7 @@ void basic_coverage_reports_with_windows_paths() {
}

private static void basicCoverageReports(String utReportPath) {
SonarScanner build = SonarScanner.create()
SonarScanner build = ORCHESTRATOR.createSonarScanner()
.setProjectDir(new File(COVERAGE_PROJECT))
.setProperty(DEPRECATED_COVERAGE_REPORT_PATH, "someReport")
.setProperty(COVERAGE_REPORT_PATHS, utReportPath+",it-coverage1.xml,it-coverage2.xml");
Expand All @@ -75,7 +74,7 @@ private static void basicCoverageReports(String utReportPath) {

@Test
void default_values() {
SonarScanner build = SonarScanner.create()
SonarScanner build = ORCHESTRATOR.createSonarScanner()
.setProjectDir(new File(COVERAGE_PROJECT));
ORCHESTRATOR.executeBuild(build);

Expand All @@ -90,7 +89,7 @@ void default_values() {

@Test
void empty_property() {
SonarScanner build = SonarScanner.create()
SonarScanner build = ORCHESTRATOR.createSonarScanner()
.setProjectDir(new File(COVERAGE_PROJECT))
.setProperty(DEPRECATED_COVERAGE_REPORT_PATH, "")
.setProperty(COVERAGE_REPORT_PATHS, "");
Expand All @@ -106,7 +105,7 @@ void empty_property() {

@Test
void empty_coverage_report() {
SonarScanner build = SonarScanner.create()
SonarScanner build = ORCHESTRATOR.createSonarScanner()
.setProjectDir(new File(COVERAGE_PROJECT))
.setProperty(DEPRECATED_COVERAGE_REPORT_PATH, EMPTY_XML)
.setProperty(COVERAGE_REPORT_PATHS, EMPTY_XML);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,32 +17,33 @@
package com.sonar.python.it.plugin;

import com.sonar.orchestrator.build.SonarScanner;
import com.sonar.orchestrator.junit5.OrchestratorExtension;
import java.io.File;
import java.util.List;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.RegisterExtension;
import org.junit.jupiter.api.parallel.ResourceLock;
import org.sonarqube.ws.Issues;
import org.sonarqube.ws.client.issues.SearchRequest;

import static com.sonar.python.it.plugin.TestsUtils.newWsClient;
import static java.util.Collections.singletonList;
import static org.assertj.core.api.Assertions.assertThat;

@ResourceLock("project/custom_rules")
class CustomRulesExampleTest {

@RegisterExtension
public static final OrchestratorExtension orchestrator = TestsUtils.ORCHESTRATOR;
public static final ConcurrentOrchestratorExtension orchestrator = TestsUtils.ORCHESTRATOR;

private static final String PROJECT_KEY = "custom-rules";
private static final String PROJECT_NAME = "Custom Rules";
private static final String PROJECT_KEY = "custom-rules-example";
private static final String PROJECT_NAME = "Custom Rules Example";

@BeforeAll
static void prepare() {
orchestrator.getServer().provisionProject(PROJECT_KEY, PROJECT_NAME);
orchestrator.getServer().associateProjectToQualityProfile(PROJECT_KEY, "py", "python-custom-rules-example-profile");
SonarScanner build = SonarScanner.create()
SonarScanner build = orchestrator.createSonarScanner()
.setProjectDir(new File("projects/custom_rules"))
.setProjectKey(PROJECT_KEY)
.setProjectName(PROJECT_NAME)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,23 +17,24 @@
package com.sonar.python.it.plugin;

import com.sonar.orchestrator.build.SonarScanner;
import com.sonar.orchestrator.junit5.OrchestratorExtension;
import java.io.File;
import java.util.List;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.RegisterExtension;
import org.junit.jupiter.api.parallel.ResourceLock;
import org.sonarqube.ws.Issues;
import org.sonarqube.ws.client.issues.SearchRequest;

import static com.sonar.python.it.plugin.TestsUtils.newWsClient;
import static java.util.Collections.singletonList;
import static org.assertj.core.api.Assertions.assertThat;

@ResourceLock("project/custom_rules")
class CustomRulesTest {

@RegisterExtension
public static final OrchestratorExtension orchestrator = TestsUtils.ORCHESTRATOR;
public static final ConcurrentOrchestratorExtension orchestrator = TestsUtils.ORCHESTRATOR;

private static final String PROJECT_KEY = "custom-rules";
private static final String PROJECT_NAME = "Custom Rules";
Expand All @@ -42,7 +43,7 @@ class CustomRulesTest {
static void prepare() {
orchestrator.getServer().provisionProject(PROJECT_KEY, PROJECT_NAME);
orchestrator.getServer().associateProjectToQualityProfile(PROJECT_KEY, "py", "python-custom-rules-profile");
SonarScanner build = SonarScanner.create()
SonarScanner build = orchestrator.createSonarScanner()
.setProjectDir(new File("projects/custom_rules"))
.setProjectKey(PROJECT_KEY)
.setProjectName(PROJECT_NAME)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,6 @@
*/
package com.sonar.python.it.plugin;

import com.sonar.orchestrator.build.SonarScanner;
import com.sonar.orchestrator.junit5.OrchestratorExtension;
import java.io.File;
import java.util.List;
import org.junit.jupiter.api.Test;
Expand All @@ -33,14 +31,14 @@ class Flake8ReportTest {
private static final String PROJECT = "flake8_project";

@RegisterExtension
public static final OrchestratorExtension ORCHESTRATOR = TestsUtils.ORCHESTRATOR;
public static final ConcurrentOrchestratorExtension ORCHESTRATOR = TestsUtils.ORCHESTRATOR;

@Test
void import_report() {
ORCHESTRATOR.getServer().provisionProject(PROJECT, PROJECT);
ORCHESTRATOR.getServer().associateProjectToQualityProfile(PROJECT, "py", "no_rule");
ORCHESTRATOR.executeBuild(
SonarScanner.create()
ORCHESTRATOR.createSonarScanner()
.setProjectDir(new File("projects/flake8_project")));

List<Issues.Issue> issues = issues(PROJECT);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@

import com.sonar.orchestrator.build.BuildResult;
import com.sonar.orchestrator.build.SonarScanner;
import com.sonar.orchestrator.junit5.OrchestratorExtension;
import java.io.File;
import org.assertj.core.data.Offset;
import org.junit.jupiter.api.BeforeAll;
Expand Down Expand Up @@ -56,15 +55,15 @@ class MetricsTest {
private static final Offset<Double> OFFSET = Offset.offset(0.01d);

@RegisterExtension
public static final OrchestratorExtension orchestrator = TestsUtils.ORCHESTRATOR;
public static final ConcurrentOrchestratorExtension orchestrator = TestsUtils.ORCHESTRATOR;

private static BuildResult buildResult;

@BeforeAll
static void startServer() {
orchestrator.getServer().provisionProject(PROJECT_KEY, PROJECT_KEY);
orchestrator.getServer().associateProjectToQualityProfile(PROJECT_KEY, "py", "no_rule");
SonarScanner build = SonarScanner.create()
SonarScanner build = orchestrator.createSonarScanner()
.setProjectDir(new File("projects/metrics"))
.setProjectKey(PROJECT_KEY)
.setProjectName(PROJECT_KEY)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,6 @@
*/
package com.sonar.python.it.plugin;

import com.sonar.orchestrator.build.SonarScanner;
import com.sonar.orchestrator.junit5.OrchestratorExtension;
import java.io.File;
import java.util.List;
import org.junit.jupiter.api.Test;
Expand All @@ -33,14 +31,14 @@ class MypyReportTest {
private static final String PROJECT = "mypy_project";

@RegisterExtension
public static final OrchestratorExtension ORCHESTRATOR = TestsUtils.ORCHESTRATOR;
public static final ConcurrentOrchestratorExtension ORCHESTRATOR = TestsUtils.ORCHESTRATOR;

@Test
void import_report() {
ORCHESTRATOR.getServer().provisionProject(PROJECT, PROJECT);
ORCHESTRATOR.getServer().associateProjectToQualityProfile(PROJECT, "py", "no_rule");
ORCHESTRATOR.executeBuild(
SonarScanner.create()
ORCHESTRATOR.createSonarScanner()
.setProjectDir(new File("projects/mypy_project")));

List<Issues.Issue> issues = issues(PROJECT);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
package com.sonar.python.it.plugin;

import com.sonar.orchestrator.build.SonarScanner;
import com.sonar.orchestrator.junit5.OrchestratorExtension;
import java.io.File;
import java.util.List;
import org.junit.jupiter.api.BeforeAll;
Expand All @@ -34,13 +33,13 @@ class NoSonarTest {
private static final String PROFILE_NAME = "nosonar";

@RegisterExtension
public static final OrchestratorExtension ORCHESTRATOR = TestsUtils.ORCHESTRATOR;
public static final ConcurrentOrchestratorExtension ORCHESTRATOR = TestsUtils.ORCHESTRATOR;

@BeforeAll
static void startServer() {
ORCHESTRATOR.getServer().provisionProject(PROJECT_KEY, PROJECT_KEY);
ORCHESTRATOR.getServer().associateProjectToQualityProfile(PROJECT_KEY, "py", PROFILE_NAME);
SonarScanner build = SonarScanner.create()
SonarScanner build = ORCHESTRATOR.createSonarScanner()
.setProjectDir(new File("projects", PROJECT_KEY))
.setProjectKey(PROJECT_KEY)
.setProjectName(PROJECT_KEY)
Expand Down
Loading
Loading