2018-07-03 14:39:20 +02:00
|
|
|
import org.junit.Before
|
|
|
|
import org.junit.Test
|
|
|
|
import org.junit.Rule
|
|
|
|
import org.junit.rules.RuleChain
|
|
|
|
|
|
|
|
import static org.hamcrest.Matchers.hasItem
|
|
|
|
import static org.hamcrest.Matchers.is
|
|
|
|
import static org.hamcrest.Matchers.containsString
|
2019-01-18 14:14:39 +02:00
|
|
|
import static org.hamcrest.Matchers.endsWith
|
|
|
|
import static org.hamcrest.Matchers.startsWith
|
2018-07-03 14:39:20 +02:00
|
|
|
|
|
|
|
import static org.junit.Assert.assertThat
|
|
|
|
|
|
|
|
import util.BasePiperTest
|
|
|
|
import util.JenkinsStepRule
|
|
|
|
import util.JenkinsLoggingRule
|
2018-08-31 10:22:43 +02:00
|
|
|
import util.JenkinsReadYamlRule
|
2018-07-03 14:39:20 +02:00
|
|
|
import util.JenkinsShellCallRule
|
|
|
|
import util.JenkinsDockerExecuteRule
|
|
|
|
import util.Rules
|
2018-08-08 11:22:08 +02:00
|
|
|
import org.junit.rules.ExpectedException
|
2018-07-03 14:39:20 +02:00
|
|
|
|
|
|
|
class NewmanExecuteTest extends BasePiperTest {
|
2018-08-08 11:22:08 +02:00
|
|
|
private ExpectedException thrown = ExpectedException.none()
|
2019-01-22 10:25:42 +02:00
|
|
|
private JenkinsStepRule stepRule = new JenkinsStepRule(this)
|
2019-01-22 10:22:15 +02:00
|
|
|
private JenkinsLoggingRule loggingRule = new JenkinsLoggingRule(this)
|
2019-01-22 10:19:28 +02:00
|
|
|
private JenkinsShellCallRule shellRule = new JenkinsShellCallRule(this)
|
2019-01-22 10:34:18 +02:00
|
|
|
private JenkinsDockerExecuteRule dockerExecuteRule = new JenkinsDockerExecuteRule(this)
|
2018-07-03 14:39:20 +02:00
|
|
|
|
|
|
|
@Rule
|
|
|
|
public RuleChain rules = Rules
|
|
|
|
.getCommonRules(this)
|
2018-08-31 10:22:43 +02:00
|
|
|
.around(new JenkinsReadYamlRule(this))
|
2018-08-08 11:22:08 +02:00
|
|
|
.around(thrown)
|
2019-01-22 10:34:18 +02:00
|
|
|
.around(dockerExecuteRule)
|
2019-01-22 10:19:28 +02:00
|
|
|
.around(shellRule)
|
2019-01-22 10:22:15 +02:00
|
|
|
.around(loggingRule)
|
2019-01-22 10:34:18 +02:00
|
|
|
.around(stepRule) // needs to be activated after dockerExecuteRule, otherwise executeDocker is not mocked
|
2018-07-03 14:39:20 +02:00
|
|
|
|
2018-08-08 11:22:08 +02:00
|
|
|
def gitMap
|
2018-07-03 14:39:20 +02:00
|
|
|
|
|
|
|
@Before
|
|
|
|
void init() throws Exception {
|
2018-08-08 11:22:08 +02:00
|
|
|
helper.registerAllowedMethod('stash', [String.class], null)
|
|
|
|
helper.registerAllowedMethod('git', [Map.class], {m ->
|
|
|
|
gitMap = m
|
2018-07-03 14:39:20 +02:00
|
|
|
})
|
|
|
|
helper.registerAllowedMethod("findFiles", [Map.class], { map ->
|
|
|
|
def files
|
2018-08-08 11:22:08 +02:00
|
|
|
if(map.glob == 'notFound.json')
|
|
|
|
files = []
|
|
|
|
else if(map.glob == '**/*.postman_collection.json')
|
2018-07-03 14:39:20 +02:00
|
|
|
files = [
|
|
|
|
new File("testCollectionsFolder/A.postman_collection.json"),
|
|
|
|
new File("testCollectionsFolder/B.postman_collection.json")
|
|
|
|
]
|
|
|
|
else
|
|
|
|
files = [new File(map.glob)]
|
|
|
|
return files.toArray()
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
@Test
|
|
|
|
void testExecuteNewmanDefault() throws Exception {
|
2019-01-22 10:25:42 +02:00
|
|
|
stepRule.step.newmanExecute(
|
2018-07-03 14:39:20 +02:00
|
|
|
script: nullScript,
|
2018-08-08 11:22:08 +02:00
|
|
|
juStabUtils: utils,
|
2018-07-03 14:39:20 +02:00
|
|
|
newmanCollection: 'testCollection',
|
|
|
|
newmanEnvironment: 'testEnvironment',
|
|
|
|
newmanGlobals: 'testGlobals'
|
|
|
|
)
|
|
|
|
// asserts
|
2019-01-22 10:19:28 +02:00
|
|
|
assertThat(shellRule.shell, hasItem(endsWith('npm install newman newman-reporter-html --global --quiet')))
|
|
|
|
assertThat(shellRule.shell, hasItem(endsWith('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\'')))
|
2019-01-22 10:34:18 +02:00
|
|
|
assertThat(dockerExecuteRule.dockerParams.dockerImage, is('node:8-stretch'))
|
2019-01-22 10:22:15 +02:00
|
|
|
assertThat(loggingRule.log, containsString('[newmanExecute] Found files [testCollection]'))
|
2018-07-03 14:39:20 +02:00
|
|
|
assertJobStatusSuccess()
|
|
|
|
}
|
|
|
|
|
2019-07-25 11:57:21 +02:00
|
|
|
@Test
|
|
|
|
void testDockerFromCustomStepConfiguration() {
|
|
|
|
|
|
|
|
def expectedImage = 'image:test'
|
|
|
|
def expectedEnvVars = ['env1': 'value1', 'env2': 'value2']
|
|
|
|
def expectedOptions = '--opt1=val1 --opt2=val2 --opt3'
|
|
|
|
def expectedWorkspace = '/path/to/workspace'
|
|
|
|
|
|
|
|
nullScript.commonPipelineEnvironment.configuration = [steps:[newmanExecute:[
|
|
|
|
dockerImage: expectedImage,
|
|
|
|
dockerOptions: expectedOptions,
|
|
|
|
dockerEnvVars: expectedEnvVars,
|
|
|
|
dockerWorkspace: expectedWorkspace
|
|
|
|
]]]
|
|
|
|
|
|
|
|
stepRule.step.newmanExecute(
|
|
|
|
script: nullScript,
|
|
|
|
juStabUtils: utils
|
|
|
|
)
|
|
|
|
|
|
|
|
assert expectedImage == dockerExecuteRule.dockerParams.dockerImage
|
|
|
|
assert expectedOptions == dockerExecuteRule.dockerParams.dockerOptions
|
|
|
|
assert expectedEnvVars.equals(dockerExecuteRule.dockerParams.dockerEnvVars)
|
|
|
|
assert expectedWorkspace == dockerExecuteRule.dockerParams.dockerWorkspace
|
|
|
|
}
|
|
|
|
|
2019-01-18 14:14:39 +02:00
|
|
|
@Test
|
|
|
|
void testGlobalInstall() throws Exception {
|
2019-01-22 10:25:42 +02:00
|
|
|
stepRule.step.newmanExecute(
|
2019-01-18 14:14:39 +02:00
|
|
|
script: nullScript,
|
|
|
|
juStabUtils: utils,
|
|
|
|
newmanCollection: 'testCollection',
|
|
|
|
newmanEnvironment: 'testEnvironment',
|
|
|
|
newmanGlobals: 'testGlobals'
|
|
|
|
)
|
|
|
|
// asserts
|
2019-01-22 10:19:28 +02:00
|
|
|
assertThat(shellRule.shell, hasItem(startsWith('NPM_CONFIG_PREFIX=~/.npm-global ')))
|
|
|
|
assertThat(shellRule.shell, hasItem(startsWith('PATH=$PATH:~/.npm-global/bin')))
|
2019-01-18 14:14:39 +02:00
|
|
|
assertJobStatusSuccess()
|
|
|
|
}
|
|
|
|
|
2018-08-08 11:22:08 +02:00
|
|
|
@Test
|
|
|
|
void testExecuteNewmanWithNoCollection() throws Exception {
|
|
|
|
thrown.expectMessage('[newmanExecute] No collection found with pattern \'notFound.json\'')
|
|
|
|
|
2019-01-22 10:25:42 +02:00
|
|
|
stepRule.step.newmanExecute(
|
2018-08-08 11:22:08 +02:00
|
|
|
script: nullScript,
|
|
|
|
juStabUtils: utils,
|
|
|
|
newmanCollection: 'notFound.json'
|
|
|
|
)
|
|
|
|
// asserts
|
|
|
|
assertJobStatusFailure()
|
|
|
|
}
|
|
|
|
|
2018-07-03 14:39:20 +02:00
|
|
|
@Test
|
|
|
|
void testExecuteNewmanFailOnError() throws Exception {
|
2019-01-22 10:25:42 +02:00
|
|
|
stepRule.step.newmanExecute(
|
2018-07-03 14:39:20 +02:00
|
|
|
script: nullScript,
|
2018-08-08 11:22:08 +02:00
|
|
|
juStabUtils: utils,
|
2018-07-03 14:39:20 +02:00
|
|
|
newmanCollection: 'testCollection',
|
|
|
|
newmanEnvironment: 'testEnvironment',
|
|
|
|
newmanGlobals: 'testGlobals',
|
|
|
|
dockerImage: 'testImage',
|
|
|
|
testRepository: 'testRepo',
|
|
|
|
failOnError: false
|
|
|
|
)
|
|
|
|
// asserts
|
2019-01-22 10:34:18 +02:00
|
|
|
assertThat(dockerExecuteRule.dockerParams.dockerImage, is('testImage'))
|
2018-08-08 11:22:08 +02:00
|
|
|
assertThat(gitMap.url, is('testRepo'))
|
2019-01-22 10:19:28 +02:00
|
|
|
assertThat(shellRule.shell, hasItem(endsWith('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')))
|
2018-07-03 14:39:20 +02:00
|
|
|
assertJobStatusSuccess()
|
|
|
|
}
|
|
|
|
|
|
|
|
@Test
|
|
|
|
void testExecuteNewmanWithFolder() throws Exception {
|
2019-01-22 10:25:42 +02:00
|
|
|
stepRule.step.newmanExecute(
|
2018-07-03 14:39:20 +02:00
|
|
|
script: nullScript,
|
2018-08-08 11:22:08 +02:00
|
|
|
juStabUtils: utils,
|
2018-07-10 12:02:58 +02:00
|
|
|
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'
|
2018-07-03 14:39:20 +02:00
|
|
|
)
|
|
|
|
// asserts
|
2019-01-22 10:19:28 +02:00
|
|
|
assertThat(shellRule.shell, hasItem(endsWith('newman run testCollectionsFolder'+File.separatorChar+'A.postman_collection.json --iteration-data testDataFile --reporters junit,html --reporter-junit-export target/newman/TEST-testCollectionsFolder_A.xml --reporter-html-export target/newman/TEST-testCollectionsFolder_A.html')))
|
|
|
|
assertThat(shellRule.shell, hasItem(endsWith('newman run testCollectionsFolder'+File.separatorChar+'B.postman_collection.json --iteration-data testDataFile --reporters junit,html --reporter-junit-export target/newman/TEST-testCollectionsFolder_B.xml --reporter-html-export target/newman/TEST-testCollectionsFolder_B.html')))
|
2018-07-03 14:39:20 +02:00
|
|
|
assertJobStatusSuccess()
|
|
|
|
}
|
|
|
|
}
|