mirror of
https://github.com/SAP/jenkins-library.git
synced 2024-12-14 11:03:09 +02:00
Tests: make use of JenkinsCredentialsRule
JenkinsCredentialsRule now closer to reality since it mimics the bevavior or the credentials plugin in case a credential is not known.
This commit is contained in:
parent
c93c1079cc
commit
5cca5fddbb
@ -6,6 +6,7 @@ import org.junit.rules.ExpectedException
|
|||||||
import org.junit.rules.RuleChain
|
import org.junit.rules.RuleChain
|
||||||
import org.yaml.snakeyaml.Yaml
|
import org.yaml.snakeyaml.Yaml
|
||||||
import util.BasePiperTest
|
import util.BasePiperTest
|
||||||
|
import util.JenkinsCredentialsRule
|
||||||
import util.JenkinsEnvironmentRule
|
import util.JenkinsEnvironmentRule
|
||||||
import util.JenkinsDockerExecuteRule
|
import util.JenkinsDockerExecuteRule
|
||||||
import util.JenkinsLoggingRule
|
import util.JenkinsLoggingRule
|
||||||
@ -39,28 +40,9 @@ class CloudFoundryDeployTest extends BasePiperTest {
|
|||||||
.around(jwfr)
|
.around(jwfr)
|
||||||
.around(jedr)
|
.around(jedr)
|
||||||
.around(jer)
|
.around(jer)
|
||||||
|
.around(new JenkinsCredentialsRule(this).withCredentials('test_cfCredentialsId', 'test_cf', '********'))
|
||||||
.around(jsr) // needs to be activated after jedr, otherwise executeDocker is not mocked
|
.around(jsr) // needs to be activated after jedr, otherwise executeDocker is not mocked
|
||||||
|
|
||||||
@Before
|
|
||||||
void init() throws Throwable {
|
|
||||||
helper.registerAllowedMethod('usernamePassword', [Map], { m -> return m })
|
|
||||||
helper.registerAllowedMethod('withCredentials', [List, Closure], { l, c ->
|
|
||||||
if(l[0].credentialsId == 'test_cfCredentialsId') {
|
|
||||||
binding.setProperty('username', 'test_cf')
|
|
||||||
binding.setProperty('password', '********')
|
|
||||||
} else if(l[0].credentialsId == 'test_camCredentialsId') {
|
|
||||||
binding.setProperty('username', 'test_cam')
|
|
||||||
binding.setProperty('password', '********')
|
|
||||||
}
|
|
||||||
try {
|
|
||||||
c()
|
|
||||||
} finally {
|
|
||||||
binding.setProperty('username', null)
|
|
||||||
binding.setProperty('password', null)
|
|
||||||
}
|
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
void testNoTool() throws Exception {
|
void testNoTool() throws Exception {
|
||||||
nullScript.commonPipelineEnvironment.configuration = [
|
nullScript.commonPipelineEnvironment.configuration = [
|
||||||
|
@ -5,8 +5,10 @@ import org.junit.rules.TemporaryFolder
|
|||||||
import org.junit.BeforeClass
|
import org.junit.BeforeClass
|
||||||
import org.junit.ClassRule
|
import org.junit.ClassRule
|
||||||
import org.junit.Ignore
|
import org.junit.Ignore
|
||||||
|
|
||||||
import org.hamcrest.BaseMatcher
|
import org.hamcrest.BaseMatcher
|
||||||
import org.hamcrest.Description
|
import org.hamcrest.Description
|
||||||
|
import org.jenkinsci.plugins.credentialsbinding.impl.CredentialNotFoundException
|
||||||
import org.junit.Assert
|
import org.junit.Assert
|
||||||
import org.junit.Before
|
import org.junit.Before
|
||||||
import org.junit.Rule
|
import org.junit.Rule
|
||||||
@ -14,6 +16,7 @@ import org.junit.Test
|
|||||||
import org.junit.rules.ExpectedException
|
import org.junit.rules.ExpectedException
|
||||||
import org.junit.rules.RuleChain
|
import org.junit.rules.RuleChain
|
||||||
import util.BasePiperTest
|
import util.BasePiperTest
|
||||||
|
import util.JenkinsCredentialsRule
|
||||||
import util.JenkinsLoggingRule
|
import util.JenkinsLoggingRule
|
||||||
import util.JenkinsShellCallRule
|
import util.JenkinsShellCallRule
|
||||||
import util.JenkinsStepRule
|
import util.JenkinsStepRule
|
||||||
@ -37,6 +40,9 @@ class NeoDeployTest extends BasePiperTest {
|
|||||||
.around(thrown)
|
.around(thrown)
|
||||||
.around(jlr)
|
.around(jlr)
|
||||||
.around(jscr)
|
.around(jscr)
|
||||||
|
.around(new JenkinsCredentialsRule(this)
|
||||||
|
.withCredentials('myCredentialsId', 'anonymous', '********')
|
||||||
|
.withCredentials('CI_CREDENTIALS_ID', 'defaultUser', '********'))
|
||||||
.around(jsr)
|
.around(jsr)
|
||||||
|
|
||||||
private static workspacePath
|
private static workspacePath
|
||||||
@ -63,24 +69,6 @@ class NeoDeployTest extends BasePiperTest {
|
|||||||
|
|
||||||
helper.registerAllowedMethod('dockerExecute', [Map, Closure], null)
|
helper.registerAllowedMethod('dockerExecute', [Map, Closure], null)
|
||||||
helper.registerAllowedMethod('fileExists', [String], { s -> return new File(workspacePath, s).exists() })
|
helper.registerAllowedMethod('fileExists', [String], { s -> return new File(workspacePath, s).exists() })
|
||||||
helper.registerAllowedMethod('usernamePassword', [Map], { m -> return m })
|
|
||||||
helper.registerAllowedMethod('withCredentials', [List, Closure], { l, c ->
|
|
||||||
if(l[0].credentialsId == 'myCredentialsId') {
|
|
||||||
binding.setProperty('username', 'anonymous')
|
|
||||||
binding.setProperty('password', '********')
|
|
||||||
} else if(l[0].credentialsId == 'CI_CREDENTIALS_ID') {
|
|
||||||
binding.setProperty('username', 'defaultUser')
|
|
||||||
binding.setProperty('password', '********')
|
|
||||||
}
|
|
||||||
try {
|
|
||||||
c()
|
|
||||||
} finally {
|
|
||||||
binding.setProperty('username', null)
|
|
||||||
binding.setProperty('password', null)
|
|
||||||
}
|
|
||||||
|
|
||||||
})
|
|
||||||
|
|
||||||
helper.registerAllowedMethod('sh', [Map], { Map m -> getVersionWithEnvVars(m) })
|
helper.registerAllowedMethod('sh', [Map], { Map m -> getVersionWithEnvVars(m) })
|
||||||
|
|
||||||
nullScript.commonPipelineEnvironment.configuration = [steps:[neoDeploy: [host: 'test.deploy.host.com', account: 'trialuser123']]]
|
nullScript.commonPipelineEnvironment.configuration = [steps:[neoDeploy: [host: 'test.deploy.host.com', account: 'trialuser123']]]
|
||||||
@ -176,8 +164,7 @@ class NeoDeployTest extends BasePiperTest {
|
|||||||
@Test
|
@Test
|
||||||
void badCredentialsIdTest() {
|
void badCredentialsIdTest() {
|
||||||
|
|
||||||
thrown.expect(MissingPropertyException)
|
thrown.expect(CredentialNotFoundException)
|
||||||
thrown.expectMessage('No such property: username')
|
|
||||||
|
|
||||||
jsr.step.call(script: nullScript,
|
jsr.step.call(script: nullScript,
|
||||||
archivePath: archiveName,
|
archivePath: archiveName,
|
||||||
|
@ -14,6 +14,7 @@ import static org.hamcrest.Matchers.nullValue
|
|||||||
import static org.junit.Assert.assertThat;
|
import static org.junit.Assert.assertThat;
|
||||||
|
|
||||||
import org.hamcrest.Matchers
|
import org.hamcrest.Matchers
|
||||||
|
import org.jenkinsci.plugins.credentialsbinding.impl.CredentialNotFoundException
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* By default a user "anonymous" with password "********"
|
* By default a user "anonymous" with password "********"
|
||||||
@ -47,16 +48,19 @@ class JenkinsCredentialsRule implements TestRule {
|
|||||||
@Override
|
@Override
|
||||||
void evaluate() throws Throwable {
|
void evaluate() throws Throwable {
|
||||||
|
|
||||||
testInstance.helper.registerAllowedMethod('usernamePassword', [Map.class], {m -> return m})
|
testInstance.helper.registerAllowedMethod('usernamePassword', [Map.class],
|
||||||
|
{ m -> if (credentials.keySet().contains(m.credentialsId)) 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('withCredentials', [List, Closure], { l, c ->
|
testInstance.helper.registerAllowedMethod('withCredentials', [List, Closure], { l, c ->
|
||||||
|
|
||||||
def credsId = l[0].credentialsId
|
def credsId = l[0].credentialsId
|
||||||
def creds = credentials.get(credsId)
|
def creds = credentials.get(credsId)
|
||||||
|
|
||||||
assertThat("Unexpected credentialsId received: '${credsId}'.",
|
|
||||||
creds, is(not(nullValue())))
|
|
||||||
|
|
||||||
binding.setProperty('username', creds?.user)
|
binding.setProperty('username', creds?.user)
|
||||||
binding.setProperty('password', creds?.passwd)
|
binding.setProperty('password', creds?.passwd)
|
||||||
try {
|
try {
|
||||||
|
Loading…
Reference in New Issue
Block a user