1
0
mirror of https://github.com/SAP/jenkins-library.git synced 2024-12-16 11:09:33 +02:00
sap-jenkins-library/test/groovy/CheckChangeInDevelopmentTest.groovy

213 lines
7.0 KiB
Groovy
Raw Normal View History

import org.junit.Before
import org.junit.After
import org.junit.Rule
import org.junit.Test
import org.junit.rules.ExpectedException
import org.junit.rules.RuleChain
2019-02-13 15:01:56 +02:00
import com.sap.piper.cm.BackendType
import com.sap.piper.cm.ChangeManagement
import com.sap.piper.cm.ChangeManagementException
import com.sap.piper.Utils
import hudson.AbortException
import util.BasePiperTest
import util.JenkinsCredentialsRule
2018-09-28 13:45:26 +02:00
import util.JenkinsLoggingRule
import util.JenkinsReadYamlRule
import util.JenkinsStepRule
import util.Rules
class CheckChangeInDevelopmentTest extends BasePiperTest {
private ExpectedException thrown = ExpectedException.none()
2019-01-22 10:25:42 +02:00
private JenkinsStepRule stepRule = new JenkinsStepRule(this)
private JenkinsLoggingRule loggingRule = new JenkinsLoggingRule(this)
@Rule
public RuleChain ruleChain = Rules
.getCommonRules(this)
.around(new JenkinsReadYamlRule(this))
.around(thrown)
2019-01-22 10:25:42 +02:00
.around(stepRule)
.around(loggingRule)
.around(new JenkinsCredentialsRule(this)
.withCredentials('CM', 'anonymous', '********'))
@Before
public void setup() {
Utils.metaClass.echo = { def m -> }
}
@After
public void tearDown() {
cmUtilReceivedParams.clear()
Utils.metaClass = null
}
private Map cmUtilReceivedParams = [:]
@Test
public void changeIsInStatusDevelopmentTest() {
ChangeManagement cm = getChangeManagementUtils(true)
2019-01-22 10:25:42 +02:00
stepRule.step.checkChangeInDevelopment(
2018-10-25 08:41:50 +02:00
script: nullScript,
cmUtils: cm,
changeManagement: [
type: 'SOLMAN',
endpoint: 'https://example.org/cm'],
failIfStatusIsNotInDevelopment: true)
assert cmUtilReceivedParams == [
2019-02-28 16:25:40 +02:00
docker: [
image: 'ppiper/cm-client',
options:[],
envVars:[:],
pullImage:true,
],
changeId: '001',
endpoint: 'https://example.org/cm',
credentialsId: 'CM',
cmclientOpts: ''
]
// no exception in thrown, so the change is in status 'in development'.
}
@Test
public void changeIsNotInStatusDevelopmentTest() {
thrown.expect(AbortException)
thrown.expectMessage("Change '001' is not in status 'in development'")
ChangeManagement cm = getChangeManagementUtils(false)
2019-01-22 10:25:42 +02:00
stepRule.step.checkChangeInDevelopment(
script: nullScript,
cmUtils: cm,
changeManagement: [type: 'SOLMAN',
endpoint: 'https://example.org/cm'])
}
@Test
public void changeIsNotInStatusDevelopmentButWeWouldLikeToSkipFailureTest() {
ChangeManagement cm = getChangeManagementUtils(false)
2019-01-22 10:25:42 +02:00
boolean inDevelopment = stepRule.step.checkChangeInDevelopment(
script: nullScript,
cmUtils: cm,
changeManagement: [endpoint: 'https://example.org/cm'],
failIfStatusIsNotInDevelopment: false)
assert !inDevelopment
}
@Test
public void changeDocumentIdRetrievalFailsTest() {
thrown.expect(IllegalArgumentException)
thrown.expectMessage("No changeDocumentId provided. Neither via parameter 'changeDocumentId' nor via " +
"label 'ChangeDocument\\s?:' in commit range [from: origin/master, to: HEAD].")
ChangeManagement cm = new ChangeManagement(nullScript, null) {
String getChangeDocumentId(
String filter,
String from,
String to,
String format) {
throw new ChangeManagementException('Something went wrong')
}
}
2019-01-22 10:25:42 +02:00
stepRule.step.checkChangeInDevelopment(
script: nullScript,
cmUtils: cm,
changeManagement: [type: 'SOLMAN',
endpoint: 'https://example.org/cm'])
}
@Test
public void nullChangeDocumentIdTest() {
thrown.expect(IllegalArgumentException)
thrown.expectMessage("No changeDocumentId provided. Neither via parameter 'changeDocumentId' " +
"nor via label 'ChangeDocument\\s?:' in commit range " +
"[from: origin/master, to: HEAD].")
ChangeManagement cm = getChangeManagementUtils(false, null)
2019-01-22 10:25:42 +02:00
stepRule.step.checkChangeInDevelopment(
script: nullScript,
cmUtils: cm,
changeManagement: [endpoint: 'https://example.org/cm',
type: 'SOLMAN'])
}
@Test
public void emptyChangeDocumentIdTest() {
thrown.expect(IllegalArgumentException)
thrown.expectMessage("No changeDocumentId provided. Neither via parameter 'changeDocumentId' " +
"nor via label 'ChangeDocument\\s?:' in commit range " +
"[from: origin/master, to: HEAD].")
ChangeManagement cm = getChangeManagementUtils(false, '')
2019-01-22 10:25:42 +02:00
stepRule.step.checkChangeInDevelopment(
script: nullScript,
cmUtils: cm,
changeManagement: [type: 'SOLMAN',
endpoint: 'https://example.org/cm'])
}
2018-09-28 13:45:26 +02:00
@Test
public void cmIntegrationSwichtedOffTest() {
loggingRule.expect('[INFO] Change management integration intentionally switched off.')
2018-09-28 13:45:26 +02:00
2019-01-22 10:25:42 +02:00
stepRule.step.checkChangeInDevelopment(
script: nullScript,
2018-09-28 13:45:26 +02:00
changeManagement: [type: 'NONE'])
}
2019-02-12 17:56:06 +02:00
@Test
2019-02-13 15:01:56 +02:00
public void stageConfigIsNotConsideredWithParamKeysTest() {
2019-02-12 17:56:06 +02:00
2019-02-13 15:01:56 +02:00
nullScript.commonPipelineEnvironment.configuration = [stages:[foo:[changeDocumentId:'12345']]]
2019-02-12 17:56:06 +02:00
ChangeManagement cm = getChangeManagementUtils(true, '')
2019-02-13 15:01:56 +02:00
thrown.expect(IllegalArgumentException)
thrown.expectMessage('No changeDocumentId provided.')
2019-02-12 17:56:06 +02:00
stepRule.step.checkChangeInDevelopment(
script: nullScript,
cmUtils: cm,
2019-02-13 15:01:56 +02:00
changeManagement: [type: BackendType.SOLMAN,
2019-02-12 17:56:06 +02:00
endpoint: 'https://example.org/cm'],
2019-02-13 15:01:56 +02:00
stageName: 'foo')
2019-02-12 17:56:06 +02:00
}
private ChangeManagement getChangeManagementUtils(boolean inDevelopment, String changeDocumentId = '001') {
return new ChangeManagement(nullScript, null) {
String getChangeDocumentId(
String filter,
String from,
String to,
String format) {
return changeDocumentId
}
2019-02-28 16:25:40 +02:00
boolean isChangeInDevelopment(Map docker, String changeId, String endpoint, String credentialsId, String cmclientOpts) {
cmUtilReceivedParams.docker = docker
cmUtilReceivedParams.changeId = changeId
cmUtilReceivedParams.endpoint = endpoint
cmUtilReceivedParams.credentialsId = credentialsId
cmUtilReceivedParams.cmclientOpts = cmclientOpts
return inDevelopment
}
}
}
}