Skip to content

Commit

Permalink
Stash via Utils.stash (#4380)
Browse files Browse the repository at this point in the history
We should use Utils.stash instead of native steps.stash calls (Jenkins)
since important logging is missing.
The default Jenkins stash step does not log any metadata like
stash name, patterns, etc.
  • Loading branch information
alxsap authored Jul 7, 2023
1 parent 1e9d8df commit 500c428
Show file tree
Hide file tree
Showing 12 changed files with 260 additions and 95 deletions.
2 changes: 1 addition & 1 deletion src/com/sap/piper/GitUtils.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ static String handleTestRepository(Script steps, Map config){
// checkout test repository
steps.git options
// stash test content
steps.stash stashName
steps.stash stashName //TODO: should use new Utils().stash
// return stash name
return stashName
}
32 changes: 22 additions & 10 deletions src/com/sap/piper/Utils.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -7,39 +7,51 @@ import groovy.text.GStringTemplateEngine
import java.nio.charset.StandardCharsets
import java.security.MessageDigest

def stash(Map params) {
if(params.includes == null) params.includes = '**/*.*'
if(params.excludes == null) params.excludes = ''
if(params.useDefaultExcludes == null) params.useDefaultExcludes = true
if(params.allowEmpty == null) params.allowEmpty = false
return stash(params.name, params.includes, params.excludes, params.useDefaultExcludes, params.allowEmpty)
}

def stash(name, include = '**/*.*', exclude = '', useDefaultExcludes = true) {
echo "Stash content: ${name} (include: ${include}, exclude: ${exclude}, useDefaultExcludes: ${useDefaultExcludes})"
def stash(String name, String includes = '**/*.*', String excludes = '', boolean useDefaultExcludes = true, boolean allowEmpty = false) {
if(!name) throw new IllegalArgumentException("name must not be '$name'")
echo "Stash content: ${name} (includes: ${includes}, excludes: ${excludes}, useDefaultExcludes: ${useDefaultExcludes}, allowEmpty: ${allowEmpty})"

Map stashParams = [
name : name,
includes: include,
excludes: exclude
includes: includes,
excludes: excludes
]
//only set the optional parameter if default excludes should not be applied
if (!useDefaultExcludes) {
stashParams.useDefaultExcludes = useDefaultExcludes
}
//only set the optional parameter if allow empty should be applied
if (allowEmpty) {
stashParams.allowEmpty = allowEmpty
}
steps.stash stashParams
}

def stashList(script, List stashes) {
for (def stash : stashes) {
def name = stash.name
def include = stash.includes
def exclude = stash.excludes
def includes = stash.includes
def excludes = stash.excludes

if (stash?.merge == true) {
String lockingResourceGroup = script.commonPipelineEnvironment.projectName?:env.JOB_NAME
String lockName = "${lockingResourceGroup}/${stash.name}"
lock(lockName) {
unstash stash.name
echo "Stash content: ${name} (include: ${include}, exclude: ${exclude})"
steps.stash name: name, includes: include, excludes: exclude, allowEmpty: true
echo "Stash content: ${name} (includes: ${includes}, excludes: ${excludes})"
steps.stash name: name, includes: includes, excludes: excludes, allowEmpty: true
}
} else {
echo "Stash content: ${name} (include: ${include}, exclude: ${exclude})"
steps.stash name: name, includes: include, excludes: exclude, allowEmpty: true
echo "Stash content: ${name} (includes: ${includes}, excludes: ${excludes})"
steps.stash name: name, includes: includes, excludes: excludes, allowEmpty: true
}
}
}
Expand Down
74 changes: 33 additions & 41 deletions test/groovy/DockerExecuteOnKubernetesTest.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ class DockerExecuteOnKubernetesTest extends BasePiperTest {
Utils newUtilsMock() {
def utilsMock = new Utils()
utilsMock.steps = [
stash : { m -> stashList.add(m) },
unstash : { m -> unstashList.add(m) }
]
utilsMock.echo = { def m -> }
Expand Down Expand Up @@ -126,23 +127,13 @@ class DockerExecuteOnKubernetesTest extends BasePiperTest {
}
body()
})
helper.registerAllowedMethod('stash', [Map.class], { m ->
stashList.add(m)
})

Utils.metaClass.echo = { def m -> }
}

@After
public void tearDown() {
Utils.metaClass = null
}

@Test
void testRunOnPodNoContainerMapOnlyDockerImage() throws Exception {
stepRule.step.dockerExecuteOnKubernetes(
script: nullScript,
juStabUtils: utils,
juStabUtils: utilsMock,
dockerImage: 'maven:3.5-jdk-8-alpine',
dockerOptions: '-it',
dockerVolumeBind: ['my_vol': '/my_vol'],
Expand All @@ -163,7 +154,7 @@ class DockerExecuteOnKubernetesTest extends BasePiperTest {
void testDockerExecuteOnKubernetesEmptyContainerMapNoDockerImage() throws Exception {
stepRule.step.dockerExecuteOnKubernetes(
script: nullScript,
juStabUtils: utils,
juStabUtils: utilsMock,
containerMap: [:],
dockerEnvVars: ['customEnvKey': 'customEnvValue']) {
bodyExecuted = true
Expand All @@ -173,7 +164,7 @@ class DockerExecuteOnKubernetesTest extends BasePiperTest {

@Test
void testDockerExecuteOnKubernetesWithCustomContainerMap() throws Exception {
stepRule.step.dockerExecuteOnKubernetes(script: nullScript,
stepRule.step.dockerExecuteOnKubernetes(script: nullScript, juStabUtils: utilsMock,
containerMap: ['maven:3.5-jdk-8-alpine': 'mavenexecute']) {
container(name: 'mavenexecute') {
bodyExecuted = true
Expand All @@ -189,7 +180,7 @@ class DockerExecuteOnKubernetesTest extends BasePiperTest {
@Test
void testInheritFromPodTemplate() throws Exception {
nullScript.commonPipelineEnvironment.configuration = ['general': ['jenkinsKubernetes': ['inheritFrom': 'default']]]
stepRule.step.dockerExecuteOnKubernetes(script: nullScript,
stepRule.step.dockerExecuteOnKubernetes(script: nullScript, juStabUtils: utilsMock,
containerMap: ['maven:3.5-jdk-8-alpine': 'mavenexecute']) {
container(name: 'mavenexecute') {
bodyExecuted = true
Expand All @@ -203,7 +194,7 @@ class DockerExecuteOnKubernetesTest extends BasePiperTest {
@Test
void testDockerExecuteOnKubernetesWithCustomJnlpWithContainerMap() throws Exception {
nullScript.commonPipelineEnvironment.configuration = ['general': ['jenkinsKubernetes': ['jnlpAgent': 'myJnalpAgent']]]
stepRule.step.dockerExecuteOnKubernetes(script: nullScript,
stepRule.step.dockerExecuteOnKubernetes(script: nullScript, juStabUtils: utilsMock,
containerMap: ['maven:3.5-jdk-8-alpine': 'mavenexecute']) {
container(name: 'mavenexecute') {
bodyExecuted = true
Expand All @@ -223,7 +214,7 @@ class DockerExecuteOnKubernetesTest extends BasePiperTest {
nullScript.commonPipelineEnvironment.configuration = ['general': ['jenkinsKubernetes': ['jnlpAgent': 'myJnalpAgent']]]
stepRule.step.dockerExecuteOnKubernetes(
script: nullScript,
juStabUtils: utils,
juStabUtils: utilsMock,
dockerImage: 'maven:3.5-jdk-8-alpine') {
bodyExecuted = true
}
Expand All @@ -237,7 +228,7 @@ class DockerExecuteOnKubernetesTest extends BasePiperTest {

@Test
void testDockerExecuteOnKubernetesWithCustomWorkspace() throws Exception {
stepRule.step.dockerExecuteOnKubernetes(script: nullScript,
stepRule.step.dockerExecuteOnKubernetes(script: nullScript, juStabUtils: utilsMock,
containerMap: ['maven:3.5-jdk-8-alpine': 'mavenexecute'],
dockerWorkspace: '/home/piper') {
container(name: 'mavenexecute') {
Expand All @@ -250,7 +241,7 @@ class DockerExecuteOnKubernetesTest extends BasePiperTest {

@Test
void testDockerExecuteOnKubernetesWithCustomEnv() throws Exception {
stepRule.step.dockerExecuteOnKubernetes(script: nullScript,
stepRule.step.dockerExecuteOnKubernetes(script: nullScript, juStabUtils: utilsMock,
containerMap: ['maven:3.5-jdk-8-alpine': 'mavenexecute'],
dockerEnvVars: ['customEnvKey': 'customEnvValue']) {
container(name: 'mavenexecute') {
Expand Down Expand Up @@ -281,7 +272,7 @@ class DockerExecuteOnKubernetesTest extends BasePiperTest {
]
]
]]
stepRule.step.dockerExecuteOnKubernetes(script: nullScript,
stepRule.step.dockerExecuteOnKubernetes(script: nullScript, juStabUtils: utilsMock,
containerMap: ['maven:3.5-jdk-8-alpine': 'mavenexecute'], {
bodyExecuted = true
})
Expand All @@ -307,7 +298,7 @@ class DockerExecuteOnKubernetesTest extends BasePiperTest {
]
]
]]]
stepRule.step.dockerExecuteOnKubernetes(script: nullScript,
stepRule.step.dockerExecuteOnKubernetes(script: nullScript, juStabUtils: utilsMock,
containerMap: ['maven:3.5-jdk-8-alpine': 'mavenexecute'], {
bodyExecuted = true
})
Expand Down Expand Up @@ -336,7 +327,7 @@ class DockerExecuteOnKubernetesTest extends BasePiperTest {
]
]
]]]
stepRule.step.dockerExecuteOnKubernetes(script: nullScript,
stepRule.step.dockerExecuteOnKubernetes(script: nullScript, juStabUtils: utilsMock,
containerMap: ['maven:3.5-jdk-8-alpine': 'mavenexecute'],
resources: [
mavenexecute: [
Expand Down Expand Up @@ -406,7 +397,7 @@ class DockerExecuteOnKubernetesTest extends BasePiperTest {
]
]
]]
stepRule.step.dockerExecuteOnKubernetes(script: nullScript,
stepRule.step.dockerExecuteOnKubernetes(script: nullScript, juStabUtils: utilsMock,
containerMap: ['maven:3.5-jdk-8-alpine': 'mavenexecute'],
sidecarImage: 'ubuntu',
sidecarName: 'mysidecar') {
Expand All @@ -421,7 +412,7 @@ class DockerExecuteOnKubernetesTest extends BasePiperTest {

@Test
void testDockerExecuteOnKubernetesUpperCaseContainerName() throws Exception {
stepRule.step.dockerExecuteOnKubernetes(script: nullScript,
stepRule.step.dockerExecuteOnKubernetes(script: nullScript, juStabUtils: utilsMock,
containerMap: ['maven:3.5-jdk-8-alpine': 'MAVENEXECUTE'],
dockerEnvVars: ['customEnvKey': 'customEnvValue']) {
container(name: 'mavenexecute') {
Expand All @@ -443,7 +434,7 @@ class DockerExecuteOnKubernetesTest extends BasePiperTest {
})
stepRule.step.dockerExecuteOnKubernetes(
script: nullScript,
juStabUtils: utils,
juStabUtils: utilsMock,
containerCommands: ['selenium/standalone-chrome': ''],
containerEnvVars: [
'selenium/standalone-chrome': ['customEnvKey': 'customEnvValue']
Expand Down Expand Up @@ -489,7 +480,7 @@ class DockerExecuteOnKubernetesTest extends BasePiperTest {
})
stepRule.step.dockerExecuteOnKubernetes(
script: nullScript,
juStabUtils: utils,
juStabUtils: utilsMock,
containerMap: ['maven:3.5-jdk-8-alpine': 'mavenexecute'],
containerName: 'mavenexecute',
dockerOptions: '-it',
Expand Down Expand Up @@ -518,7 +509,7 @@ class DockerExecuteOnKubernetesTest extends BasePiperTest {
void testDockerExecuteOnKubernetesWithCustomShell() {
stepRule.step.dockerExecuteOnKubernetes(
script: nullScript,
juStabUtils: utils,
juStabUtils: utilsMock,
dockerImage: 'maven:3.5-jdk-8-alpine',
containerShell: '/busybox/sh'
) {
Expand All @@ -531,7 +522,7 @@ class DockerExecuteOnKubernetesTest extends BasePiperTest {
void testDockerExecuteOnKubernetesWithCustomContainerCommand() {
stepRule.step.dockerExecuteOnKubernetes(
script: nullScript,
juStabUtils: utils,
juStabUtils: utilsMock,
dockerImage: 'maven:3.5-jdk-8-alpine',
containerCommand: '/busybox/tail -f /dev/null'
) {
Expand All @@ -544,6 +535,7 @@ class DockerExecuteOnKubernetesTest extends BasePiperTest {
void testSkipDockerImagePull() throws Exception {
stepRule.step.dockerExecuteOnKubernetes(
script: nullScript,
juStabUtils: utilsMock,
dockerPullImage: false,
containerMap: ['maven:3.5-jdk-8-alpine': 'mavenexecute']
) {
Expand All @@ -559,7 +551,7 @@ class DockerExecuteOnKubernetesTest extends BasePiperTest {
void testSkipSidecarImagePull() throws Exception {
stepRule.step.dockerExecuteOnKubernetes(
script: nullScript,
juStabUtils: utils,
juStabUtils: utilsMock,
containerCommands: ['selenium/standalone-chrome': ''],
containerEnvVars: [
'selenium/standalone-chrome': ['customEnvKey': 'customEnvValue']
Expand Down Expand Up @@ -592,7 +584,7 @@ class DockerExecuteOnKubernetesTest extends BasePiperTest {

stepRule.step.dockerExecuteOnKubernetes(
script: nullScript,
juStabUtils: utils,
juStabUtils: utilsMock,
dockerImage: 'maven:3.5-jdk-8-alpine',
) { bodyExecuted = true }
assertTrue(bodyExecuted)
Expand Down Expand Up @@ -624,7 +616,7 @@ class DockerExecuteOnKubernetesTest extends BasePiperTest {

stepRule.step.dockerExecuteOnKubernetes(
script: nullScript,
juStabUtils: utils,
juStabUtils: utilsMock,
dockerImage: 'maven:3.5-jdk-8-alpine',
) { bodyExecuted = true }
assertTrue(bodyExecuted)
Expand Down Expand Up @@ -666,7 +658,7 @@ class DockerExecuteOnKubernetesTest extends BasePiperTest {

stepRule.step.dockerExecuteOnKubernetes(
script: nullScript,
juStabUtils: utils,
juStabUtils: utilsMock,
dockerImage: 'maven:3.5-jdk-8-alpine',
) { bodyExecuted = true }
assertTrue(bodyExecuted)
Expand All @@ -689,7 +681,7 @@ class DockerExecuteOnKubernetesTest extends BasePiperTest {
securityContext: expectedSecurityContext]]]
stepRule.step.dockerExecuteOnKubernetes(
script: nullScript,
juStabUtils: utils,
juStabUtils: utilsMock,
dockerImage: 'maven:3.5-jdk-8-alpine',
) { bodyExecuted = true }
assertTrue(bodyExecuted)
Expand All @@ -701,7 +693,7 @@ class DockerExecuteOnKubernetesTest extends BasePiperTest {

stepRule.step.dockerExecuteOnKubernetes(
script: nullScript,
juStabUtils: utils,
juStabUtils: utilsMock,
dockerImage: 'maven:3.5-jdk-8-alpine',
nodeSelector: 'size:big'
) { bodyExecuted = true }
Expand All @@ -718,7 +710,7 @@ class DockerExecuteOnKubernetesTest extends BasePiperTest {
binding.variables.env.JENKINS_JNLP_IMAGE = 'env/jnlp:latest'
stepRule.step.dockerExecuteOnKubernetes(
script: nullScript,
juStabUtils: utils,
juStabUtils: utilsMock,
dockerImage: 'maven:3.5-jdk-8-alpine',
) { bodyExecuted = true }
assertTrue(bodyExecuted)
Expand All @@ -742,7 +734,7 @@ class DockerExecuteOnKubernetesTest extends BasePiperTest {
//binding.variables.env.JENKINS_JNLP_IMAGE = 'config/jnlp:latest'
stepRule.step.dockerExecuteOnKubernetes(
script: nullScript,
juStabUtils: utils,
juStabUtils: utilsMock,
dockerImage: 'maven:3.5-jdk-8-alpine',
) { bodyExecuted = true }
assertTrue(bodyExecuted)
Expand All @@ -769,7 +761,7 @@ class DockerExecuteOnKubernetesTest extends BasePiperTest {
//binding.variables.env.JENKINS_JNLP_IMAGE = 'config/jnlp:latest'
stepRule.step.dockerExecuteOnKubernetes(
script: nullScript,
juStabUtils: utils,
juStabUtils: utilsMock,
dockerImage: 'maven:3.5-jdk-8-alpine',
) { throw new AbortException('Execution failed.') }
}
Expand All @@ -778,7 +770,7 @@ class DockerExecuteOnKubernetesTest extends BasePiperTest {
void testDockerExecuteOnKubernetesAnnotations(){
stepRule.step.dockerExecuteOnKubernetes(
script: nullScript,
juStabUtils: utils,
juStabUtils: utilsMock,
annotations: ['testAnnotation':'testValue']
)
{
Expand All @@ -805,7 +797,7 @@ class DockerExecuteOnKubernetesTest extends BasePiperTest {
]
stepRule.step.dockerExecuteOnKubernetes(
script: nullScript,
juStabUtils: utils,
juStabUtils: utilsMock,
dockerImage: 'maven:3.5-jdk-8-alpine',
) {
bodyExecuted = true
Expand Down Expand Up @@ -836,7 +828,7 @@ class DockerExecuteOnKubernetesTest extends BasePiperTest {
void testDockerExecuteWithVolumeProperties() {
stepRule.step.dockerExecuteOnKubernetes(
script: nullScript,
juStabUtils: utils,
juStabUtils: utilsMock,
containerName: 'mycontainer',
initContainerImage: 'ppiper/cf-cli',
dockerImage: 'maven:3.5-jdk-8-alpine',
Expand Down Expand Up @@ -866,7 +858,7 @@ class DockerExecuteOnKubernetesTest extends BasePiperTest {
void testInitContainerDefaultWithParameters() {
stepRule.step.dockerExecuteOnKubernetes(
script: nullScript,
juStabUtils: utils,
juStabUtils: utilsMock,
containerName: 'mycontainer',
initContainerImage: 'ppiper/cf-cli@sha256:latest',
initContainerCommand: 'cp /usr/local/bin/cf7 /opt/bin/cf',
Expand All @@ -885,7 +877,7 @@ class DockerExecuteOnKubernetesTest extends BasePiperTest {
void testInitContainerWithoutContainerCommand() {
stepRule.step.dockerExecuteOnKubernetes(
script: nullScript,
juStabUtils: utils,
juStabUtils: utilsMock,
containerName: 'mycontainer',
initContainerImage: 'ppiper/cf-cli@sha256:latest',
dockerImage: 'maven:3.5-jdk-8-alpine',
Expand Down
Loading

0 comments on commit 500c428

Please sign in to comment.