Skip to content

Commit

Permalink
Split integration tests
Browse files Browse the repository at this point in the history
  • Loading branch information
wolfs committed Apr 3, 2017
1 parent c4400c7 commit e509a7c
Show file tree
Hide file tree
Showing 5 changed files with 118 additions and 119 deletions.
@@ -0,0 +1,17 @@
package hudson.plugins.gradle

import org.junit.Rule
import org.junit.rules.RuleChain
import org.jvnet.hudson.test.JenkinsRule
import spock.lang.Specification

class AbstractIntegrationTest extends Specification {
final JenkinsRule j = new JenkinsRule()
final GradleInstallationRule gradleInstallationRule = new GradleInstallationRule(j)
@Rule

This comment has been minimized.

Copy link
@darxriggs

darxriggs Dec 29, 2018

Contributor

Hi @wolfs, I was wondering why tests are taking so long to run (locally about 30min).
Then I found out that some tests are data-driven, testing many data points (e.g. GradlePluginIntegrationTest).
For each single test a new Jenkins instance is started (due to this @Rule) and for all the tests that are really using Gradle inside a Jenkins job, a complete Gradle installation is downloaded. I guess overall that's many hundred MBs if not GB. So there is room for improvement here.

public final RuleChain rules = RuleChain.outerRule(j).around(gradleInstallationRule)

Map getDefaults() {
[gradleName: gradleInstallationRule.gradleVersion, useWorkspaceAsHome: true, switches: '--no-daemon']
}
}
@@ -0,0 +1,32 @@
package hudson.plugins.gradle

import hudson.model.FreeStyleProject
import org.jvnet.hudson.test.CreateFileBuilder

class BuildScanIntegrationTest extends AbstractIntegrationTest {
def 'build scan is discovered'() {
given:
gradleInstallationRule.addInstallation()
FreeStyleProject p = j.createFreeStyleProject()
p.buildersList.add(new CreateFileBuilder("build.gradle", """
plugins {
id 'com.gradle.build-scan' version '1.6'
}
buildScan {
licenseAgreementUrl = 'https://gradle.com/terms-of-service'
licenseAgree = 'yes'
}
task hello << { println 'Hello' }"""))
p.buildersList.add(new Gradle(tasks: 'hello', *: defaults, switches: '-Dscan --no-daemon'))

when:
def build = j.buildAndAssertSuccess(p)

then:
def action = build.getAction(BuildScanAction)
action.scanUrl.contains('gradle.com')
new URL(action.scanUrl)
}
}
67 changes: 67 additions & 0 deletions src/test/groovy/hudson/plugins/gradle/CliIntegrationTest.groovy
@@ -0,0 +1,67 @@
package hudson.plugins.gradle

import hudson.cli.CLICommandInvoker
import net.sf.json.JSON
import net.sf.json.JSONArray
import net.sf.json.JSONObject

class CliIntegrationTest extends AbstractIntegrationTest {
def 'list installations through CLI'() {
when:
CLICommandInvoker.Result result = new CLICommandInvoker(j, "get-gradle").invoke()

then:
assertCLIResult(result, '{}')

when:
gradleInstallationRule.addInstallations("inst1")
result = new CLICommandInvoker(j, "get-gradle").invoke()

then:
assertCLIResult(result, expectedOutputForVersion('{"inst1":["%s"]}'))

when:
gradleInstallationRule.addInstallations("inst1", "inst2")
result = new CLICommandInvoker(j, "get-gradle").invoke()

then:
assertCLIResult(result, expectedOutputForVersion('{"inst1":["%s"],"inst2":["%s"]}'))

when:
result = new CLICommandInvoker(j, "get-gradle").invokeWithArgs("--name=inst1")

then:
assertCLIResult(result, expectedOutputForVersion('["%s"]'))

when:
result = new CLICommandInvoker(j, "get-gradle").invokeWithArgs("--name=unknown")

then:
assertCLIError(result, 'Requested gradle installation not found: unknown')
}

private static void assertCLIResult(CLICommandInvoker.Result result, String expectedOutput) {
assert result.returnCode() == 0

JSON expectedJson, resultJson

if (expectedOutput.startsWith("[")) {
expectedJson = JSONArray.fromObject(expectedOutput)
resultJson = JSONArray.fromObject(result.stdout().trim())
} else {
expectedJson = JSONObject.fromObject(expectedOutput)
resultJson = JSONObject.fromObject(result.stdout().trim())
}

assert resultJson == expectedJson
}

private static void assertCLIError(CLICommandInvoker.Result result, String expectedOutput) {
assert result.returnCode() == 1
assert result.stderr().trim() == expectedOutput
}

private String expectedOutputForVersion(String output) {
return String.format(output, gradleInstallationRule.gradleVersion, gradleInstallationRule.gradleVersion)
}
}
Expand Up @@ -29,35 +29,21 @@ import com.gargoylesoftware.htmlunit.html.HtmlForm
import com.gargoylesoftware.htmlunit.html.HtmlPage
import com.google.common.base.Joiner
import hudson.EnvVars
import hudson.cli.CLICommandInvoker
import hudson.model.FreeStyleBuild
import hudson.model.FreeStyleProject
import hudson.model.Result
import hudson.tools.InstallSourceProperty
import hudson.util.VersionNumber
import net.sf.json.JSON
import net.sf.json.JSONArray
import net.sf.json.JSONObject
import org.junit.Rule
import org.junit.rules.RuleChain
import org.jvnet.hudson.test.CreateFileBuilder
import org.jvnet.hudson.test.JenkinsRule
import org.jvnet.hudson.test.JenkinsRule.WebClient
import spock.lang.Specification
import spock.lang.Unroll

