From 34b675a030e10780dc5a818f57aa7f56705896fc Mon Sep 17 00:00:00 2001 From: Christopher Fenner Date: Mon, 8 Oct 2018 11:30:42 +0200 Subject: [PATCH] handle test repositories (#324) * handle test repositories * use GitUtils * add test case * fix test cases * return stash name * handle seleniumExecuteTests step * Revert "handle seleniumExecuteTests step" This reverts commit 2b33d274fe48ee4d294189bf55d007ba15bc2824. * handle seleniumExecuteTests step * add import of GitUtils --- src/com/sap/piper/GitUtils.groovy | 15 +++++++ test/groovy/BatsExecuteTestsTest.groovy | 7 ++-- test/groovy/SeleniumExecuteTestsTest.groovy | 1 + test/groovy/com/sap/piper/GitUtilsTest.groovy | 42 ++++++++++++++++--- vars/batsExecuteTests.groovy | 19 +++------ vars/newmanExecute.groovy | 17 +++----- vars/seleniumExecuteTests.groovy | 12 ++---- 7 files changed, 71 insertions(+), 42 deletions(-) diff --git a/src/com/sap/piper/GitUtils.groovy b/src/com/sap/piper/GitUtils.groovy index cb41f911c..d3c67e4d6 100644 --- a/src/com/sap/piper/GitUtils.groovy +++ b/src/com/sap/piper/GitUtils.groovy @@ -37,3 +37,18 @@ String[] extractLogLines(String filter = '', ?.findAll { line -> line ==~ /${filter}/ } } + +static String handleTestRepository(Script steps, Map config){ + def stashName = "testContent-${UUID.randomUUID()}".toString() + def options = [url: config.testRepository] + if (config.gitSshKeyCredentialsId) + options.put('credentialsId', config.gitSshKeyCredentialsId) + if (config.gitBranch) + options.put('branch', config.gitBranch) + // checkout test repository + steps.git options + // stash test content + steps.stash stashName + // return stash name + return stashName +} diff --git a/test/groovy/BatsExecuteTestsTest.groovy b/test/groovy/BatsExecuteTestsTest.groovy index 1616ca92a..0e8bd3350 100644 --- a/test/groovy/BatsExecuteTestsTest.groovy +++ b/test/groovy/BatsExecuteTestsTest.groovy @@ -7,6 +7,7 @@ import util.* import static org.hamcrest.Matchers.hasItem import static org.hamcrest.Matchers.is +import static org.hamcrest.Matchers.startsWith import static org.junit.Assert.assertThat class BatsExecuteTestsTest extends BasePiperTest { @@ -104,7 +105,7 @@ class BatsExecuteTestsTest extends BasePiperTest { gitRepository = m }) helper.registerAllowedMethod('stash', [String.class], {s -> - assertThat(s, is('batsTests')) + assertThat(s, startsWith('testContent-')) }) jsr.step.batsExecuteTests( @@ -115,7 +116,7 @@ class BatsExecuteTestsTest extends BasePiperTest { assertThat(gitRepository.size(), is(1)) assertThat(gitRepository.url, is('testRepo')) - assertThat(jder.dockerParams.stashContent, hasItem('batsTests')) + assertThat(jder.dockerParams.stashContent, hasItem(startsWith('testContent-'))) } @Test @@ -125,7 +126,7 @@ class BatsExecuteTestsTest extends BasePiperTest { gitRepository = m }) helper.registerAllowedMethod('stash', [String.class], {s -> - assertThat(s, is('batsTests')) + assertThat(s, startsWith('testContent-')) }) jsr.step.batsExecuteTests( diff --git a/test/groovy/SeleniumExecuteTestsTest.groovy b/test/groovy/SeleniumExecuteTestsTest.groovy index ec9f10e8f..1b73081cd 100644 --- a/test/groovy/SeleniumExecuteTestsTest.groovy +++ b/test/groovy/SeleniumExecuteTestsTest.groovy @@ -31,6 +31,7 @@ class SeleniumExecuteTestsTest extends BasePiperTest { @Before void init() throws Exception { bodyExecuted = false + helper.registerAllowedMethod('stash', [String.class], null) helper.registerAllowedMethod('git', [Map.class], {m -> gitMap = m }) diff --git a/test/groovy/com/sap/piper/GitUtilsTest.groovy b/test/groovy/com/sap/piper/GitUtilsTest.groovy index 121cc2c76..3b23df1a4 100644 --- a/test/groovy/com/sap/piper/GitUtilsTest.groovy +++ b/test/groovy/com/sap/piper/GitUtilsTest.groovy @@ -1,20 +1,26 @@ package com.sap.piper +import static org.hamcrest.Matchers.equalTo +import static org.hamcrest.Matchers.hasEntry +import static org.hamcrest.Matchers.hasItem +import static org.hamcrest.Matchers.is +import static org.hamcrest.Matchers.notNullValue +import static org.hamcrest.Matchers.startsWith + import org.junit.Before import org.junit.Rule import org.junit.Test import org.junit.rules.ExpectedException import org.junit.rules.RuleChain + import util.BasePiperTest +import util.JenkinsLoggingRule import util.JenkinsShellCallRule import util.Rules import static org.junit.Assert.assertEquals -import static org.hamcrest.Matchers.equalTo import static org.junit.Assert.assertTrue import static org.junit.Assert.assertFalse -import static org.hamcrest.Matchers.is -import static org.hamcrest.Matchers.notNullValue import static org.junit.Assert.assertNotNull import static org.junit.Assert.assertNull import static org.junit.Assert.assertThat @@ -26,11 +32,15 @@ class GitUtilsTest extends BasePiperTest { @Autowired GitUtils gitUtils - JenkinsShellCallRule jscr = new JenkinsShellCallRule(this) - ExpectedException thrown = ExpectedException.none() + private JenkinsLoggingRule jlr = new JenkinsLoggingRule(this) + private JenkinsShellCallRule jscr = new JenkinsShellCallRule(this) + private ExpectedException thrown = ExpectedException.none() @Rule - public RuleChain ruleChain = Rules.getCommonRules(this).around(jscr).around(thrown) + public RuleChain ruleChain = Rules.getCommonRules(this) + .around(jlr) + .around(jscr) + .around(thrown) @Before void init() throws Exception { @@ -96,4 +106,24 @@ class GitUtilsTest extends BasePiperTest { assertNotNull(log) assertThat(log.size(),is(equalTo(0))) } + + @Test + void testHandleTestRepository() { + def result, gitMap, stashName, config = [ + testRepository: 'repoUrl', + gitSshKeyCredentialsId: 'abc', + gitBranch: 'master' + ] + + helper.registerAllowedMethod('git', [Map.class], {m -> gitMap = m }) + helper.registerAllowedMethod("stash", [String.class], { s -> stashName = s}) + + result = GitUtils.handleTestRepository(nullScript, config) + // asserts + assertThat(gitMap, hasEntry('url', config.testRepository)) + assertThat(gitMap, hasEntry('credentialsId', config.gitSshKeyCredentialsId)) + assertThat(gitMap, hasEntry('branch', config.gitBranch)) + assertThat(stashName, startsWith('testContent-')) + assertThat(result, startsWith('testContent-')) + } } diff --git a/vars/batsExecuteTests.groovy b/vars/batsExecuteTests.groovy index c9bf894bd..67a8e3c1c 100644 --- a/vars/batsExecuteTests.groovy +++ b/vars/batsExecuteTests.groovy @@ -1,11 +1,12 @@ -import com.sap.piper.Utils import com.sap.piper.ConfigurationHelper +import com.sap.piper.GitUtils +import com.sap.piper.Utils import groovy.text.SimpleTemplateEngine import groovy.transform.Field @Field String STEP_NAME = 'batsExecuteTests' @Field Set STEP_CONFIG_KEYS = [ - 'dockerImage', // + 'dockerImage', 'dockerWorkspace', 'envVars', 'failOnError', @@ -39,17 +40,9 @@ def call(Map parameters = [:]) { script.commonPipelineEnvironment.setInfluxStepData('bats', false) - - if (config.testRepository) { - def gitParameters = [url: config.testRepository] - if (config.gitSshKeyCredentialsId?.length()>0) gitParameters.credentialsId = config.gitSshKeyCredentialsId - if (config.gitBranch?.length()>0) gitParameters.branch = config.gitBranch - git gitParameters - stash 'batsTests' - config.stashContent = ['batsTests'] - } else { - config.stashContent = utils.unstashAll(config.stashContent) - } + config.stashContent = config.testRepository + ?[GitUtils.handleTestRepository(this, config)] + :utils.unstashAll(config.stashContent) //resolve commonPipelineEnvironment references in envVars config.envVarList = [] diff --git a/vars/newmanExecute.groovy b/vars/newmanExecute.groovy index 543ce4a96..00a78c40a 100644 --- a/vars/newmanExecute.groovy +++ b/vars/newmanExecute.groovy @@ -1,8 +1,8 @@ -import com.sap.piper.Utils import com.sap.piper.ConfigurationHelper +import com.sap.piper.GitUtils import com.sap.piper.Utils -import groovy.transform.Field import groovy.text.SimpleTemplateEngine +import groovy.transform.Field @Field String STEP_NAME = 'newmanExecute' @Field Set STEP_CONFIG_KEYS = [ @@ -35,16 +35,9 @@ def call(Map parameters = [:]) { new Utils().pushToSWA([step: STEP_NAME], config) - 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'] - } else { - config.stashContent = utils.unstashAll(config.stashContent) - } + config.stashContent = config.testRepository + ?[GitUtils.handleTestRepository(this, config)] + :utils.unstashAll(config.stashContent) List collectionList = findFiles(glob: config.newmanCollection)?.toList() if (collectionList.isEmpty()) { diff --git a/vars/seleniumExecuteTests.groovy b/vars/seleniumExecuteTests.groovy index 0404839ca..79895eae3 100644 --- a/vars/seleniumExecuteTests.groovy +++ b/vars/seleniumExecuteTests.groovy @@ -1,5 +1,6 @@ import com.sap.piper.Utils import com.sap.piper.ConfigurationHelper +import com.sap.piper.GitUtils import com.sap.piper.Utils import com.sap.piper.k8s.ContainerMap import groovy.transform.Field @@ -54,14 +55,9 @@ def call(Map parameters = [:], Closure body) { sidecarVolumeBind: config.sidecarVolumeBind ) { try { - if (config.testRepository) { - def gitParameters = [url: config.testRepository] - if (config.gitSshKeyCredentialsId) gitParameters.credentialsId = config.gitSshKeyCredentialsId - if (config.gitBranch) gitParameters.branch = config.gitBranch - git gitParameters - } else { - config.stashContent = utils.unstashAll(config.stashContent) - } + config.stashContent = config.testRepository + ?[GitUtils.handleTestRepository(this, config)] + :utils.unstashAll(config.stashContent) body() } catch (err) { if (config.failOnError) {