mirror of
https://github.com/SAP/jenkins-library.git
synced 2024-12-14 11:03:09 +02:00
ef0b1bd9dd
Starting point for that refactoring: it turned out that the tests was not independent. The DefaultValueCache which is a singleton keeps the status over various tests. Success of test execution depends on the order test execution. We have now * a dedicated rule for resetting the default value cache * JenkinsConfiguration rule (which already provided facilities for dealing with the configuration) has been replaced by a readYaml rule. From the PipelineUnit test framework we get already a handler for libraryResource, which is also part of the setup of the default values. * An auxiliar class which combines the * JenkinsSetupRule (registers the lib) * JenkinsReadYamlRule (provides facilities for Yaml parsing) * JenkinsResetDefaultValueCacheRule (cleans up the DefaultValueCache) into a rule chain. By using this rule chain we ensure that our setup OK (piper lib registered, and default config can be setup in a clean way).
137 lines
4.0 KiB
Groovy
137 lines
4.0 KiB
Groovy
|
|
import org.junit.Before
|
|
import org.junit.Rule
|
|
import org.junit.Test
|
|
import org.junit.rules.RuleChain
|
|
|
|
import com.lesfurets.jenkins.unit.BasePipelineTest
|
|
|
|
import util.JenkinsLoggingRule
|
|
import util.Rules
|
|
|
|
import static org.junit.Assert.assertEquals
|
|
import static org.junit.Assert.assertTrue
|
|
import static org.junit.Assert.assertFalse
|
|
|
|
class DockerExecuteTest extends BasePipelineTest {
|
|
private DockerMock docker
|
|
|
|
private JenkinsLoggingRule jlr = new JenkinsLoggingRule(this)
|
|
|
|
@Rule
|
|
public RuleChain ruleChain = RuleChain.outerRule(Rules.getCommonRules(this))
|
|
.around(jlr)
|
|
|
|
int whichDockerReturnValue = 0
|
|
|
|
def bodyExecuted
|
|
|
|
def cpe
|
|
def dockerExecuteScript;
|
|
|
|
@Before
|
|
void init() {
|
|
|
|
bodyExecuted = false
|
|
docker = new DockerMock()
|
|
binding.setVariable('docker', docker)
|
|
binding.setVariable('Jenkins', [instance: [pluginManager: [plugins: [new PluginMock()]]]])
|
|
|
|
helper.registerAllowedMethod('sh', [Map.class], {return whichDockerReturnValue})
|
|
|
|
cpe = loadScript('commonPipelineEnvironment.groovy').commonPipelineEnvironment
|
|
dockerExecuteScript = loadScript('dockerExecute.groovy').dockerExecute
|
|
}
|
|
|
|
@Test
|
|
void testExecuteInsideDocker() throws Exception {
|
|
|
|
dockerExecuteScript.call(script: [commonPipelineEnvironment: cpe],
|
|
dockerImage: 'maven:3.5-jdk-8-alpine') {
|
|
bodyExecuted = true
|
|
}
|
|
|
|
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())
|
|
assertTrue(bodyExecuted)
|
|
}
|
|
|
|
@Test
|
|
void testExecuteInsideDockerWithParameters() throws Exception {
|
|
|
|
dockerExecuteScript.call(script: [commonPipelineEnvironment: cpe],
|
|
dockerImage: 'maven:3.5-jdk-8-alpine',
|
|
dockerOptions: '-it',
|
|
dockerVolumeBind: ['my_vol': '/my_vol'],
|
|
dockerEnvVars: ['http_proxy': 'http://proxy:8000']) {
|
|
bodyExecuted = true
|
|
}
|
|
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'))
|
|
assertTrue(bodyExecuted)
|
|
}
|
|
|
|
@Test
|
|
void testDockerNotInstalledResultsInLocalExecution() throws Exception {
|
|
|
|
whichDockerReturnValue = 1
|
|
|
|
dockerExecuteScript.call(script: [commonPipelineEnvironment: cpe],
|
|
dockerImage: 'maven:3.5-jdk-8-alpine',
|
|
dockerOptions: '-it',
|
|
dockerVolumeBind: ['my_vol': '/my_vol'],
|
|
dockerEnvVars: ['http_proxy': 'http://proxy:8000']) {
|
|
bodyExecuted = true
|
|
}
|
|
|
|
assertTrue(jlr.log.contains('No docker environment found'))
|
|
assertTrue(jlr.log.contains('Running on local environment'))
|
|
assertTrue(bodyExecuted)
|
|
assertFalse(docker.isImagePulled())
|
|
}
|
|
|
|
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
|
|
}
|
|
}
|
|
|
|
private class PluginMock {
|
|
def getShortName() {
|
|
return 'docker-workflow'
|
|
}
|
|
boolean isActive() {
|
|
return true
|
|
}
|
|
}
|
|
}
|