mirror of
https://github.com/SAP/jenkins-library.git
synced 2024-12-14 11:03:09 +02:00
a6df3b3053
Mocking with LesFurets. Check that everything is at the right place. 1. Test with only the mandatory parameter 'repoUrl'. 1. Test setting all available parameters. 1. Test with no parameters throws Exception for missing mandatory parameter 'repoUrl'.
126 lines
3.2 KiB
Groovy
126 lines
3.2 KiB
Groovy
import hudson.AbortException
|
|
import org.junit.rules.TemporaryFolder
|
|
import org.junit.Before
|
|
import org.junit.Rule
|
|
import org.junit.Test
|
|
import org.junit.rules.ExpectedException
|
|
|
|
class CentralPipelineLoadTest extends PiperTestBase {
|
|
|
|
@Rule
|
|
public ExpectedException thrown = new ExpectedException().none()
|
|
|
|
def pipelinePath
|
|
def checkoutParameters = [:]
|
|
def load
|
|
|
|
@Before
|
|
void setUp() {
|
|
|
|
super.setUp()
|
|
|
|
pipelinePath = null
|
|
checkoutParameters.clear()
|
|
load = null
|
|
|
|
helper.registerAllowedMethod('deleteDir', [], null)
|
|
helper.registerAllowedMethod("dir", [String, Closure], {
|
|
dirName, c ->
|
|
pipelinePath = dirName
|
|
c()
|
|
})
|
|
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()
|
|
assert load == "${pipelinePath}/Jenkinsfile"
|
|
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()
|
|
assert load == "${pipelinePath}/path/to/Jenkinsfile"
|
|
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() {
|
|
|
|
node() {
|
|
centralPipelineLoad repoUrl: "https://test.com/myRepo.git"
|
|
}
|
|
|
|
}
|
|
|
|
return this
|
|
"""
|
|
}
|
|
|
|
private parameterizePipeline() {
|
|
return """
|
|
@Library('piper-library-os')
|
|
|
|
execute() {
|
|
|
|
node() {
|
|
centralPipelineLoad repoUrl: "https://test.com/anotherRepo.git", branch: 'feature', jenkinsfilePath: 'path/to/Jenkinsfile', credentialsId: 'abcd1234'
|
|
}
|
|
|
|
}
|
|
|
|
return this
|
|
"""
|
|
}
|
|
|
|
private noRepoUrlPipeline() {
|
|
return """
|
|
@Library('piper-library-os')
|
|
|
|
execute() {
|
|
|
|
node() {
|
|
centralPipelineLoad()
|
|
}
|
|
|
|
}
|
|
|
|
return this
|
|
"""
|
|
}
|
|
}
|