Skip to content

Commit 19dc388

Browse files
authored
Merge pull request #311 from jenkinsci/fix/declarative-with-credentials
fix(declarative) withCredentails usernamePassword mocking conflict
2 parents 5bf9179 + 0d201e2 commit 19dc388

File tree

5 files changed

+101
-4
lines changed

5 files changed

+101
-4
lines changed

src/main/groovy/com/lesfurets/jenkins/unit/declarative/DeclarativePipelineTest.groovy

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,19 @@ abstract class DeclarativePipelineTest extends BasePipelineTest {
1616
addParam(desc.name, desc.defaultValue, false)
1717
}
1818

19+
def stringInterceptor = { Map desc->
20+
if (desc) {
21+
// we are in context of paremetes { string(...)}
22+
if (desc.name) {
23+
addParam(desc.name, desc.defaultValue, false)
24+
}
25+
// we are in context of withCredentaials([string()..]) { }
26+
if(desc.variable) {
27+
return desc.variable
28+
}
29+
}
30+
}
31+
1932
@Override
2033
void setUp() throws Exception {
2134
super.setUp()
@@ -29,10 +42,9 @@ abstract class DeclarativePipelineTest extends BasePipelineTest {
2942
helper.registerAllowedMethod('pollSCM', [String])
3043
helper.registerAllowedMethod('script', [Closure])
3144
helper.registerAllowedMethod('skipDefaultCheckout')
32-
helper.registerAllowedMethod('string', [Map], paramInterceptor)
45+
helper.registerAllowedMethod('string', [Map], stringInterceptor)
3346
helper.registerAllowedMethod('timeout', [Integer, Closure])
3447
helper.registerAllowedMethod('timestamps')
35-
helper.registerAllowedMethod('usernamePassword', [Map], { creds -> return creds })
3648
binding.setVariable('credentials', [:])
3749
binding.setVariable('params', [:])
3850
}
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
package com.lesfurets.jenkins.unit.declarative
2+
3+
import com.lesfurets.jenkins.unit.declarative.DeclarativePipelineTest
4+
import org.junit.Before
5+
import org.junit.Test
6+
7+
import static org.junit.Assert.assertTrue
8+
9+
class TestDeclaraticeWithCredentials extends DeclarativePipelineTest {
10+
11+
@Override
12+
@Before
13+
void setUp() throws Exception {
14+
scriptRoots += 'src/test/jenkins/jenkinsfiles'
15+
scriptExtension = ''
16+
super.setUp()
17+
}
18+
19+
@Test
20+
void should_run_script_with_credentials() {
21+
// when:
22+
runScript("withCredentials_Jenkinsfile")
23+
24+
// then:
25+
assertJobStatusSuccess()
26+
assertCallStack().contains("echo(User/Pass = user/pass)")
27+
assertCallStack().contains("echo(Nested User/Pass = user/pass)")
28+
assertCallStack().contains("echo(Restored User/Pass = user/pass)")
29+
assertCallStack().contains("echo(Cleared User/Pass = null/null)")
30+
31+
assertCallStack().contains("echo(Docker = docker_pass)")
32+
assertCallStack().contains("echo(Nested Docker = docker_pass)")
33+
assertCallStack().contains("echo(Restored Docker = docker_pass)")
34+
assertCallStack().contains("echo(Cleared Docker = null)")
35+
36+
assertCallStack().contains("echo(SSH = ssh_pass)")
37+
assertCallStack().contains("echo(Nested SSH = ssh_pass)")
38+
assertCallStack().contains("echo(Restored SSH = ssh_pass)")
39+
assertCallStack().contains("echo(Cleared SSH = null)")
40+
41+
42+
}
43+
}

src/test/groovy/com/lesfurets/jenkins/unit/declarative/TestDeclarativePipeline.groovy

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ class TestDeclarativePipeline extends DeclarativePipelineTest {
88
@Before
99
@Override
1010
void setUp() throws Exception {
11-
scriptRoots = ['src/test/jenkins/jenkinsfiles']
11+
scriptRoots += 'src/test/jenkins/jenkinsfiles'
1212
scriptExtension = ''
1313
super.setUp()
1414
}

src/test/groovy/com/lesfurets/jenkins/unit/declarative/TestDockerAgentInStep.groovy

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,14 @@ class TestDockerAgentInStep extends DeclarativePipelineTest {
88
@Before
99
@Override
1010
void setUp() throws Exception {
11+
scriptRoots += 'src/test/jenkins/jenkinsfiles'
12+
scriptExtension = ''
1113
super.setUp()
1214
}
1315

1416
@Test
1517
void test_example() {
16-
runScript("src/test/jenkins/jenkinsfiles/Docker_agentInStep_JenkinsFile")
18+
runScript("Docker_agentInStep_JenkinsFile")
1719
assertJobStatusSuccess()
1820
}
1921
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
2+
pipeline {
3+
agent any
4+
stages {
5+
stage("run") {
6+
withCredentials([
7+
usernamePassword( credentialsId: 'my_cred_id', usernameVariable: 'user', passwordVariable: 'pass' ),
8+
string( credentialsId: 'docker_cred', variable: 'docker_pass' ),
9+
string( credentialsId: 'ssh_cred', variable: 'ssh_pass' )
10+
]) {
11+
// Ensure values are set on entry
12+
echo "User/Pass = $user/$pass"
13+
echo "Docker = $docker_pass"
14+
echo "SSH = $ssh_pass"
15+
16+
withCredentials([
17+
usernamePassword( credentialsId: 'my_cred_id', usernameVariable: 'user', passwordVariable: 'pass' ),
18+
string( credentialsId: 'docker_cred', variable: 'docker_pass' ),
19+
string( credentialsId: 'ssh_cred', variable: 'ssh_pass' )
20+
]) {
21+
// Ensure nested values are in place
22+
echo "Nested User/Pass = $user/$pass"
23+
echo "Nested Docker = $docker_pass"
24+
echo "Nested SSH = $ssh_pass"
25+
}
26+
27+
// Ensure original values are restored
28+
echo "Restored User/Pass = $user/$pass"
29+
echo "Restored Docker = $docker_pass"
30+
echo "Restored SSH = $ssh_pass"
31+
}
32+
33+
// Ensure original state where the values are not set
34+
echo "Cleared User/Pass = $user/$pass"
35+
echo "Cleared Docker = $docker_pass"
36+
echo "Cleared SSH = $ssh_pass"
37+
}
38+
}
39+
}
40+

0 commit comments

Comments
 (0)