Skip to content

Commit

Permalink
Fix parallel 'its'
Browse files Browse the repository at this point in the history
  • Loading branch information
alban-auzeill committed Oct 23, 2024
1 parent 40023f0 commit 94705dd
Show file tree
Hide file tree
Showing 12 changed files with 169 additions and 63 deletions.
2 changes: 1 addition & 1 deletion .cirrus.yml
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ qa_task:
<<: *ONLY_SONARSOURCE_QA
eks_container:
<<: *CONTAINER_DEFINITION
cpu: 2
cpu: 6
memory: 4G
env:
matrix:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
<version>1.0-SNAPSHOT</version>
<relativePath>parent/pom.xml</relativePath>
</parent>
<artifactId>aggregator</artifactId>
<artifactId>aggregator-inherit-parent</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>pom</packaging>

Expand Down
13 changes: 13 additions & 0 deletions its/projects/maven/bootstrap-small-project/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<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>
<groupId>org.sonarsource.maven.its</groupId>
<artifactId>bootstrap-small-project</artifactId>
<version>1.0-SNAPSHOT</version>

<properties>
<maven.compiler.source>22</maven.compiler.source>
<maven.compiler.target>22</maven.compiler.target>
</properties>

</project>
4 changes: 2 additions & 2 deletions its/projects/project-default-config/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@
<artifactId>maven-compiler-plugin</artifactId>
<version>3.3</version>
<configuration>
<source>1.7</source>
<target>1.7</target>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
</plugins>
Expand Down
2 changes: 1 addition & 1 deletion its/projects/version/compilerPluginConfig/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
<artifactId>maven-compiler-plugin</artifactId>
<version>3.3</version>
<configuration>
<source>1.7</source>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
Expand Down
2 changes: 1 addition & 1 deletion its/projects/version/properties/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
<name>Sonar :: Integration Tests :: Java Version</name>

<properties>
<maven.compiler.source>1.7</maven.compiler.source>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
</properties>

Expand Down
104 changes: 103 additions & 1 deletion its/src/test/java/com/sonar/maven/it/suite/AbstractMavenTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@

import com.eclipsesource.json.Json;
import com.eclipsesource.json.JsonValue;
import com.sonar.orchestrator.build.Build;
import com.sonar.orchestrator.build.BuildResult;
import com.sonar.orchestrator.build.MavenBuild;
import com.sonar.orchestrator.container.Server;
Expand All @@ -33,6 +34,7 @@
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;
import java.util.regex.Matcher;
Expand All @@ -44,22 +46,32 @@
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.BeforeEach;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.sonarqube.ws.Ce.TaskStatus;
import org.sonarqube.ws.Components.Component;
import org.sonarqube.ws.Measures;
import org.sonarqube.ws.Measures.Measure;
import org.sonarqube.ws.client.HttpConnector;
import org.sonarqube.ws.client.HttpException;
import org.sonarqube.ws.client.WsClient;
import org.sonarqube.ws.client.WsClientFactories;
import org.sonarqube.ws.client.ce.TaskRequest;
import org.sonarqube.ws.client.components.ShowRequest;
import org.sonarqube.ws.client.components.TreeRequest;
import org.sonarqube.ws.client.measures.ComponentRequest;

import static java.util.Collections.singletonList;
import static org.assertj.core.api.Assertions.assertThat;


