mirror of
https://github.com/SAP/jenkins-library.git
synced 2024-12-12 10:55:20 +02:00
Add option to always run stages in a pod (#1414)
This commit is contained in:
parent
a09482a14f
commit
1c247de9d0
@ -42,6 +42,9 @@ class DebugReport implements Serializable {
|
||||
String dockerImage = EnvironmentUtils.getDockerFile(serverConfigContents)
|
||||
environment.put('docker_image', dockerImage)
|
||||
}
|
||||
else if(Boolean.valueOf(env.ON_K8S)){
|
||||
DebugReport.instance.environment.put("environment", "Kubernetes")
|
||||
}
|
||||
}
|
||||
|
||||
private static String getServerConfigContents(String... possibleFileLocations) {
|
||||
|
@ -76,7 +76,10 @@ class DockerExecuteOnKubernetesTest extends BasePiperTest {
|
||||
bodyExecuted = false
|
||||
JenkinsUtils.metaClass.static.isPluginActive = { def s -> new PluginMock(s).isActive() }
|
||||
helper.registerAllowedMethod('sh', [Map.class], { return whichDockerReturnValue })
|
||||
helper.registerAllowedMethod('container', [Map.class, Closure.class], { Map config, Closure body -> container(config){body()}
|
||||
helper.registerAllowedMethod('container', [Map.class, Closure.class], { Map config, Closure body ->
|
||||
container(config) {
|
||||
body()
|
||||
}
|
||||
})
|
||||
helper.registerAllowedMethod('merge', [], {
|
||||
return 'merge'
|
||||
@ -133,6 +136,18 @@ class DockerExecuteOnKubernetesTest extends BasePiperTest {
|
||||
assertThat(containerCommands.size(), is(1))
|
||||
}
|
||||
|
||||
@Test
|
||||
void testDockerExecuteOnKubernetesEmptyContainerMapNoDockerImage() throws Exception {
|
||||
stepRule.step.dockerExecuteOnKubernetes(
|
||||
script: nullScript,
|
||||
juStabUtils: utils,
|
||||
containerMap: [:],
|
||||
dockerEnvVars: ['customEnvKey': 'customEnvValue']) {
|
||||
bodyExecuted = true
|
||||
}
|
||||
assertTrue(bodyExecuted)
|
||||
}
|
||||
|
||||
@Test
|
||||
void testDockerExecuteOnKubernetesWithCustomContainerMap() throws Exception {
|
||||
stepRule.step.dockerExecuteOnKubernetes(script: nullScript,
|
||||
@ -238,21 +253,6 @@ class DockerExecuteOnKubernetesTest extends BasePiperTest {
|
||||
assertTrue(bodyExecuted)
|
||||
}
|
||||
|
||||
@Test
|
||||
void testDockerExecuteOnKubernetesEmptyContainerMapNoDockerImage() throws Exception {
|
||||
exception.expect(IllegalArgumentException.class)
|
||||
stepRule.step.dockerExecuteOnKubernetes(
|
||||
script: nullScript,
|
||||
juStabUtils: utils,
|
||||
containerMap: [:],
|
||||
dockerEnvVars: ['customEnvKey': 'customEnvValue']) {
|
||||
container(name: 'jnlp') {
|
||||
bodyExecuted = true
|
||||
}
|
||||
}
|
||||
assertFalse(bodyExecuted)
|
||||
}
|
||||
|
||||
@Test
|
||||
void testSidecarDefaultWithContainerMap() {
|
||||
List portMapping = []
|
||||
|
@ -6,12 +6,14 @@ import org.junit.Test
|
||||
import org.junit.rules.ExpectedException
|
||||
import org.junit.rules.RuleChain
|
||||
import util.BasePiperTest
|
||||
import util.JenkinsEnvironmentRule
|
||||
import util.JenkinsLoggingRule
|
||||
import util.JenkinsReadYamlRule
|
||||
import util.JenkinsStepRule
|
||||
import util.Rules
|
||||
|
||||
import static org.hamcrest.CoreMatchers.containsString
|
||||
import static org.hamcrest.Matchers.contains
|
||||
import static org.hamcrest.Matchers.is
|
||||
import static org.hamcrest.Matchers.not
|
||||
import static org.junit.Assert.assertThat
|
||||
@ -26,6 +28,8 @@ class PiperStageWrapperTest extends BasePiperTest {
|
||||
private Map lockMap = [:]
|
||||
private int countNodeUsage = 0
|
||||
private String nodeLabel = ''
|
||||
private boolean executedOnKubernetes = false
|
||||
private List customEnv = []
|
||||
|
||||
@Rule
|
||||
public RuleChain rules = Rules
|
||||
@ -53,6 +57,18 @@ class PiperStageWrapperTest extends BasePiperTest {
|
||||
body()
|
||||
|
||||
})
|
||||
|
||||
helper.registerAllowedMethod('dockerExecuteOnKubernetes', [Map.class, Closure.class], {params, body ->
|
||||
executedOnKubernetes = true
|
||||
body()
|
||||
})
|
||||
|
||||
helper.registerAllowedMethod('withEnv', [List.class, Closure.class], {env, body ->
|
||||
customEnv = env
|
||||
body()
|
||||
})
|
||||
|
||||
|
||||
helper.registerAllowedMethod('fileExists', [String.class], {s ->
|
||||
return false
|
||||
})
|
||||
@ -71,6 +87,7 @@ class PiperStageWrapperTest extends BasePiperTest {
|
||||
executed = true
|
||||
}
|
||||
assertThat(executed, is(true))
|
||||
assertThat(executedOnKubernetes, is(false))
|
||||
assertThat(lockMap.size(), is(2))
|
||||
assertThat(countNodeUsage, is(1))
|
||||
}
|
||||
@ -95,6 +112,26 @@ class PiperStageWrapperTest extends BasePiperTest {
|
||||
assertThat(nodeLabel, is('testLabel'))
|
||||
}
|
||||
|
||||
@Test
|
||||
void testExecuteStageOnKubernetes() {
|
||||
def executed = false
|
||||
|
||||
binding.variables.env.ON_K8S = true
|
||||
nullScript.commonPipelineEnvironment.configuration = [general: [runStageInPod: true]]
|
||||
|
||||
stepRule.step.piperStageWrapper(
|
||||
script: nullScript,
|
||||
juStabUtils: utils,
|
||||
stageName: 'test',
|
||||
ordinal: 10
|
||||
) {
|
||||
executed = true
|
||||
}
|
||||
assertThat(executed, is(true))
|
||||
assertThat(executedOnKubernetes, is(true))
|
||||
assertThat(customEnv[0].toString(), is("POD_NAME=test"))
|
||||
}
|
||||
|
||||
@Test
|
||||
void testStageExit() {
|
||||
helper.registerAllowedMethod('fileExists', [String.class], {s ->
|
||||
|
@ -20,7 +20,7 @@ class DebugReportTest extends BasePiperTest {
|
||||
DebugReport.instance.initFromEnvironment(env)
|
||||
|
||||
Assert.assertTrue(DebugReport.instance.environment.containsKey('build_details'))
|
||||
Assert.assertEquals('custom', DebugReport.instance.environment.get('environment'))
|
||||
Assert.assertEquals('Kubernetes', DebugReport.instance.environment.get('environment'))
|
||||
|
||||
Set<String> buildDetails = DebugReport.instance.environment.build_details as Set<String>
|
||||
Assert.assertTrue(buildDetails.size() > 0)
|
||||
@ -48,7 +48,7 @@ class DebugReportTest extends BasePiperTest {
|
||||
Assert.assertTrue(debugReport.contains('## Pipeline Environment'))
|
||||
Assert.assertTrue(debugReport.contains('## Local Extensions'))
|
||||
Assert.assertTrue(debugReport.contains('#### Environment\n' +
|
||||
'`custom`'))
|
||||
'`Kubernetes`'))
|
||||
Assert.assertFalse(debugReport.contains('Repository | Branch'))
|
||||
Assert.assertFalse(debugReport.contains('some-branch'))
|
||||
}
|
||||
@ -63,7 +63,7 @@ class DebugReportTest extends BasePiperTest {
|
||||
Assert.assertTrue(debugReport.contains('## Pipeline Environment'))
|
||||
Assert.assertTrue(debugReport.contains('## Local Extensions'))
|
||||
Assert.assertTrue(debugReport.contains('#### Environment\n' +
|
||||
'`custom`'))
|
||||
'`Kubernetes`'))
|
||||
Assert.assertTrue(debugReport.contains('Repository | Branch'))
|
||||
Assert.assertTrue(debugReport.contains('some-branch'))
|
||||
}
|
||||
|
@ -83,7 +83,7 @@ import hudson.AbortException
|
||||
*/
|
||||
'dockerEnvVars',
|
||||
/**
|
||||
* Name of the docker image that should be used. If empty, Docker is not used.
|
||||
* Optional name of the docker image that should be used. If no docker image is provided, the closure will be executed in the jnlp agent container.
|
||||
*/
|
||||
'dockerImage',
|
||||
/**
|
||||
@ -198,8 +198,7 @@ void call(Map parameters = [:], body) {
|
||||
stepParam1 : parameters?.script == null
|
||||
], config)
|
||||
|
||||
if (!config.containerMap) {
|
||||
configHelper.withMandatoryProperty('dockerImage')
|
||||
if (!config.containerMap && config.dockerImage) {
|
||||
config.containerName = 'container-exec'
|
||||
config.containerMap = [(config.get('dockerImage')): config.containerName]
|
||||
config.containerCommands = config.containerCommand ? [(config.get('dockerImage')): config.containerCommand] : null
|
||||
|
@ -30,8 +30,7 @@ void call(Map parameters = [:], body) {
|
||||
|
||||
stageLocking(config) {
|
||||
def containerMap = ContainerMap.instance.getMap().get(stageName) ?: [:]
|
||||
if (Boolean.valueOf(env.ON_K8S) && containerMap.size() > 0) {
|
||||
DebugReport.instance.environment.put("environment", "Kubernetes")
|
||||
if (Boolean.valueOf(env.ON_K8S) && (containerMap.size() > 0 || config.runStageInPod)) {
|
||||
withEnv(["POD_NAME=${stageName}"]) {
|
||||
dockerExecuteOnKubernetes(script: script, containerMap: containerMap, stageName: stageName) {
|
||||
executeStage(script, body, stageName, config, utils, parameters.telemetryDisabled)
|
||||
|
Loading…
Reference in New Issue
Block a user