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/BatsExecuteTestsTest.groovy

172 lines
5.9 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.hasItem
import static org.hamcrest.Matchers.is
import static org.hamcrest.Matchers.startsWith
import static org.junit.Assert.assertThat
class BatsExecuteTestsTest extends BasePiperTest {
private ExpectedException thrown = ExpectedException.none()
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)
@Rule
public RuleChain rules = Rules
.getCommonRules(this)
.around(new JenkinsReadYamlRule(this))
.around(thrown)
.around(dockerExecuteRule)
2019-01-22 10:19:28 +02:00
.around(shellRule)
.around(loggingRule)
2019-01-22 10:25:42 +02:00
.around(stepRule)
List withEnvArgs = []
@Before
void init() throws Exception {
helper.registerAllowedMethod("withEnv", [List.class, Closure.class], {arguments, closure ->
arguments.each {arg ->
withEnvArgs.add(arg.toString())
}
return closure()
})
}
@Test
void testDefault() {
nullScript.commonPipelineEnvironment.configuration = [general: [container: 'test-container']]
2019-01-22 10:25:42 +02:00
stepRule.step.batsExecuteTests(
script: nullScript,
juStabUtils: utils,
dockerContainerName: 'test-container',
dockerImageNameAndTag: 'test/image',
envVars: [
IMAGE_NAME: 'test/image',
CONTAINER_NAME: '${commonPipelineEnvironment.configuration.general.container}'
],
testPackage: 'testPackage'
)
// asserts
assertThat(withEnvArgs, hasItem('IMAGE_NAME=test/image'))
assertThat(withEnvArgs, hasItem('CONTAINER_NAME=test-container'))
2019-01-22 10:19:28 +02:00
assertThat(shellRule.shell, hasItem('git clone https://github.com/bats-core/bats-core.git'))
assertThat(shellRule.shell, hasItem('bats-core/bin/bats --recursive --tap src/test > \'TEST-testPackage.tap\''))
assertThat(shellRule.shell, hasItem('cat \'TEST-testPackage.tap\''))
assertThat(dockerExecuteRule.dockerParams.dockerImage, is('node:lts-stretch'))
assertThat(dockerExecuteRule.dockerParams.dockerWorkspace, is('/home/node'))
assertThat(shellRule.shell, hasItem('NPM_CONFIG_PREFIX=~/.npm-global npm install tap-xunit -g'))
assertThat(shellRule.shell, hasItem('cat \'TEST-testPackage.tap\' | PATH=\$PATH:~/.npm-global/bin tap-xunit --package=\'testPackage\' > TEST-testPackage.xml'))
assertJobStatusSuccess()
}
@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:[batsExecuteTests:[
dockerImage: expectedImage,
dockerOptions: expectedOptions,
dockerEnvVars: expectedEnvVars,
dockerWorkspace: expectedWorkspace
]]]
stepRule.step.batsExecuteTests(
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
}
@Test
void testTap() {
2019-01-22 10:25:42 +02:00
stepRule.step.batsExecuteTests(
script: nullScript,
juStabUtils: utils,
outputFormat: 'tap'
)
assertThat(dockerExecuteRule.dockerParams.size(), is(0))
}
@Test
void testFailOnError() {
helper.registerAllowedMethod('sh', [String.class], {s ->
if (s.startsWith('bats-core/bin/bats')) {
throw new Exception('Shell call failed')
} else {
return null
}
})
thrown.expectMessage('ERROR: The execution of the bats tests failed, see the log for details.')
2019-01-22 10:25:42 +02:00
stepRule.step.batsExecuteTests(
script: nullScript,
juStabUtils: utils,
failOnError: true,
)
}
@Test
void testGit() {
def gitRepository
helper.registerAllowedMethod('git', [Map.class], {m ->
gitRepository = m
})
helper.registerAllowedMethod('stash', [String.class], {s ->
assertThat(s, startsWith('testContent-'))
})
2019-01-22 10:25:42 +02:00
stepRule.step.batsExecuteTests(
script: nullScript,
juStabUtils: utils,
testRepository: 'testRepo',
)
assertThat(gitRepository.size(), is(1))
assertThat(gitRepository.url, is('testRepo'))
assertThat(dockerExecuteRule.dockerParams.stashContent, hasItem(startsWith('testContent-')))
}
@Test
void testGitBranchAndCredentials() {
def gitRepository
helper.registerAllowedMethod('git', [Map.class], {m ->
gitRepository = m
})
helper.registerAllowedMethod('stash', [String.class], {s ->
assertThat(s, startsWith('testContent-'))
})
2019-01-22 10:25:42 +02:00
stepRule.step.batsExecuteTests(
script: nullScript,
juStabUtils: utils,
gitBranch: 'test',
gitSshKeyCredentialsId: 'testCredentials',
testRepository: 'testRepo',
)
assertThat(gitRepository.size(), is(3))
assertThat(gitRepository.credentialsId, is('testCredentials'))
assertThat(gitRepository.branch, is('test'))
}
}