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

upload file to transport for CTS use case

This commit is contained in:
Marcus Holl 2018-09-24 14:06:48 +02:00
parent 141210beb2
commit f34308ffe0
5 changed files with 130 additions and 31 deletions

View File

@ -0,0 +1,5 @@
package com.sap.piper.cm;
public enum BackendType {
SOLMAN, CTS, NONE
}

View File

@ -9,7 +9,6 @@ import hudson.AbortException
public class ChangeManagement implements Serializable {
public enum BackendType {SOLMAN, CTS, NONE}
private script
private GitUtils gitUtils
@ -89,11 +88,27 @@ public class ChangeManagement implements Serializable {
}
void uploadFileToTransportRequest(String changeId, String transportRequestId, String applicationId, String filePath, String endpoint, String credentialsId, String cmclientOpts = '') {
int rc = executeWithCredentials(BackendType.SOLMAN, endpoint, credentialsId, 'upload-file-to-transport', ['-cID', changeId,
'-tID', transportRequestId,
applicationId, "\"$filePath\""],
cmclientOpts) as int
void uploadFileToTransportRequest(BackendType type, String changeId, String transportRequestId, String applicationId, String filePath, String endpoint, String credentialsId, String cmclientOpts = '') {
def args = null
if(type == BackendType.SOLMAN) {
args = ['-cID', changeId,
'-tID', transportRequestId,
applicationId, "\"$filePath\""]
} else if (type == BackendType.CTS) {
args = ['-tID', transportRequestId,
"\"$filePath\""]
} else {
throw new IllegalArgumentException("Invalid backend type: ${type}")
}
int rc = executeWithCredentials(type,
endpoint,
credentialsId,
'upload-file-to-transport',
args,
cmclientOpts) as int
if(rc == 0) {
return

View File

@ -6,6 +6,7 @@ import org.junit.Test
import org.junit.rules.ExpectedException
import org.junit.rules.RuleChain
import com.sap.piper.cm.BackendType
import com.sap.piper.cm.ChangeManagement
import com.sap.piper.cm.ChangeManagementException
@ -51,7 +52,11 @@ public class TransportRequestUploadFileTest extends BasePiperTest {
}
@Test
public void changeDocumentIdNotProvidedTest() {
public void changeDocumentIdNotProvidedSOLMANTest() {
// we expect the failure only for SOLMAN (which is the default).
// Use case for CTS without change document id is checked by the
// straight forward test case for CTS
thrown.expect(IllegalArgumentException)
thrown.expectMessage("Change document id not provided (parameter: 'changeDocumentId' or via commit history).")
@ -91,7 +96,11 @@ public class TransportRequestUploadFileTest extends BasePiperTest {
}
@Test
public void applicationIdNotProvidedTest() {
public void applicationIdNotProvidedSOLMANTest() {
// we expect the failure only for SOLMAN (which is the default).
// Use case for CTS without applicationId is checked by the
// straight forward test case for CTS
thrown.expect(IllegalArgumentException)
thrown.expectMessage("ERROR - NO VALUE AVAILABLE FOR applicationId")
@ -112,7 +121,7 @@ public class TransportRequestUploadFileTest extends BasePiperTest {
public void uploadFileToTransportRequestFailureTest() {
ChangeManagement cm = new ChangeManagement(nullScript) {
void uploadFileToTransportRequest(ChangeManagement.BackendType type,
void uploadFileToTransportRequest(BackendType type,
String changeId,
String transportRequestId,
String applicationId,
@ -136,13 +145,59 @@ public class TransportRequestUploadFileTest extends BasePiperTest {
}
@Test
public void uploadFileToTransportRequestSuccessTest() {
public void uploadFileToTransportRequestCTSSuccessTest() {
jlr.expect("[INFO] Uploading file '/path' to transport request '002'.")
jlr.expect("[INFO] File '/path' has been successfully uploaded to transport request '002'.")
ChangeManagement cm = new ChangeManagement(nullScript) {
void uploadFileToTransportRequest(BackendType type,
String changeId,
String transportRequestId,
String applicationId,
String filePath,
String endpoint,
String credentialsId,
String cmclientOpts) {
cmUtilReceivedParams.type = type
cmUtilReceivedParams.changeId = changeId
cmUtilReceivedParams.transportRequestId = transportRequestId
cmUtilReceivedParams.applicationId = applicationId
cmUtilReceivedParams.filePath = filePath
cmUtilReceivedParams.endpoint = endpoint
cmUtilReceivedParams.credentialsId = credentialsId
cmUtilReceivedParams.cmclientOpts = cmclientOpts
}
}
jsr.step.call(script: nullScript,
changeManagement: [type: 'CTS'],
transportRequestId: '002',
filePath: '/path',
cmUtils: cm)
assert cmUtilReceivedParams ==
[
type: BackendType.CTS,
changeId: null,
transportRequestId: '002',
applicationId: null,
filePath: '/path',
endpoint: 'https://example.org/cm',
credentialsId: 'CM',
cmclientOpts: ''
]
}
@Test
public void uploadFileToTransportRequestSOLMANSuccessTest() {
jlr.expect("[INFO] Uploading file '/path' to transport request '002' of change document '001'.")
jlr.expect("[INFO] File '/path' has been successfully uploaded to transport request '002' of change document '001'.")
ChangeManagement cm = new ChangeManagement(nullScript) {
void uploadFileToTransportRequest(ChangeManagement.BackendType type,
void uploadFileToTransportRequest(BackendType type,
String changeId,
String transportRequestId,
String applicationId,
@ -171,7 +226,7 @@ public class TransportRequestUploadFileTest extends BasePiperTest {
assert cmUtilReceivedParams ==
[
type: ChangeManagement.BackendType.SOLMAN,
type: BackendType.SOLMAN,
changeId: '001',
transportRequestId: '002',
applicationId: 'app',
@ -190,7 +245,7 @@ public class TransportRequestUploadFileTest extends BasePiperTest {
[applicationId: 'AppIdfromConfig']]])
ChangeManagement cm = new ChangeManagement(nullScript) {
void uploadFileToTransportRequest(ChangeManagement.BackendType type,
void uploadFileToTransportRequest(BackendType type,
String changeId,
String transportRequestId,
String applicationId,
@ -220,7 +275,7 @@ public class TransportRequestUploadFileTest extends BasePiperTest {
nullScript.commonPipelineEnvironment.setMtarFilePath('/path2')
ChangeManagement cm = new ChangeManagement(nullScript) {
void uploadFileToTransportRequest(ChangeManagement.BackendType type,
void uploadFileToTransportRequest(BackendType type,
String changeId,
String transportRequestId,
String applicationId,
@ -250,7 +305,7 @@ public class TransportRequestUploadFileTest extends BasePiperTest {
nullScript.commonPipelineEnvironment.setMtarFilePath('/path2')
ChangeManagement cm = new ChangeManagement(nullScript) {
void uploadFileToTransportRequest(ChangeManagement.BackendType type,
void uploadFileToTransportRequest(BackendType type,
String changeId,
String transportRequestId,
String applicationId,
@ -279,7 +334,7 @@ public class TransportRequestUploadFileTest extends BasePiperTest {
thrown.expectMessage('Upload failure.')
ChangeManagement cm = new ChangeManagement(nullScript) {
void uploadFileToTransportRequest(ChangeManagement.BackendType type,
void uploadFileToTransportRequest(BackendType type,
String changeId,
String transportRequestId,
String applicationId,

View File

@ -130,7 +130,7 @@ public class ChangeManagementTest extends BasePiperTest {
@Test
public void testGetCommandLineWithoutCMClientOpts() {
String commandLine = new ChangeManagement(nullScript, null)
.getCMCommandLine(ChangeManagement.BackendType.SOLMAN,
.getCMCommandLine(BackendType.SOLMAN,
'https://example.org/cm',
"me",
"topSecret",
@ -144,7 +144,7 @@ public class ChangeManagementTest extends BasePiperTest {
@Test
public void testGetCommandLineWithCMClientOpts() {
String commandLine = new ChangeManagement(nullScript, null)
.getCMCommandLine(ChangeManagement.BackendType.SOLMAN,
.getCMCommandLine(BackendType.SOLMAN,
'https://example.org/cm',
"me",
"topSecret",
@ -176,7 +176,8 @@ public void testGetCommandLineWithCMClientOpts() {
thrown.expectMessage('Cannot upload file \'/path\' for change document \'001\''+
' with transport request \'002\'. Return code from cmclient: 1.')
new ChangeManagement(nullScript).uploadFileToTransportRequest('001',
new ChangeManagement(nullScript).uploadFileToTransportRequest(BackendType.SOLMAN,
'001',
'002',
'XXX',
'/path',
@ -185,12 +186,14 @@ public void testGetCommandLineWithCMClientOpts() {
}
@Test
public void testUploadFileToTransportSucceeds() {
public void testUploadFileToTransportSucceedsSOLMAN() {
// the regex provided below is an implicit check that the command line is fine.
script.setReturnValue(JenkinsShellCallRule.Type.REGEX,, 'upload-file-to-transport.*-cID 001 -tID 002 XXX "/path"', 0)
script.setReturnValue(JenkinsShellCallRule.Type.REGEX, 'upload-file-to-transport.*-cID 001 -tID 002 XXX "/path"', 0)
new ChangeManagement(nullScript).uploadFileToTransportRequest('001',
new ChangeManagement(nullScript).uploadFileToTransportRequest(
BackendType.SOLMAN,
'001',
'002',
'XXX',
'/path',
@ -201,6 +204,25 @@ public void testGetCommandLineWithCMClientOpts() {
// the command line.
}
@Test
public void testUploadFileToTransportSucceedsCTS() {
// the regex provided below is an implicit check that the command line is fine.
script.setReturnValue(JenkinsShellCallRule.Type.REGEX, '-t CTS upload-file-to-transport -tID 002 "/path"', 0)
new ChangeManagement(nullScript).uploadFileToTransportRequest(
BackendType.CTS,
null,
'002',
null,
'/path',
'https://example.org/cm',
'me')
// no assert required here, since the regex registered above to the script rule is an implicit check for
// the command line.
}
@Test
public void testUploadFileToTransportFails() {
@ -210,7 +232,8 @@ public void testGetCommandLineWithCMClientOpts() {
script.setReturnValue(JenkinsShellCallRule.Type.REGEX,, 'upload-file-to-transport', 1)
new ChangeManagement(nullScript).uploadFileToTransportRequest('001',
new ChangeManagement(nullScript).uploadFileToTransportRequest(BackendType.SOLMAN,
'001',
'002',
'XXX',
'/path',

View File

@ -5,6 +5,7 @@ import groovy.transform.Field
import com.sap.piper.ConfigurationHelper
import com.sap.piper.ConfigurationMerger
import com.sap.piper.cm.ChangeManagement
import com.sap.piper.cm.BackendType
import com.sap.piper.cm.ChangeManagementException
import hudson.AbortException
@ -39,7 +40,6 @@ def call(parameters = [:]) {
.mixinStageConfig(script.commonPipelineEnvironment, parameters.stageName?:env.STAGE_NAME, stepConfigurationKeys)
.mixin(parameters, parameterKeys)
.addIfEmpty('filePath', script.commonPipelineEnvironment.getMtarFilePath())
.withMandatoryProperty('applicationId')
.withMandatoryProperty('changeManagement/changeDocumentLabel')
.withMandatoryProperty('changeManagement/clientOpts')
.withMandatoryProperty('changeManagement/credentialsId')
@ -54,19 +54,19 @@ def call(parameters = [:]) {
new Utils().pushToSWA([step: STEP_NAME, stepParam1: configuration.changeManagement.type], configuration)
ChangeManagement.BackendType backendType
BackendType backendType
try {
backendType = configuration.changeManagement.type as ChangeManagement.BackendType
backendType = configuration.changeManagement.type as BackendType
} catch(IllegalArgumentException e) {
error "Invalid backend type: '${configuration.changeManagement.type}'. " +
"Valid values: [${ChangeManagement.BackendType.values().join(', ')}]. " +
"Valid values: [${BackendType.values().join(', ')}]. " +
"Configuration: 'changeManagement/type'."
}
def changeDocumentId = null
if(backendType == ChangeManagement.BackendType.SOLMAN) {
if(backendType == BackendType.SOLMAN) {
changeDocumentId = configuration.changeDocumentId
@ -125,10 +125,11 @@ def call(parameters = [:]) {
.mixin([changeDocumentId: changeDocumentId?.trim() ?: null,
transportRequestId: transportRequestId?.trim() ?: null], ['changeDocumentId', 'transportRequestId'] as Set)
if(backendType == ChangeManagement.BackendType.SOLMAN) {
if(backendType == BackendType.SOLMAN) {
configHelper
.withMandatoryProperty('changeDocumentId',
"Change document id not provided (parameter: \'changeDocumentId\' or via commit history).")
.withMandatoryProperty('applicationId')
}
configuration = configHelper
.withMandatoryProperty('transportRequestId',
@ -136,7 +137,7 @@ def call(parameters = [:]) {
.use()
def uploadingMessage = ["[INFO] Uploading file '${configuration.filePath}' to transport request '${configuration.transportRequestId}'"]
if(backendType == ChangeManagement.BackendType.SOLMAN)
if(backendType == BackendType.SOLMAN)
uploadingMessage << " of change document '${configuration.changeDocumentId}'"
uploadingMessage << '.'
@ -160,7 +161,7 @@ def call(parameters = [:]) {
def uploadedMessage = ["[INFO] File '${configuration.filePath}' has been successfully uploaded to transport request '${configuration.transportRequestId}'"]
if(backendType == ChangeManagement.BackendType.SOLMAN)
if(backendType == BackendType.SOLMAN)
uploadedMessage << " of change document '${configuration.changeDocumentId}'"
uploadedMessage << '.'
echo uploadedMessage.join()