2017-12-06 13:03:06 +02:00
|
|
|
|
|
|
|
import org.junit.Before
|
2018-01-16 10:33:13 +02:00
|
|
|
import org.junit.Rule
|
2017-12-06 13:03:06 +02:00
|
|
|
import org.junit.Test
|
2018-01-16 10:33:13 +02:00
|
|
|
import org.junit.rules.RuleChain
|
|
|
|
|
2018-01-16 18:06:25 +02:00
|
|
|
import com.lesfurets.jenkins.unit.BasePipelineTest
|
|
|
|
|
2018-01-16 10:33:13 +02:00
|
|
|
import util.JenkinsLoggingRule
|
2018-01-26 15:55:15 +02:00
|
|
|
import util.Rules
|
2018-02-28 14:12:03 +02:00
|
|
|
import util.JenkinsStepRule
|
|
|
|
import util.JenkinsEnvironmentRule
|
2017-12-06 13:03:06 +02:00
|
|
|
|
|
|
|
import static org.junit.Assert.assertEquals
|
|
|
|
import static org.junit.Assert.assertTrue
|
2017-12-15 17:41:10 +02:00
|
|
|
import static org.junit.Assert.assertFalse
|
2017-12-06 13:03:06 +02:00
|
|
|
|
2018-01-16 18:06:25 +02:00
|
|
|
class DockerExecuteTest extends BasePipelineTest {
|
2018-03-02 11:54:50 +02:00
|
|
|
private DockerMock docker
|
|
|
|
|
2018-01-16 10:33:13 +02:00
|
|
|
private JenkinsLoggingRule jlr = new JenkinsLoggingRule(this)
|
2018-02-28 14:12:03 +02:00
|
|
|
private JenkinsStepRule jsr = new JenkinsStepRule(this)
|
|
|
|
private JenkinsEnvironmentRule jer = new JenkinsEnvironmentRule(this)
|
2018-01-16 10:33:13 +02:00
|
|
|
|
|
|
|
@Rule
|
2018-02-28 14:12:03 +02:00
|
|
|
public RuleChain ruleChain = Rules
|
|
|
|
.getCommonRules(this)
|
|
|
|
.around(jlr)
|
|
|
|
.around(jsr)
|
2018-03-02 11:55:27 +02:00
|
|
|
|
|
|
|
int whichDockerReturnValue = 0
|
|
|
|
def bodyExecuted
|
2017-12-06 13:03:06 +02:00
|
|
|
|
|
|
|
@Before
|
2018-01-16 10:33:13 +02:00
|
|
|
void init() {
|
2018-01-16 18:06:25 +02:00
|
|
|
bodyExecuted = false
|
2017-12-06 13:03:06 +02:00
|
|
|
docker = new DockerMock()
|
|
|
|
binding.setVariable('docker', docker)
|
2017-12-13 11:58:22 +02:00
|
|
|
binding.setVariable('Jenkins', [instance: [pluginManager: [plugins: [new PluginMock()]]]])
|
2017-12-06 13:03:06 +02:00
|
|
|
|
2017-12-15 17:41:10 +02:00
|
|
|
helper.registerAllowedMethod('sh', [Map.class], {return whichDockerReturnValue})
|
2017-12-06 13:03:06 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@Test
|
|
|
|
void testExecuteInsideDocker() throws Exception {
|
2018-01-16 18:06:25 +02:00
|
|
|
|
2018-02-28 14:12:03 +02:00
|
|
|
jsr.step.call(script: [commonPipelineEnvironment: jer.env],
|
2018-01-16 18:06:25 +02:00
|
|
|
dockerImage: 'maven:3.5-jdk-8-alpine') {
|
|
|
|
bodyExecuted = true
|
|
|
|
}
|
|
|
|
|
2017-12-06 13:03:06 +02:00
|
|
|
assertEquals('maven:3.5-jdk-8-alpine', docker.getImageName())
|
|
|
|
assertTrue(docker.isImagePulled())
|
|
|
|
assertEquals(' --env http_proxy --env https_proxy --env no_proxy --env HTTP_PROXY --env HTTPS_PROXY --env NO_PROXY', docker.getParameters())
|
2018-01-16 18:06:25 +02:00
|
|
|
assertTrue(bodyExecuted)
|
2017-12-06 13:03:06 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@Test
|
|
|
|
void testExecuteInsideDockerWithParameters() throws Exception {
|
|
|
|
|
2018-02-28 14:12:03 +02:00
|
|
|
jsr.step.call(script: [commonPipelineEnvironment: jer.env],
|
2018-01-16 18:06:25 +02:00
|
|
|
dockerImage: 'maven:3.5-jdk-8-alpine',
|
|
|
|
dockerOptions: '-it',
|
|
|
|
dockerVolumeBind: ['my_vol': '/my_vol'],
|
|
|
|
dockerEnvVars: ['http_proxy': 'http://proxy:8000']) {
|
|
|
|
bodyExecuted = true
|
|
|
|
}
|
2017-12-06 13:03:06 +02:00
|
|
|
assertTrue(docker.getParameters().contains(' --env https_proxy '))
|
|
|
|
assertTrue(docker.getParameters().contains(' --env http_proxy=http://proxy:8000'))
|
|
|
|
assertTrue(docker.getParameters().contains(' -it'))
|
|
|
|
assertTrue(docker.getParameters().contains(' --volume my_vol:/my_vol'))
|
2018-01-16 18:06:25 +02:00
|
|
|
assertTrue(bodyExecuted)
|
2017-12-06 13:03:06 +02:00
|
|
|
}
|
|
|
|
|
2018-01-16 16:42:11 +02:00
|
|
|
@Test
|
|
|
|
void testDockerNotInstalledResultsInLocalExecution() throws Exception {
|
2017-12-15 17:41:10 +02:00
|
|
|
|
|
|
|
whichDockerReturnValue = 1
|
|
|
|
|
2018-02-28 14:12:03 +02:00
|
|
|
jsr.step.call(script: [commonPipelineEnvironment: jer.env],
|
2018-01-16 18:06:25 +02:00
|
|
|
dockerImage: 'maven:3.5-jdk-8-alpine',
|
|
|
|
dockerOptions: '-it',
|
|
|
|
dockerVolumeBind: ['my_vol': '/my_vol'],
|
|
|
|
dockerEnvVars: ['http_proxy': 'http://proxy:8000']) {
|
|
|
|
bodyExecuted = true
|
|
|
|
}
|
|
|
|
|
2018-01-16 10:33:13 +02:00
|
|
|
assertTrue(jlr.log.contains('No docker environment found'))
|
|
|
|
assertTrue(jlr.log.contains('Running on local environment'))
|
2018-01-16 18:06:25 +02:00
|
|
|
assertTrue(bodyExecuted)
|
2017-12-15 17:41:10 +02:00
|
|
|
assertFalse(docker.isImagePulled())
|
|
|
|
}
|
2017-12-06 13:03:06 +02:00
|
|
|
|
|
|
|
private class DockerMock {
|
|
|
|
private String imageName
|
|
|
|
private boolean imagePulled = false
|
|
|
|
private String parameters
|
|
|
|
|
|
|
|
DockerMock image(String imageName) {
|
|
|
|
this.imageName = imageName
|
|
|
|
return this
|
|
|
|
}
|
|
|
|
|
|
|
|
void pull() {
|
|
|
|
imagePulled = true
|
|
|
|
}
|
|
|
|
|
|
|
|
void inside(String parameters, body) {
|
|
|
|
this.parameters = parameters
|
|
|
|
body()
|
|
|
|
}
|
|
|
|
|
|
|
|
String getImageName() {
|
|
|
|
return imageName
|
|
|
|
}
|
|
|
|
|
|
|
|
boolean isImagePulled() {
|
|
|
|
return imagePulled
|
|
|
|
}
|
|
|
|
|
|
|
|
String getParameters() {
|
|
|
|
return parameters
|
|
|
|
}
|
|
|
|
}
|
2017-12-13 11:58:22 +02:00
|
|
|
|
|
|
|
private class PluginMock {
|
|
|
|
def getShortName() {
|
|
|
|
return 'docker-workflow'
|
|
|
|
}
|
|
|
|
boolean isActive() {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
2017-12-06 13:03:06 +02:00
|
|
|
}
|