mirror of
https://github.com/SAP/jenkins-library.git
synced 2024-12-12 10:55:20 +02:00
Use JenkinsCredentialsRule
This commit is contained in:
parent
4e144b80a1
commit
a43760d34a
@ -36,6 +36,9 @@ class WhitesourceExecuteScanTest extends BasePiperTest {
|
||||
.around(loggingRule)
|
||||
.around(writeFileRule)
|
||||
.around(stepRule)
|
||||
.around(new JenkinsCredentialsRule(this)
|
||||
.withCredentials('ID-123456789', 'token-0815')
|
||||
.withCredentials('ID-9876543', 'token-0816'))
|
||||
|
||||
def whitesourceOrgAdminRepositoryStub
|
||||
def whitesourceStub
|
||||
@ -45,52 +48,6 @@ class WhitesourceExecuteScanTest extends BasePiperTest {
|
||||
|
||||
@Before
|
||||
void init() {
|
||||
def credentialsStore = ['ID-123456789': 'token-0815', 'ID-9876543': 'token-0816', 'ID-abcdefg': ['testUser', 'testPassword']]
|
||||
def withCredentialsBindings
|
||||
helper.registerAllowedMethod('string', [Map], {
|
||||
m ->
|
||||
withCredentialsBindings = ["${m.credentialsId}": "${m.variable}"]
|
||||
return m
|
||||
})
|
||||
helper.registerAllowedMethod('usernamePassword', [Map], {
|
||||
m ->
|
||||
withCredentialsBindings = ["${m.credentialsId}": ["${m.usernameVariable}", "${m.passwordVariable}"]]
|
||||
return m
|
||||
})
|
||||
helper.registerAllowedMethod('withCredentials', [List.class, Closure.class], {
|
||||
l, body ->
|
||||
def index = 0
|
||||
withCredentialsBindings.each {
|
||||
entry ->
|
||||
if(entry.value instanceof List) {
|
||||
entry.value.each {
|
||||
subEntry ->
|
||||
def value = credentialsStore[entry.key]
|
||||
getBinding().setProperty(subEntry, value[index])
|
||||
index++
|
||||
|
||||
}
|
||||
} else {
|
||||
getBinding().setProperty(entry.value, credentialsStore[entry.key])
|
||||
}
|
||||
}
|
||||
try {
|
||||
body()
|
||||
} finally {
|
||||
withCredentialsBindings.each {
|
||||
entry ->
|
||||
if(entry.value instanceof List) {
|
||||
entry.value.each {
|
||||
subEntry ->
|
||||
getBinding().setProperty(subEntry, null)
|
||||
|
||||
}
|
||||
} else {
|
||||
getBinding().setProperty(entry.value, null)
|
||||
}
|
||||
}
|
||||
}
|
||||
})
|
||||
helper.registerAllowedMethod("archiveArtifacts", [Map.class], { m ->
|
||||
if (m.artifacts == null) {
|
||||
throw new Exception('artifacts cannot be null')
|
||||
|
@ -14,6 +14,7 @@ import org.jenkinsci.plugins.credentialsbinding.impl.CredentialNotFoundException
|
||||
class JenkinsCredentialsRule implements TestRule {
|
||||
|
||||
Map credentials = [:]
|
||||
Map bindingTypes = [:]
|
||||
|
||||
final BasePipelineTest testInstance
|
||||
|
||||
@ -26,6 +27,11 @@ class JenkinsCredentialsRule implements TestRule {
|
||||
return this
|
||||
}
|
||||
|
||||
JenkinsCredentialsRule withCredentials(String credentialsId, String token) {
|
||||
credentials.put(credentialsId, [token: token])
|
||||
return this
|
||||
}
|
||||
|
||||
@Override
|
||||
Statement apply(Statement base, Description description) {
|
||||
return statement(base)
|
||||
@ -39,7 +45,17 @@ class JenkinsCredentialsRule implements TestRule {
|
||||
void evaluate() throws Throwable {
|
||||
|
||||
testInstance.helper.registerAllowedMethod('usernamePassword', [Map.class],
|
||||
{ m -> if (credentials.keySet().contains(m.credentialsId)) return m;
|
||||
{ m ->
|
||||
if (credentials.keySet().contains(m.credentialsId)) { bindingTypes[m.credentialsId] = 'usernamePassword'; return m }
|
||||
// this is what really happens in case of an unknown credentials id,
|
||||
// checked with reality using credentials plugin 2.1.18.
|
||||
throw new CredentialNotFoundException(
|
||||
"Could not find credentials entry with ID '${m.credentialsId}'")
|
||||
})
|
||||
|
||||
testInstance.helper.registerAllowedMethod('string', [Map.class],
|
||||
{ m ->
|
||||
if (credentials.keySet().contains(m.credentialsId)) { bindingTypes[m.credentialsId] = 'string'; return m }
|
||||
// this is what really happens in case of an unknown credentials id,
|
||||
// checked with reality using credentials plugin 2.1.18.
|
||||
throw new CredentialNotFoundException(
|
||||
@ -49,17 +65,38 @@ class JenkinsCredentialsRule implements TestRule {
|
||||
testInstance.helper.registerAllowedMethod('withCredentials', [List, Closure], { config, closure ->
|
||||
|
||||
def credsId = config[0].credentialsId
|
||||
def passwordVariable = config[0].passwordVariable
|
||||
def usernameVariable = config[0].usernameVariable
|
||||
def credentialsBindingType = bindingTypes.get(credsId)
|
||||
def creds = credentials.get(credsId)
|
||||
|
||||
def tokenVariable, usernameVariable, passwordVariable, prepare, destruct
|
||||
if(credentialsBindingType == "usernamePassword") {
|
||||
passwordVariable = config[0].passwordVariable
|
||||
usernameVariable = config[0].usernameVariable
|
||||
prepare = {
|
||||
binding.setProperty(usernameVariable, creds?.user)
|
||||
binding.setProperty(passwordVariable, creds?.passwd)
|
||||
}
|
||||
destruct = {
|
||||
binding.setProperty(usernameVariable, null)
|
||||
binding.setProperty(passwordVariable, null)
|
||||
}
|
||||
} else if(credentialsBindingType == "string") {
|
||||
tokenVariable = config[0].variable
|
||||
prepare = {
|
||||
binding.setProperty(tokenVariable, creds?.token)
|
||||
}
|
||||
destruct = {
|
||||
binding.setProperty(tokenVariable, null)
|
||||
}
|
||||
} else {
|
||||
throw new RuntimeException("Unknown binding type")
|
||||
}
|
||||
|
||||
prepare()
|
||||
try {
|
||||
closure()
|
||||
} finally {
|
||||
binding.setProperty(usernameVariable, null)
|
||||
binding.setProperty(passwordVariable, null)
|
||||
destruct()
|
||||
}
|
||||
})
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user