1
0
mirror of https://github.com/SAP/jenkins-library.git synced 2024-12-14 11:03:09 +02:00
sap-jenkins-library/test/groovy/GaugeExecuteTestsTest.groovy

165 lines
6.1 KiB
Groovy
Raw Normal View History

import org.junit.Before
import org.junit.Rule
import org.junit.Test
import org.junit.rules.ExpectedException
import org.junit.rules.RuleChain
import util.*
import static org.hamcrest.Matchers.*
import static org.junit.Assert.assertThat
class GaugeExecuteTestsTest extends BasePiperTest {
2019-01-22 10:25:42 +02:00
private JenkinsStepRule stepRule = new JenkinsStepRule(this)
private JenkinsLoggingRule loggingRule = new JenkinsLoggingRule(this)
2019-01-22 10:19:28 +02:00
private JenkinsShellCallRule shellRule = new JenkinsShellCallRule(this)
private JenkinsDockerExecuteRule dockerExecuteRule = new JenkinsDockerExecuteRule(this)
2019-01-22 10:27:45 +02:00
private JenkinsEnvironmentRule environmentRule = new JenkinsEnvironmentRule(this)
private ExpectedException thrown = ExpectedException.none()
@Rule
public RuleChain rules = Rules
.getCommonRules(this)
.around(new JenkinsReadYamlRule(this))
2019-01-22 10:19:28 +02:00
.around(shellRule)
.around(loggingRule)
.around(dockerExecuteRule)
2019-01-22 10:27:45 +02:00
.around(environmentRule)
2019-01-22 10:25:42 +02:00
.around(stepRule)
.around(thrown)
def gitParams = [:]
def seleniumParams = [:]
@Before
void init() throws Exception {
helper.registerAllowedMethod("git", [Map.class], { map -> gitParams = map })
helper.registerAllowedMethod("unstash", [String.class], { s -> return [s]})
helper.registerAllowedMethod('seleniumExecuteTests', [Map.class, Closure.class], {map, body ->
seleniumParams = map
return body()
})
}
@Test
void testExecuteGaugeDefaultSuccess() throws Exception {
2019-01-22 10:25:42 +02:00
stepRule.step.gaugeExecuteTests(
script: nullScript,
juStabUtils: utils,
testServerUrl: 'http://test.url'
)
2019-01-22 10:19:28 +02:00
assertThat(shellRule.shell, hasItem(stringContainsInOrder([
'export HOME=${HOME:-$(pwd)}',
'if [ "$HOME" = "/" ]; then export HOME=$(pwd); fi',
'export PATH=$HOME/bin/gauge:$PATH',
'mkdir -p $HOME/bin/gauge',
'curl -SsL https://downloads.gauge.org/stable | sh -s -- --location=$HOME/bin/gauge',
'gauge telemetry off',
'gauge install java',
'gauge install html-report',
'gauge install xml-report',
'mvn test-compile gauge:execute -DspecsDir=specs'
])))
assertThat(seleniumParams.dockerImage, is('maven:3.5-jdk-8'))
assertThat(seleniumParams.dockerEnvVars, hasEntry('TARGET_SERVER_URL', 'http://test.url'))
assertThat(seleniumParams.dockerName, is('maven'))
assertThat(seleniumParams.dockerWorkspace, is(''))
assertThat(seleniumParams.stashContent, hasSize(2))
assertThat(seleniumParams.stashContent, allOf(hasItem('buildDescriptor'), hasItem('tests')))
assertJobStatusSuccess()
}
@Test
void testDockerFromCustomStepConfiguration() {
def expectedImage = 'image:test'
def expectedEnvVars = ['HUB':'', 'HUB_URL':'', 'env1': 'value1', 'env2': 'value2']
def expectedOptions = '--opt1=val1 --opt2=val2 --opt3'
def expectedWorkspace = '/path/to/workspace'
nullScript.commonPipelineEnvironment.configuration = [steps:[gaugeExecuteTests:[
dockerImage: expectedImage,
dockerOptions: expectedOptions,
dockerEnvVars: expectedEnvVars,
dockerWorkspace: expectedWorkspace
]]]
stepRule.step.gaugeExecuteTests(
script: nullScript,
juStabUtils: utils
)
assert expectedImage == seleniumParams.dockerImage
assert expectedOptions == seleniumParams.dockerOptions
assert expectedEnvVars.equals(seleniumParams.dockerEnvVars)
assert expectedWorkspace == seleniumParams.dockerWorkspace
}
@Test
void testExecuteGaugeNode() throws Exception {
2019-01-22 10:25:42 +02:00
stepRule.step.gaugeExecuteTests(
script: nullScript,
buildTool: 'npm',
dockerEnvVars: ['TARGET_SERVER_URL':'http://custom.url'],
juStabUtils: utils,
testOptions: 'testSpec'
)
2019-01-22 10:19:28 +02:00
assertThat(shellRule.shell, hasItem(stringContainsInOrder([
'gauge install js',
'gauge run testSpec'
])))
assertThat(seleniumParams.dockerImage, is('node:8-stretch'))
assertThat(seleniumParams.dockerEnvVars, hasEntry('TARGET_SERVER_URL', 'http://custom.url'))
assertThat(seleniumParams.dockerName, is('npm'))
assertThat(seleniumParams.dockerWorkspace, is('/home/node'))
assertJobStatusSuccess()
}
@Test
void testExecuteCustomWithError() throws Exception {
helper.registerAllowedMethod("sh", [String.class], { s ->
throw new RuntimeException('Test Error')
})
thrown.expect(RuntimeException)
thrown.expectMessage('Test Error')
try {
2019-01-22 10:25:42 +02:00
stepRule.step.gaugeExecuteTests(
script: nullScript,
juStabUtils: utils,
dockerImage: 'testImage',
dockerName: 'testImageName',
dockerWorkspace: '/home/test',
failOnError: true,
stashContent: ['testStash'],
)
} finally{
assertThat(seleniumParams.dockerImage, is('testImage'))
assertThat(seleniumParams.dockerName, is('testImageName'))
assertThat(seleniumParams.dockerWorkspace, is('/home/test'))
assertThat(seleniumParams.stashContent, hasSize(1))
assertThat(loggingRule.log, containsString('[gaugeExecuteTests] One or more tests failed'))
assertThat(nullScript.currentBuild.result, is('UNSTABLE'))
}
}
@Test
void testExecuteGaugeCustomRepo() throws Exception {
helper.registerAllowedMethod('git', [String.class], null)
helper.registerAllowedMethod('stash', [String.class], null)
2019-01-22 10:25:42 +02:00
stepRule.step.gaugeExecuteTests(
script: nullScript,
juStabUtils: utils,
testRepository: 'myTestRepo',
failOnError: true
)
// nested matchers do not work correctly
assertThat(seleniumParams.stashContent, hasItem(startsWith('testContent-')))
assertJobStatusSuccess()
}
}