public abstract class AbstractMavenTest {

private static final Logger LOG = LoggerFactory.getLogger(AbstractMavenTest.class);

public static final int EXEC_SUCCESS = 0;
public static final int EXEC_FAILED = 1;

private static final Pattern VERSION_REGEX = Pattern.compile("Apache Maven\\s(\\d+\\.\\d+(?:\\.\\d+)?)(?:-\\S+)?\\s");

private static Version mojoVersion;
Expand Down Expand Up @@ -94,6 +106,9 @@ public static void setUp() {
public static void tearDown() {
synchronized (AbstractMavenTest.class) {
orchestratorAccessingClassCount--;
if (orchestratorAccessingClassCount < 0) {
throw new IllegalStateException("tearDown called too many times");
}
if (orchestratorAccessingClassCount == 0) {
ORCHESTRATOR.stop();
}
Expand Down Expand Up @@ -219,6 +234,13 @@ static WsClient newWsClient() {
.build());
}

static WsClient newAuthenticatedWsClient() {
return WsClientFactories.getDefault().newClient(HttpConnector.newBuilder()
.url(ORCHESTRATOR.getServer().getUrl())
.credentials(Server.ADMIN_LOGIN, Server.ADMIN_PASSWORD)
.build());
}

Version mavenVersion = null;

protected Version getMavenVersion() {
Expand All @@ -229,7 +251,7 @@ protected Version getMavenVersion() {

MavenBuild build = MavenBuild.create()
.setGoals("-version");
BuildResult result = ORCHESTRATOR.executeBuild(build);
BuildResult result = assertBuildResultStatuses( ORCHESTRATOR.executeBuild(build), 0);

String logs = result.getLogs();
Matcher matcher = VERSION_REGEX.matcher(logs);
Expand All @@ -246,4 +268,84 @@ private static String getSonarVersion() {
return versionProperty != null ? versionProperty : "LATEST_RELEASE";
}

public BuildResult executeBuildAndValidateWithCE(Build<?> build) {
return validateBuildWithCE(ORCHESTRATOR.executeBuild(build));
}

public static BuildResult validateBuildWithCE(BuildResult result) {
assertBuildResultStatuses(result, 0);
List<String> ceTaskIds = extractCETaskIds(result);
if (ceTaskIds.isEmpty()) {
throw new AssertionError("No CE task id found in logs, can't wait for the CE task to be finished");
}
for (String ceTaskId : ceTaskIds) {
waitForCeTaskToBeFinished(ceTaskId);
}
return result;
}

public BuildResult executeBuildAndValidateWithoutCE(Build<?> build) {
return validateBuildWithoutCE(ORCHESTRATOR.executeBuild(build));
}

public static BuildResult validateBuildWithoutCE(BuildResult result) {
return validateBuildWithoutCE(result, EXEC_SUCCESS);
}

public static BuildResult validateBuildWithoutCE(BuildResult result, int expectedStatus) {
assertBuildResultStatuses(result, expectedStatus);
assertThat(extractCETaskIds(result))
.withFailMessage("The build result contains unexpected CE task ids")
.isEmpty();
return result;
}

public static BuildResult assertBuildResultStatuses(BuildResult result, int expectedStatus) {
for (Integer status : result.getStatuses()) {
assertThat(status).isEqualTo(expectedStatus);
}
return result;
}

// [INFO] More about the report processing at http://127.0.0.1:63532/api/ce/task?id=bedf3100-4d72-497b-8103-68402821e49c
private static final Pattern CE_TASK_ID_PATTERN = Pattern.compile("More about the report processing at[^?]++\\?id=([\\w\\-]++)");

public static final List<String> extractCETaskIds(BuildResult result) {
Matcher matcher = CE_TASK_ID_PATTERN.matcher(result.getLogs());
List<String> ids = new ArrayList<>();
while (matcher.find()) {
ids.add(matcher.group(1));
}
return ids;
}

private static final long POLLING_TIME = 500; // 0.5 second
private static final long MAX_WAIT_TIME = 20_000; // 20 seconds

private static void waitForCeTaskToBeFinished(String ceTaskId) {
LOG.info("Waiting for CE task {} to be finished", ceTaskId);
try {
WsClient wsClient = newAuthenticatedWsClient();
long start = System.currentTimeMillis();
while (true) {
TaskStatus status = wsClient.ce().task(new TaskRequest().setId(ceTaskId)).getTask().getStatus();
if (status == TaskStatus.PENDING || status == TaskStatus.IN_PROGRESS) {
if (System.currentTimeMillis() - start > MAX_WAIT_TIME) {
throw new AssertionError("CE task " + ceTaskId + " did not finish after " + (MAX_WAIT_TIME / 1000) + " seconds");
}
Thread.sleep(POLLING_TIME);
} else if (status == TaskStatus.SUCCESS) {
LOG.info("CE task {} succeeded", ceTaskId);
return;
} else {
// FAILED or CANCELED
throw new AssertionError("CE task " + ceTaskId + " failed: " + status.name());
}

}
} catch (InterruptedException e) {
throw new AssertionError("Interrupted while waiting for CE task to be finished", e);
}
}

}
8 changes: 4 additions & 4 deletions its/src/test/java/com/sonar/maven/it/suite/BootstrapTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -40,14 +40,14 @@ void test_unsupported_platform() {
String arch = "amd64";

BuildRunner runner = new BuildRunner(ORCHESTRATOR.getConfiguration());
MavenBuild build = MavenBuild.create(ItUtils.locateProjectPom("maven/aggregator-inherit-parent"))
MavenBuild build = MavenBuild.create(ItUtils.locateProjectPom("maven/bootstrap-small-project"))
.setProperty("sonar.scanner.os", unsupportedOS)
.setProperty("sonar.scanner.arch", arch)
.setProperty("sonar.login", ORCHESTRATOR.getDefaultAdminToken())
.setProperty("sonar.host.url", ORCHESTRATOR.getServer().getUrl())
.setGoals(cleanSonarGoal());

BuildResult result = runner.runQuietly(null, build);
BuildResult result = validateBuildWithoutCE(runner.runQuietly(null, build), EXEC_FAILED);
if (ORCHESTRATOR.getServer().version().isGreaterThanOrEquals(10, 6)) {
assertThat(result.isSuccess()).isFalse();

Expand All @@ -63,14 +63,14 @@ void test_unsupported_platform() {
@Test
void test_supported_arch_to_assert_jre_used() throws IOException {
BuildRunner runner = new BuildRunner(ORCHESTRATOR.getConfiguration());
String projectName = "maven/aggregator-inherit-parent";
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())
.setGoals(cleanSonarGoal());


BuildResult result = runner.runQuietly(null, build);
BuildResult result = validateBuildWithCE(runner.runQuietly(null, build));
assertThat(result.isSuccess()).isTrue();
Path propertiesFile = ItUtils.locateProjectDir(projectName).toPath().resolve("target/sonar/dumpSensor.system.properties");
Properties props = new Properties();
Expand Down
22 changes: 11 additions & 11 deletions its/src/test/java/com/sonar/maven/it/suite/JavaTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ void shouldPopulateLibraries() throws IOException {
MavenBuild build = MavenBuild.create(projectPom)
.setGoals(cleanPackageSonarGoal())
.setProperty("sonar.scanner.internal.dumpToFile", outputProps.getAbsolutePath());
ORCHESTRATOR.executeBuild(build);
executeBuildAndValidateWithoutCE(build);

Properties generatedProps = getProps(outputProps);
String[] moduleIds = generatedProps.getProperty("sonar.modules").split(",");
Expand All @@ -85,14 +85,14 @@ void read_default_from_plugins_config() throws Exception {
MavenBuild build = MavenBuild.create(pom)
.setGoals(cleanPackageSonarGoal())
.setProperty("sonar.scanner.internal.dumpToFile", outputProps.getAbsolutePath());
ORCHESTRATOR.executeBuild(build);
executeBuildAndValidateWithoutCE(build);

Properties props = getProps(outputProps);
assertThat(props).contains(
entry("sonar.findbugs.excludeFilters", new File(pom.getParentFile(), "findbugs-filter.xml").toString()),
entry("sonar.junit.reportsPath", new File(pom.getParentFile(), "target/surefire-output").toString()),
entry("sonar.junit.reportPaths", new File(pom.getParentFile(), "target/surefire-output").toString()),
entry("sonar.java.source", "1.7"));
entry("sonar.java.source", "1.8"));
}

@Test
Expand All @@ -104,11 +104,11 @@ void setJavaVersionCompilerConfiguration() throws IOException {
MavenBuild build = MavenBuild.create(pom)
.setGoals(cleanPackageSonarGoal())
.setProperty("sonar.scanner.internal.dumpToFile", outputProps.getAbsolutePath());
ORCHESTRATOR.executeBuild(build);
executeBuildAndValidateWithoutCE(build);

Properties props = getProps(outputProps);
assertThat(props).contains(
entry("sonar.java.source", "1.7"),
entry("sonar.java.source", "1.8"),
entry("sonar.java.target", "1.8"));
}

Expand All @@ -121,11 +121,11 @@ void setJavaVersionProperties() throws IOException {
MavenBuild build = MavenBuild.create(pom)
.setGoals(cleanPackageSonarGoal())
.setProperty("sonar.scanner.internal.dumpToFile", outputProps.getAbsolutePath());
ORCHESTRATOR.executeBuild(build);
executeBuildAndValidateWithoutCE(build);

Properties props = getProps(outputProps);
assertThat(props).contains(
entry("sonar.java.source", "1.7"),
entry("sonar.java.source", "1.8"),
entry("sonar.java.target", "1.8"));
}

Expand All @@ -138,7 +138,7 @@ void setJdkHomeFromCompilerExecutableConfiguration() throws IOException {
MavenBuild build = MavenBuild.create(pom)
.setGoals(sonarGoal())
.setProperty("sonar.scanner.internal.dumpToFile", outputProps.getAbsolutePath());
ORCHESTRATOR.executeBuild(build);
executeBuildAndValidateWithoutCE(build);

Properties props = getProps(outputProps);
String expected = "path/to/java_executable".replace('/', File.separatorChar);
Expand All @@ -157,7 +157,7 @@ void setJdkHomeFromGlobalToolchainsPlugin() throws IOException {
.setGoals("toolchains:toolchain " + sonarGoal())
.addArguments("--toolchains", new File(pom.getParent(), "toolchains.xml").getAbsolutePath())
.setProperty("sonar.scanner.internal.dumpToFile", outputProps.getAbsolutePath());
ORCHESTRATOR.executeBuild(build);
executeBuildAndValidateWithoutCE(build);

Properties props = getProps(outputProps);
assertThat(props).contains(entry("sonar.java.jdkHome", "fake_jdk_1.5"));
Expand All @@ -177,7 +177,7 @@ void setJdkHomeFromCompilerToolchainsConfiguration() throws IOException {
.setGoals(sonarGoal())
.addArguments("--toolchains", new File(pom.getParent(), "toolchains.xml").getAbsolutePath())
.setProperty("sonar.scanner.internal.dumpToFile", outputProps.getAbsolutePath());
ORCHESTRATOR.executeBuild(build);
executeBuildAndValidateWithoutCE(build);

Properties props = getProps(outputProps);
assertThat(props).contains(entry("sonar.java.jdkHome", "fake_jdk_1.6"));
Expand All @@ -197,7 +197,7 @@ void takeFirstToolchainIfMultipleExecutions() throws IOException {
.setGoals(sonarGoal())
.addArguments("--toolchains", new File(pom.getParent(), "toolchains.xml").getAbsolutePath())
.setProperty("sonar.scanner.internal.dumpToFile", outputProps.getAbsolutePath());
ORCHESTRATOR.executeBuild(build);
executeBuildAndValidateWithoutCE(build);

Properties props = getProps(outputProps);
assertThat(props).contains(entry("sonar.java.jdkHome", "fake_jdk_9"));
Expand Down
7 changes: 2 additions & 5 deletions its/src/test/java/com/sonar/maven/it/suite/LinksTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -44,17 +44,14 @@ void shouldUseLinkPropertiesOverPomLinksInMaven() {
MavenBuild build = MavenBuild.create(ItUtils.locateProjectPom("batch/links-project"))
.setGoals(cleanPackageSonarGoal())
.setProperty("sonar.scm.disabled", "true");
ORCHESTRATOR.executeBuild(build);
executeBuildAndValidateWithCE(build);

checkLinks();
}

private void checkLinks() {
Server server = ORCHESTRATOR.getServer();
WsClient client = WsClientFactories.getDefault().newClient(HttpConnector.newBuilder()
.url(server.getUrl())
.credentials(Server.ADMIN_LOGIN, Server.ADMIN_PASSWORD)
.build());
WsClient client = newAuthenticatedWsClient();
SearchWsResponse response = client.projectLinks().search(new SearchRequest().setProjectKey("com.sonarsource.it.samples:simple-sample"));
if (server.version().isGreaterThanOrEquals(7, 1)) {
// SONAR-10299
Expand Down
Loading

0 comments on commit 94705dd

Please sign in to comment.