2017-11-24 14:59:40 +02:00
|
|
|
import hudson.AbortException
|
2018-01-16 16:42:11 +02:00
|
|
|
import util.JenkinsConfigRule
|
2018-01-16 10:33:13 +02:00
|
|
|
import util.JenkinsSetupRule
|
|
|
|
|
2017-11-24 14:59:40 +02:00
|
|
|
import org.junit.rules.TemporaryFolder
|
|
|
|
import org.junit.Before
|
|
|
|
import org.junit.Rule
|
|
|
|
import org.junit.Test
|
|
|
|
import org.junit.rules.ExpectedException
|
2018-01-16 10:33:13 +02:00
|
|
|
import org.junit.rules.RuleChain
|
2017-11-24 14:59:40 +02:00
|
|
|
|
2017-12-01 11:51:11 +02:00
|
|
|
class PipelineExecuteTest extends PiperTestBase {
|
2017-11-24 14:59:40 +02:00
|
|
|
|
2018-01-16 10:33:13 +02:00
|
|
|
private ExpectedException thrown = new ExpectedException().none()
|
|
|
|
|
2017-11-24 14:59:40 +02:00
|
|
|
@Rule
|
2018-01-16 10:33:13 +02:00
|
|
|
public RuleChain ruleChain = RuleChain.outerRule(thrown)
|
|
|
|
.around(new JenkinsSetupRule(this))
|
2018-01-16 16:42:11 +02:00
|
|
|
.around(new JenkinsConfigRule(this))
|
2017-11-24 14:59:40 +02:00
|
|
|
|
|
|
|
def pipelinePath
|
|
|
|
def checkoutParameters = [:]
|
|
|
|
def load
|
|
|
|
|
|
|
|
@Before
|
2018-01-16 10:33:13 +02:00
|
|
|
void init() {
|
2017-11-24 14:59:40 +02:00
|
|
|
|
|
|
|
pipelinePath = null
|
|
|
|
checkoutParameters.clear()
|
|
|
|
load = null
|
|
|
|
|
|
|
|
helper.registerAllowedMethod('deleteDir', [], null)
|
|
|
|
helper.registerAllowedMethod('checkout', [Map], { m ->
|
|
|
|
checkoutParameters.branch = m.branches[0].name
|
|
|
|
checkoutParameters.repoUrl = m.userRemoteConfigs[0].url
|
|
|
|
checkoutParameters.credentialsId = m.userRemoteConfigs[0].credentialsId
|
|
|
|
checkoutParameters.path = m.extensions[0].sparseCheckoutPaths[0].path
|
|
|
|
})
|
|
|
|
helper.registerAllowedMethod('load', [String], { s -> load = s })
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
@Test
|
|
|
|
void straightForwardTest() {
|
|
|
|
|
|
|
|
withPipeline(defaultPipeline()).execute()
|
2017-11-28 10:34:54 +02:00
|
|
|
assert load == "Jenkinsfile"
|
2017-11-24 14:59:40 +02:00
|
|
|
assert checkoutParameters.branch == 'master'
|
|
|
|
assert checkoutParameters.repoUrl == "https://test.com/myRepo.git"
|
|
|
|
assert checkoutParameters.credentialsId == ''
|
|
|
|
assert checkoutParameters.path == 'Jenkinsfile'
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
@Test
|
|
|
|
void parameterizeTest() {
|
|
|
|
|
|
|
|
withPipeline(parameterizePipeline()).execute()
|
2017-11-28 10:34:54 +02:00
|
|
|
assert load == "path/to/Jenkinsfile"
|
2017-11-24 14:59:40 +02:00
|
|
|
assert checkoutParameters.branch == 'feature'
|
|
|
|
assert checkoutParameters.repoUrl == "https://test.com/anotherRepo.git"
|
|
|
|
assert checkoutParameters.credentialsId == 'abcd1234'
|
|
|
|
assert checkoutParameters.path == 'path/to/Jenkinsfile'
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
@Test
|
|
|
|
void noRepoUrlTest() {
|
|
|
|
|
|
|
|
thrown.expect(Exception)
|
|
|
|
thrown.expectMessage("ERROR - NO VALUE AVAILABLE FOR repoUrl")
|
|
|
|
|
|
|
|
withPipeline(noRepoUrlPipeline()).execute()
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
private defaultPipeline() {
|
|
|
|
return """
|
|
|
|
@Library('piper-library-os')
|
|
|
|
|
|
|
|
execute() {
|
|
|
|
|
2017-12-01 11:51:11 +02:00
|
|
|
pipelineExecute repoUrl: "https://test.com/myRepo.git"
|
2017-11-24 14:59:40 +02:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
return this
|
|
|
|
"""
|
|
|
|
}
|
|
|
|
|
|
|
|
private parameterizePipeline() {
|
|
|
|
return """
|
|
|
|
@Library('piper-library-os')
|
|
|
|
|
|
|
|
execute() {
|
|
|
|
|
2017-12-01 11:51:11 +02:00
|
|
|
pipelineExecute repoUrl: "https://test.com/anotherRepo.git", branch: 'feature', path: 'path/to/Jenkinsfile', credentialsId: 'abcd1234'
|
2017-11-24 14:59:40 +02:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
return this
|
|
|
|
"""
|
|
|
|
}
|
|
|
|
|
|
|
|
private noRepoUrlPipeline() {
|
|
|
|
return """
|
|
|
|
@Library('piper-library-os')
|
|
|
|
|
|
|
|
execute() {
|
|
|
|
|
2017-12-01 11:51:11 +02:00
|
|
|
pipelineExecute()
|
2017-11-24 14:59:40 +02:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
return this
|
|
|
|
"""
|
|
|
|
}
|
|
|
|
}
|