Skip to content

Commit

Permalink
Unstash via Utils.unstash (SAP#4381)
Browse files Browse the repository at this point in the history
We should only unstash using the Utils class.
The Jenkins unstash step does not log which stash was unstashed.
  • Loading branch information
alxsap authored and maxatsap committed Jul 23, 2024
1 parent 757e2e7 commit a1bbfb1
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 3 deletions.
23 changes: 23 additions & 0 deletions test/groovy/DockerExecuteOnKubernetesTest.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,17 @@ class DockerExecuteOnKubernetesTest extends BasePiperTest {
def podSpec
Map resources = [:]
List stashList = []
List unstashList = []
def utilsMock = newUtilsMock()

Utils newUtilsMock() {
def utilsMock = new Utils()
utilsMock.steps = [
unstash : { m -> unstashList.add(m) }
]
utilsMock.echo = { def m -> }
return utilsMock
}

@Before
void init() {
Expand Down Expand Up @@ -809,6 +820,18 @@ class DockerExecuteOnKubernetesTest extends BasePiperTest {
hasEntry('excludes', 'container/exclude.test'))))
}

@Test
void testUnstash() {
stepRule.step.dockerExecuteOnKubernetes(
script: nullScript,
juStabUtils: utilsMock,
dockerImage: 'maven:3.5-jdk-8-alpine',
) {}
assertEquals(2, unstashList.size())
assertTrue(unstashList[0].startsWith('workspace-'))
assertTrue(unstashList[1].startsWith('container-'))
}

@Test
void testDockerExecuteWithVolumeProperties() {
stepRule.step.dockerExecuteOnKubernetes(
Expand Down
6 changes: 3 additions & 3 deletions vars/dockerExecuteOnKubernetes.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -346,7 +346,7 @@ void executeOnPod(Map config, utils, Closure body, Script script) {
}
} finally {
if (config.containerName)
unstashWorkspace(config, 'container')
unstashWorkspace(config, utils, 'container')
}
}

Expand Down Expand Up @@ -454,9 +454,9 @@ private Map getSecurityContext(Map config) {
return config.securityContext ?: config.jenkinsKubernetes.securityContext ?: [:]
}

private void unstashWorkspace(config, prefix) {
private void unstashWorkspace(config, utils, prefix) {
try {
unstash "${prefix}-${config.uniqueId}"
utils.unstash "${prefix}-${config.uniqueId}"
} catch (AbortException | IOException e) {
echo "${e.getMessage()}\n${e.getCause()}"
} catch (Throwable e) {
Expand Down

0 comments on commit a1bbfb1

Please sign in to comment.