import static org.jvnet.hudson.test.JenkinsRule.getLog

/**
* Tests for the Gradle build step.
*/
@Unroll
class GradlePluginIntegrationTest extends Specification {
private final JenkinsRule j = new JenkinsRule()
private final GradleInstallationRule gradleInstallationRule = new GradleInstallationRule(j)
@Rule
public final RuleChain rules = RuleChain.outerRule(j).around(gradleInstallationRule)

class GradlePluginIntegrationTest extends AbstractIntegrationTest {
def 'run the default tasks'() {
given:
gradleInstallationRule.addInstallation()
Expand Down Expand Up @@ -100,32 +86,6 @@ class GradlePluginIntegrationTest extends Specification {
getLog(build).contains "Hello"
}

def 'build scan is discovered'() {
given:
gradleInstallationRule.addInstallation()
FreeStyleProject p = j.createFreeStyleProject()
p.buildersList.add(new CreateFileBuilder("build.gradle", """
plugins {
id 'com.gradle.build-scan' version '1.6'
}
buildScan {
licenseAgreementUrl = 'https://gradle.com/terms-of-service'
licenseAgree = 'yes'
}
task hello << { println 'Hello' }"""))
p.buildersList.add(new Gradle(tasks: 'hello', *: defaults, switches: '-Dscan --no-daemon'))

when:
def build = j.buildAndAssertSuccess(p)

then:
def action = build.getAction(BuildScanAction)
action.scanUrl.contains('gradle.com')
new URL(action.scanUrl)
}

def 'wrapper in base dir'() {
given:
gradleInstallationRule.addInstallation()
Expand Down Expand Up @@ -248,44 +208,6 @@ task hello << { println 'Hello' }"""))
installationConfigured()
}

def 'list installations through CLI'() {
when:
CLICommandInvoker.Result result = new CLICommandInvoker(j, "get-gradle").invoke()

then:
assertCLIResult(result, '{}')

when:
gradleInstallationRule.addInstallations("inst1")
result = new CLICommandInvoker(j, "get-gradle").invoke()

then:
assertCLIResult(result, expectedOutputForVersion('{"inst1":["%s"]}'))

when:
gradleInstallationRule.addInstallations("inst1", "inst2")
result = new CLICommandInvoker(j, "get-gradle").invoke()

then:
assertCLIResult(result, expectedOutputForVersion('{"inst1":["%s"],"inst2":["%s"]}'))

when:
result = new CLICommandInvoker(j, "get-gradle").invokeWithArgs("--name=inst1")

then:
assertCLIResult(result, expectedOutputForVersion('["%s"]'))

when:
result = new CLICommandInvoker(j, "get-gradle").invokeWithArgs("--name=unknown")

then:
assertCLIError(result, 'Requested gradle installation not found: unknown')
}

Map getDefaults() {
[gradleName: gradleInstallationRule.gradleVersion, useWorkspaceAsHome: true, switches: '--no-daemon']
}

private void installationConfigured() {
GradleInstallation[] installations = j.get(Gradle.DescriptorImpl).getInstallations()
assert installations.size() == 1
Expand All @@ -305,30 +227,4 @@ task hello << { println 'Hello' }"""))
assert installers.size() == 1
assert installers.get(GradleInstaller)
}

private void assertCLIResult(hudson.cli.CLICommandInvoker.Result result, String expectedOutput) {
assert result.returnCode() == 0

JSON expectedJson, resultJson

if (expectedOutput.startsWith("[")) {
expectedJson = JSONArray.fromObject(expectedOutput)
resultJson = JSONArray.fromObject(result.stdout().trim())
} else {
expectedJson = JSONObject.fromObject(expectedOutput)
resultJson = JSONObject.fromObject(result.stdout().trim())
}

assert resultJson == expectedJson
}

private void assertCLIError(hudson.cli.CLICommandInvoker.Result result, String expectedOutput) {
assert result.returnCode() == 1
assert result.stderr().trim() == expectedOutput
}

private String expectedOutputForVersion(String output) {
return String.format(output, gradleInstallationRule.gradleVersion, gradleInstallationRule.gradleVersion)
}

}
Expand Up @@ -9,22 +9,13 @@ import hudson.model.TextParameterDefinition
import hudson.model.TextParameterValue
import hudson.model.queue.QueueTaskFuture
import hudson.remoting.Launcher
import org.junit.Rule
import org.junit.rules.RuleChain
import org.jvnet.hudson.test.CreateFileBuilder
import org.jvnet.hudson.test.JenkinsRule
import spock.lang.Specification
import spock.lang.Unroll

import static org.jvnet.hudson.test.JenkinsRule.getLog

@Unroll
class PropertyPassingIntegrationTest extends Specification {
private final JenkinsRule j = new JenkinsRule()
private final GradleInstallationRule gradleInstallationRule = new GradleInstallationRule(j)
@Rule
public final RuleChain rules = RuleChain.outerRule(j).around(gradleInstallationRule)

class PropertyPassingIntegrationTest extends AbstractIntegrationTest {
def "pass '#escapedPropertyValue' via parameter in system properties"() {
given:
gradleInstallationRule.addInstallation()
Expand Down Expand Up @@ -137,10 +128,6 @@ class PropertyPassingIntegrationTest extends Specification {
]
}

Map getDefaults() {
[gradleName: gradleInstallationRule.gradleVersion, useWorkspaceAsHome: false, switches: '--no-daemon']
}

private static boolean createBuildScript(FreeStyleProject p, String buildScript) {
p.buildersList.add(new CreateFileBuilder("build.gradle", buildScript))
}
Expand Down

0 comments on commit e509a7c

Please sign in to comment.