From 0d201e2a2caf67f138453c10b8e36eb94dbe8de1 Mon Sep 17 00:00:00 2001
From: stchar
Date: Thu, 15 Oct 2020 10:44:44 +0300
Subject: [PATCH] fix(declarative) withCredentails usernamePassword mocking
conflict
---
.../DeclarativePipelineTest.groovy | 16 ++++++-
.../TestDeclaraticeWithCredentials.groovy | 43 +++++++++++++++++++
.../TestDeclarativePipeline.groovy | 2 +-
.../declarative/TestDockerAgentInStep.groovy | 4 +-
.../jenkinsfiles/withCredentials_Jenkinsfile | 40 +++++++++++++++++
5 files changed, 101 insertions(+), 4 deletions(-)
create mode 100644 src/test/groovy/com/lesfurets/jenkins/unit/declarative/TestDeclaraticeWithCredentials.groovy
create mode 100644 src/test/jenkins/jenkinsfiles/withCredentials_Jenkinsfile
diff --git a/src/main/groovy/com/lesfurets/jenkins/unit/declarative/DeclarativePipelineTest.groovy b/src/main/groovy/com/lesfurets/jenkins/unit/declarative/DeclarativePipelineTest.groovy
index 101f8e24..6d76c993 100644
--- a/src/main/groovy/com/lesfurets/jenkins/unit/declarative/DeclarativePipelineTest.groovy
+++ b/src/main/groovy/com/lesfurets/jenkins/unit/declarative/DeclarativePipelineTest.groovy
@@ -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()
@@ -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', [:])
}
diff --git a/src/test/groovy/com/lesfurets/jenkins/unit/declarative/TestDeclaraticeWithCredentials.groovy b/src/test/groovy/com/lesfurets/jenkins/unit/declarative/TestDeclaraticeWithCredentials.groovy
new file mode 100644
index 00000000..e82ae780
--- /dev/null
+++ b/src/test/groovy/com/lesfurets/jenkins/unit/declarative/TestDeclaraticeWithCredentials.groovy
@@ -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)")
+
+
+ }
+}
diff --git a/src/test/groovy/com/lesfurets/jenkins/unit/declarative/TestDeclarativePipeline.groovy b/src/test/groovy/com/lesfurets/jenkins/unit/declarative/TestDeclarativePipeline.groovy
index 6be725d7..7647b54f 100644
--- a/src/test/groovy/com/lesfurets/jenkins/unit/declarative/TestDeclarativePipeline.groovy
+++ b/src/test/groovy/com/lesfurets/jenkins/unit/declarative/TestDeclarativePipeline.groovy
@@ -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()
}
diff --git a/src/test/groovy/com/lesfurets/jenkins/unit/declarative/TestDockerAgentInStep.groovy b/src/test/groovy/com/lesfurets/jenkins/unit/declarative/TestDockerAgentInStep.groovy
index caf1a205..e8288426 100644
--- a/src/test/groovy/com/lesfurets/jenkins/unit/declarative/TestDockerAgentInStep.groovy
+++ b/src/test/groovy/com/lesfurets/jenkins/unit/declarative/TestDockerAgentInStep.groovy
@@ -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()
}
}
diff --git a/src/test/jenkins/jenkinsfiles/withCredentials_Jenkinsfile b/src/test/jenkins/jenkinsfiles/withCredentials_Jenkinsfile
new file mode 100644
index 00000000..64ecc8fe
--- /dev/null
+++ b/src/test/jenkins/jenkinsfiles/withCredentials_Jenkinsfile
@@ -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"
+ }
+ }
+}
+