Skip to content

Commit

Permalink
SONARJNKNS-250 Properly save SQ Scanner selection on jobs
Browse files Browse the repository at this point in the history
  • Loading branch information
henryju committed May 25, 2016
1 parent a06ebb0 commit 04ac3f2
Show file tree
Hide file tree
Showing 8 changed files with 129 additions and 90 deletions.
12 changes: 10 additions & 2 deletions its/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -32,12 +32,12 @@
<dependency>
<groupId>org.sonarsource.orchestrator</groupId>
<artifactId>sonar-orchestrator</artifactId>
<version>3.10.1</version>
<version>3.11-build502</version>
</dependency>
<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-server</artifactId>
<version>2.45.0</version>
<version>2.53.0</version>
</dependency>

<dependency>
Expand All @@ -59,6 +59,14 @@
<version>${jenkins.runtimeVersion}</version>
<type>war</type>
</dependency>

<!-- Install SQ Scanner 2.6.1 in Maven repo so that it is found by Orchestrator -->
<dependency>
<groupId>org.sonarsource.scanner.cli</groupId>
<artifactId>sonar-scanner-cli</artifactId>
<version>2.6.1</version>
<type>zip</type>
</dependency>
</dependencies>

<build>
Expand Down
12 changes: 6 additions & 6 deletions its/src/test/java/com/sonar/it/jenkins/JenkinsTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,17 +26,16 @@
import com.sonar.orchestrator.locator.FileLocation;
import com.sonar.orchestrator.locator.Location;
import com.sonar.orchestrator.locator.URLLocation;

import java.io.File;
import java.net.MalformedURLException;
import java.net.URL;

import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.ClassRule;
import org.junit.Test;
import org.sonar.wsclient.services.PropertyUpdateQuery;
import org.sonar.wsclient.services.ResourceQuery;

import static org.fest.assertions.Assertions.assertThat;
import static org.junit.Assume.assumeFalse;

