2017-12-06 12:03:06 +01:00
|
|
|
|
|
|
|
import org.junit.Before
|
2018-01-16 09:33:13 +01:00
|
|
|
import org.junit.Rule
|
2017-12-06 12:03:06 +01:00
|
|
|
import org.junit.Test
|
2018-01-16 09:33:13 +01:00
|
|
|
import org.junit.rules.RuleChain
|
|
|
|
|
|
|
|
import util.JenkinsLoggingRule
|
|
|
|
import util.JenkinsSetupRule
|
2017-12-06 12:03:06 +01:00
|
|
|
|
|
|
|
import static org.junit.Assert.assertEquals
|
|
|
|
import static org.junit.Assert.assertTrue
|
2017-12-15 16:41:10 +01:00
|
|
|
import static org.junit.Assert.assertFalse
|
2017-12-06 12:03:06 +01:00
|
|
|
|
|
|
|
class DockerExecuteTest extends PiperTestBase {
|
|
|
|
private DockerMock docker
|
|
|
|
|
2018-01-16 09:33:13 +01:00
|
|
|
private JenkinsLoggingRule jlr = new JenkinsLoggingRule(this)
|
|
|
|
|
|
|
|
@Rule
|
|
|
|
public RuleChain ruleChain = RuleChain.outerRule(new JenkinsSetupRule(this))
|
|
|
|
.around(jlr)
|
2017-12-06 12:03:06 +01:00
|
|
|
|
2017-12-15 16:41:10 +01:00
|
|
|
int whichDockerReturnValue = 0
|
|
|
|
|
2017-12-06 12:03:06 +01:00
|
|
|
@Before
|
2018-01-16 09:33:13 +01:00
|
|
|
void init() {
|
2017-12-06 12:03:06 +01:00
|
|
|
|
|
|
|
docker = new DockerMock()
|
|
|
|
binding.setVariable('docker', docker)
|
2017-12-13 10:58:22 +01:00
|
|
|
binding.setVariable('Jenkins', [instance: [pluginManager: [plugins: [new PluginMock()]]]])
|
2017-12-06 12:03:06 +01:00
|
|
|
|
2017-12-15 16:41:10 +01:00
|
|
|
helper.registerAllowedMethod('sh', [Map.class], {return whichDockerReturnValue})
|
2017-12-06 12:03:06 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
@Test
|
|
|
|
void testExecuteInsideDocker() throws Exception {
|
|
|
|
def script = loadScript("test/resources/pipelines/dockerExecuteTest/executeInsideDocker.groovy")
|
|
|
|
script.execute()
|
|
|
|
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 09:33:13 +01:00
|
|
|
assertTrue(jlr.log.contains('Inside Docker'))
|
2017-12-06 12:03:06 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
@Test
|
|
|
|
void testExecuteInsideDockerWithParameters() throws Exception {
|
|
|
|
def script = loadScript("test/resources/pipelines/dockerExecuteTest/executeInsideDockerWithParameters.groovy")
|
|
|
|
|
|
|
|
script.execute()
|
|
|
|
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'))
|
|
|
|
}
|
|
|
|
|
2017-12-15 16:41:10 +01:00
|
|
|
@Test
|
|
|
|
void testDockerNotInstalledResultsInLocalExecution() throws Exception {
|
|
|
|
|
|
|
|
whichDockerReturnValue = 1
|
|
|
|
def script = loadScript("test/resources/pipelines/dockerExecuteTest/executeInsideDockerWithParameters.groovy")
|
|
|
|
|
|
|
|
script.execute()
|
2018-01-16 09:33:13 +01:00
|
|
|
assertTrue(jlr.log.contains('No docker environment found'))
|
|
|
|
assertTrue(jlr.log.contains('Running on local environment'))
|
2017-12-15 16:41:10 +01:00
|
|
|
assertFalse(docker.isImagePulled())
|
|
|
|
}
|
2017-12-06 12:03:06 +01: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 10:58:22 +01:00
|
|
|
|
|
|
|
private class PluginMock {
|
|
|
|
def getShortName() {
|
|
|
|
return 'docker-workflow'
|
|
|
|
}
|
|
|
|
boolean isActive() {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-12-06 12:03:06 +01:00
|
|
|
}
|