From e4c63ddcb456602c011d1f335bc685d8e4996858 Mon Sep 17 00:00:00 2001 From: Alban Auzeill Date: Thu, 31 Oct 2024 11:45:00 +0100 Subject: [PATCH] SCANMAVEN-243 The SonarScanner for Maven does not try to contact the server when the project is skipped (#250) --- .../maven/bootstrap-small-project/pom.xml | 32 +++++ .../sonar/maven/it/suite/BootstrapTest.java | 128 ++++++++++++++++-- 2 files changed, 149 insertions(+), 11 deletions(-) diff --git a/its/projects/maven/bootstrap-small-project/pom.xml b/its/projects/maven/bootstrap-small-project/pom.xml index a7262ddf..96bc2e62 100644 --- a/its/projects/maven/bootstrap-small-project/pom.xml +++ b/its/projects/maven/bootstrap-small-project/pom.xml @@ -26,4 +26,36 @@ + + + + test-sonar-skip + + false + + + true + + + + + test-plugin-skip + + false + + + + + org.sonarsource.scanner.maven + sonar-maven-plugin + + true + + + + + + + + diff --git a/its/src/test/java/com/sonar/maven/it/suite/BootstrapTest.java b/its/src/test/java/com/sonar/maven/it/suite/BootstrapTest.java index 411f1ff7..826d0812 100644 --- a/its/src/test/java/com/sonar/maven/it/suite/BootstrapTest.java +++ b/its/src/test/java/com/sonar/maven/it/suite/BootstrapTest.java @@ -37,6 +37,13 @@ class BootstrapTest extends AbstractMavenTest { + private static final String EFFECTIVE_JRE_PROVISIONING_LOG = "JRE provisioning:"; + private static final String COMMUNICATING_WITH_SONARQUBE = "Communicating with SonarQube Server"; + private static final String STARTING_SCANNER_ENGINE = "Starting SonarScanner Engine"; + private static final String SKIPPING_ANALYSIS = "Skipping analysis"; + public static final String JRE_PROVISIONING_IS_DISABLED = "JRE provisioning is disabled"; + public static final String USING_CONFIGURED_JRE = "Using the configured java executable"; + @Test void test_unsupported_platform() { String unsupportedOS = "unsupportedOS"; @@ -50,8 +57,7 @@ void test_unsupported_platform() { .setProperty("sonar.host.url", ORCHESTRATOR.getServer().getUrl()) .setGoals(cleanSonarGoal()); - boolean sonarQubeThatSupportJREProvisioning = ORCHESTRATOR.getServer().version().isGreaterThanOrEquals(10, 6); - if (sonarQubeThatSupportJREProvisioning) { + if (isSonarQubeSupportsJREProvisioning()) { BuildResult result = validateBuildWithoutCE(runner.runQuietly(null, build), EXEC_FAILED); String url = ORCHESTRATOR.getServer().getUrl() + String.format("/api/v2/analysis/jres?os=%s&arch=%s", unsupportedOS, arch); String expectedLog = String.format("Error status returned by url [%s]: 400", url); @@ -61,6 +67,93 @@ void test_unsupported_platform() { } } + @Test + void test_bootstrapping_with_sonar_skip_in_pom_xml() { + MavenBuild build = MavenBuild.create(ItUtils.locateProjectPom("maven/bootstrap-small-project")) + .setProperty("sonar.login", ORCHESTRATOR.getDefaultAdminToken()) + .setProperty("sonar.host.url", ORCHESTRATOR.getServer().getUrl()) + // activate in the pom.xml true + .addArguments("-Ptest-sonar-skip") + .setGoals(sonarGoal()); + BuildResult result = executeBuildAndValidateWithoutCE(build); + assertThat(result.getLogs()) + .contains(SKIPPING_ANALYSIS) + .doesNotContain(EFFECTIVE_JRE_PROVISIONING_LOG, COMMUNICATING_WITH_SONARQUBE, STARTING_SCANNER_ENGINE); + } + + @Test + void test_bootstrapping_with_sonar_skip_in_system_property() { + MavenBuild build = MavenBuild.create(ItUtils.locateProjectPom("maven/bootstrap-small-project")) + .setProperty("sonar.login", ORCHESTRATOR.getDefaultAdminToken()) + .setProperty("sonar.host.url", ORCHESTRATOR.getServer().getUrl()) + // analyze using: mvn sonar:sonar -Dsonar.skip=true + .setProperty("sonar.skip", "true") + .setGoals(sonarGoal()); + BuildResult result = executeBuildAndValidateWithoutCE(build); + assertThat(result.getLogs()) + .contains(SKIPPING_ANALYSIS) + .doesNotContain(EFFECTIVE_JRE_PROVISIONING_LOG, COMMUNICATING_WITH_SONARQUBE, STARTING_SCANNER_ENGINE); + } + + @Test + void test_bootstrapping_with_sonar_skip_in_plugin_configuration() { + MavenBuild build = MavenBuild.create(ItUtils.locateProjectPom("maven/bootstrap-small-project")) + .setProperty("sonar.login", ORCHESTRATOR.getDefaultAdminToken()) + .setProperty("sonar.host.url", ORCHESTRATOR.getServer().getUrl()) + // activate in the pom.xml true + .addArguments("-Ptest-plugin-skip") + .setGoals(sonarGoal()); + BuildResult result = executeBuildAndValidateWithoutCE(build); + assertThat(result.getLogs()) + .contains(SKIPPING_ANALYSIS) + .doesNotContain(EFFECTIVE_JRE_PROVISIONING_LOG, COMMUNICATING_WITH_SONARQUBE, STARTING_SCANNER_ENGINE); + } + + @Test + void test_bootstrapping_that_skip_the_JRE_provisioning() throws IOException { + String projectName = "maven/bootstrap-small-project"; + MavenBuild build = MavenBuild.create(ItUtils.locateProjectPom(projectName)) + .setProperty("sonar.login", ORCHESTRATOR.getDefaultAdminToken()) + .setProperty("sonar.host.url", ORCHESTRATOR.getServer().getUrl()) + .setProperty("sonar.scanner.skipJreProvisioning", "true") + .setEnvironmentVariable("DUMP_SYSTEM_PROPERTIES", "java.home") + .setGoals(sonarGoal()); + BuildResult result = executeBuildAndValidateWithCE(build); + assertThat(result.getLogs()) + .doesNotContain(EFFECTIVE_JRE_PROVISIONING_LOG); + if (isSonarQubeSupportsJREProvisioning()) { + assertThat(result.getLogs()) + .contains(JRE_PROVISIONING_IS_DISABLED, COMMUNICATING_WITH_SONARQUBE, STARTING_SCANNER_ENGINE); + } + Path propertiesFile = ItUtils.locateProjectDir(projectName).toPath().resolve("target/sonar/dumpSensor.system.properties"); + Properties props = new Properties(); + props.load(Files.newInputStream(propertiesFile)); + assertThat(props.getProperty("java.home")).isEqualTo(guessJavaHomeSelectedByMvn()); + } + + @Test + void test_bootstrapping_that_use_the_provided_JRE_instead_of_downloading_a_JRE() throws IOException { + String mvnJavaHome = guessJavaHomeSelectedByMvn(); + String projectName = "maven/bootstrap-small-project"; + MavenBuild build = MavenBuild.create(ItUtils.locateProjectPom(projectName)) + .setProperty("sonar.login", ORCHESTRATOR.getDefaultAdminToken()) + .setProperty("sonar.host.url", ORCHESTRATOR.getServer().getUrl()) + .setProperty("sonar.scanner.javaExePath", mvnJavaHome + File.separator + "bin" + File.separator + "java") + .setEnvironmentVariable("DUMP_SYSTEM_PROPERTIES", "java.home") + .setGoals(sonarGoal()); + BuildResult result = executeBuildAndValidateWithCE(build); + assertThat(result.getLogs()) + .doesNotContain(EFFECTIVE_JRE_PROVISIONING_LOG, JRE_PROVISIONING_IS_DISABLED); + if (isSonarQubeSupportsJREProvisioning()) { + assertThat(result.getLogs()) + .contains(USING_CONFIGURED_JRE, COMMUNICATING_WITH_SONARQUBE, STARTING_SCANNER_ENGINE); + } + Path propertiesFile = ItUtils.locateProjectDir(projectName).toPath().resolve("target/sonar/dumpSensor.system.properties"); + Properties props = new Properties(); + props.load(Files.newInputStream(propertiesFile)); + assertThat(props.getProperty("java.home")).isEqualTo(mvnJavaHome); + } + @Test void test_supported_arch_to_assert_jre_used() throws IOException { BuildRunner runner = new BuildRunner(ORCHESTRATOR.getConfiguration()); @@ -131,30 +224,30 @@ void test_supported_arch_to_assert_jre_used() throws IOException { softly.assertThat(props.getProperty("sonar.java.source")).isEqualTo( "11"); softly.assertThat(props.getProperty("sonar.java.target")).isEqualTo( "11"); softly.assertThat(props.getProperty("sonar.java.test.libraries")).contains("jsr305-3.0.2.jar"); - // sonar.java.jdkHome should be the one used by "mvn sonar:sonar", by default maven uses JAVA_HOME - String javaHome = System.getenv("JAVA_HOME"); - if (javaHome == null) { - javaHome = System.getProperty("java.home"); - } - softly.assertThat(props.getProperty("sonar.java.jdkHome")).isEqualTo( new File(javaHome).getCanonicalPath()); + // sonar.java.jdkHome should be the one used by "mvn sonar:sonar" + softly.assertThat(props.getProperty("sonar.java.jdkHome")).isEqualTo(guessJavaHomeSelectedByMvn()); StringAssert javaHomeAssertion = softly.assertThat(props.getProperty("java.home")).isNotEmpty(); - if (ORCHESTRATOR.getServer().version().isGreaterThanOrEquals(10, 6)) { + if (isSonarQubeSupportsJREProvisioning()) { //we test that we are actually using the JRE downloaded from SQ javaHomeAssertion .isNotEqualTo(System.getProperty("java.home")) + .isNotEqualTo(guessJavaHomeSelectedByMvn()) .contains(".sonar" + File.separator + "cache"); // System properties of the initial JRE are intentionally not set on the provisioned JRE softly.assertThat(props.getProperty("http.nonProxyHosts")) - .isEmpty(); + .isNotEqualTo("localhost|my-custom-non-proxy.server.com"); // System properties defined in "sonar.scanner.javaOpts" are set on the provisioned JRE softly.assertThat(props.getProperty("http.proxyUser")).isEqualTo("my-custom-user-from-system-properties"); + + softly.assertThat(result.getLogs()) + .contains(EFFECTIVE_JRE_PROVISIONING_LOG, COMMUNICATING_WITH_SONARQUBE, STARTING_SCANNER_ENGINE); } else { //we test that we are using the system JRE javaHomeAssertion - .isEqualTo(System.getProperty("java.home")) + .isEqualTo(guessJavaHomeSelectedByMvn()) .doesNotContain(".sonar" + File.separator + "cache"); softly.assertThat(props.getProperty("http.nonProxyHosts")) @@ -166,4 +259,17 @@ void test_supported_arch_to_assert_jre_used() throws IOException { softly.assertAll(); } + private static boolean isSonarQubeSupportsJREProvisioning() { + return ORCHESTRATOR.getServer().version().isGreaterThanOrEquals(10, 6); + } + + private static String guessJavaHomeSelectedByMvn() throws IOException { + // By default maven uses JAVA_HOME if it exists, otherwise we don't know, we hope it uses the one that is currently running + String javaHome = System.getenv("JAVA_HOME"); + if (javaHome == null) { + javaHome = System.getProperty("java.home"); + } + return new File(javaHome).getCanonicalPath(); + } + }