1
0
mirror of https://github.com/SAP/jenkins-library.git synced 2025-02-21 19:48:53 +02:00

Merge pull request #5 from marcusholl/pr/piperTestBase

piper test base
This commit is contained in:
Marcus Holl 2017-11-21 16:42:22 +01:00 committed by GitHub
commit a72cd278b4
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 215 additions and 281 deletions

View File

@ -23,7 +23,7 @@ class FileUtilsTest {
@Before
void setup() {
void setUp() {
emptyDir = tmp.newFolder('emptyDir')
notEmptyDir = tmp.newFolder('notEmptyDir')

View File

@ -5,15 +5,10 @@ import org.junit.Before
import org.junit.Rule
import org.junit.Test
import com.lesfurets.jenkins.unit.BasePipelineTest
import org.junit.rules.ExpectedException
import org.junit.rules.TemporaryFolder
import static com.lesfurets.jenkins.unit.global.lib.LibraryConfiguration.library
import static ProjectSource.projectSource
public class MTABuildTest extends BasePipelineTest {
public class MTABuildTest extends PiperTestBase {
@Rule
public ExpectedException thrown = new ExpectedException()
@ -21,34 +16,18 @@ public class MTABuildTest extends BasePipelineTest {
@Rule
public TemporaryFolder tmp = new TemporaryFolder()
def shellCalls = []
def echoes = []
def currentDir
def otherDir
def pipeline
def mtaBuildShEnv
@Before
public void setup(){
super.setUp()
void setUp() {
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"
def piperLib = library()
.name('piper-library-os')
.retriever(projectSource())
.targetPath('clonePath/is/not/necessary')
.defaultVersion('<irrelevant>')
.allowOverride(true)
.implicit(false)
.build()
helper.registerSharedLibrary(piperLib)
helper.registerAllowedMethod('sh', [String], {s -> shellCalls.add(s)} )
helper.registerAllowedMethod('echo', [String], {s -> echoes.add(s)} )
helper.registerAllowedMethod('readYaml', [Map], {
m ->
return new Yaml().load((m.file as File).text)
@ -80,9 +59,7 @@ public class MTABuildTest extends BasePipelineTest {
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"$/
@ -92,7 +69,7 @@ public class MTABuildTest extends BasePipelineTest {
assert mtarFilePath == "${currentDir}/com.mycompany.northwind.mtar"
assert echoes[1] == "[mtaBuild] MTA JAR \"/opt/mta/mta.jar\" retrieved from environment."
assert messages[1] == "[mtaBuild] MTA JAR \"/opt/mta/mta.jar\" retrieved from environment."
}
@ -103,9 +80,7 @@ public class MTABuildTest extends BasePipelineTest {
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"$/
@ -115,7 +90,7 @@ public class MTABuildTest extends BasePipelineTest {
assert mtarFilePath == "${currentDir}/com.mycompany.northwind.mtar"
assert echoes[1] == "[mtaBuild] MTA JAR \"/opt/mta/mta.jar\" retrieved from environment."
assert messages[1] == "[mtaBuild] MTA JAR \"/opt/mta/mta.jar\" retrieved from environment."
}
@ -128,9 +103,7 @@ public class MTABuildTest extends BasePipelineTest {
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"$/
@ -140,7 +113,7 @@ public class MTABuildTest extends BasePipelineTest {
assert mtarFilePath == "${currentDir}/com.mycompany.northwind.mtar"
assert echoes[1] == "[mtaBuild] MTA JAR \"/opt/mta/mta.jar\" retrieved from environment."
assert messages[1] == "[mtaBuild] MTA JAR \"/opt/mta/mta.jar\" retrieved from environment."
}
@Test
@ -148,9 +121,7 @@ public class MTABuildTest extends BasePipelineTest {
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"$/
@ -160,7 +131,7 @@ public class MTABuildTest extends BasePipelineTest {
assert mtarFilePath == "${currentDir}/com.mycompany.northwind.mtar"
assert echoes[1] == "[mtaBuild] Using MTA JAR from current working directory."
assert messages[1] == "[mtaBuild] Using MTA JAR from current working directory."
}
@ -169,9 +140,7 @@ public class MTABuildTest extends BasePipelineTest {
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"$/
@ -181,7 +150,7 @@ public class MTABuildTest extends BasePipelineTest {
assert mtarFilePath == "${currentDir}/com.mycompany.northwind.mtar"
assert echoes[1] == "[mtaBuild] MTA JAR \"/etc/mta/mta.jar\" retrieved from parameters."
assert messages[1] == "[mtaBuild] MTA JAR \"/etc/mta/mta.jar\" retrieved from parameters."
}
@ -189,9 +158,7 @@ public class MTABuildTest extends BasePipelineTest {
public void noMtaPresentTest(){
thrown.expect(FileNotFoundException)
defaultPipeline()
def script = loadScript(pipeline)
script.execute()
withPipeline(defaultPipeline()).execute()
}
@ -202,9 +169,7 @@ public class MTABuildTest extends BasePipelineTest {
new File("${currentDir}/mta.yaml") << badMtaYaml()
defaultPipeline()
def script = loadScript(pipeline)
script.execute()
withPipeline(defaultPipeline()).execute()
}
@ -215,9 +180,7 @@ public class MTABuildTest extends BasePipelineTest {
new File("${currentDir}/mta.yaml") << noIdMtaYaml()
defaultPipeline()
def script = loadScript(pipeline)
script.execute()
withPipeline(defaultPipeline()).execute()
}
@ -228,74 +191,71 @@ public class MTABuildTest extends BasePipelineTest {
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')
return '''
@Library('piper-library-os')
execute(){
mtaBuild buildTarget: 'NEO'
}
return this
'''
execute(){
mtaBuild buildTarget: 'NEO'
}
return this
'''
}
private returnMtarFilePathFromCommonPipelineEnvironmentPipeline(){
new File(pipeline) << '''
@Library('piper-library-os')
return '''
@Library('piper-library-os')
execute(){
mtaBuild buildTarget: 'NEO'
return commonPipelineEnvironment.getMtarFilePath()
}
return this
'''
execute(){
mtaBuild buildTarget: 'NEO'
return commonPipelineEnvironment.getMtarFilePath()
}
return this
'''
}
private mtaJarLocationAsParameterPipeline(){
new File(pipeline) << '''
@Library('piper-library-os')
return '''
@Library('piper-library-os')
execute(){
mtaBuild mtaJarLocation: '/etc/mta', buildTarget: 'NEO'
}
return this
'''
execute(){
mtaBuild mtaJarLocation: '/etc/mta', buildTarget: 'NEO'
}
return this
'''
}
private withSurroundingDirPipeline(){
new File(pipeline) << '''
@Library('piper-library-os')
return '''
@Library('piper-library-os')
execute(dirPath){
dir("${dirPath}"){
mtaBuild buildTarget: 'NEO'
}
}
return this
'''
execute(dirPath){
dir("${dirPath}"){
mtaBuild buildTarget: 'NEO'
}
}
return this
'''
}
private noBuildTargetPipeline(){
new File(pipeline) << '''
@Library('piper-library-os')
return '''
@Library('piper-library-os')
execute(){
mtaBuild()
}
return this
'''
execute(){
mtaBuild()
}
return this
'''
}

View File

@ -10,10 +10,7 @@ import org.junit.Rule
import org.junit.Test
import org.junit.rules.ExpectedException
import com.lesfurets.jenkins.unit.BasePipelineTest
class NeoDeploymentTest extends BasePipelineTest {
class NeoDeploymentTest extends PiperTestBase {
@Rule
public ExpectedException thrown = new ExpectedException().none()
@ -21,37 +18,16 @@ class NeoDeploymentTest extends BasePipelineTest {
@Rule
public TemporaryFolder tmp = new TemporaryFolder()
def script
def shellCalls = []
def pipeline
def echoes = []
def archivePath
@Before
void setup() {
void setUp() {
super.setUp()
archivePath = "${tmp.newFolder("workspace").toURI().getPath()}archiveName.mtar"
pipeline = "${tmp.newFolder("pipeline").toURI().getPath()}pipeline"
def piperLib = library()
.name('piper-library-os')
.retriever(projectSource())
.targetPath('clonePath/is/not/necessary')
.defaultVersion('irrelevant')
.allowOverride(true)
.implicit(false)
.build()
helper.registerSharedLibrary(piperLib)
helper.registerAllowedMethod('sh', [String], { GString s ->
shellCalls.add(s.replaceAll(/\s+/, " ").trim())
})
helper.registerAllowedMethod('error', [String], { s -> throw new AbortException(s) })
helper.registerAllowedMethod('echo', [String], { s -> echoes.add(s) })
helper.registerAllowedMethod('usernamePassword', [Map], { m -> return m })
helper.registerAllowedMethod('withCredentials', [List, Closure], { l, c ->
if(l[0].credentialsId == 'myCredentialsId') {
@ -78,19 +54,15 @@ class NeoDeploymentTest extends BasePipelineTest {
@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/
assert echoes[1] == "[neoDeploy] Neo executable \"/opt/neo/tools/neo.sh\" retrieved from environment."
assert messages[1] == "[neoDeploy] Neo executable \"/opt/neo/tools/neo.sh\" retrieved from environment."
}
@ -98,17 +70,13 @@ class NeoDeploymentTest extends BasePipelineTest {
@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')
}
@ -116,53 +84,41 @@ class NeoDeploymentTest extends BasePipelineTest {
@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/
assert echoes[1] == "[neoDeploy] Neo executable \"/opt/neo/tools/neo.sh\" retrieved from environment."
assert messages[1] == "[neoDeploy] Neo executable \"/opt/neo/tools/neo.sh\" retrieved from environment."
}
@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/
assert echoes[1] == "Using Neo executable from PATH."
assert messages[1] == "Using Neo executable from PATH."
}
@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/
assert echoes[1] == "[neoDeploy] Neo executable \"/etc/neo/tools/neo.sh\" retrieved from parameters."
assert messages[1] == "[neoDeploy] Neo executable \"/etc/neo/tools/neo.sh\" retrieved from parameters."
}
@ -170,14 +126,10 @@ class NeoDeploymentTest extends BasePipelineTest {
@Test
void archiveNotProvidedTest() {
noArchivePathPipeline()
thrown.expect(Exception)
thrown.expectMessage('ERROR - NO VALUE AVAILABLE FOR archivePath')
script = loadScript(pipeline)
script.execute()
withPipeline(noArchivePathPipeline()).execute()
}
@ -185,14 +137,10 @@ class NeoDeploymentTest extends BasePipelineTest {
@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')
}
@ -200,110 +148,106 @@ class NeoDeploymentTest extends BasePipelineTest {
@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')
return """
@Library('piper-library-os')
execute(archivePath, neoCredentialsId) {
commonPipelineEnvironment.setConfigProperty('DEPLOY_HOST', 'test.deploy.host.com')
commonPipelineEnvironment.setConfigProperty('CI_DEPLOY_ACCOUNT', 'trialuser123')
node() {
neoDeploy script: this, archivePath: archivePath, neoCredentialsId: neoCredentialsId
}
}
return this
"""
execute(archivePath, neoCredentialsId) {
commonPipelineEnvironment.setConfigProperty('DEPLOY_HOST', 'test.deploy.host.com')
commonPipelineEnvironment.setConfigProperty('CI_DEPLOY_ACCOUNT', 'trialuser123')
node() {
neoDeploy script: this, archivePath: archivePath, neoCredentialsId: neoCredentialsId
}
}
return this
"""
}
private noCredentialsIdPipeline(){
new File(pipeline) << """
@Library('piper-library-os')
execute(archivePath) {
commonPipelineEnvironment.setConfigProperty('DEPLOY_HOST', 'test.deploy.host.com')
commonPipelineEnvironment.setConfigProperty('CI_DEPLOY_ACCOUNT', 'trialuser123')
node() {
neoDeploy script: this, archivePath: archivePath
}
}
return this
"""
return """
@Library('piper-library-os')
execute(archivePath) {
commonPipelineEnvironment.setConfigProperty('DEPLOY_HOST', 'test.deploy.host.com')
commonPipelineEnvironment.setConfigProperty('CI_DEPLOY_ACCOUNT', 'trialuser123')
node() {
neoDeploy script: this, archivePath: archivePath
}
}
return this
"""
}
private neoHomeParameterPipeline(){
new File(pipeline) << """
@Library('piper-library-os')
execute(archivePath, neoCredentialsId) {
commonPipelineEnvironment.setConfigProperty('DEPLOY_HOST', 'test.deploy.host.com')
commonPipelineEnvironment.setConfigProperty('CI_DEPLOY_ACCOUNT', 'trialuser123')
node() {
neoDeploy script: this, archivePath: archivePath, neoCredentialsId: neoCredentialsId, neoHome: '/etc/neo'
}
}
return this
"""
return """
@Library('piper-library-os')
execute(archivePath, neoCredentialsId) {
commonPipelineEnvironment.setConfigProperty('DEPLOY_HOST', 'test.deploy.host.com')
commonPipelineEnvironment.setConfigProperty('CI_DEPLOY_ACCOUNT', 'trialuser123')
node() {
neoDeploy script: this, archivePath: archivePath, neoCredentialsId: neoCredentialsId, neoHome: '/etc/neo'
}
}
return this
"""
}
private noArchivePathPipeline(){
new File(pipeline) << """
@Library('piper-library-os')
execute() {
commonPipelineEnvironment.setConfigProperty('DEPLOY_HOST', 'test.deploy.host.com')
commonPipelineEnvironment.setConfigProperty('CI_DEPLOY_ACCOUNT', 'trialuser123')
node() {
neoDeploy script: this
}
}
return this
"""
return """
@Library('piper-library-os')
execute() {
commonPipelineEnvironment.setConfigProperty('DEPLOY_HOST', 'test.deploy.host.com')
commonPipelineEnvironment.setConfigProperty('CI_DEPLOY_ACCOUNT', 'trialuser123')
node() {
neoDeploy script: this
}
}
return this
"""
}
private noScriptPipeline(){
new File(pipeline) << """
@Library('piper-library-os')
execute(archivePath) {
node() {
neoDeploy archivePath: archivePath
}
}
return this
"""
return """
@Library('piper-library-os')
execute(archivePath) {
node() {
neoDeploy archivePath: archivePath
}
}
return this
"""
}
}

View File

@ -0,0 +1,51 @@
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 = []
void setUp() {
super.setUp()
messages.clear()
shellCalls.clear()
preparePiperLib()
helper.registerAllowedMethod('echo', [String], {s -> messages.add(s)} )
helper.registerAllowedMethod('sh', [String], { s ->
shellCalls.add(s.replaceAll(/\s+/, " ").trim())
})
pipeline = pipelineFolder.newFile()
}
protected withPipeline(p) {
pipeline << p
loadScript(pipeline.toURI().getPath())
}
private preparePiperLib() {
def piperLib = library()
.name('piper-library-os')
.retriever(projectSource())
.targetPath('clonePath/is/not/necessary')
.defaultVersion('<irrelevant>')
.allowOverride(true)
.implicit(false)
.build()
helper.registerSharedLibrary(piperLib)
}
}

View File

@ -1,6 +1,3 @@
import static com.lesfurets.jenkins.unit.global.lib.LibraryConfiguration.library
import static ProjectSource.projectSource
import org.apache.commons.exec.*
import hudson.AbortException
import org.junit.Before
@ -8,10 +5,8 @@ import org.junit.Rule
import org.junit.Test
import org.junit.rules.ExpectedException
import org.junit.rules.TemporaryFolder
import com.lesfurets.jenkins.unit.BasePipelineTest
class ToolValidateTest extends BasePipelineTest {
class ToolValidateTest extends PiperTestBase {
@Rule
@ -21,31 +16,15 @@ class ToolValidateTest extends BasePipelineTest {
public TemporaryFolder tmp = new TemporaryFolder()
private notEmptyDir
private messages = []
private script
@Before
void setup() {
void setUp() {
super.setUp()
def piperLib = library()
.name('piper-library-os')
.retriever(projectSource())
.targetPath('clonePath/is/not/necessary')
.defaultVersion('irrelevant')
.allowOverride(true)
.implicit(false)
.build()
helper.registerSharedLibrary(piperLib)
helper.registerAllowedMethod('echo', [String], {s -> messages.add(s)})
def pipelinePath = "${tmp.newFolder("pipeline").toURI().getPath()}pipeline"
createPipeline(pipelinePath)
script = loadScript(pipelinePath)
script = withPipeline(defaultPipeline())
notEmptyDir = tmp.newFolder('notEmptyDir')
def path = "${notEmptyDir.getAbsolutePath()}${File.separator}test.txt"
@ -269,20 +248,20 @@ class ToolValidateTest extends BasePipelineTest {
}
private createPipeline(pipelinePath){
new File(pipelinePath) << """
@Library('piper-library-os')
private defaultPipeline(){
return """
@Library('piper-library-os')
execute() {
execute() {
node() {
node() {
toolValidate tool: tool, home: home
}
}
toolValidate tool: tool, home: home
}
}
return this
"""
return this
"""
}
private getNoVersion(Map m) {