Expand All @@ -62,7 +61,8 @@ public static void setUpJenkins() throws MalformedURLException {
.installPlugin(URLLocation.create(new URL("http://mirrors.jenkins-ci.org/plugins/filesystem_scm/1.20/filesystem_scm.hpi")))
.installPlugin(sqJenkinsPluginLocation)
.configureMavenInstallation()
.configureSonarRunner2_4Installation()
// Single installation
.configureSQScannerInstallation("2.4", 0)
.configureMsBuildSQScanner_installation()
.configureSonarInstallation(orchestrator);
jenkins.checkSavedSonarInstallation(orchestrator);
Expand Down Expand Up @@ -149,7 +149,7 @@ public void testFreestyleJobWithSonarRunner() throws Exception {
String projectKey = "abacus-runner";
assertThat(orchestrator.getServer().getWsClient().find(ResourceQuery.create(projectKey))).isNull();
jenkins
.newFreestyleJobWithSonarRunner(jobName, "-X", new File("projects", "abacus"),
.newFreestyleJobWithSQScanner(jobName, "-X", new File("projects", "abacus"), null,
"sonar.projectKey", projectKey,
"sonar.projectVersion", "1.0",
"sonar.projectName", "Abacus",
Expand Down Expand Up @@ -186,7 +186,7 @@ public void testFreestyleJobWithSonarRunnerAndBranch() throws Exception {
String projectKey = "abacus-runner:branch";
assertThat(orchestrator.getServer().getWsClient().find(ResourceQuery.create(projectKey))).isNull();
BuildResult result = jenkins
.newFreestyleJobWithSonarRunner(jobName, "-X -Duseless=Y -e", new File("projects", "abacus"),
.newFreestyleJobWithSQScanner(jobName, "-X -Duseless=Y -e", new File("projects", "abacus"), null,
"sonar.projectKey", "abacus-runner",
"sonar.projectVersion", "1.0",
"sonar.projectName", "Abacus",
Expand All @@ -211,7 +211,7 @@ public void testFreestyleJobWithTask() throws Exception {
assumeFalse(orchestrator.getServer().version().isGreaterThanOrEquals("5.2"));
String jobName = "refresh-views";
BuildResult result = jenkins
.newFreestyleJobWithSonarRunner(jobName, null, new File("projects", "abacus"), "sonar.task", "views")
.newFreestyleJobWithSQScanner(jobName, null, new File("projects", "abacus"), null, "sonar.task", "views")
.executeJobQuietly(jobName);
// Since views is not installed
assertThat(result.getLogs()).contains("Task views does not exist");
Expand Down
49 changes: 32 additions & 17 deletions its/src/test/java/com/sonar/it/jenkins/JenkinsWithoutMaven.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,22 +21,22 @@

import com.sonar.it.jenkins.orchestrator.JenkinsOrchestrator;
import com.sonar.orchestrator.Orchestrator;
import com.sonar.orchestrator.build.BuildResult;
import com.sonar.orchestrator.build.SynchronousAnalyzer;
import com.sonar.orchestrator.locator.FileLocation;
import com.sonar.orchestrator.locator.Location;
import com.sonar.orchestrator.locator.URLLocation;
import java.io.File;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.ClassRule;
import org.junit.Test;
import org.sonar.wsclient.services.PropertyUpdateQuery;
import org.sonar.wsclient.services.ResourceQuery;

import java.io.File;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;

import static org.fest.assertions.Assertions.assertThat;

public class JenkinsWithoutMaven {
Expand All @@ -62,7 +62,8 @@ public static void setUpJenkins() throws IOException {
.installPlugin(URLLocation.create(new URL("http://mirrors.jenkins-ci.org/plugins/filesystem_scm/1.20/filesystem_scm.hpi")))
.installPlugin(sqJenkinsPluginLocation)
.configureMavenInstallation()
.configureSonarRunner2_4Installation()
.configureSQScannerInstallation("2.4", 0)
.configureSQScannerInstallation("2.6.1", 1)
.configureMsBuildSQScanner_installation()
.configureSonarInstallation(orchestrator);
jenkins.checkSavedSonarInstallation(orchestrator);
Expand All @@ -75,29 +76,43 @@ public void resetData() throws Exception {
}

@Test
public void testFreestyleJobWithSonarRunner() throws Exception {
String jobName = "abacus-runner";
String projectKey = "abacus-runner";
public void testFreestyleJobWithSonarRunner_use_sq_scanner_2_4() throws Exception {
String jobName = "abacus-runner-sq-2.4";
String projectKey = "abacus-runner-2.4";
assertThat(orchestrator.getServer().getWsClient().find(ResourceQuery.create(projectKey))).isNull();
jenkins
.newFreestyleJobWithSonarRunner(jobName, "-X", new File("projects", "abacus"),
BuildResult result = jenkins
.newFreestyleJobWithSQScanner(jobName, "-v", new File("projects", "abacus"), "2.4",
"sonar.projectKey", projectKey,
"sonar.projectVersion", "1.0",
"sonar.projectName", "Abacus",
"sonar.sources", "src/main/java")
.executeJob(jobName);
waitForComputationOnSQServer();
assertThat(orchestrator.getServer().getWsClient().find(ResourceQuery.create(projectKey))).isNotNull();
assertSonarUrlOnJob(jobName, projectKey);
jenkins.assertQGOnProjectPage(jobName);

assertThat(result.getLogs()).contains("SonarQube Runner 2.4");
}


@Test
public void testFreestyleJobWithSonarRunner_use_sq_scanner_2_6_1() throws Exception {
String jobName = "abacus-runner-sq-2.6.1";
String projectKey = "abacus-runner-2.6.1";
assertThat(orchestrator.getServer().getWsClient().find(ResourceQuery.create(projectKey))).isNull();
BuildResult result = jenkins
.newFreestyleJobWithSQScanner(jobName, "-v", new File("projects", "abacus"), "2.6.1",
"sonar.projectKey", projectKey,
"sonar.projectVersion", "1.0",
"sonar.projectName", "Abacus",
"sonar.sources", "src/main/java")
.executeJob(jobName);

assertThat(result.getLogs()).contains("SonarQube Scanner 2.6.1");
}

@Test
public void testNoSonarPublisher() {
String jobName = "no Sonar Publisher";
jenkins.assertNoSonarPublisher(jobName, new File("projects", "noPublisher"));
}

private void assertSonarUrlOnJob(String jobName, String projectKey) {
assertThat(jenkins.getSonarUrlOnJob(jobName)).startsWith(orchestrator.getServer().getUrl());
assertThat(jenkins.getSonarUrlOnJob(jobName)).endsWith(projectKey);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,6 @@
*/
package com.sonar.it.jenkins.orchestrator;

import javax.annotation.Nullable;

import com.google.common.base.Function;
import com.sonar.it.jenkins.orchestrator.container.JenkinsDistribution;
import com.sonar.it.jenkins.orchestrator.container.JenkinsServer;
Expand All @@ -37,8 +35,6 @@
import com.sonar.orchestrator.util.NetworkUtils;
import com.sonar.orchestrator.version.Version;
import hudson.cli.CLI;
import static org.fest.assertions.Assertions.assertThat;

import java.io.File;
import java.io.IOException;
import java.net.URL;
Expand All @@ -47,7 +43,7 @@
import java.util.Map;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicBoolean;

import javax.annotation.Nullable;
import org.apache.commons.io.FileUtils;
import org.apache.commons.io.output.WriterOutputStream;
import org.apache.commons.lang.StringUtils;
Expand All @@ -62,7 +58,6 @@
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.firefox.FirefoxProfile;
import org.openqa.selenium.internal.Locatable;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.Select;
import org.openqa.selenium.support.ui.WebDriverWait;
Expand All @@ -71,6 +66,8 @@
import org.sonar.wsclient.jsonsimple.JSONValue;
import org.sonar.wsclient.qualitygate.QualityGates;

import static org.fest.assertions.Assertions.assertThat;

public class JenkinsOrchestrator extends SingleStartExternalResource {
private static final Logger LOG = LoggerFactory.getLogger(JenkinsOrchestrator.class);

Expand Down Expand Up @@ -247,7 +244,8 @@ public JenkinsOrchestrator newFreestyleJobWithMaven(String jobName, File project
return this;
}

public JenkinsOrchestrator newFreestyleJobWithSonarRunner(String jobName, @Nullable String additionalArgs, File projectPath, String... properties) {
public JenkinsOrchestrator newFreestyleJobWithSQScanner(String jobName, @Nullable String additionalArgs, File projectPath, @Nullable String sqScannerVersion,
String... properties) {
newFreestyleJobConfig(jobName, projectPath);

findElement(buttonByText("Add build step")).click();
Expand All @@ -264,6 +262,10 @@ public JenkinsOrchestrator newFreestyleJobWithSonarRunner(String jobName, @Nulla
}
setTextValue(findElement(By.name("_.properties")), builder.toString());

if (sqScannerVersion != null) {
select(findElement(By.name("sonar.sonarScannerName")), getSQScannerInstallName(sqScannerVersion));
}

if (additionalArgs != null) {
setTextValue(findElement(By.name("_.additionalArguments")), additionalArgs);
}
Expand Down Expand Up @@ -296,7 +298,6 @@ public JenkinsOrchestrator newFreestyleJobWithMsBuildSQRunner(String jobName, @N

private void activateSonarPostBuildMaven(String branch) {
WebElement addPostBuildButton = findElement(buttonByText("Add post-build action"));
scrollToElement(addPostBuildButton);
addPostBuildButton.click();
findElement(By.linkText("SonarQube analysis with Maven")).click();
// Here we need to wait for the Sonar step to be really activated
Expand All @@ -319,7 +320,6 @@ public JenkinsOrchestrator configureMavenInstallation() {
driver.get(server.getUrl() + "/configure");

WebElement addMavenButton = findElement(buttonByText("Add Maven"));
scrollToElement(addMavenButton);
addMavenButton.click();
setTextValue(findElement(By.name("_.name")), "Maven");
findElement(By.name("hudson-tools-InstallSourceProperty")).click();
Expand All @@ -330,49 +330,54 @@ public JenkinsOrchestrator configureMavenInstallation() {
}

/**
* Scroll to element plus some additional scroll to make element visible even with Jenkins bottom floating bar
* Scroll so that element is centered to make element visible even with Jenkins bottom/top floating bars
*/
public void scrollToElement(WebElement e) {
Locatable element = (Locatable) e;
Point p = element.getCoordinates().inViewPort();
public WebElement scrollTo(WebElement e) {
JavascriptExecutor js = (JavascriptExecutor) driver;
js.executeScript("window.scrollTo(" + p.getX() + "," + (p.getY() + 250) + ");");
js.executeScript(
"const element = arguments[0]; const elementRect = element.getBoundingClientRect(); const absoluteElementTop = elementRect.top + window.pageYOffset; const top = absoluteElementTop - (window.innerHeight / 2); window.scrollTo(0, top);",
e);
// Give the time for the floating bar to move at the bottom
try {
Thread.sleep(1000);
} catch (InterruptedException e1) {
e1.printStackTrace();
}
return e;
}

public JenkinsOrchestrator configureSonarRunner2_4Installation() {
public JenkinsOrchestrator configureSQScannerInstallation(String version, int index) {
driver.get(server.getUrl() + "/configure");
WebDriverWait wait = new WebDriverWait(driver, 5);
wait.until(ExpectedConditions.textToBePresentInElement(By.id("footer"), "Page generated"));

SonarScannerInstaller installer = new SonarScannerInstaller(config.fileSystem());
File runnerScript = installer.install(Version.create("2.4"), config.fileSystem().workspace());
File runnerScript = installer.install(Version.create(version), config.fileSystem().workspace(), true);

WebElement addSonarRunnerButton = findElement(buttonByText("Add SonarQube Scanner"));
scrollToElement(addSonarRunnerButton);
addSonarRunnerButton.click();
setTextValue(findElement(By.name("_.name")), "Sonar Scanner");
findElement(By.name("hudson-tools-InstallSourceProperty")).click();
WebElement homeDir = findElement(By.name("_.home"));
if (index > 0) {
findElement(buttonByText("SonarQube Scanner installations...")).click();
}

findElement(buttonByText("Add SonarQube Scanner")).click();
setTextValue(findElement(By.name("_.name"), index), getSQScannerInstallName(version));
findElement(By.name("hudson-tools-InstallSourceProperty"), index).click();
WebElement homeDir = findElement(By.name("_.home"), index);
setTextValue(homeDir, runnerScript.getParentFile().getParentFile().getAbsolutePath());
findElement(buttonByText("Save")).click();

return this;
}

private String getSQScannerInstallName(String version) {
return "SonarQube Scanner " + version;
}

public JenkinsOrchestrator configureMsBuildSQScanner_installation() {
driver.get(server.getUrl() + "/configure");
WebDriverWait wait = new WebDriverWait(driver, 5);
wait.until(ExpectedConditions.textToBePresentInElement(By.id("footer"), "Page generated"));

WebElement addMSBuildSQRunnerButton = findElement(buttonByText("Add SonarQube Scanner for MSBuild"));
scrollToElement(addMSBuildSQRunnerButton);
addMSBuildSQRunnerButton.click();
findElement(buttonByText("Add SonarQube Scanner for MSBuild")).click();
setTextValue(findElement(By.name("_.name")), "SQ runner");
findElement(buttonByText("Save")).click();

Expand Down Expand Up @@ -556,7 +561,7 @@ public WebDriver getDriver() {

public WebElement findElement(By by) {
try {
return driver.findElement(by);
return scrollTo(driver.findElement(by));
} catch (NoSuchElementException e) {
System.err.println("Element not found. Save screenshot to: target/no_such_element.png");
takeScreenshot(new File("target/no_such_element.png"));
Expand All @@ -575,7 +580,7 @@ public void assertNoElement(By by) {
public WebElement findElement(By by, int index) {
try {
List<WebElement> elms = driver.findElements(by);
return elms.get(index);
return scrollTo(elms.get(index));
} catch (NoSuchElementException e) {
System.err.println("Element not found. Save screenshot to: target/no_such_element.png");
takeScreenshot(new File("target/no_such_element.png"));
Expand Down Expand Up @@ -612,7 +617,7 @@ public void assertNoSonarPublisher(String jobName, File projectPath) {
newFreestyleJobConfig(jobName, projectPath);

WebElement addPostBuildButton = findElement(buttonByText("Add post-build action"));
scrollToElement(addPostBuildButton);
scrollTo(addPostBuildButton);
addPostBuildButton.click();
findElement(By.linkText("SonarQube analysis with Maven")).click();
assertNoElement(By.xpath("//div[@descriptorid='hudson.plugins.sonar.SonarPublisher']"));
Expand Down
Loading

0 comments on commit 04ac3f2

Please sign in to comment.