Skip to content

Commit

Permalink
Add Bootstrap IT and property-dump-plugin
Browse files Browse the repository at this point in the history
  • Loading branch information
leonardo-pilastri-sonarsource committed Oct 18, 2024
1 parent 271626d commit 3b73f13
Show file tree
Hide file tree
Showing 5 changed files with 202 additions and 2 deletions.
1 change: 1 addition & 0 deletions .cirrus.yml
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ build_task:
script:
- source cirrus-env BUILD
- regular_mvn_build_deploy_analyze
- mvn -f property-dump-plugin/pom.xml install
cleanup_before_cache_script:
- cleanup_maven_repository

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@
import com.sonar.orchestrator.http.HttpMethod;
import com.sonar.orchestrator.http.HttpResponse;
import com.sonar.orchestrator.junit5.OrchestratorExtension;
import com.sonar.orchestrator.locator.MavenLocation;
import com.sonar.orchestrator.version.Version;

import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
Expand All @@ -39,7 +39,6 @@
import java.util.regex.Pattern;
import java.util.stream.Collectors;
import javax.annotation.CheckForNull;

import org.apache.commons.lang.StringUtils;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
Expand Down Expand Up @@ -70,6 +69,8 @@ public abstract class AbstractMavenTest {
.addBundledPluginToKeep("sonar-java-plugin")
.addBundledPluginToKeep("sonar-xml-plugin")
.addBundledPluginToKeep("sonar-html-plugin")
// This plugin should have been built locally from the property-dump-plugin module
.addPlugin(MavenLocation.of("org.sonarsource.scanner.maven", "property-dump-plugin", "1"))
.build();

protected HttpConnector wsConnector;
Expand Down
94 changes: 94 additions & 0 deletions its/src/test/java/com/sonar/maven/it/suite/BootstrapTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
/*
* SonarSource :: IT :: SonarQube Maven
* Copyright (C) 2009-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 GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 3 of the License, or (at your option) any later version.
*
* 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 GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program; if not, write to the Free Software Foundation,
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
package com.sonar.maven.it.suite;

import com.sonar.maven.it.ItUtils;
import com.sonar.orchestrator.build.BuildResult;
import com.sonar.orchestrator.build.BuildRunner;
import com.sonar.orchestrator.build.MavenBuild;
import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.Properties;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.io.TempDir;

import static org.assertj.core.api.Assertions.assertThat;

public class BootstrapTest extends AbstractMavenTest {

@TempDir
public Path temp;

@Test
void test_unsupported_platform() {
String unsupportedOS = "unsupportedOS";
String arch = "amd64";
BuildRunner runner = new BuildRunner(ORCHESTRATOR.getConfiguration());
MavenBuild build = MavenBuild.create(ItUtils.locateProjectPom("maven/aggregator-inherit-parent"))
.setProperty("sonar.scanner.os", unsupportedOS)
.setProperty("sonar.scanner.arch", arch)
.setProperty("sonar.login", ORCHESTRATOR.getDefaultAdminToken())
.setProperty("sonar.host.url", ORCHESTRATOR.getServer().getUrl())
// .setEnvironmentVariable("MAVEN_OPTS","-Xdebug -Xnoagent -Djava.compiler=NONE -Xrunjdwp:transport=dt_socket,server=y,suspend=y,address=8000")
.setGoals(cleanSonarGoal());

BuildResult result = runner.runQuietly(null, build);
assertThat(result.isSuccess()).isFalse();

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);
assertThat(result.getLogs()).contains(expectedLog);
}

@Test
void test_supported_arch_to_assert_jre_used() throws IOException {
BuildRunner runner = new BuildRunner(ORCHESTRATOR.getConfiguration());
String projectName = "maven/aggregator-inherit-parent";
MavenBuild build = MavenBuild.create(ItUtils.locateProjectPom(projectName))
.setProperty("sonar.login", ORCHESTRATOR.getDefaultAdminToken())
.setProperty("sonar.host.url", ORCHESTRATOR.getServer().getUrl())
.setGoals(cleanSonarGoal());


BuildResult result = runner.runQuietly(null, build);
assertThat(result.isSuccess()).isTrue();
//load properties file
Path propertiesFile = ItUtils.locateProjectDir(projectName).toPath().resolve("target/sonar/dumpSensor.properties");
Properties props = new Properties();
props.load(Files.newInputStream(propertiesFile));

if (ORCHESTRATOR.getServer().version().isGreaterThanOrEquals(10, 6)) {
//we test that we are actually using the JRE downloaded from SQ
assertThat(props.getProperty("java.home"))
.isNotEmpty()
.isNotEqualTo(System.getProperty("java.home"))
.contains(".sonar" + File.separator + "cache");
} else {
//we test that we are using the system JRE
assertThat(props.getProperty("java.home"))
.isNotEmpty()
.isEqualTo(System.getProperty("java.home"))
.doesNotContain(".sonar" + File.separator + "cache");
}
}

}
52 changes: 52 additions & 0 deletions property-dump-plugin/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<parent>
<groupId>org.sonarsource.parent</groupId>
<artifactId>parent</artifactId>
<version>74.0.0.1768</version>
<relativePath />
</parent>

<groupId>org.sonarsource.scanner.maven</groupId>
<artifactId>property-dump-plugin</artifactId>
<version>1</version>
<packaging>sonar-plugin</packaging>

<properties>
<jdk.min.version>17</jdk.min.version>
</properties>

<dependencies>
<dependency>
<groupId>org.sonarsource.api.plugin</groupId>
<artifactId>sonar-plugin-api</artifactId>
<version>10.12.0.2522</version>
<scope>provided</scope>
</dependency>
</dependencies>


<build>
<plugins>
<plugin>
<groupId>org.sonarsource.sonar-packaging-maven-plugin</groupId>
<artifactId>sonar-packaging-maven-plugin</artifactId>
<version>1.23.0.740</version>
<extensions>true</extensions>
<configuration>
<pluginName>Property Dump Plugin</pluginName>
<skipDependenciesPackaging>true</skipDependenciesPackaging>
<pluginClass>org.sonar.dump.PropertyDumpPlugin</pluginClass>
<sonarLintSupported>true</sonarLintSupported>
<pluginApiMinVersion>9.14.0.375</pluginApiMinVersion>
<jreMinVersion>17</jreMinVersion>
</configuration>
</plugin>
</plugins>
</build>

</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
/*
* property-dump-plugin
* Copyright (C) 2009-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 GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 3 of the License, or (at your option) any later version.
*
* 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 GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program; if not, write to the Free Software Foundation,
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
package org.sonar.dump;

import java.io.IOException;
import java.nio.file.Files;
import org.sonar.api.Plugin;
import org.sonar.api.batch.sensor.Sensor;
import org.sonar.api.batch.sensor.SensorContext;
import org.sonar.api.batch.sensor.SensorDescriptor;

public class PropertyDumpPlugin implements Plugin, Sensor {

@Override
public void define(Context context) {
context.addExtension(this);
}

@Override
public void describe(SensorDescriptor sensorDescriptor) {
sensorDescriptor.name("Property Dump Sensor");
}

@Override
public void execute(SensorContext sensorContext) {
var props = System.getProperties();
//please copilot, save properties to a file
try {
props.store(Files.newOutputStream(sensorContext.fileSystem().workDir().toPath().resolve("dumpSensor.properties")), null);
} catch (IOException e) {
throw new RuntimeException(e);
}
}

}

0 comments on commit 3b73f13

Please sign in to comment.