1
0
mirror of https://github.com/SAP/jenkins-library.git synced 2024-12-12 10:55:20 +02:00
sap-jenkins-library/test/groovy/TransportRequestCreateTest.groovy

347 lines
12 KiB
Groovy
Raw Normal View History

import static org.hamcrest.Matchers.allOf
import static org.hamcrest.Matchers.containsString
2019-02-14 13:01:28 +02:00
import java.util.Map
import org.hamcrest.Matchers
import org.hamcrest.core.StringContains
import org.junit.After
2018-06-20 11:36:41 +02:00
import org.junit.Before
import org.junit.Rule
import org.junit.Test
import org.junit.rules.ExpectedException
import org.junit.rules.RuleChain
2018-09-25 09:12:07 +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
2018-06-20 11:36:41 +02:00
import util.BasePiperTest
import util.JenkinsCredentialsRule
2018-06-20 11:36:41 +02:00
import util.JenkinsStepRule
import util.JenkinsLoggingRule
import util.JenkinsReadYamlRule
2018-06-20 11:36:41 +02:00
import util.Rules
import hudson.AbortException
public class TransportRequestCreateTest extends BasePiperTest {
private ExpectedException thrown = new ExpectedException()
2019-01-22 10:25:42 +02:00
private JenkinsStepRule stepRule = new JenkinsStepRule(this)
private JenkinsLoggingRule loggingRule = new JenkinsLoggingRule(this)
2018-06-20 11:36:41 +02:00
@Rule
public RuleChain ruleChain = Rules.getCommonRules(this)
.around(new JenkinsReadYamlRule(this))
2018-06-20 11:36:41 +02:00
.around(thrown)
2019-01-22 10:25:42 +02:00
.around(stepRule)
.around(loggingRule)
.around(new JenkinsCredentialsRule(this)
.withCredentials('CM', 'anonymous', '********'))
2018-06-20 11:36:41 +02:00
@Before
public void setup() {
Utils.metaClass.echo = { def m -> }
2018-06-20 11:36:41 +02:00
nullScript.commonPipelineEnvironment.configuration = [general:
[changeManagement:
2018-06-20 11:36:41 +02:00
[
2018-06-28 16:24:14 +02:00
credentialsId: 'CM',
type: 'SOLMAN',
endpoint: 'https://example.org/cm',
clientOpts: '-DmyProp=myVal',
changeDocumentLabel: 'ChangeId\\s?:',
git: [from: 'origin/master',
to: 'HEAD',
format: '%b']
2018-06-20 11:36:41 +02:00
]
]
]
helper.registerAllowedMethod('addBadge', [Map], {return})
helper.registerAllowedMethod('createSummary', [Map], {return})
2018-06-20 11:36:41 +02:00
}
@After
public void tearDown() {
Utils.metaClass = null
}
2018-06-20 11:36:41 +02:00
@Test
public void changeIdNotProvidedSOLANTest() {
2018-06-20 11:36:41 +02:00
thrown.expect(IllegalArgumentException)
thrown.expectMessage("Change document id not provided (parameter: 'changeDocumentId' provided to the step call or via commit history).")
ChangeManagement cm = new ChangeManagement(nullScript) {
String getChangeDocumentId(
String from,
String to,
String label,
String format
) {
throw new ChangeManagementException('Cannot retrieve changeId from git commits.')
}
}
2019-01-22 10:25:42 +02:00
stepRule.step.transportRequestCreate(script: nullScript, developmentSystemId: '001', cmUtils: cm)
2018-06-20 11:36:41 +02:00
}
@Test
public void developmentSystemIdNotProvidedSOLMANTest() {
2018-06-20 11:36:41 +02:00
thrown.expect(IllegalArgumentException)
thrown.expectMessage("ERROR - NO VALUE AVAILABLE FOR developmentSystemId")
2018-06-20 11:36:41 +02:00
2019-01-22 10:25:42 +02:00
stepRule.step.transportRequestCreate(script: nullScript, changeDocumentId: '001')
2018-06-20 11:36:41 +02:00
}
@Test
public void createTransportRequestFailureSOLMANTest() {
2018-06-20 11:36:41 +02:00
ChangeManagement cm = new ChangeManagement(nullScript) {
2018-09-25 09:12:07 +02:00
String createTransportRequestSOLMAN(
Map docker,
2018-09-25 09:12:07 +02:00
String changeId,
String developmentSystemId,
String cmEndpoint,
String credentialId,
String clientOpts) {
throw new ChangeManagementException('Exception message.')
}
}
2018-06-20 11:36:41 +02:00
thrown.expect(AbortException)
thrown.expectMessage("Exception message.")
2018-06-20 11:36:41 +02:00
2019-01-22 10:25:42 +02:00
stepRule.step.transportRequestCreate(script: nullScript, changeDocumentId: '001', developmentSystemId: '001', cmUtils: cm)
2018-06-20 11:36:41 +02:00
}
@Test
2018-09-25 09:12:07 +02:00
public void createTransportRequestSuccessSOLMANTest() {
2018-06-20 11:36:41 +02:00
def result = [:]
ChangeManagement cm = new ChangeManagement(nullScript) {
2018-09-25 09:12:07 +02:00
String createTransportRequestSOLMAN(
Map docker,
2018-09-25 09:12:07 +02:00
String changeId,
String developmentSystemId,
String cmEndpoint,
String credentialId,
String clientOpts) {
result.docker = docker
result.changeId = changeId
result.developmentSystemId = developmentSystemId
result.cmEndpoint = cmEndpoint
result.credentialId = credentialId
result.clientOpts = clientOpts
return '001'
}
}
2019-01-22 10:25:42 +02:00
stepRule.step.transportRequestCreate(script: nullScript, changeDocumentId: '001', developmentSystemId: '001', cmUtils: cm)
2018-06-20 11:36:41 +02:00
assert nullScript.commonPipelineEnvironment.getValue('transportRequestId') == '001'
assert result == [
docker: [
image: 'ppiper/cm-client:2.0.1.0',
pullImage: true,
options: [],
envVars: [:],
],
changeId: '001',
developmentSystemId: '001',
cmEndpoint: 'https://example.org/cm',
credentialId: 'CM',
clientOpts: '-DmyProp=myVal'
]
2018-06-20 11:36:41 +02:00
assert loggingRule.log.contains("[INFO] Creating transport request for change document '001' and development system '001'.")
assert loggingRule.log.contains("[INFO] Transport Request '001' has been successfully created.")
2018-06-20 11:36:41 +02:00
}
2018-09-25 09:12:07 +02:00
@Test
public void createTransportRequestSuccessCTSTest() {
def result = [:]
ChangeManagement cm = new ChangeManagement(nullScript) {
String createTransportRequestCTS(
2019-02-28 10:59:46 +02:00
Map docker,
2018-09-25 09:12:07 +02:00
String transportType,
String targetSystemId,
String description,
String endpoint,
String credentialsId,
String clientOpts) {
2019-02-28 10:59:46 +02:00
result.docker = docker
2018-09-25 09:12:07 +02:00
result.transportType = transportType
result.targetSystemId = targetSystemId
result.description = description
result.endpoint = endpoint
result.credentialsId = credentialsId
result.clientOpts = clientOpts
return '001'
}
}
2019-01-22 10:25:42 +02:00
stepRule.step.call(script: nullScript,
transportType: 'W',
targetSystem: 'XYZ',
description: 'desc',
changeManagement: [type: 'CTS'],
cmUtils: cm)
2018-09-25 09:12:07 +02:00
assert nullScript.commonPipelineEnvironment.getValue('transportRequestId') == '001'
2019-02-28 10:59:46 +02:00
assert result == [
docker: [
image: 'ppiper/cm-client:2.0.1.0',
2019-02-28 10:59:46 +02:00
pullImage: true,
envVars: [:],
options: [],
],
transportType: 'W',
2018-09-25 09:12:07 +02:00
targetSystemId: 'XYZ',
description: 'desc',
endpoint: 'https://example.org/cm',
credentialsId: 'CM',
clientOpts: '-DmyProp=myVal'
]
assert loggingRule.log.contains("[INFO] Creating transport request.")
assert loggingRule.log.contains("[INFO] Transport Request '001' has been successfully created.")
2018-09-25 09:12:07 +02:00
}
2019-02-14 13:01:28 +02:00
@Test
public void createTransportRequestSuccessRFCTest() {
def result = [:]
ChangeManagement cm = new ChangeManagement(nullScript) {
String createTransportRequestRFC(
Map docker,
String endpoint,
String developmentInstance,
String developmentClient,
2019-02-14 13:01:28 +02:00
String credentialsId,
String description,
boolean verbose) {
2019-02-14 13:01:28 +02:00
result.docker = docker
result.endpoint = endpoint
result.developmentClient = developmentClient
result.developmentInstance= developmentInstance
result.credentialsId = credentialsId
result.description = description
result.verbose = verbose
2019-02-14 13:01:28 +02:00
return '001'
}
}
stepRule.step.transportRequestCreate(
script: nullScript,
changeManagement: [
type: 'RFC',
rfc: [
developmentInstance: '01',
developmentClient: '001',
],
endpoint: 'https://example.org/rfc',
],
developmentSystemId: '001',
description: '',
cmUtils: cm,
verbose: true)
2019-02-14 13:01:28 +02:00
assert nullScript.commonPipelineEnvironment.getValue('transportRequestId') == '001'
2019-02-14 13:01:28 +02:00
assert result == [
docker: [
image: 'rfc',
options: [],
envVars: [:],
2019-02-18 17:59:44 +02:00
pullImage: true
2019-02-14 13:01:28 +02:00
],
endpoint: 'https://example.org/rfc',
developmentClient: '001',
developmentInstance: '01',
2019-02-14 13:01:28 +02:00
credentialsId: 'CM',
description: '',
verbose: true
2019-02-14 13:01:28 +02:00
]
assert loggingRule.log.contains("[INFO] Creating transport request.")
assert loggingRule.log.contains("[INFO] Transport Request '001' has been successfully created.")
}
@Test
public void createTransportRequestFailureRFCTest() {
thrown.expect(AbortException)
thrown.expectMessage('upload failed')
ChangeManagement cm = new ChangeManagement(nullScript) {
String createTransportRequestRFC(
Map docker,
String endpoint,
String developmentClient,
String developmentInstance,
String credentialsId,
String description,
boolean verbose) {
throw new ChangeManagementException('upload failed')
}
}
stepRule.step.transportRequestCreate(
script: nullScript,
changeManagement: [
type: 'RFC',
rfc: [
developmentInstance: '01',
developmentClient: '001',
],
endpoint: 'https://example.org/rfc',
],
developmentSystemId: '001',
description: '',
cmUtils: cm)
}
@Test
public void createTransportRequestSanityChecksRFCTest() {
thrown.expect(IllegalArgumentException)
thrown.expectMessage(allOf(
containsString('changeManagement/rfc/developmentInstance'),
containsString('changeManagement/rfc/developmentClient'),
))
stepRule.step.transportRequestCreate(
script: nullScript,
changeManagement: [
type: 'RFC',
])
}
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.transportRequestCreate(script: nullScript,
2018-09-28 13:45:26 +02:00
changeManagement: [type: 'NONE'])
}
2018-06-20 11:36:41 +02:00
}