1
0
mirror of https://github.com/SAP/jenkins-library.git synced 2025-09-16 09:26:22 +02:00

Merge pull request #71 from alejandraferreirovidal/cleanUpMtaBuilTest

Clean up MTABuildTest
This commit is contained in:
Alejandra Ferreiro Vidal
2018-02-06 16:05:38 +01:00
committed by GitHub

View File

@@ -30,8 +30,7 @@ public class MTABuildTest extends BasePipelineTest {
def currentDir def currentDir
def otherDir def mtaYaml
def mtaBuildShEnv
def mtaBuildScript def mtaBuildScript
def cpe def cpe
@@ -40,86 +39,57 @@ public class MTABuildTest extends BasePipelineTest {
void init() { void init() {
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 '/' mtaYaml = new File("$currentDir/mta.yaml")
mtaYaml << defaultMtaYaml()
helper.registerAllowedMethod('readYaml', [Map], { helper.registerAllowedMethod('readYaml', [Map], {
m -> m ->
return new Yaml().load((m.file as File).text) return new Yaml().load((m.file as File).text)
}) })
helper.registerAllowedMethod("dir", [String, Closure], {
s, c ->
currentDir = "${currentDir}/${s}"
c()
})
helper.registerAllowedMethod('pwd', [], { currentDir } ) helper.registerAllowedMethod('pwd', [], { currentDir } )
helper.registerAllowedMethod("withEnv", [List.class, Closure.class],
{ l, c ->
mtaBuildShEnv = l
c()
})
binding.setVariable('PATH', '/usr/bin') binding.setVariable('PATH', '/usr/bin')
binding.setVariable('JAVA_HOME', '/opt/java')
binding.setVariable('env', [:])
mtaBuildScript = loadScript("mtaBuild.groovy").mtaBuild mtaBuildScript = loadScript('mtaBuild.groovy').mtaBuild
cpe = loadScript('commonPipelineEnvironment.groovy').commonPipelineEnvironment cpe = loadScript('commonPipelineEnvironment.groovy').commonPipelineEnvironment
} }
@Test @Test
public void straightForwardTest(){ void environmentPathTest() {
binding.getVariable('env')['MTA_JAR_LOCATION'] = '/opt/mta' mtaBuildScript.call(buildTarget: 'NEO')
new File("${currentDir}/mta.yaml") << defaultMtaYaml() assert jscr.shell[1].contains('PATH=./node_modules/.bin:/usr/bin')
def mtarFilePath = mtaBuildScript.call(script: [commonPipelineEnvironment: cpe],
buildTarget: 'NEO')
assert jscr.shell[0] =~ /sed -ie "s\/\\\$\{timestamp\}\/`date \+%Y%m%d%H%M%S`\/g" ".*\/mta.yaml"$/
assert jscr.shell[1].contains("PATH=./node_modules/.bin:/usr/bin")
assert jscr.shell[1].contains(' -jar /opt/mta/mta.jar --mtar ')
assert mtarFilePath == "${currentDir}/com.mycompany.northwind.mtar"
assert jlr.log.contains( "[mtaBuild] MTA JAR \"/opt/mta/mta.jar\" retrieved from environment.")
} }
@Test @Test
public void mtarFilePathFromCommonPipelineEnviromentTest(){ void sedTest() {
binding.getVariable('env')['MTA_JAR_LOCATION'] = '/opt/mta' mtaBuildScript.call(buildTarget: 'NEO')
new File("${currentDir}/mta.yaml") << defaultMtaYaml() assert jscr.shell[0] =~ /sed -ie "s\/\\\$\{timestamp\}\/`date \+%Y%m%d%H%M%S`\/g" ".*\/mta.yaml"$/
}
@Test
void mtarFilePathFromCommonPipelineEnviromentTest() {
mtaBuildScript.call(script: [commonPipelineEnvironment: cpe], mtaBuildScript.call(script: [commonPipelineEnvironment: cpe],
buildTarget: 'NEO') buildTarget: 'NEO')
def mtarFilePath = cpe.getMtarFilePath() def mtarFilePath = cpe.getMtarFilePath()
assert jscr.shell[0] =~ /sed -ie "s\/\\\$\{timestamp\}\/`date \+%Y%m%d%H%M%S`\/g" ".*\/mta.yaml"$/ assert mtarFilePath == "$currentDir/com.mycompany.northwind.mtar"
assert jscr.shell[1].contains("PATH=./node_modules/.bin:/usr/bin")
assert jscr.shell[1].contains(' -jar /opt/mta/mta.jar --mtar ')
assert mtarFilePath == "${currentDir}/com.mycompany.northwind.mtar"
assert jlr.log.contains("[mtaBuild] MTA JAR \"/opt/mta/mta.jar\" retrieved from environment.")
} }
@Test @Test
public void mtaBuildWithSurroundingDirTest(){ void mtaBuildWithSurroundingDirTest() {
binding.getVariable('env')['MTA_JAR_LOCATION'] = '/opt/mta'
def newDirName = 'newDir' def newDirName = 'newDir'
def newDirPath = "${currentDir}/${newDirName}" def newDirPath = "$currentDir/$newDirName"
def newDir = new File(newDirPath) def newDir = new File(newDirPath)
newDir.mkdirs() newDir.mkdirs()
@@ -127,109 +97,102 @@ public class MTABuildTest extends BasePipelineTest {
helper.registerAllowedMethod('pwd', [], { newDirPath } ) helper.registerAllowedMethod('pwd', [], { newDirPath } )
def mtarFilePath = mtaBuildScript.call(script: [commonPipelineEnvironment: cpe], buildTarget: 'NEO') def mtarFilePath = mtaBuildScript.call(buildTarget: 'NEO')
assert jscr.shell[0] =~ /sed -ie "s\/\\\$\{timestamp\}\/`date \+%Y%m%d%H%M%S`\/g" ".*\/newDir\/mta.yaml"$/ assert jscr.shell[0] =~ /sed -ie "s\/\\\$\{timestamp\}\/`date \+%Y%m%d%H%M%S`\/g" ".*\/newDir\/mta.yaml"$/
assert jscr.shell[1].contains("PATH=./node_modules/.bin:/usr/bin") assert mtarFilePath == "$currentDir/$newDirName/com.mycompany.northwind.mtar"
assert jscr.shell[1].contains(' -jar /opt/mta/mta.jar --mtar ')
assert mtarFilePath == "${currentDir}/${newDirName}/com.mycompany.northwind.mtar"
assert jlr.log.contains("[mtaBuild] MTA JAR \"/opt/mta/mta.jar\" retrieved from environment.")
} }
@Test @Test
void mtaHomeNotSetTest() { void mtaJarLocationNotSetTest() {
new File("${currentDir}/mta.yaml") << defaultMtaYaml() mtaBuildScript.call(buildTarget: 'NEO')
def mtarFilePath = mtaBuildScript.call(script: [commonPipelineEnvironment: cpe], buildTarget: 'NEO')
assert jscr.shell[0] =~ /sed -ie "s\/\\\$\{timestamp\}\/`date \+%Y%m%d%H%M%S`\/g" ".*\/mta.yaml"$/
assert jscr.shell[1].contains("PATH=./node_modules/.bin:/usr/bin")
assert jscr.shell[1].contains(' -jar mta.jar --mtar ') assert jscr.shell[1].contains(' -jar mta.jar --mtar ')
assert mtarFilePath == "${currentDir}/com.mycompany.northwind.mtar" assert jlr.log.contains('[mtaBuild] Using MTA JAR from current working directory.')
assert jlr.log.contains( "[mtaBuild] Using MTA JAR from current working directory." )
} }
@Test @Test
void mtaHomeAsParameterTest() { void mtaJarLocationAsParameterTest() {
new File("${currentDir}/mta.yaml") << defaultMtaYaml() mtaBuildScript.call(mtaJarLocation: '/mylocation/mta', buildTarget: 'NEO')
def mtarFilePath = mtaBuildScript.call(mtaJarLocation: '/mylocation/mta', buildTarget: 'NEO')
assert jscr.shell[0] =~ /sed -ie "s\/\\\$\{timestamp\}\/`date \+%Y%m%d%H%M%S`\/g" ".*\/mta.yaml"$/
assert jscr.shell[1].contains("PATH=./node_modules/.bin:/usr/bin")
assert jscr.shell[1].contains(' -jar /mylocation/mta/mta.jar --mtar ') assert jscr.shell[1].contains(' -jar /mylocation/mta/mta.jar --mtar ')
assert mtarFilePath == "${currentDir}/com.mycompany.northwind.mtar" assert jlr.log.contains('[mtaBuild] MTA JAR "/mylocation/mta/mta.jar" retrieved from parameters.')
assert jlr.log.contains("[mtaBuild] MTA JAR \"/mylocation/mta/mta.jar\" retrieved from parameters.".toString())
} }
@Test @Test
public void noMtaPresentTest(){ void noMtaPresentTest() {
mtaYaml.delete()
thrown.expect(FileNotFoundException) thrown.expect(FileNotFoundException)
mtaBuildScript.call(script: [commonPipelineEnvironment: cpe], mtaBuildScript.call(buildTarget: 'NEO')
buildTarget: 'NEO')
} }
@Test @Test
public void badMtaTest(){ void badMtaTest() {
thrown.expect(ParserException) thrown.expect(ParserException)
thrown.expectMessage('while parsing a block mapping') thrown.expectMessage('while parsing a block mapping')
new File("${currentDir}/mta.yaml") << badMtaYaml() mtaYaml.text = badMtaYaml()
mtaBuildScript.call(script: [commonPipelineEnvironment: cpe], mtaBuildScript.call(buildTarget: 'NEO')
buildTarget: 'NEO')
} }
@Test @Test
public void noIdInMtaTest(){ void noIdInMtaTest() {
thrown.expect(AbortException) thrown.expect(AbortException)
thrown.expectMessage("Property 'ID' not found in mta.yaml file at: '") thrown.expectMessage("Property 'ID' not found in mta.yaml file at: '")
new File("${currentDir}/mta.yaml") << noIdMtaYaml() mtaYaml.text = noIdMtaYaml()
mtaBuildScript.call(script: [commonPipelineEnvironment: cpe], mtaBuildScript.call(buildTarget: 'NEO')
buildTarget: 'NEO')
} }
@Test @Test
public void noBuildTargetTest(){ void noBuildTargetTest() {
thrown.expect(Exception) thrown.expect(Exception)
thrown.expectMessage("ERROR - NO VALUE AVAILABLE FOR buildTarget") thrown.expectMessage('ERROR - NO VALUE AVAILABLE FOR buildTarget')
new File("${currentDir}/mta.yaml") << defaultMtaYaml() mtaBuildScript.call()
mtaBuildScript.call(script: [commonPipelineEnvironment: cpe])
} }
private defaultMtaYaml(){
@Test
void mtaJarLocationFromEnvironmentTest() {
binding.setVariable('env', [:])
binding.getVariable('env')['MTA_JAR_LOCATION'] = '/env/mta'
mtaBuildScript.call(buildTarget: 'NEO')
assert jscr.shell[1].contains('-jar /env/mta/mta.jar --mtar')
assert jlr.log.contains('[mtaBuild] MTA JAR "/env/mta/mta.jar" retrieved from environment.')
}
private defaultMtaYaml() {
return ''' return '''
_schema-version: "2.0.0" _schema-version: "2.0.0"
ID: "com.mycompany.northwind" ID: "com.mycompany.northwind"
version: 1.0.0 version: 1.0.0
parameters: parameters:
hcp-deployer-version: "1.0.0" hcp-deployer-version: "1.0.0"
modules: modules:
- name: "fiorinorthwind" - name: "fiorinorthwind"
type: html5 type: html5
@@ -242,15 +205,15 @@ public class MTABuildTest extends BasePipelineTest {
''' '''
} }
private badMtaYaml(){ private badMtaYaml() {
return ''' return '''
_schema-version: "2.0.0 _schema-version: "2.0.0
ID: "com.mycompany.northwind" ID: "com.mycompany.northwind"
version: 1.0.0 version: 1.0.0
parameters: parameters:
hcp-deployer-version: "1.0.0" hcp-deployer-version: "1.0.0"
modules: modules:
- name: "fiorinorthwind" - name: "fiorinorthwind"
type: html5 type: html5
@@ -263,14 +226,14 @@ public class MTABuildTest extends BasePipelineTest {
''' '''
} }
private noIdMtaYaml(){ private noIdMtaYaml() {
return ''' return '''
_schema-version: "2.0.0" _schema-version: "2.0.0"
version: 1.0.0 version: 1.0.0
parameters: parameters:
hcp-deployer-version: "1.0.0" hcp-deployer-version: "1.0.0"
modules: modules:
- name: "fiorinorthwind" - name: "fiorinorthwind"
type: html5 type: html5