From 240df888825605ea336d621b609057a47944c34a Mon Sep 17 00:00:00 2001 From: Christopher Fenner Date: Wed, 8 Aug 2018 11:22:08 +0200 Subject: [PATCH 1/2] add stashing & private test repositories --- resources/default_pipeline_environment.yml | 2 + test/groovy/NewmanExecuteTest.groovy | 33 ++++++++++-- vars/newmanExecute.groovy | 58 ++++++++++++++-------- 3 files changed, 68 insertions(+), 25 deletions(-) diff --git a/resources/default_pipeline_environment.yml b/resources/default_pipeline_environment.yml index a0c9101d7..ed5aa2794 100644 --- a/resources/default_pipeline_environment.yml +++ b/resources/default_pipeline_environment.yml @@ -123,6 +123,8 @@ steps: newmanEnvironment: '' newmanGlobals: '' newmanRunCommand: "run ${config.newmanCollection} --environment '${config.newmanEnvironment}' --globals '${config.newmanGlobals}' --reporters junit,html --reporter-junit-export target/newman/TEST-${collectionDisplayName}.xml --reporter-html-export target/newman/TEST-${collectionDisplayName}.html" + stashContent: + - 'tests' pipelineStashFilesAfterBuild: runOpaTests: false stashIncludes: diff --git a/test/groovy/NewmanExecuteTest.groovy b/test/groovy/NewmanExecuteTest.groovy index 8b80b55dc..ee8ff2e59 100644 --- a/test/groovy/NewmanExecuteTest.groovy +++ b/test/groovy/NewmanExecuteTest.groovy @@ -15,8 +15,10 @@ import util.JenkinsLoggingRule import util.JenkinsShellCallRule import util.JenkinsDockerExecuteRule import util.Rules +import org.junit.rules.ExpectedException class NewmanExecuteTest extends BasePiperTest { + private ExpectedException thrown = ExpectedException.none() private JenkinsStepRule jsr = new JenkinsStepRule(this) private JenkinsLoggingRule jlr = new JenkinsLoggingRule(this) private JenkinsShellCallRule jscr = new JenkinsShellCallRule(this) @@ -25,21 +27,25 @@ class NewmanExecuteTest extends BasePiperTest { @Rule public RuleChain rules = Rules .getCommonRules(this) + .around(thrown) .around(jedr) .around(jscr) .around(jlr) .around(jsr) // needs to be activated after jedr, otherwise executeDocker is not mocked - def testRepository + def gitMap @Before void init() throws Exception { - helper.registerAllowedMethod('git', [String.class], {s -> - testRepository = s + helper.registerAllowedMethod('stash', [String.class], null) + helper.registerAllowedMethod('git', [Map.class], {m -> + gitMap = m }) helper.registerAllowedMethod("findFiles", [Map.class], { map -> def files - if(map.glob == '**/*.postman_collection.json') + if(map.glob == 'notFound.json') + files = [] + else if(map.glob == '**/*.postman_collection.json') files = [ new File("testCollectionsFolder/A.postman_collection.json"), new File("testCollectionsFolder/B.postman_collection.json") @@ -54,6 +60,7 @@ class NewmanExecuteTest extends BasePiperTest { void testExecuteNewmanDefault() throws Exception { jsr.step.newmanExecute( script: nullScript, + juStabUtils: utils, newmanCollection: 'testCollection', newmanEnvironment: 'testEnvironment', newmanGlobals: 'testGlobals' @@ -61,13 +68,28 @@ class NewmanExecuteTest extends BasePiperTest { // asserts assertThat(jscr.shell, hasItem('newman run testCollection --environment \'testEnvironment\' --globals \'testGlobals\' --reporters junit,html --reporter-junit-export target/newman/TEST-testCollection.xml --reporter-html-export target/newman/TEST-testCollection.html')) assertThat(jedr.dockerParams.dockerImage, is('node:8-stretch')) + assertThat(jlr.log, containsString('[newmanExecute] Found files [testCollection]')) assertJobStatusSuccess() } + @Test + void testExecuteNewmanWithNoCollection() throws Exception { + thrown.expectMessage('[newmanExecute] No collection found with pattern \'notFound.json\'') + + jsr.step.newmanExecute( + script: nullScript, + juStabUtils: utils, + newmanCollection: 'notFound.json' + ) + // asserts + assertJobStatusFailure() + } + @Test void testExecuteNewmanFailOnError() throws Exception { jsr.step.newmanExecute( script: nullScript, + juStabUtils: utils, newmanCollection: 'testCollection', newmanEnvironment: 'testEnvironment', newmanGlobals: 'testGlobals', @@ -77,7 +99,7 @@ class NewmanExecuteTest extends BasePiperTest { ) // asserts assertThat(jedr.dockerParams.dockerImage, is('testImage')) - assertThat(testRepository, is('testRepo')) + assertThat(gitMap.url, is('testRepo')) assertThat(jscr.shell, hasItem('newman run testCollection --environment \'testEnvironment\' --globals \'testGlobals\' --reporters junit,html --reporter-junit-export target/newman/TEST-testCollection.xml --reporter-html-export target/newman/TEST-testCollection.html --suppress-exit-code')) assertJobStatusSuccess() } @@ -86,6 +108,7 @@ class NewmanExecuteTest extends BasePiperTest { void testExecuteNewmanWithFolder() throws Exception { jsr.step.newmanExecute( script: nullScript, + juStabUtils: utils, newmanRunCommand: 'run ${config.newmanCollection} --iteration-data testDataFile --reporters junit,html --reporter-junit-export target/newman/TEST-${config.newmanCollection.toString().replace(File.separatorChar,(char)\'_\').tokenize(\'.\').first()}.xml --reporter-html-export target/newman/TEST-${config.newmanCollection.toString().replace(File.separatorChar,(char)\'_\').tokenize(\'.\').first()}.html' ) // asserts diff --git a/vars/newmanExecute.groovy b/vars/newmanExecute.groovy index 876622641..68be9c2f5 100644 --- a/vars/newmanExecute.groovy +++ b/vars/newmanExecute.groovy @@ -1,3 +1,4 @@ +import com.sap.piper.Utils import com.sap.piper.ConfigurationHelper import groovy.transform.Field @@ -7,10 +8,13 @@ import groovy.text.SimpleTemplateEngine @Field Set STEP_CONFIG_KEYS = [ 'dockerImage', 'failOnError', + 'gitBranch', + 'gitSshKeyCredentialsId', 'newmanCollection', 'newmanEnvironment', 'newmanGlobals', 'newmanRunCommand', + 'stashContent', 'testRepository' ] @Field Set PARAMETER_KEYS = STEP_CONFIG_KEYS @@ -18,6 +22,7 @@ import groovy.text.SimpleTemplateEngine def call(Map parameters = [:]) { handlePipelineStepErrors(stepName: STEP_NAME, stepParameters: parameters) { def script = parameters?.script ?: [commonPipelineEnvironment: commonPipelineEnvironment] + def utils = parameters?.juStabUtils ?: new Utils() // load default & individual configuration Map config = ConfigurationHelper @@ -27,27 +32,40 @@ def call(Map parameters = [:]) { .mixin(parameters, PARAMETER_KEYS) .use() - List collectionList = findFiles(glob: config.newmanCollection)?.toList() + config.stashContent = utils.unstashAll(config.stashContent) - if (!config.dockerImage.isEmpty()) { - if (config.testRepository) - git config.testRepository - dockerExecute( - dockerImage: config.dockerImage - ) { - sh 'npm install newman --global --quiet' - for(String collection : collectionList){ - def collectionDisplayName = collection.toString().replace(File.separatorChar,(char)'_').tokenize('.').first() - // resolve templates - def command = SimpleTemplateEngine.newInstance() - .createTemplate(config.newmanRunCommand) - .make([ - config: config.plus([newmanCollection: collection]), - collectionDisplayName: collectionDisplayName - ]).toString() - if(!config.failOnError) command += ' --suppress-exit-code' - sh "newman ${command}" - } + if (config.testRepository) { + def gitParameters = [url: config.testRepository] + if (config.gitSshKeyCredentialsId) gitParameters.credentialsId = config.gitSshKeyCredentialsId + if (config.gitBranch) gitParameters.branch = config.gitBranch + git gitParameters + stash 'newmanContent' + config.stashContent = ['newmanContent'] + } + + List collectionList = findFiles(glob: config.newmanCollection)?.toList() + if (collectionList.isEmpty()) { + error "[${STEP_NAME}] No collection found with pattern '${config.newmanCollection}'" + } else { + echo "[${STEP_NAME}] Found files ${collectionList}" + } + + dockerExecute( + dockerImage: config.dockerImage, + stashContent: config.stashContent + ) { + sh 'npm install newman --global --quiet' + for(String collection : collectionList){ + def collectionDisplayName = collection.toString().replace(File.separatorChar,(char)'_').tokenize('.').first() + // resolve templates + def command = SimpleTemplateEngine.newInstance() + .createTemplate(config.newmanRunCommand) + .make([ + config: config.plus([newmanCollection: collection]), + collectionDisplayName: collectionDisplayName + ]).toString() + if(!config.failOnError) command += ' --suppress-exit-code' + sh "newman ${command}" } } } From d083ef6e39dfa24258467c91497864a325ed09a0 Mon Sep 17 00:00:00 2001 From: Christopher Fenner Date: Wed, 8 Aug 2018 14:23:29 +0200 Subject: [PATCH 2/2] Update newmanExecute.groovy --- vars/newmanExecute.groovy | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/vars/newmanExecute.groovy b/vars/newmanExecute.groovy index 68be9c2f5..1b3611fd0 100644 --- a/vars/newmanExecute.groovy +++ b/vars/newmanExecute.groovy @@ -32,8 +32,6 @@ def call(Map parameters = [:]) { .mixin(parameters, PARAMETER_KEYS) .use() - config.stashContent = utils.unstashAll(config.stashContent) - if (config.testRepository) { def gitParameters = [url: config.testRepository] if (config.gitSshKeyCredentialsId) gitParameters.credentialsId = config.gitSshKeyCredentialsId @@ -41,6 +39,8 @@ def call(Map parameters = [:]) { git gitParameters stash 'newmanContent' config.stashContent = ['newmanContent'] + } else { + config.stashContent = utils.unstashAll(config.stashContent) } List collectionList = findFiles(glob: config.newmanCollection)?.toList()