1
0
mirror of https://github.com/SAP/jenkins-library.git synced 2024-12-10 10:50:11 +02:00

Handling pipeline scripts in base class

This commit is contained in:
Marcus Holl 2017-11-14 12:57:16 +01:00
parent 2c53ade200
commit 98a45bd961
4 changed files with 55 additions and 99 deletions

View File

@ -18,7 +18,6 @@ public class MTABuildTest extends PiperTestBase {
def currentDir
def otherDir
def pipeline
def mtaBuildShEnv
@ -27,7 +26,6 @@ public class MTABuildTest extends PiperTestBase {
super._setUp()
currentDir = tmp.newFolder().toURI().getPath()[0..-2] //omit final '/'
otherDir = tmp.newFolder().toURI().getPath()[0..-2] //omit final '/'
pipeline = "${tmp.newFolder("pipeline").toURI().getPath()}pipeline"
helper.registerAllowedMethod('readYaml', [Map], {
m ->
@ -60,9 +58,7 @@ public class MTABuildTest extends PiperTestBase {
new File("${currentDir}/mta.yaml") << defaultMtaYaml()
defaultPipeline()
def script = loadScript(pipeline)
def mtarFilePath = script.execute()
def mtarFilePath = withPipeline(defaultPipeline()).execute()
assert shellCalls[0] =~ /sed -ie "s\/\\\$\{timestamp\}\/`date \+%Y%m%d%H%M%S`\/g" ".*\/mta.yaml"$/
@ -83,9 +79,7 @@ public class MTABuildTest extends PiperTestBase {
new File("${currentDir}/mta.yaml") << defaultMtaYaml()
returnMtarFilePathFromCommonPipelineEnvironmentPipeline()
def script = loadScript(pipeline)
def mtarFilePath = script.execute()
def mtarFilePath = withPipeline(returnMtarFilePathFromCommonPipelineEnvironmentPipeline()).execute()
assert shellCalls[0] =~ /sed -ie "s\/\\\$\{timestamp\}\/`date \+%Y%m%d%H%M%S`\/g" ".*\/mta.yaml"$/
@ -108,9 +102,7 @@ public class MTABuildTest extends PiperTestBase {
new File("${currentDir}/${newDirName}").mkdirs()
new File("${currentDir}/${newDirName}/mta.yaml") << defaultMtaYaml()
withSurroundingDirPipeline()
def script = loadScript(pipeline)
def mtarFilePath = script.execute(newDirName)
def mtarFilePath = withPipeline(withSurroundingDirPipeline()).execute(newDirName)
assert shellCalls[0] =~ /sed -ie "s\/\\\$\{timestamp\}\/`date \+%Y%m%d%H%M%S`\/g" ".*\/newDir\/mta.yaml"$/
@ -128,9 +120,7 @@ public class MTABuildTest extends PiperTestBase {
new File("${currentDir}/mta.yaml") << defaultMtaYaml()
defaultPipeline()
def script = loadScript(pipeline)
def mtarFilePath = script.execute()
def mtarFilePath = withPipeline(defaultPipeline()).execute()
assert shellCalls[0] =~ /sed -ie "s\/\\\$\{timestamp\}\/`date \+%Y%m%d%H%M%S`\/g" ".*\/mta.yaml"$/
@ -149,9 +139,7 @@ public class MTABuildTest extends PiperTestBase {
new File("${currentDir}/mta.yaml") << defaultMtaYaml()
mtaJarLocationAsParameterPipeline()
def script = loadScript(pipeline)
def mtarFilePath = script.execute()
def mtarFilePath = withPipeline(mtaJarLocationAsParameterPipeline()).execute()
assert shellCalls[0] =~ /sed -ie "s\/\\\$\{timestamp\}\/`date \+%Y%m%d%H%M%S`\/g" ".*\/mta.yaml"$/
@ -169,9 +157,7 @@ public class MTABuildTest extends PiperTestBase {
public void noMtaPresentTest(){
thrown.expect(FileNotFoundException)
defaultPipeline()
def script = loadScript(pipeline)
script.execute()
withPipeline(defaultPipeline()).execute()
}
@ -182,9 +168,7 @@ public class MTABuildTest extends PiperTestBase {
new File("${currentDir}/mta.yaml") << badMtaYaml()
defaultPipeline()
def script = loadScript(pipeline)
script.execute()
withPipeline(defaultPipeline()).execute()
}
@ -195,9 +179,7 @@ public class MTABuildTest extends PiperTestBase {
new File("${currentDir}/mta.yaml") << noIdMtaYaml()
defaultPipeline()
def script = loadScript(pipeline)
script.execute()
withPipeline(defaultPipeline()).execute()
}
@ -208,14 +190,12 @@ public class MTABuildTest extends PiperTestBase {
new File("${currentDir}/mta.yaml") << defaultMtaYaml()
noBuildTargetPipeline()
def script = loadScript(pipeline)
script.execute()
withPipeline(noBuildTargetPipeline()).execute()
}
private defaultPipeline(){
new File(pipeline) << '''
{ -> '''
@Library('piper-library-os')
execute(){
@ -223,11 +203,11 @@ public class MTABuildTest extends PiperTestBase {
}
return this
'''
''' }
}
private returnMtarFilePathFromCommonPipelineEnvironmentPipeline(){
new File(pipeline) << '''
{ ->'''
@Library('piper-library-os')
execute(){
@ -236,11 +216,11 @@ public class MTABuildTest extends PiperTestBase {
}
return this
'''
'''}
}
private mtaJarLocationAsParameterPipeline(){
new File(pipeline) << '''
{ -> '''
@Library('piper-library-os')
execute(){
@ -248,11 +228,11 @@ public class MTABuildTest extends PiperTestBase {
}
return this
'''
'''}
}
private withSurroundingDirPipeline(){
new File(pipeline) << '''
{ ->'''
@Library('piper-library-os')
execute(dirPath){
@ -262,12 +242,12 @@ public class MTABuildTest extends PiperTestBase {
}
return this
'''
'''}
}
private noBuildTargetPipeline(){
new File(pipeline) << '''
{ -> '''
@Library('piper-library-os')
execute(){
@ -275,7 +255,7 @@ public class MTABuildTest extends PiperTestBase {
}
return this
'''
'''}
}

View File

@ -18,9 +18,6 @@ class NeoDeploymentTest extends PiperTestBase {
@Rule
public TemporaryFolder tmp = new TemporaryFolder()
def script
def pipeline
def archivePath
@Before
@ -29,7 +26,6 @@ class NeoDeploymentTest extends PiperTestBase {
super._setUp()
archivePath = "${tmp.newFolder("workspace").toURI().getPath()}archiveName.mtar"
pipeline = "${tmp.newFolder("pipeline").toURI().getPath()}pipeline"
helper.registerAllowedMethod('error', [String], { s -> throw new AbortException(s) })
helper.registerAllowedMethod('usernamePassword', [Map], { m -> return m })
@ -58,15 +54,11 @@ class NeoDeploymentTest extends PiperTestBase {
@Test
void straightForwardTest() {
defaultPipeline()
binding.getVariable('env')['NEO_HOME'] = '/opt/neo'
new File(archivePath) << "dummy archive"
script = loadScript(pipeline)
script.execute(archivePath, 'myCredentialsId')
withPipeline(defaultPipeline()).execute(archivePath, 'myCredentialsId')
assert shellCalls[0] =~ /#!\/bin\/bash \/opt\/neo\/tools\/neo\.sh deploy-mta --user anonymous --host test\.deploy\.host\.com --source ".*" --account trialuser123 --password \*\*\*\*\*\*\*\* --synchronous/
@ -78,17 +70,13 @@ class NeoDeploymentTest extends PiperTestBase {
@Test
void badCredentialsIdTest() {
defaultPipeline()
binding.getVariable('env')['NEO_HOME'] = '/opt/neo'
new File(archivePath) << "dummy archive"
thrown.expect(MissingPropertyException)
script = loadScript(pipeline)
script.execute(archivePath, 'badCredentialsId')
withPipeline(defaultPipeline()).execute(archivePath, 'badCredentialsId')
}
@ -96,15 +84,11 @@ class NeoDeploymentTest extends PiperTestBase {
@Test
void credentialsIdNotProvidedTest() {
noCredentialsIdPipeline()
binding.getVariable('env')['NEO_HOME'] = '/opt/neo'
new File(archivePath) << "dummy archive"
script = loadScript(pipeline)
script.execute(archivePath)
withPipeline(noCredentialsIdPipeline()).execute(archivePath)
assert shellCalls[0] =~ /#!\/bin\/bash \/opt\/neo\/tools\/neo\.sh deploy-mta --user defaultUser --host test\.deploy\.host\.com --source ".*" --account trialuser123 --password \*\*\*\*\*\*\*\* --synchronous/
@ -115,13 +99,9 @@ class NeoDeploymentTest extends PiperTestBase {
@Test
void neoHomeNotSetTest() {
noCredentialsIdPipeline()
new File(archivePath) << "dummy archive"
script = loadScript(pipeline)
script.execute(archivePath)
withPipeline(noCredentialsIdPipeline()).execute(archivePath)
assert shellCalls[0] =~ /#!\/bin\/bash neo deploy-mta --user defaultUser --host test\.deploy\.host\.com --source ".*" --account trialuser123 --password \*\*\*\*\*\*\*\* --synchronous/
@ -132,13 +112,9 @@ class NeoDeploymentTest extends PiperTestBase {
@Test
void neoHomeAsParameterTest() {
neoHomeParameterPipeline()
new File(archivePath) << "dummy archive"
script = loadScript(pipeline)
script.execute(archivePath, 'myCredentialsId')
withPipeline(neoHomeParameterPipeline()).execute(archivePath, 'myCredentialsId')
assert shellCalls[0] =~ /#!\/bin\/bash \/etc\/neo\/tools\/neo\.sh deploy-mta --user anonymous --host test\.deploy\.host\.com --source ".*" --account trialuser123 --password \*\*\*\*\*\*\*\* --synchronous/
@ -150,14 +126,10 @@ class NeoDeploymentTest extends PiperTestBase {
@Test
void archiveNotProvidedTest() {
noArchivePathPipeline()
thrown.expect(Exception)
thrown.expectMessage('ERROR - NO VALUE AVAILABLE FOR archivePath')
script = loadScript(pipeline)
script.execute()
withPipeline(noArchivePathPipeline()).execute()
}
@ -165,14 +137,10 @@ class NeoDeploymentTest extends PiperTestBase {
@Test
void wrongArchivePathProvidedTest() {
defaultPipeline()
thrown.expect(AbortException)
thrown.expectMessage("Archive cannot be found with parameter archivePath: '")
script = loadScript(pipeline)
script.execute(archivePath, 'myCredentialsId')
withPipeline(defaultPipeline()).execute(archivePath, 'myCredentialsId')
}
@ -180,22 +148,18 @@ class NeoDeploymentTest extends PiperTestBase {
@Test
void scriptNotProvidedTest() {
noScriptPipeline()
new File(archivePath) << "dummy archive"
thrown.expect(Exception)
thrown.expectMessage('ERROR - NO VALUE AVAILABLE FOR deployHost')
script = loadScript(pipeline)
script.execute(archivePath)
withPipeline(noScriptPipeline()).execute(archivePath)
}
private defaultPipeline(){
new File(pipeline) << """
{ -> """
@Library('piper-library-os')
execute(archivePath, neoCredentialsId) {
@ -210,11 +174,11 @@ class NeoDeploymentTest extends PiperTestBase {
}
return this
"""
"""}
}
private noCredentialsIdPipeline(){
new File(pipeline) << """
{ ->"""
@Library('piper-library-os')
execute(archivePath) {
@ -229,11 +193,11 @@ class NeoDeploymentTest extends PiperTestBase {
}
return this
"""
""" }
}
private neoHomeParameterPipeline(){
new File(pipeline) << """
{ ->"""
@Library('piper-library-os')
execute(archivePath, neoCredentialsId) {
@ -248,11 +212,11 @@ class NeoDeploymentTest extends PiperTestBase {
}
return this
"""
""" }
}
private noArchivePathPipeline(){
new File(pipeline) << """
{ -> """
@Library('piper-library-os')
execute() {
@ -267,11 +231,11 @@ class NeoDeploymentTest extends PiperTestBase {
}
return this
"""
""" }
}
private noScriptPipeline(){
new File(pipeline) << """
{ -> """
@Library('piper-library-os')
execute(archivePath) {
@ -283,7 +247,7 @@ class NeoDeploymentTest extends PiperTestBase {
}
return this
"""
""" }
}
}

View File

@ -3,8 +3,16 @@ import com.lesfurets.jenkins.unit.BasePipelineTest
import static ProjectSource.projectSource
import static com.lesfurets.jenkins.unit.global.lib.LibraryConfiguration.library
import org.junit.Rule
import org.junit.rules.TemporaryFolder
public class PiperTestBase extends BasePipelineTest {
@Rule
public TemporaryFolder pipelineFolder = new TemporaryFolder()
private File pipeline
protected messages = [], shellCalls = []
protected final void _setUp() {
@ -21,6 +29,12 @@ public class PiperTestBase extends BasePipelineTest {
shellCalls.add(s.replaceAll(/\s+/, " ").trim())
})
pipeline = pipelineFolder.newFile()
}
protected withPipeline(p) {
pipeline << p()
loadScript(pipeline.getAbsolutePath())
}
private preparePiperLib() {

View File

@ -24,9 +24,7 @@ class ToolValidateTest extends PiperTestBase {
super._setUp()
def pipelinePath = "${tmp.newFolder("pipeline").toURI().getPath()}pipeline"
createPipeline(pipelinePath)
script = loadScript(pipelinePath)
script = withPipeline(createPipeline())
notEmptyDir = tmp.newFolder('notEmptyDir')
def path = "${notEmptyDir.getAbsolutePath()}${File.separator}test.txt"
@ -250,8 +248,8 @@ class ToolValidateTest extends PiperTestBase {
}
private createPipeline(pipelinePath){
new File(pipelinePath) << """
private createPipeline(){
{ -> """
@Library('piper-library-os')
execute() {
@ -263,7 +261,7 @@ class ToolValidateTest extends PiperTestBase {
}
return this
"""
"""}
}
private getNoVersion(Map m) {