From 9c544bb6e07708cb7fc3ddcbe776eb0b7f768292 Mon Sep 17 00:00:00 2001 From: Marcus Holl Date: Wed, 21 Feb 2018 12:46:55 +0100 Subject: [PATCH 1/8] Make test more tolerant --- test/groovy/MTABuildTest.groovy | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/test/groovy/MTABuildTest.groovy b/test/groovy/MTABuildTest.groovy index 42d2c704d..ab37ce21a 100644 --- a/test/groovy/MTABuildTest.groovy +++ b/test/groovy/MTABuildTest.groovy @@ -67,7 +67,7 @@ public class MtaBuildTest extends BasePipelineTest { jsr.step.call(buildTarget: 'NEO') - assert jscr.shell[1].contains('PATH=./node_modules/.bin:/usr/bin') + assert jscr.shell.find { c -> c.contains('PATH=./node_modules/.bin:/usr/bin')} } @@ -76,7 +76,7 @@ public class MtaBuildTest extends BasePipelineTest { jsr.step.call(buildTarget: 'NEO') - assert jscr.shell[0] =~ /sed -ie "s\/\\\$\{timestamp\}\/`date \+%Y%m%d%H%M%S`\/g" ".*\/mta.yaml"$/ + assert jscr.shell.find { c -> c =~ /sed -ie "s\/\\\$\{timestamp\}\/`date \+%Y%m%d%H%M%S`\/g" ".*\/mta.yaml"$/} } @@ -99,7 +99,7 @@ public class MtaBuildTest extends BasePipelineTest { def mtarFilePath = jsr.step.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.find { c -> c =~ /sed -ie "s\/\\\$\{timestamp\}\/`date \+%Y%m%d%H%M%S`\/g" ".*\/newDir\/mta.yaml"$/} assert mtarFilePath == "$newDir/com.mycompany.northwind.mtar" } @@ -110,7 +110,7 @@ public class MtaBuildTest extends BasePipelineTest { jsr.step.call(buildTarget: 'NEO') - assert jscr.shell[1].contains(' -jar mta.jar --mtar ') + assert jscr.shell.find { c -> c.contains(' -jar mta.jar --mtar ')} assert jlr.log.contains('[mtaBuild] Using MTA JAR from current working directory.') } @@ -121,7 +121,7 @@ public class MtaBuildTest extends BasePipelineTest { jsr.step.call(mtaJarLocation: '/mylocation/mta', buildTarget: 'NEO') - assert jscr.shell[1].contains(' -jar /mylocation/mta/mta.jar --mtar ') + assert jscr.shell.find { c -> c.contains(' -jar /mylocation/mta/mta.jar --mtar ')} assert jlr.log.contains('[mtaBuild] MTA JAR "/mylocation/mta/mta.jar" retrieved from configuration.') } @@ -169,7 +169,7 @@ public class MtaBuildTest extends BasePipelineTest { jsr.step.call(buildTarget: 'NEO') - assert jscr.shell[1].contains('-jar /env/mta/mta.jar --mtar') + assert jscr.shell.find { c -> c.contains('-jar /env/mta/mta.jar --mtar')} assert jlr.log.contains('[mtaBuild] MTA JAR "/env/mta/mta.jar" retrieved from environment.') } @@ -182,7 +182,7 @@ public class MtaBuildTest extends BasePipelineTest { jsr.step.call(script: [commonPipelineEnvironment: jer.env], buildTarget: 'NEO') - assert jscr.shell[1].contains('-jar /step/mta/mta.jar --mtar') + assert jscr.shell.find(){ c -> c.contains('-jar /step/mta/mta.jar --mtar')} assert jlr.log.contains('[mtaBuild] MTA JAR "/step/mta/mta.jar" retrieved from configuration.') } @@ -192,7 +192,7 @@ public class MtaBuildTest extends BasePipelineTest { jsr.step.call(buildTarget: 'NEO') - assert jscr.shell[1].contains('java -jar mta.jar --mtar com.mycompany.northwind.mtar --build-target=NEO build') + assert jscr.shell.find { c -> c.contains('java -jar mta.jar --mtar com.mycompany.northwind.mtar --build-target=NEO build')} } @@ -203,7 +203,7 @@ public class MtaBuildTest extends BasePipelineTest { jsr.step.call(script: [commonPipelineEnvironment: jer.env]) - assert jscr.shell[1].contains('java -jar mta.jar --mtar com.mycompany.northwind.mtar --build-target=NEO build') + assert jscr.shell.find(){ c -> c.contains('java -jar mta.jar --mtar com.mycompany.northwind.mtar --build-target=NEO build')} } @@ -214,7 +214,7 @@ public class MtaBuildTest extends BasePipelineTest { jsr.step.call(script: [commonPipelineEnvironment: jer.env]) - assert jscr.shell[1].contains('java -jar mta.jar --mtar com.mycompany.northwind.mtar --build-target=NEO build') + assert jscr.shell.find { c -> c.contains('java -jar mta.jar --mtar com.mycompany.northwind.mtar --build-target=NEO build')} } From d93b865f8dcbd36ac0618bced18134b5d4c5dbd1 Mon Sep 17 00:00:00 2001 From: Marcus Holl Date: Tue, 20 Feb 2018 13:09:16 +0100 Subject: [PATCH 2/8] java home validation inside mtabuild --- test/groovy/MTABuildTest.groovy | 24 ++++++++++++++++++++++++ vars/mtaBuild.groovy | 13 +++++++++++++ 2 files changed, 37 insertions(+) diff --git a/test/groovy/MTABuildTest.groovy b/test/groovy/MTABuildTest.groovy index ab37ce21a..37a4b7d93 100644 --- a/test/groovy/MTABuildTest.groovy +++ b/test/groovy/MTABuildTest.groovy @@ -20,6 +20,9 @@ import util.JenkinsEnvironmentRule import util.Rules public class MtaBuildTest extends BasePipelineTest { + + def toolJavaValidateCalled = false + @ClassRule public static TemporaryFolder tmp = new TemporaryFolder() @@ -59,6 +62,12 @@ public class MtaBuildTest extends BasePipelineTest { helper.registerAllowedMethod('pwd', [], { currentDir } ) binding.setVariable('PATH', '/usr/bin') + + helper.registerAllowedMethod('toolValidate', [Map], { m -> + + if(m.tool == 'java') + toolJavaValidateCalled = true + }) } @@ -217,7 +226,22 @@ public class MtaBuildTest extends BasePipelineTest { assert jscr.shell.find { c -> c.contains('java -jar mta.jar --mtar com.mycompany.northwind.mtar --build-target=NEO build')} } + @Test + void toolJavaValidateCalled() { + jsr.step.call(buildTarget: 'NEO') + + assert toolJavaValidateCalled + } + + @Test + void toolValidateNotCalledWhenJavaHomeIsUnsetButJavaIsInPath() { + + jscr.setReturnValue('which java', 0) + jsr.step.call(buildTarget: 'NEO') + + assert !toolJavaValidateCalled +} private static defaultMtaYaml() { return ''' _schema-version: "2.0.0" diff --git a/vars/mtaBuild.groovy b/vars/mtaBuild.groovy index 1d3b85aea..97aa58102 100644 --- a/vars/mtaBuild.groovy +++ b/vars/mtaBuild.groovy @@ -28,6 +28,19 @@ def call(Map parameters = [:]) { null, stepConfigurationKeys) + JAVA_HOME_CHECK : { + + // in case JAVA_HOME is not set, but java is in the path we should not fail + // in order to be backward compatible. Before introducing that check here + // is worked also in case JAVA_HOME was not set, but java was in the path. + // toolValidate works only upon JAVA_HOME and fails in case it is not set. + + def rc = sh script: 'which java' , returnStatus: true + if(script.JAVA_HOME || (!script.JAVA_HOME && rc != 0)) { + toolValidate tool: 'java', home: script.JAVA_HOME + } + } + def mtaYaml = readYaml file: "${pwd()}/mta.yaml" //[Q]: Why not yaml.dump()? [A]: This reformats the whole file. From b8760201c71d5fc2b918f4ab52540e4c8e2e593f Mon Sep 17 00:00:00 2001 From: Marcus Holl Date: Thu, 22 Feb 2018 11:13:34 +0100 Subject: [PATCH 3/8] Emit log message in case tool validation is skipped. --- test/groovy/MTABuildTest.groovy | 1 + vars/mtaBuild.groovy | 2 ++ 2 files changed, 3 insertions(+) diff --git a/test/groovy/MTABuildTest.groovy b/test/groovy/MTABuildTest.groovy index 37a4b7d93..c6a120b22 100644 --- a/test/groovy/MTABuildTest.groovy +++ b/test/groovy/MTABuildTest.groovy @@ -241,6 +241,7 @@ public class MtaBuildTest extends BasePipelineTest { jsr.step.call(buildTarget: 'NEO') assert !toolJavaValidateCalled + assert jlr.log.contains('Tool validation (java) skipped. JAVA_HOME not set, but java executable in path.') } private static defaultMtaYaml() { return ''' diff --git a/vars/mtaBuild.groovy b/vars/mtaBuild.groovy index 97aa58102..40e6f2e37 100644 --- a/vars/mtaBuild.groovy +++ b/vars/mtaBuild.groovy @@ -38,6 +38,8 @@ def call(Map parameters = [:]) { def rc = sh script: 'which java' , returnStatus: true if(script.JAVA_HOME || (!script.JAVA_HOME && rc != 0)) { toolValidate tool: 'java', home: script.JAVA_HOME + } else { + echo 'Tool validation (java) skipped. JAVA_HOME not set, but java executable in path.' } } From 3bc53b906fa5686bd7f0a8f4e0dee13ff72cbd15 Mon Sep 17 00:00:00 2001 From: Marcus Holl Date: Wed, 21 Feb 2018 12:46:55 +0100 Subject: [PATCH 4/8] Make test more tolerant --- test/groovy/MTABuildTest.groovy | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/test/groovy/MTABuildTest.groovy b/test/groovy/MTABuildTest.groovy index 42d2c704d..ab37ce21a 100644 --- a/test/groovy/MTABuildTest.groovy +++ b/test/groovy/MTABuildTest.groovy @@ -67,7 +67,7 @@ public class MtaBuildTest extends BasePipelineTest { jsr.step.call(buildTarget: 'NEO') - assert jscr.shell[1].contains('PATH=./node_modules/.bin:/usr/bin') + assert jscr.shell.find { c -> c.contains('PATH=./node_modules/.bin:/usr/bin')} } @@ -76,7 +76,7 @@ public class MtaBuildTest extends BasePipelineTest { jsr.step.call(buildTarget: 'NEO') - assert jscr.shell[0] =~ /sed -ie "s\/\\\$\{timestamp\}\/`date \+%Y%m%d%H%M%S`\/g" ".*\/mta.yaml"$/ + assert jscr.shell.find { c -> c =~ /sed -ie "s\/\\\$\{timestamp\}\/`date \+%Y%m%d%H%M%S`\/g" ".*\/mta.yaml"$/} } @@ -99,7 +99,7 @@ public class MtaBuildTest extends BasePipelineTest { def mtarFilePath = jsr.step.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.find { c -> c =~ /sed -ie "s\/\\\$\{timestamp\}\/`date \+%Y%m%d%H%M%S`\/g" ".*\/newDir\/mta.yaml"$/} assert mtarFilePath == "$newDir/com.mycompany.northwind.mtar" } @@ -110,7 +110,7 @@ public class MtaBuildTest extends BasePipelineTest { jsr.step.call(buildTarget: 'NEO') - assert jscr.shell[1].contains(' -jar mta.jar --mtar ') + assert jscr.shell.find { c -> c.contains(' -jar mta.jar --mtar ')} assert jlr.log.contains('[mtaBuild] Using MTA JAR from current working directory.') } @@ -121,7 +121,7 @@ public class MtaBuildTest extends BasePipelineTest { jsr.step.call(mtaJarLocation: '/mylocation/mta', buildTarget: 'NEO') - assert jscr.shell[1].contains(' -jar /mylocation/mta/mta.jar --mtar ') + assert jscr.shell.find { c -> c.contains(' -jar /mylocation/mta/mta.jar --mtar ')} assert jlr.log.contains('[mtaBuild] MTA JAR "/mylocation/mta/mta.jar" retrieved from configuration.') } @@ -169,7 +169,7 @@ public class MtaBuildTest extends BasePipelineTest { jsr.step.call(buildTarget: 'NEO') - assert jscr.shell[1].contains('-jar /env/mta/mta.jar --mtar') + assert jscr.shell.find { c -> c.contains('-jar /env/mta/mta.jar --mtar')} assert jlr.log.contains('[mtaBuild] MTA JAR "/env/mta/mta.jar" retrieved from environment.') } @@ -182,7 +182,7 @@ public class MtaBuildTest extends BasePipelineTest { jsr.step.call(script: [commonPipelineEnvironment: jer.env], buildTarget: 'NEO') - assert jscr.shell[1].contains('-jar /step/mta/mta.jar --mtar') + assert jscr.shell.find(){ c -> c.contains('-jar /step/mta/mta.jar --mtar')} assert jlr.log.contains('[mtaBuild] MTA JAR "/step/mta/mta.jar" retrieved from configuration.') } @@ -192,7 +192,7 @@ public class MtaBuildTest extends BasePipelineTest { jsr.step.call(buildTarget: 'NEO') - assert jscr.shell[1].contains('java -jar mta.jar --mtar com.mycompany.northwind.mtar --build-target=NEO build') + assert jscr.shell.find { c -> c.contains('java -jar mta.jar --mtar com.mycompany.northwind.mtar --build-target=NEO build')} } @@ -203,7 +203,7 @@ public class MtaBuildTest extends BasePipelineTest { jsr.step.call(script: [commonPipelineEnvironment: jer.env]) - assert jscr.shell[1].contains('java -jar mta.jar --mtar com.mycompany.northwind.mtar --build-target=NEO build') + assert jscr.shell.find(){ c -> c.contains('java -jar mta.jar --mtar com.mycompany.northwind.mtar --build-target=NEO build')} } @@ -214,7 +214,7 @@ public class MtaBuildTest extends BasePipelineTest { jsr.step.call(script: [commonPipelineEnvironment: jer.env]) - assert jscr.shell[1].contains('java -jar mta.jar --mtar com.mycompany.northwind.mtar --build-target=NEO build') + assert jscr.shell.find { c -> c.contains('java -jar mta.jar --mtar com.mycompany.northwind.mtar --build-target=NEO build')} } From 3cafd0b6d5ba45545270f51d4ab2812a53ea14c9 Mon Sep 17 00:00:00 2001 From: Marcus Holl Date: Wed, 21 Feb 2018 12:48:14 +0100 Subject: [PATCH 5/8] Validating mta jar inside mtaBuild --- test/groovy/MTABuildTest.groovy | 30 ++++++++++++++++++++++++++++++ vars/mtaBuild.groovy | 13 +++++++++++++ 2 files changed, 43 insertions(+) diff --git a/test/groovy/MTABuildTest.groovy b/test/groovy/MTABuildTest.groovy index ab37ce21a..662ef3868 100644 --- a/test/groovy/MTABuildTest.groovy +++ b/test/groovy/MTABuildTest.groovy @@ -20,6 +20,9 @@ import util.JenkinsEnvironmentRule import util.Rules public class MtaBuildTest extends BasePipelineTest { + + def toolMtaValidateCalled = false + @ClassRule public static TemporaryFolder tmp = new TemporaryFolder() @@ -59,6 +62,20 @@ public class MtaBuildTest extends BasePipelineTest { helper.registerAllowedMethod('pwd', [], { currentDir } ) binding.setVariable('PATH', '/usr/bin') + + // + // needs to be after loading the scripts. Here we have a different behaviour + // for usual steps and for steps contained in the shared lib itself. + // + // toolValidate mocked here since we are not interested in testing + // toolValidate here. This is expected to be done in a test class for + // toolValidate. + // + helper.registerAllowedMethod('toolValidate', [Map], { m -> + + if(m.tool == 'mta') + toolMtaValidateCalled = true + }) } @@ -217,6 +234,19 @@ public class MtaBuildTest extends BasePipelineTest { assert jscr.shell.find { c -> c.contains('java -jar mta.jar --mtar com.mycompany.northwind.mtar --build-target=NEO build')} } + @Test + void skipValidationInCaseMtarJarFileIsUsedFromWorkingDir() { + jscr.setReturnValue('ls mta.jar', 0) + jsr.step.call(script: [commonPipelineEnvironment: jer.env]) + assert !toolMtaValidateCalled + } + + @Test + void performValidationInCaseMtarJarFileIsNotUsedFromWorkingDir() { + jscr.setReturnValue('ls mta.jar', 1) + jsr.step.call(script: [commonPipelineEnvironment: jer.env]) + assert toolMtaValidateCalled + } private static defaultMtaYaml() { return ''' diff --git a/vars/mtaBuild.groovy b/vars/mtaBuild.groovy index 1d3b85aea..3dfc4eba0 100644 --- a/vars/mtaBuild.groovy +++ b/vars/mtaBuild.groovy @@ -1,6 +1,9 @@ import com.sap.piper.ConfigurationLoader import com.sap.piper.ConfigurationMerger +import groovy.transform.Field + +@Field def DEFAULT_MTA_JAR_NAME = 'mta.jar' def call(Map parameters = [:]) { @@ -28,6 +31,16 @@ def call(Map parameters = [:]) { null, stepConfigurationKeys) + + MTA_JAR_FILE_VALIDATE: { + // same order like inside getMtaJar, + def mtaJarLocation = configuration?.mtaJarLocation ?: env?.MTA_JAR_LOCATION + def returnCodeLsMtaJar = sh script: 'ls mta.jar', returnStatus:true + if(mtaJarLocation || ( !mtaJarLocation && returnCodeLsMtaJar != 0)) { + toolValidate tool: 'mta', home: mtaJarLocation + } + } + def mtaYaml = readYaml file: "${pwd()}/mta.yaml" //[Q]: Why not yaml.dump()? [A]: This reformats the whole file. From d4c4f864fc4069f78f4763d877d49c557ee65aa8 Mon Sep 17 00:00:00 2001 From: Marcus Holl Date: Wed, 21 Feb 2018 12:52:05 +0100 Subject: [PATCH 6/8] Provide variable for default mta jar file name. --- vars/mtaBuild.groovy | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/vars/mtaBuild.groovy b/vars/mtaBuild.groovy index 3dfc4eba0..14c677014 100644 --- a/vars/mtaBuild.groovy +++ b/vars/mtaBuild.groovy @@ -35,7 +35,7 @@ def call(Map parameters = [:]) { MTA_JAR_FILE_VALIDATE: { // same order like inside getMtaJar, def mtaJarLocation = configuration?.mtaJarLocation ?: env?.MTA_JAR_LOCATION - def returnCodeLsMtaJar = sh script: 'ls mta.jar', returnStatus:true + def returnCodeLsMtaJar = sh script: "ls ${DEFAULT_MTA_JAR_NAME}", returnStatus:true if(mtaJarLocation || ( !mtaJarLocation && returnCodeLsMtaJar != 0)) { toolValidate tool: 'mta', home: mtaJarLocation } @@ -69,7 +69,7 @@ def call(Map parameters = [:]) { } private getMtaJar(stepName, configuration) { - def mtaJarLocation = 'mta.jar' //default, maybe it is in current working directory + def mtaJarLocation = DEFAULT_MTA_JAR_NAME //default, maybe it is in current working directory if(configuration?.mtaJarLocation){ mtaJarLocation = "${configuration.mtaJarLocation}/mta.jar" From 93a2f028cdc5956e1cfbdaf410bf26ed735199e9 Mon Sep 17 00:00:00 2001 From: Willenbacher Date: Fri, 23 Feb 2018 13:33:14 +0100 Subject: [PATCH 7/8] replaced mta.jar with DEFAULT_MTA_JAR_NAME --- vars/mtaBuild.groovy | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/vars/mtaBuild.groovy b/vars/mtaBuild.groovy index 14c677014..f36b7e9f4 100644 --- a/vars/mtaBuild.groovy +++ b/vars/mtaBuild.groovy @@ -72,13 +72,13 @@ private getMtaJar(stepName, configuration) { def mtaJarLocation = DEFAULT_MTA_JAR_NAME //default, maybe it is in current working directory if(configuration?.mtaJarLocation){ - mtaJarLocation = "${configuration.mtaJarLocation}/mta.jar" + mtaJarLocation = "${configuration.mtaJarLocation}/${DEFAULT_MTA_JAR_NAME}" echo "[$stepName] MTA JAR \"${mtaJarLocation}\" retrieved from configuration." return mtaJarLocation } if(env?.MTA_JAR_LOCATION){ - mtaJarLocation = "${env.MTA_JAR_LOCATION}/mta.jar" + mtaJarLocation = "${env.MTA_JAR_LOCATION}/${DEFAULT_MTA_JAR_NAME}" echo "[$stepName] MTA JAR \"${mtaJarLocation}\" retrieved from environment." return mtaJarLocation } From b59b7123e2cf1e4f832939ad9f853476d47bd380 Mon Sep 17 00:00:00 2001 From: Marcus Holl Date: Tue, 6 Mar 2018 13:17:20 +0100 Subject: [PATCH 8/8] Add comment in case mta toolset is used from current working dir --- vars/mtaBuild.groovy | 2 ++ 1 file changed, 2 insertions(+) diff --git a/vars/mtaBuild.groovy b/vars/mtaBuild.groovy index f36b7e9f4..c930241ab 100644 --- a/vars/mtaBuild.groovy +++ b/vars/mtaBuild.groovy @@ -38,6 +38,8 @@ def call(Map parameters = [:]) { def returnCodeLsMtaJar = sh script: "ls ${DEFAULT_MTA_JAR_NAME}", returnStatus:true if(mtaJarLocation || ( !mtaJarLocation && returnCodeLsMtaJar != 0)) { toolValidate tool: 'mta', home: mtaJarLocation + } else { + echo "mta toolset (${DEFAULT_MTA_JAR_NAME}) has been found in current working directory. Using this version without further tool validation." } }