1
0
mirror of https://github.com/SAP/jenkins-library.git synced 2025-01-18 05:18:24 +02:00

Merge pull request #211 from marcusholl/pr/IntroduceCredentialsRule

Introduce CredentialsRule
This commit is contained in:
Marcus Holl 2018-07-19 09:22:05 +02:00 committed by GitHub
commit d2a16f2ded
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 87 additions and 58 deletions

View File

@ -11,6 +11,7 @@ import com.sap.piper.cm.ChangeManagementException
import hudson.AbortException
import util.BasePiperTest
import util.JenkinsCredentialsRule
import util.JenkinsStepRule
import util.Rules
@ -24,18 +25,8 @@ class CheckChangeInDevelopmentTest extends BasePiperTest {
.getCommonRules(this)
.around(thrown)
.around(jsr)
@Before
public void setup() {
helper.registerAllowedMethod('usernamePassword', [Map], { Map m ->
binding.setProperty('username', 'defaultUser')
binding.setProperty('password', '********')
})
helper.registerAllowedMethod('withCredentials', [List, Closure], { List l, Closure c ->
c()
})
}
.around(new JenkinsCredentialsRule(this)
.withCredentials('CM', 'anonymous', '********'))
@After
public void tearDown() {
@ -57,7 +48,7 @@ class CheckChangeInDevelopmentTest extends BasePiperTest {
assert cmUtilReceivedParams == [
changeId: '001',
endpoint: 'https://example.org/cm',
userName: 'defaultUser',
userName: 'anonymous',
password: '********',
cmclientOpts: null
]

View File

@ -8,6 +8,7 @@ import com.sap.piper.cm.ChangeManagement
import com.sap.piper.cm.ChangeManagementException
import util.BasePiperTest
import util.JenkinsCredentialsRule
import util.JenkinsStepRule
import util.JenkinsLoggingRule
import util.Rules
@ -26,25 +27,12 @@ public class TransportRequestCreateTest extends BasePiperTest {
.around(thrown)
.around(jsr)
.around(jlr)
.around(new JenkinsCredentialsRule(this)
.withCredentials('CM', 'anonymous', '********'))
@Before
public void setup() {
helper.registerAllowedMethod('usernamePassword', [Map.class], {m -> return m})
helper.registerAllowedMethod('withCredentials', [List, Closure], { l, c ->
credentialsId = l[0].credentialsId
binding.setProperty('username', 'anonymous')
binding.setProperty('password', '********')
try {
c()
} finally {
binding.setProperty('username', null)
binding.setProperty('password', null)
}
})
nullScript.commonPipelineEnvironment.configuration = [steps:
[transportRequestCreate:
[

View File

@ -8,6 +8,7 @@ import com.sap.piper.cm.ChangeManagement
import com.sap.piper.cm.ChangeManagementException
import util.BasePiperTest
import util.JenkinsCredentialsRule
import util.JenkinsStepRule
import util.JenkinsLoggingRule
import util.Rules
@ -27,25 +28,12 @@ public class TransportRequestReleaseTest extends BasePiperTest {
.around(thrown)
.around(jsr)
.around(jlr)
.around(new JenkinsCredentialsRule(this)
.withCredentials('CM', 'anonymous', '********'))
@Before
public void setup() {
helper.registerAllowedMethod('usernamePassword', [Map.class], {m -> return m})
helper.registerAllowedMethod('withCredentials', [List, Closure], { l, c ->
credentialsId = l[0].credentialsId
binding.setProperty('username', 'anonymous')
binding.setProperty('password', '********')
try {
c()
} finally {
binding.setProperty('username', null)
binding.setProperty('password', null)
}
})
nullScript.commonPipelineEnvironment.configuration = [steps:
[transportRequestRelease:
[

View File

@ -10,6 +10,7 @@ import com.sap.piper.cm.ChangeManagement
import com.sap.piper.cm.ChangeManagementException
import util.BasePiperTest
import util.JenkinsCredentialsRule
import util.JenkinsStepRule
import util.JenkinsLoggingRule
import util.Rules
@ -28,27 +29,14 @@ public class TransportRequestUploadFileTest extends BasePiperTest {
.around(thrown)
.around(jsr)
.around(jlr)
.around(new JenkinsCredentialsRule(this)
.withCredentials('CM', 'anonymous', '********'))
private Map cmUtilReceivedParams = [:]
@Before
public void setup() {
helper.registerAllowedMethod('usernamePassword', [Map.class], {m -> return m})
helper.registerAllowedMethod('withCredentials', [List, Closure], { l, c ->
credentialsId = l[0].credentialsId
binding.setProperty('username', 'anonymous')
binding.setProperty('password', '********')
try {
c()
} finally {
binding.setProperty('username', null)
binding.setProperty('password', null)
}
})
cmUtilReceivedParams.clear()
nullScript.commonPipelineEnvironment.configuration = [steps:

View File

@ -0,0 +1,74 @@
package util
import com.lesfurets.jenkins.unit.BasePipelineTest
import org.junit.Assert
import org.junit.rules.TestRule
import org.junit.runner.Description
import org.junit.runners.model.Statement
import static org.hamcrest.Matchers.containsString
import static org.hamcrest.Matchers.is
import static org.hamcrest.Matchers.not
import static org.hamcrest.Matchers.nullValue
import static org.junit.Assert.assertThat;
import org.hamcrest.Matchers
/**
* By default a user "anonymous" with password "********"
* is provided.
*
*/
class JenkinsCredentialsRule implements TestRule {
Map credentials = [:]
final BasePipelineTest testInstance
JenkinsCredentialsRule(BasePipelineTest testInstance) {
this.testInstance = testInstance
}
JenkinsCredentialsRule withCredentials(String credentialsId, String user, String passwd) {
credentials.put(credentialsId, [user: user, passwd: passwd])
return this
}
@Override
Statement apply(Statement base, Description description) {
return statement(base)
}
private Statement statement(final Statement base) {
return new Statement() {
@Override
void evaluate() throws Throwable {
testInstance.helper.registerAllowedMethod('usernamePassword', [Map.class], {m -> return m})
testInstance.helper.registerAllowedMethod('withCredentials', [List, Closure], { l, c ->
def credsId = l[0].credentialsId
def creds = credentials.get(credsId)
assertThat("Unexpected credentialsId received: '${credsId}'.",
creds, is(not(nullValue())))
binding.setProperty('username', creds?.user)
binding.setProperty('password', creds?.passwd)
try {
c()
} finally {
binding.setProperty('username', null)
binding.setProperty('password', null)
}
})
base.evaluate()
}
}
}
}