1
0
mirror of https://github.com/SAP/jenkins-library.git synced 2024-12-12 10:55:20 +02:00
sap-jenkins-library/test/groovy/BatsExecuteTestsTest.groovy
Christopher Fenner 92441577d8
fix(NPM): change NodeJS image to LTS (#1069)
* change NodeJS image to current LTS

* Update default_pipeline_environment.yml

* Update SonarExecuteScanTest.groovy

* use node:lts-stretch image
2020-01-29 11:17:56 +01:00

172 lines
5.9 KiB
Groovy

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()
private JenkinsStepRule stepRule = new JenkinsStepRule(this)
private JenkinsLoggingRule loggingRule = new JenkinsLoggingRule(this)
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)
.around(shellRule)
.around(loggingRule)
.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']]
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'))
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() {
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('Shell call failed')
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-'))
})
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-'))
})
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'))
}
}