Skip to content

Commit

Permalink
SCANMAVEN-243 The SonarScanner for Maven does not try to contact the …
Browse files Browse the repository at this point in the history
…server when the project is skipped (#250)
  • Loading branch information
alban-auzeill authored Oct 31, 2024
1 parent 69c0e1a commit e4c63dd
Show file tree
Hide file tree
Showing 2 changed files with 149 additions and 11 deletions.
32 changes: 32 additions & 0 deletions its/projects/maven/bootstrap-small-project/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -26,4 +26,36 @@
</dependency>
</dependencies>

<profiles>

<profile>
<id>test-sonar-skip</id>
<activation>
<activeByDefault>false</activeByDefault>
</activation>
<properties>
<sonar.skip>true</sonar.skip>
</properties>
</profile>

<profile>
<id>test-plugin-skip</id>
<activation>
<activeByDefault>false</activeByDefault>
</activation>
<build>
<plugins>
<plugin>
<groupId>org.sonarsource.scanner.maven</groupId>
<artifactId>sonar-maven-plugin</artifactId>
<configuration>
<skip>true</skip>
</configuration>
</plugin>
</plugins>
</build>
</profile>

</profiles>

</project>
128 changes: 117 additions & 11 deletions its/src/test/java/com/sonar/maven/it/suite/BootstrapTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand All @@ -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);
Expand All @@ -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 <properties><sonar.skip>true</sonar.skip></properties>
.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 <configuration><skip>true</skip></configuration>
.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());
Expand Down Expand Up @@ -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"))
Expand All @@ -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();
}

}

0 comments on commit e4c63dd

Please sign in to comment.