Skip to content

Commit

Permalink
fix(declarative) withCredentails usernamePassword mocking conflict
Browse files Browse the repository at this point in the history
  • Loading branch information
stchar committed Oct 15, 2020
1 parent aba88e7 commit 0d201e2
Show file tree
Hide file tree
Showing 5 changed files with 101 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,19 @@ abstract class DeclarativePipelineTest extends BasePipelineTest {
addParam(desc.name, desc.defaultValue, false)
}

def stringInterceptor = { Map desc->
if (desc) {
// we are in context of paremetes { string(...)}
if (desc.name) {
addParam(desc.name, desc.defaultValue, false)
}
// we are in context of withCredentaials([string()..]) { }
if(desc.variable) {
return desc.variable
}
}
}

@Override
void setUp() throws Exception {
super.setUp()
Expand All @@ -29,10 +42,9 @@ abstract class DeclarativePipelineTest extends BasePipelineTest {
helper.registerAllowedMethod('pollSCM', [String])
helper.registerAllowedMethod('script', [Closure])
helper.registerAllowedMethod('skipDefaultCheckout')
helper.registerAllowedMethod('string', [Map], paramInterceptor)
helper.registerAllowedMethod('string', [Map], stringInterceptor)
helper.registerAllowedMethod('timeout', [Integer, Closure])
helper.registerAllowedMethod('timestamps')
helper.registerAllowedMethod('usernamePassword', [Map], { creds -> return creds })
binding.setVariable('credentials', [:])
binding.setVariable('params', [:])
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package com.lesfurets.jenkins.unit.declarative

import com.lesfurets.jenkins.unit.declarative.DeclarativePipelineTest
import org.junit.Before
import org.junit.Test

import static org.junit.Assert.assertTrue

class TestDeclaraticeWithCredentials extends DeclarativePipelineTest {

@Override
@Before
void setUp() throws Exception {
scriptRoots += 'src/test/jenkins/jenkinsfiles'
scriptExtension = ''
super.setUp()
}

@Test
void should_run_script_with_credentials() {
// when:
runScript("withCredentials_Jenkinsfile")

// then:
assertJobStatusSuccess()
assertCallStack().contains("echo(User/Pass = user/pass)")
assertCallStack().contains("echo(Nested User/Pass = user/pass)")
assertCallStack().contains("echo(Restored User/Pass = user/pass)")
assertCallStack().contains("echo(Cleared User/Pass = null/null)")

assertCallStack().contains("echo(Docker = docker_pass)")
assertCallStack().contains("echo(Nested Docker = docker_pass)")
assertCallStack().contains("echo(Restored Docker = docker_pass)")
assertCallStack().contains("echo(Cleared Docker = null)")

assertCallStack().contains("echo(SSH = ssh_pass)")
assertCallStack().contains("echo(Nested SSH = ssh_pass)")
assertCallStack().contains("echo(Restored SSH = ssh_pass)")
assertCallStack().contains("echo(Cleared SSH = null)")


}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ class TestDeclarativePipeline extends DeclarativePipelineTest {
@Before
@Override
void setUp() throws Exception {
scriptRoots = ['src/test/jenkins/jenkinsfiles']
scriptRoots += 'src/test/jenkins/jenkinsfiles'
scriptExtension = ''
super.setUp()
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,14 @@ class TestDockerAgentInStep extends DeclarativePipelineTest {
@Before
@Override
void setUp() throws Exception {
scriptRoots += 'src/test/jenkins/jenkinsfiles'
scriptExtension = ''
super.setUp()
}

@Test
void test_example() {
runScript("src/test/jenkins/jenkinsfiles/Docker_agentInStep_JenkinsFile")
runScript("Docker_agentInStep_JenkinsFile")
assertJobStatusSuccess()
}
}
40 changes: 40 additions & 0 deletions src/test/jenkins/jenkinsfiles/withCredentials_Jenkinsfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@

pipeline {
agent any
stages {
stage("run") {
withCredentials([
usernamePassword( credentialsId: 'my_cred_id', usernameVariable: 'user', passwordVariable: 'pass' ),
string( credentialsId: 'docker_cred', variable: 'docker_pass' ),
string( credentialsId: 'ssh_cred', variable: 'ssh_pass' )
]) {
// Ensure values are set on entry
echo "User/Pass = $user/$pass"
echo "Docker = $docker_pass"
echo "SSH = $ssh_pass"

withCredentials([
usernamePassword( credentialsId: 'my_cred_id', usernameVariable: 'user', passwordVariable: 'pass' ),
string( credentialsId: 'docker_cred', variable: 'docker_pass' ),
string( credentialsId: 'ssh_cred', variable: 'ssh_pass' )
]) {
// Ensure nested values are in place
echo "Nested User/Pass = $user/$pass"
echo "Nested Docker = $docker_pass"
echo "Nested SSH = $ssh_pass"
}

// Ensure original values are restored
echo "Restored User/Pass = $user/$pass"
echo "Restored Docker = $docker_pass"
echo "Restored SSH = $ssh_pass"
}

// Ensure original state where the values are not set
echo "Cleared User/Pass = $user/$pass"
echo "Cleared Docker = $docker_pass"
echo "Cleared SSH = $ssh_pass"
}
}
}

0 comments on commit 0d201e2

Please sign in to comment.