2018-08-15 11:41:01 +02:00
|
|
|
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
|
2018-10-08 11:30:42 +02:00
|
|
|
import static org.hamcrest.Matchers.startsWith
|
2018-08-15 11:41:01 +02:00
|
|
|
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)
|
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:26:08 +02:00
|
|
|
private JenkinsDockerExecuteRule dockerExecuteRule = new JenkinsDockerExecuteRule(this)
|
2018-08-15 11:41:01 +02:00
|
|
|
|
|
|
|
@Rule
|
|
|
|
public RuleChain rules = Rules
|
|
|
|
.getCommonRules(this)
|
2018-08-31 10:22:43 +02:00
|
|
|
.around(new JenkinsReadYamlRule(this))
|
2018-08-15 11:41:01 +02:00
|
|
|
.around(thrown)
|
2019-01-22 10:26:08 +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:25:42 +02:00
|
|
|
.around(stepRule)
|
2018-08-15 11:41:01 +02:00
|
|
|
|
|
|
|
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(
|
2018-08-15 11:41:01 +02:00
|
|
|
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\''))
|
2018-08-15 11:41:01 +02:00
|
|
|
|
2020-01-29 12:17:56 +02:00
|
|
|
assertThat(dockerExecuteRule.dockerParams.dockerImage, is('node:lts-stretch'))
|
2019-01-22 10:26:08 +02:00
|
|
|
assertThat(dockerExecuteRule.dockerParams.dockerWorkspace, is('/home/node'))
|
2018-08-15 11:41:01 +02:00
|
|
|
|
2019-07-05 16:32:24 +02:00
|
|
|
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'))
|
2018-08-15 11:41:01 +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:[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
|
|
|
|
}
|
|
|
|
|
2018-08-15 11:41:01 +02:00
|
|
|
@Test
|
|
|
|
void testTap() {
|
2019-01-22 10:25:42 +02:00
|
|
|
stepRule.step.batsExecuteTests(
|
2018-08-15 11:41:01 +02:00
|
|
|
script: nullScript,
|
|
|
|
juStabUtils: utils,
|
|
|
|
outputFormat: 'tap'
|
|
|
|
)
|
2019-01-22 10:26:08 +02:00
|
|
|
assertThat(dockerExecuteRule.dockerParams.size(), is(0))
|
2018-08-15 11:41:01 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@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')
|
2019-01-22 10:25:42 +02:00
|
|
|
stepRule.step.batsExecuteTests(
|
2018-08-15 11:41:01 +02:00
|
|
|
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 ->
|
2018-10-08 11:30:42 +02:00
|
|
|
assertThat(s, startsWith('testContent-'))
|
2018-08-15 11:41:01 +02:00
|
|
|
})
|
|
|
|
|
2019-01-22 10:25:42 +02:00
|
|
|
stepRule.step.batsExecuteTests(
|
2018-08-15 11:41:01 +02:00
|
|
|
script: nullScript,
|
|
|
|
juStabUtils: utils,
|
|
|
|
testRepository: 'testRepo',
|
|
|
|
)
|
|
|
|
|
|
|
|
assertThat(gitRepository.size(), is(1))
|
|
|
|
assertThat(gitRepository.url, is('testRepo'))
|
2019-01-22 10:26:08 +02:00
|
|
|
assertThat(dockerExecuteRule.dockerParams.stashContent, hasItem(startsWith('testContent-')))
|
2018-08-15 11:41:01 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@Test
|
|
|
|
void testGitBranchAndCredentials() {
|
|
|
|
def gitRepository
|
|
|
|
helper.registerAllowedMethod('git', [Map.class], {m ->
|
|
|
|
gitRepository = m
|
|
|
|
})
|
|
|
|
helper.registerAllowedMethod('stash', [String.class], {s ->
|
2018-10-08 11:30:42 +02:00
|
|
|
assertThat(s, startsWith('testContent-'))
|
2018-08-15 11:41:01 +02:00
|
|
|
})
|
|
|
|
|
2019-01-22 10:25:42 +02:00
|
|
|
stepRule.step.batsExecuteTests(
|
2018-08-15 11:41:01 +02:00
|
|
|
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'))
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
}
|