mirror of
https://github.com/SAP/jenkins-library.git
synced 2026-06-19 22:58:55 +02:00
Merge pull request #241 from SAP/CCFenner/newman
newmanExecute: handle private test repositories
This commit is contained in:
@@ -139,6 +139,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:
|
||||
|
||||
@@ -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
|
||||
|
||||
+38
-20
@@ -1,3 +1,4 @@
|
||||
import com.sap.piper.Utils
|
||||
import com.sap.piper.ConfigurationHelper
|
||||
import com.sap.piper.Utils
|
||||
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
|
||||
@@ -30,27 +35,40 @@ def call(Map parameters = [:]) {
|
||||
|
||||
new Utils().pushToSWA([step: STEP_NAME], config)
|
||||
|
||||
List collectionList = findFiles(glob: config.newmanCollection)?.toList()
|
||||
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)
|
||||
}
|
||||
|
||||
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}"
|
||||
}
|
||||
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}"
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user