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

View File

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

View File

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

View File

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