From 4a9c0695ca2aada2e6b9edcd44ebd02e66916c0d Mon Sep 17 00:00:00 2001 From: Marcus Holl Date: Tue, 10 Jul 2018 16:33:23 +0200 Subject: [PATCH 01/12] transportRequestRelease: get changeDocumentId from commit history --- .../docs/steps/transportRequestRelease.md | 8 ++++ .../groovy/TransportRequestReleaseTest.groovy | 18 ++++++-- vars/transportRequestRelease.groovy | 45 +++++++++++++++++-- 3 files changed, 64 insertions(+), 7 deletions(-) diff --git a/documentation/docs/steps/transportRequestRelease.md b/documentation/docs/steps/transportRequestRelease.md index 5e1efb12b..c88eb9fc3 100644 --- a/documentation/docs/steps/transportRequestRelease.md +++ b/documentation/docs/steps/transportRequestRelease.md @@ -14,12 +14,20 @@ Releases a Transport Request for a Change Document on the Solution Manager. | `transportRequestId`| yes | | | | `credentialsId` | yes | | | | `endpoint` | yes | | | +| `gitFrom` | no | `origin/master` | | +| `gitTo` | no | `HEAD` | | +| `gitChangeDocumentLabel` | no | `ChangeDocument\s?:` | regex pattern | +| `gitFormat` | no | `%b` | see `git log --help` | * `script` - The common script environment of the Jenkinsfile running. Typically the reference to the script calling the pipeline step is provided with the `this` parameter, as in `script: this`. This allows the function to access the [`commonPipelineEnvironment`](commonPipelineEnvironment.md) for retrieving, for example, configuration parameters. * `changeDocumentId` - The id of the change document related to the transport request to release. * `transportRequestId` - The id of the transport request to release. * `credentialsId` - The credentials to connect to the Solution Manager. * `endpoint` - The address of the Solution Manager. +* `gitFrom` - The starting point for retrieving the change document id +* `gitTo` - The end point for retrieving the change document id +* `gitChangeDocumentLabel` - A pattern used for identifying lines holding the change document id. +* `gitFormat` - Specifies what part of the commit is scanned. By default the body of the commit message is scanned. ## Step configuration The following parameters can also be specified as step parameters using the global configuration file: diff --git a/test/groovy/TransportRequestReleaseTest.groovy b/test/groovy/TransportRequestReleaseTest.groovy index dcb023421..6bb8fc1f1 100644 --- a/test/groovy/TransportRequestReleaseTest.groovy +++ b/test/groovy/TransportRequestReleaseTest.groovy @@ -4,6 +4,9 @@ import org.junit.Test import org.junit.rules.ExpectedException import org.junit.rules.RuleChain +import com.sap.piper.cm.ChangeManagement +import com.sap.piper.cm.ChangeManagementException + import util.BasePiperTest import util.JenkinsStepRule import util.JenkinsLoggingRule @@ -57,10 +60,19 @@ public class TransportRequestReleaseTest extends BasePiperTest { @Test public void changeIdNotProvidedTest() { - thrown.expect(AbortException) - thrown.expectMessage("Change document id not provided (parameter: 'changeDocumentId').") + ChangeManagement cm = new ChangeManagement(nullScript) { + String getChangeDocumentId(String from, + String to, + String label, + String format) { + throw new ChangeManagementException('Cannot retrieve change documentId') + } + } - jsr.step.call(script: nullScript, transportRequestId: '001') + thrown.expect(AbortException) + thrown.expectMessage("Change document id not provided (parameter: 'changeDocumentId' or via commit history).") + + jsr.step.call(script: nullScript, transportRequestId: '001', cmUtils: cm) } @Test diff --git a/vars/transportRequestRelease.groovy b/vars/transportRequestRelease.groovy index bfa5c44b4..e549377c6 100644 --- a/vars/transportRequestRelease.groovy +++ b/vars/transportRequestRelease.groovy @@ -14,12 +14,20 @@ import hudson.AbortException 'changeDocumentId', 'transportRequestId', 'credentialsId', - 'endpoint' + 'endpoint', + 'gitChangeDocumentLabel', + 'gitFormat', + 'gitFrom', + 'gitTo' ] @Field Set stepConfigurationKeys = [ 'credentialsId', - 'endpoint' + 'endpoint', + 'gitChangeDocumentLabel', + 'gitFormat', + 'gitFrom', + 'gitTo' ] def call(parameters = [:]) { @@ -28,14 +36,43 @@ def call(parameters = [:]) { def script = parameters?.script ?: [commonPipelineEnvironment: commonPipelineEnvironment] - ChangeManagement cm = new ChangeManagement(script) + ChangeManagement cm = parameters.cmUtils ?: new ChangeManagement(script) Map configuration = ConfigurationMerger.merge(script, STEP_NAME, parameters, parameterKeys, stepConfigurationKeys) def changeDocumentId = configuration.changeDocumentId - if(!changeDocumentId) throw new AbortException("Change document id not provided (parameter: 'changeDocumentId').") + + if(changeDocumentId?.trim()) { + + echo "[INFO] ChangeDocumentId '${changeDocumentId}' retrieved from parameters." + + } else { + + echo "[INFO] Retrieving ChangeDocumentId from commit history [from: ${configuration.gitFrom}, to: ${configuration.gitTo}]." + + "Searching for pattern '${configuration.gitChangeDocumentLabel}'. Searching with format '${configuration.gitFormat}'." + + try { + changeDocumentId = cm.getChangeDocumentId( + configuration.gitFrom, + configuration.gitTo, + configuration.gitChangeDocumentLabel, + configuration.gitFormat + ) + + echo "[INFO] ChangeDocumentId '${changeDocumentId}' retrieved from commit history" + + } catch(ChangeManagementException ex) { + echo "[WARN] Cannot retrieve changeDocumentId from commit history: ${ex.getMessage()}." + } + } + + if(!changeDocumentId) { + throw new AbortException("Change document id not provided (parameter: 'changeDocumentId' or via commit history).") + } + + boolean isInDevelopment def transportRequestId = configuration.transportRequestId if(!transportRequestId) throw new AbortException("Transport Request id not provided (parameter: 'transportRequestId').") From 5c657f2253b8da04ef5da332e19f6031aeb99049 Mon Sep 17 00:00:00 2001 From: Marcus Holl Date: Thu, 12 Jul 2018 14:14:44 +0200 Subject: [PATCH 02/12] Introduce getTransportRequestId in ChangeManagement utils --- src/com/sap/piper/cm/ChangeManagement.groovy | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/src/com/sap/piper/cm/ChangeManagement.groovy b/src/com/sap/piper/cm/ChangeManagement.groovy index 679d17876..f33ae313a 100644 --- a/src/com/sap/piper/cm/ChangeManagement.groovy +++ b/src/com/sap/piper/cm/ChangeManagement.groovy @@ -27,6 +27,16 @@ public class ChangeManagement implements Serializable { return getLabeledItem('ChangeDocumentId', from, to, label, format) } + String getTransportRequestId( + String from = 'origin/master', + String to = 'HEAD', + String label = 'TransportRequest\\s?:', + String format = '%b' + ) { + + return getLabeledItem('ChangeDocumentId', from, to, label, format) + } + private String getLabeledItem( String name, String from, From 1a32ac46d8f2751e2b5bc76e89a213f79d94cfe4 Mon Sep 17 00:00:00 2001 From: Marcus Holl Date: Thu, 12 Jul 2018 15:58:45 +0200 Subject: [PATCH 03/12] Prepare transportRequestRelease for layered tests --- vars/transportRequestRelease.groovy | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/vars/transportRequestRelease.groovy b/vars/transportRequestRelease.groovy index fa6899b27..5e7a7d9d0 100644 --- a/vars/transportRequestRelease.groovy +++ b/vars/transportRequestRelease.groovy @@ -33,7 +33,7 @@ def call(parameters = [:]) { def script = parameters?.script ?: [commonPipelineEnvironment: commonPipelineEnvironment] - ChangeManagement cm = new ChangeManagement(script) + ChangeManagement cm = parameters.cmUtils ?: new ChangeManagement(script) Map configuration = ConfigurationHelper .loadStepDefaults(this) From b73df0f682c81be5690d42301a9a7383d06d6a16 Mon Sep 17 00:00:00 2001 From: Marcus Holl Date: Thu, 12 Jul 2018 15:23:12 +0200 Subject: [PATCH 04/12] ReleaseTransportRequest: Read transportRequestId from commit history --- .../docs/steps/transportRequestRelease.md | 11 ++++++ resources/default_pipeline_environment.yml | 1 + .../groovy/TransportRequestReleaseTest.groovy | 20 ++++++++-- vars/transportRequestRelease.groovy | 39 +++++++++++++++++-- 4 files changed, 65 insertions(+), 6 deletions(-) diff --git a/documentation/docs/steps/transportRequestRelease.md b/documentation/docs/steps/transportRequestRelease.md index 5e1efb12b..42778ddce 100644 --- a/documentation/docs/steps/transportRequestRelease.md +++ b/documentation/docs/steps/transportRequestRelease.md @@ -14,12 +14,23 @@ Releases a Transport Request for a Change Document on the Solution Manager. | `transportRequestId`| yes | | | | `credentialsId` | yes | | | | `endpoint` | yes | | | +| `gitTransportRequestLabel` | no | `TransportRequest\s?:` | regex pattern | +| `gitFrom` | no | `origin/master` | | +| `gitTo` | no | `HEAD` | | +| `gitChangeDocumentLabel` | no | `ChangeDocument\s?:` | regex pattern | +| `gitFormat` | no | `%b` | see `git log --help` | * `script` - The common script environment of the Jenkinsfile running. Typically the reference to the script calling the pipeline step is provided with the `this` parameter, as in `script: this`. This allows the function to access the [`commonPipelineEnvironment`](commonPipelineEnvironment.md) for retrieving, for example, configuration parameters. * `changeDocumentId` - The id of the change document related to the transport request to release. * `transportRequestId` - The id of the transport request to release. * `credentialsId` - The credentials to connect to the Solution Manager. * `endpoint` - The address of the Solution Manager. +* `gitFrom` - The starting point for retrieving the change document id +* `gitTo` - The end point for retrieving the change document id +* `gitChangeDocumentLabel` - A pattern used for identifying lines holding the change document id. +* `gitTransportReqeustLabel` - A pattern used for identifying lines holding the transport request id. +* `gitFormat` - Specifies what part of the commit is scanned. By default the body of the commit message is scanned. + ## Step configuration The following parameters can also be specified as step parameters using the global configuration file: diff --git a/resources/default_pipeline_environment.yml b/resources/default_pipeline_environment.yml index d95541665..49a52e231 100644 --- a/resources/default_pipeline_environment.yml +++ b/resources/default_pipeline_environment.yml @@ -197,4 +197,5 @@ steps: gitFrom: 'origin/master' gitTo: 'HEAD' gitChangeDocumentLabel: 'ChangeDocument\s?:' + gitTransportRequestLabel: 'TransportRequest\s?:' gitFormat: '%b' diff --git a/test/groovy/TransportRequestReleaseTest.groovy b/test/groovy/TransportRequestReleaseTest.groovy index dcb023421..e07383c9c 100644 --- a/test/groovy/TransportRequestReleaseTest.groovy +++ b/test/groovy/TransportRequestReleaseTest.groovy @@ -4,12 +4,17 @@ import org.junit.Test import org.junit.rules.ExpectedException import org.junit.rules.RuleChain +import com.sap.piper.cm.ChangeManagement +import com.sap.piper.cm.ChangeManagementException + +import groovy.json.StringEscapeUtils import util.BasePiperTest import util.JenkinsStepRule import util.JenkinsLoggingRule import util.Rules import hudson.AbortException +import hudson.scm.NullSCM public class TransportRequestReleaseTest extends BasePiperTest { @@ -66,10 +71,19 @@ public class TransportRequestReleaseTest extends BasePiperTest { @Test public void transportRequestIdNotProvidedTest() { - thrown.expect(AbortException) - thrown.expectMessage("Transport Request id not provided (parameter: 'transportRequestId').") + ChangeManagement cm = new ChangeManagement(nullScript) { + String getTransportRequestId(String from, + String to, + String label, + String format) { + throw new ChangeManagementException('Cannot retrieve transportRequestId') + } + } - jsr.step.call(script: nullScript, changeDocumentId: '001') + thrown.expect(AbortException) + thrown.expectMessage("Transport Request id not provided (parameter: 'transportRequestId' or via commit history).") + + jsr.step.call(script: nullScript, changeDocumentId: '001', cmUtils: cm) } @Test diff --git a/vars/transportRequestRelease.groovy b/vars/transportRequestRelease.groovy index 5e7a7d9d0..bfb2f291c 100644 --- a/vars/transportRequestRelease.groovy +++ b/vars/transportRequestRelease.groovy @@ -16,13 +16,21 @@ import hudson.AbortException 'cmClientOpts', 'transportRequestId', 'credentialsId', - 'endpoint' + 'endpoint', + 'gitFrom', + 'gitTo', + 'gitTransportRequestLabel', + 'gitFormat' ] @Field Set stepConfigurationKeys = [ 'credentialsId', 'cmClientOpts', - 'endpoint' + 'endpoint', + 'gitFrom', + 'gitTo', + 'gitTransportRequestLabel', + 'gitFormat' ] @Field Set generalConfigurationKeys = stepConfigurationKeys @@ -47,7 +55,32 @@ def call(parameters = [:]) { if(!changeDocumentId) throw new AbortException("Change document id not provided (parameter: 'changeDocumentId').") def transportRequestId = configuration.transportRequestId - if(!transportRequestId) throw new AbortException("Transport Request id not provided (parameter: 'transportRequestId').") + + if(transportRequestId?.trim()) { + + echo "[INFO] Transport request id '${transportRequestId}' retrieved from parameters." + + } else { + + echo "[INFO] Retrieving transport request id from commit history [from: ${configuration.gitFrom}, to: ${configuration.gitTo}]." + + " Searching for pattern '${configuration.gitTransportRequestLabel}'. Searching with format '${configuration.gitFormat}'." + + try { + transportRequestId = cm.getTransportRequestId( + configuration.gitFrom, + configuration.gitTo, + configuration.gitTransportRequestLabel, + configuration.gitFormat + ) + + echo "[INFO] Transport request id '${transportRequestId}' retrieved from commit history" + + } catch(ChangeManagementException ex) { + echo "[WARN] Cannot retrieve transportRequestId from commit history: ${ex.getMessage()}." + } + } + + if(!transportRequestId) throw new AbortException("Transport Request id not provided (parameter: 'transportRequestId' or via commit history).") def credentialsId = configuration.credentialsId if(!credentialsId) throw new AbortException("Credentials id not provided (parameter: 'credentialsId').") From 64291cd88f78e01a49db58b22d430d63b8d1f608 Mon Sep 17 00:00:00 2001 From: Marcus Holl Date: Fri, 13 Jul 2018 11:28:55 +0200 Subject: [PATCH 05/12] [refactor] re-use getConfigPropery inside getMandatoryProperty ... instead of accessing the config map directy. With that we ensure that getMandatoryProperty behaves the same like getConfigProperty. Currently we differ e.g. with trim(). --- src/com/sap/piper/ConfigurationHelper.groovy | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/src/com/sap/piper/ConfigurationHelper.groovy b/src/com/sap/piper/ConfigurationHelper.groovy index 4e6bb6627..ee78dc459 100644 --- a/src/com/sap/piper/ConfigurationHelper.groovy +++ b/src/com/sap/piper/ConfigurationHelper.groovy @@ -109,10 +109,7 @@ class ConfigurationHelper implements Serializable { def getMandatoryProperty(key, defaultValue = null, errorMessage = null) { - def paramValue = config[key] - - if (paramValue == null) - paramValue = defaultValue + def paramValue = getConfigProperty(key, defaultValue) if (paramValue == null) { if(! errorMessage) errorMessage = "ERROR - NO VALUE AVAILABLE FOR ${key}" From 254a4e2017aa732aed402638ae5f9230bf858769 Mon Sep 17 00:00:00 2001 From: Marcus Holl Date: Fri, 13 Jul 2018 13:23:10 +0200 Subject: [PATCH 06/12] Remove unused sh registerAllowedMethod registerAllowedMethod for sh call not used anymore since we mock the ChangeManagement utils class. The sh calls was triggered from the utils class. Since we mock that class, there is no sh call anymore. --- test/groovy/TransportRequestCreateTest.groovy | 2 -- test/groovy/TransportRequestReleaseTest.groovy | 2 -- test/groovy/TransportRequestUploadFileTest.groovy | 2 -- 3 files changed, 6 deletions(-) diff --git a/test/groovy/TransportRequestCreateTest.groovy b/test/groovy/TransportRequestCreateTest.groovy index 269b8ec98..66e65668b 100644 --- a/test/groovy/TransportRequestCreateTest.groovy +++ b/test/groovy/TransportRequestCreateTest.groovy @@ -45,8 +45,6 @@ public class TransportRequestCreateTest extends BasePiperTest { } }) - helper.registerAllowedMethod('sh', [Map], { Map m -> return 0 }) - nullScript.commonPipelineEnvironment.configuration = [steps: [transportRequestCreate: [ diff --git a/test/groovy/TransportRequestReleaseTest.groovy b/test/groovy/TransportRequestReleaseTest.groovy index 619556c76..cf74e4fe2 100644 --- a/test/groovy/TransportRequestReleaseTest.groovy +++ b/test/groovy/TransportRequestReleaseTest.groovy @@ -42,8 +42,6 @@ public class TransportRequestReleaseTest extends BasePiperTest { } }) - helper.registerAllowedMethod('sh', [Map], { Map m -> return 0 }) - nullScript.commonPipelineEnvironment.configuration = [steps: [transportRequestRelease: [ diff --git a/test/groovy/TransportRequestUploadFileTest.groovy b/test/groovy/TransportRequestUploadFileTest.groovy index 08ab99eee..75e96525a 100644 --- a/test/groovy/TransportRequestUploadFileTest.groovy +++ b/test/groovy/TransportRequestUploadFileTest.groovy @@ -45,8 +45,6 @@ public class TransportRequestUploadFileTest extends BasePiperTest { } }) - helper.registerAllowedMethod('sh', [Map], { Map m -> return 0 }) - nullScript.commonPipelineEnvironment.configuration = [steps: [transportRequestUploadFile: [ From 0337b34cefaf7e214969ec6d7785c12ba3ef7af8 Mon Sep 17 00:00:00 2001 From: Marcus Holl Date: Fri, 13 Jul 2018 14:40:29 +0200 Subject: [PATCH 07/12] [whitespace only] increase readability --- test/groovy/TransportRequestUploadFileTest.groovy | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/test/groovy/TransportRequestUploadFileTest.groovy b/test/groovy/TransportRequestUploadFileTest.groovy index 75e96525a..9ce045ee8 100644 --- a/test/groovy/TransportRequestUploadFileTest.groovy +++ b/test/groovy/TransportRequestUploadFileTest.groovy @@ -134,7 +134,11 @@ public class TransportRequestUploadFileTest extends BasePiperTest { helper.registerAllowedMethod('sh', [Map], { Map m -> return 0 }) - jsr.step.call(script: nullScript, changeDocumentId: '001', transportRequestId: '001', applicationId: 'app', filePath: '/path') + jsr.step.call(script: nullScript, + changeDocumentId: '001', + transportRequestId: '001', + applicationId: 'app', + filePath: '/path') assert jlr.log.contains("[INFO] Uploading file '/path' to transport request '001' of change document '001'.") assert jlr.log.contains("[INFO] File '/path' has been successfully uploaded to transport request '001' of change document '001'.") From bb0e9b92ce5122ead5d29659109e8a64f41985a8 Mon Sep 17 00:00:00 2001 From: Marcus Holl Date: Fri, 13 Jul 2018 15:13:58 +0200 Subject: [PATCH 08/12] transport request upload file: decouple tests. --- .../TransportRequestUploadFileTest.groovy | 76 +++++++++++++++++-- .../sap/piper/cm/ChangeManagementTest.groovy | 18 +++++ 2 files changed, 89 insertions(+), 5 deletions(-) diff --git a/test/groovy/TransportRequestUploadFileTest.groovy b/test/groovy/TransportRequestUploadFileTest.groovy index 9ce045ee8..a38db3459 100644 --- a/test/groovy/TransportRequestUploadFileTest.groovy +++ b/test/groovy/TransportRequestUploadFileTest.groovy @@ -1,3 +1,5 @@ +import java.util.Map + import org.junit.Before import org.junit.Rule import org.junit.Test @@ -27,6 +29,8 @@ public class TransportRequestUploadFileTest extends BasePiperTest { .around(jsr) .around(jlr) + private Map cmUtilReceivedParams = [:] + @Before public void setup() { @@ -45,6 +49,8 @@ public class TransportRequestUploadFileTest extends BasePiperTest { } }) + cmUtilReceivedParams.clear() + nullScript.commonPipelineEnvironment.configuration = [steps: [transportRequestUploadFile: [ @@ -132,15 +138,75 @@ public class TransportRequestUploadFileTest extends BasePiperTest { @Test public void uploadFileToTransportRequestSuccessTest() { - helper.registerAllowedMethod('sh', [Map], { Map m -> return 0 }) + 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(String changeId, + String transportRequestId, + String applicationId, + String filePath, + String endpoint, + String username, + String password, + String cmclientOpts) { + + cmUtilReceivedParams.changeId = changeId + cmUtilReceivedParams.transportRequestId = transportRequestId + cmUtilReceivedParams.applicationId = applicationId + cmUtilReceivedParams.filePath = filePath + cmUtilReceivedParams.endpoint = endpoint + cmUtilReceivedParams.username = username + cmUtilReceivedParams.password = password + cmUtilReceivedParams.cmclientOpts = cmclientOpts + } + } + + jsr.step.call(script: nullScript, + changeDocumentId: '001', + transportRequestId: '002', + applicationId: 'app', + filePath: '/path', + cmUtils: cm) + + assert cmUtilReceivedParams == + [ + changeId: '001', + transportRequestId: '002', + applicationId: 'app', + filePath: '/path', + endpoint: 'https://example.org/cm', + username: 'anonymous', + password: '********', + cmclientOpts: null + ] + } + + @Test + public void uploadFileToTransportRequestUploadFailureTest() { + + thrown.expect(AbortException) + thrown.expectMessage('Upload failure.') + + ChangeManagement cm = new ChangeManagement(nullScript) { + void uploadFileToTransportRequest(String changeId, + String transportRequestId, + String applicationId, + String filePath, + String endpoint, + String username, + String password, + String cmclientOpts) { + throw new ChangeManagementException('Upload failure.') + } + } jsr.step.call(script: nullScript, changeDocumentId: '001', transportRequestId: '001', applicationId: 'app', - filePath: '/path') - - assert jlr.log.contains("[INFO] Uploading file '/path' to transport request '001' of change document '001'.") - assert jlr.log.contains("[INFO] File '/path' has been successfully uploaded to transport request '001' of change document '001'.") + filePath: '/path', + cmUtils: cm) } + } diff --git a/test/groovy/com/sap/piper/cm/ChangeManagementTest.groovy b/test/groovy/com/sap/piper/cm/ChangeManagementTest.groovy index f3492aa4a..2747e0ccb 100644 --- a/test/groovy/com/sap/piper/cm/ChangeManagementTest.groovy +++ b/test/groovy/com/sap/piper/cm/ChangeManagementTest.groovy @@ -202,6 +202,24 @@ public void testGetCommandLineWithCMClientOpts() { // the command line. } + @Test + public void testUploadFileToTransportFails() { + + thrown.expect(ChangeManagementException) + thrown.expectMessage("Cannot upload file '/path' for change document '001' with transport request '002'. " + + "Return code from cmclient: 1.") + + script.setReturnValue(JenkinsShellCallRule.Type.REGEX,, 'upload-file-to-transport', 1) + + new ChangeManagement(nullScript).uploadFileToTransportRequest('001', + '002', + 'XXX', + '/path', + 'https://example.org/cm', + 'me', + 'openSesame') + } + private GitUtils gitUtilsMock(boolean insideWorkTree, String[] changeIds) { return new GitUtils() { public boolean insideWorkTree() { From 4b2ee0d4adbd74ed1563358678fee2f888eb1eaf Mon Sep 17 00:00:00 2001 From: Marcus Holl Date: Tue, 17 Jul 2018 10:08:47 +0200 Subject: [PATCH 09/12] [fix] Excpetion message contains config key instead of value. --- test/groovy/CheckChangeInDevelopmentTest.groovy | 4 ++-- vars/checkChangeInDevelopment.groovy | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/test/groovy/CheckChangeInDevelopmentTest.groovy b/test/groovy/CheckChangeInDevelopmentTest.groovy index 95a7494a4..d030dabe1 100644 --- a/test/groovy/CheckChangeInDevelopmentTest.groovy +++ b/test/groovy/CheckChangeInDevelopmentTest.groovy @@ -136,7 +136,7 @@ class CheckChangeInDevelopmentTest extends BasePiperTest { thrown.expect(IllegalArgumentException) thrown.expectMessage("No changeDocumentId provided. Neither via parameter 'changeDocumentId' " + - "nor via label 'configuration.gitChangeIdLabel' in commit range " + + "nor via label 'ChangeDocument\\s?:' in commit range " + "[from: origin/master, to: HEAD].") ChangeManagement cm = getChangeManagementUtils(false, null) @@ -150,7 +150,7 @@ class CheckChangeInDevelopmentTest extends BasePiperTest { thrown.expect(IllegalArgumentException) thrown.expectMessage("No changeDocumentId provided. Neither via parameter 'changeDocumentId' " + - "nor via label 'configuration.gitChangeIdLabel' in commit range " + + "nor via label 'ChangeDocument\\s?:' in commit range " + "[from: origin/master, to: HEAD].") ChangeManagement cm = getChangeManagementUtils(false, '') diff --git a/vars/checkChangeInDevelopment.groovy b/vars/checkChangeInDevelopment.groovy index 7bd6f44e3..64b2c6b49 100644 --- a/vars/checkChangeInDevelopment.groovy +++ b/vars/checkChangeInDevelopment.groovy @@ -73,7 +73,7 @@ def call(parameters = [:]) { .withMandatoryProperty('endpoint') .withMandatoryProperty('changeDocumentId', "No changeDocumentId provided. Neither via parameter 'changeDocumentId' " + - "nor via label 'configuration.gitChangeIdLabel' in commit range " + + "nor via label '${configuration.gitChangeDocumentLabel}' in commit range " + "[from: ${configuration.gitFrom}, to: ${configuration.gitTo}].") .use() From 38360f1d0cb717e5ee9be5a0852d9376a9523bae Mon Sep 17 00:00:00 2001 From: Roland Stengel Date: Mon, 16 Jul 2018 15:46:18 +0200 Subject: [PATCH 10/12] drop NEO version verification - introduce getVersion --- src/com/sap/piper/VersionUtils.groovy | 27 +++--- .../com/sap/piper/VersionUtilsTest.groovy | 83 +++++++++++++++++++ 2 files changed, 100 insertions(+), 10 deletions(-) create mode 100644 test/groovy/com/sap/piper/VersionUtilsTest.groovy diff --git a/src/com/sap/piper/VersionUtils.groovy b/src/com/sap/piper/VersionUtils.groovy index 270125837..95c67afa8 100644 --- a/src/com/sap/piper/VersionUtils.groovy +++ b/src/com/sap/piper/VersionUtils.groovy @@ -5,9 +5,12 @@ import hudson.AbortException class VersionUtils implements Serializable { - def static verifyVersion(script, name, executable, String version, versionOption) { + def static getVersion(script, name, executable, versionOption) { - script.echo "Verifying $name version $version or compatible version." + return new Version(getVersionDesc(script, name, executable, versionOption)) + } + + def static getVersionDesc(script, name, executable, versionOption) { def toolVersion try { @@ -16,7 +19,16 @@ class VersionUtils implements Serializable { } catch(AbortException e) { throw new AbortException("The verification of $name failed. Please check '$executable'. $e.message.") } - def installedVersion = new Version(toolVersion) + + return toolVersion + } + + def static verifyVersion(script, name, executable, String version, versionOption) { + + script.echo "Verifying $name version $version or compatible version." + + Version installedVersion = getVersion(script, name, executable, versionOption) + if (!installedVersion.isCompatibleVersion(new Version(version))) { throw new AbortException("The installed version of $name is ${installedVersion.toString()}. Please install version $version or a compatible version.") } @@ -25,13 +37,8 @@ class VersionUtils implements Serializable { def static verifyVersion(script, name, executable, Map versions, versionOption) { - def toolVersion - try { - toolVersion = script.sh returnStdout: true, script: """#!/bin/bash - $executable $versionOption""" - } catch(AbortException e) { - throw new AbortException("The verification of $name failed. Please check '$executable'. $e.message.") - } + def toolVersion = getVersionDesc(script, name, executable, versionOption) + for (def entry : versions) { if (toolVersion.contains(entry.getKey())) { def installedVersion = new Version(toolVersion) diff --git a/test/groovy/com/sap/piper/VersionUtilsTest.groovy b/test/groovy/com/sap/piper/VersionUtilsTest.groovy new file mode 100644 index 000000000..118b7d8d2 --- /dev/null +++ b/test/groovy/com/sap/piper/VersionUtilsTest.groovy @@ -0,0 +1,83 @@ +package com.sap.piper + +import hudson.AbortException +import org.junit.Before +import org.junit.Rule +import org.junit.Test +import org.junit.rules.ExpectedException +import org.junit.rules.RuleChain +import util.BasePiperTest +import util.JenkinsShellCallRule +import util.Rules + +import static org.junit.Assert.assertEquals +import static org.hamcrest.Matchers.equalTo +import static org.junit.Assert.assertTrue +import static org.junit.Assert.assertFalse +import static org.hamcrest.Matchers.is +import static org.hamcrest.Matchers.notNullValue +import static org.junit.Assert.assertNotNull +import static org.junit.Assert.assertNull +import static org.junit.Assert.assertThat + +class VersionUtilsTest extends BasePiperTest { + + ExpectedException thrown = ExpectedException.none() + + @Rule + public RuleChain ruleChain = Rules.getCommonRules(this).around(thrown) + + @Before + void init() throws Exception { + } + + @Test + void test_if_getVersionDesc_returns_desc() { + helper.registerAllowedMethod('sh', [Map], { Map m -> return 'SAP Cloud Platform Console Client\n\n\nSDK version : 2.129.5.1\nRuntime : neo-javaee6-wp\n'}) + + assertEquals('SAP Cloud Platform Console Client\n\n\nSDK version : 2.129.5.1\nRuntime : neo-javaee6-wp\n',VersionUtils.getVersionDesc(nullScript, "test", "test.sh", "version")) + } + + @Test + void test_if_getVersion_returns_version() { + helper.registerAllowedMethod('sh', [Map], { Map m -> return 'SAP Cloud Platform Console Client\n\n\nSDK version : 2.129.5.1\nRuntime : neo-javaee6-wp\n'}) + + assertEquals(new Version('2.129.5.1'),VersionUtils.getVersion(nullScript, "test", "test.sh", "version")) + } + + @Test + void test_if_verifyVersion_succeeds_compatible() { + helper.registerAllowedMethod('sh', [Map], { Map m -> return 'version : 1.0.0\runtime: key' }) + VersionUtils.verifyVersion(nullScript, "test", "test.sh", '1.0.0', "version") + } + + @Test + void test_if_verifyVersion_fails_incompatible() { + helper.registerAllowedMethod('sh', [Map], { Map m -> return 'version : 1.0.0\runtime: key' }) + + thrown.expect(AbortException) + thrown.expectMessage("The installed version of test is 1.0.0. Please install version 1.0.1 or a compatible version.") + + VersionUtils.verifyVersion(nullScript, "test", "test.sh", '1.0.1', "version") + } + + @Test + void test_if_verifyVersion_map_succeeds_compatible() { + Map versionMap = ['key1': '1.0.0', 'key2': '2.0.0', 'key3': '3.0.0'] + helper.registerAllowedMethod('sh', [Map], { Map m -> return 'version : 1.0.0\runtime: key1' }) + VersionUtils.verifyVersion(nullScript, "test", "test.sh", versionMap, "version") + } + + @Test + void test_if_verifyVersion_map_fails_incompatible() { + Map versionMap = ['key1': '1.0.1', 'key2': '2.0.1', 'key3': '3.0.1'] + helper.registerAllowedMethod('sh', [Map], { Map m -> return 'version : 1.0.0\runtime: key1' }) + + thrown.expect(AbortException) + thrown.expectMessage("The installed version of test is 1.0.0. Please install version 1.0.1 or a compatible version.") + + VersionUtils.verifyVersion(nullScript, "test", "test.sh", versionMap, "version") + } + + +} From b7a2a84e2d0748c12dea236fc110e01ac0ee01da Mon Sep 17 00:00:00 2001 From: Roland Stengel Date: Tue, 17 Jul 2018 11:18:18 +0200 Subject: [PATCH 11/12] drop NEO version verification - decouple verifyversion from getversion --- src/com/sap/piper/VersionUtils.groovy | 24 ++++++++++++++++--- src/com/sap/piper/tools/ToolDescriptor.groovy | 5 ++-- 2 files changed, 24 insertions(+), 5 deletions(-) diff --git a/src/com/sap/piper/VersionUtils.groovy b/src/com/sap/piper/VersionUtils.groovy index 95c67afa8..9e1087e26 100644 --- a/src/com/sap/piper/VersionUtils.groovy +++ b/src/com/sap/piper/VersionUtils.groovy @@ -35,13 +35,31 @@ class VersionUtils implements Serializable { script.echo "Verification success. $name version ${installedVersion.toString()} is installed." } + def static verifyVersion(script, name, String versionDesc, String versionExpected) { + + script.echo "Verifying $name version $versionExpected or compatible version." + + Version versionAvailable = new Version(versionDesc) + + if (!versionAvailable.isCompatibleVersion(new Version(versionExpected))) { + throw new AbortException("The installed version of $name is ${versionAvailable.toString()}. Please install version $versionExpected or a compatible version.") + } + script.echo "Verification success. $name version ${versionAvailable.toString()} is installed." + } + + def static verifyVersion(script, name, executable, Map versions, versionOption) { - def toolVersion = getVersionDesc(script, name, executable, versionOption) + def versionDesc = getVersionDesc(script, name, executable, versionOption) + verifyVersion(script, name, versionDesc, versions) + } + + def static verifyVersion(script, name, String versionDesc, Map versions) { + for (def entry : versions) { - if (toolVersion.contains(entry.getKey())) { - def installedVersion = new Version(toolVersion) + if (versionDesc.contains(entry.getKey())) { + def installedVersion = new Version(versionDesc) def expectedVersion = entry.getValue() script.echo "Verifying $name version $expectedVersion or compatible version." if (!installedVersion.isCompatibleVersion(new Version(expectedVersion))) { diff --git a/src/com/sap/piper/tools/ToolDescriptor.groovy b/src/com/sap/piper/tools/ToolDescriptor.groovy index 0f4681889..7886d5c9b 100644 --- a/src/com/sap/piper/tools/ToolDescriptor.groovy +++ b/src/com/sap/piper/tools/ToolDescriptor.groovy @@ -108,8 +108,9 @@ class ToolDescriptor implements Serializable { def verifyVersion(script, configuration) { def executable = getToolExecutable(script, configuration, false) - if (singleVersion) VersionUtils.verifyVersion(script, name, executable, singleVersion, versionOption) - if (multipleVersions) VersionUtils.verifyVersion(script, name, executable, multipleVersions, versionOption) + def versionDesc = VersionUtils.getVersionDesc(script, name, executable, versionOption) + if (singleVersion) VersionUtils.verifyVersion(script, name, versionDesc, singleVersion) + if (multipleVersions) VersionUtils.verifyVersion(script, name, versionDesc, multipleVersions) } def getMessage() { From 4851ba94bfabfd2e503aabb7036be466836c81ae Mon Sep 17 00:00:00 2001 From: Roland Stengel Date: Tue, 17 Jul 2018 11:47:39 +0200 Subject: [PATCH 12/12] drop NEO version verification - remove neo version check --- test/groovy/ToolValidateTest.groovy | 14 -------------- .../com/sap/piper/tools/ToolDescriptorTest.groovy | 11 +++++++++++ vars/neoDeploy.groovy | 3 +-- vars/toolValidate.groovy | 3 +-- 4 files changed, 13 insertions(+), 18 deletions(-) diff --git a/test/groovy/ToolValidateTest.groovy b/test/groovy/ToolValidateTest.groovy index c3d52b6a6..c6538b752 100644 --- a/test/groovy/ToolValidateTest.groovy +++ b/test/groovy/ToolValidateTest.groovy @@ -144,17 +144,6 @@ class ToolValidateTest extends BasePiperTest { jsr.step.call(tool: 'mta', home: home) } - @Test - void validateNeoIncompatibleVersionTest() { - - thrown.expect(AbortException) - thrown.expectMessage('The installed version of SAP Cloud Platform Console Client is 1.126.51.') - - helper.registerAllowedMethod('sh', [Map], { Map m -> getIncompatibleVersion(m) }) - - jsr.step.call(tool: 'neo', home: home) - } - @Test void validateCmIncompatibleVersionTest() { @@ -195,9 +184,6 @@ class ToolValidateTest extends BasePiperTest { helper.registerAllowedMethod('sh', [Map], { Map m -> getVersion(m) }) jsr.step.call(tool: 'neo', home: home) - - assert jlr.log.contains('Verifying SAP Cloud Platform Console Client version 3.39.10 or compatible version.') - assert jlr.log.contains('SAP Cloud Platform Console Client version 3.39.10 is installed.') } @Test diff --git a/test/groovy/com/sap/piper/tools/ToolDescriptorTest.groovy b/test/groovy/com/sap/piper/tools/ToolDescriptorTest.groovy index 69506d281..11da4eff4 100644 --- a/test/groovy/com/sap/piper/tools/ToolDescriptorTest.groovy +++ b/test/groovy/com/sap/piper/tools/ToolDescriptorTest.groovy @@ -193,6 +193,17 @@ class ToolDescriptorTest extends BasePiperTest { tool.verifyVersion(script, configuration) } + @Test + void verifyToolVersion_without_version_check() { + + def tool = new ToolDescriptor('SAP Cloud Platform Console Client', 'NEO_HOME', 'neoHome', '/tools/', 'neo.sh', null, 'version') + + helper.registerAllowedMethod('sh', [Map], { Map m -> getVersion(m) }) + + tool.verifyVersion(script, configuration) + } + + private getEnvVars(Map m) { diff --git a/vars/neoDeploy.groovy b/vars/neoDeploy.groovy index f4150ad9b..8cfec18df 100644 --- a/vars/neoDeploy.groovy +++ b/vars/neoDeploy.groovy @@ -148,8 +148,7 @@ def call(parameters = [:]) { deployAccount = utils.getMandatoryParameter(configuration, 'account') } - def neoVersions = ['neo-java-web': '3.39.10', 'neo-javaee6-wp': '2.132.6', 'neo-javaee7-wp': '1.21.13'] - def neo = new ToolDescriptor('SAP Cloud Platform Console Client', 'NEO_HOME', 'neoHome', '/tools/', 'neo.sh', neoVersions, 'version') + def neo = new ToolDescriptor('SAP Cloud Platform Console Client', 'NEO_HOME', 'neoHome', '/tools/', 'neo.sh', null, 'version') def neoExecutable = neo.getToolExecutable(this, configuration) def neoDeployScript = """#!/bin/bash "${neoExecutable}" ${warAction} \ diff --git a/vars/toolValidate.groovy b/vars/toolValidate.groovy index f1551a28b..eb840e060 100644 --- a/vars/toolValidate.groovy +++ b/vars/toolValidate.groovy @@ -31,8 +31,7 @@ def call(Map parameters = [:]) { mta.verifyVersion(this, [mtaJarLocation: home]) return case 'neo': - def neoVersions = ['neo-java-web': '3.39.10', 'neo-javaee6-wp': '2.132.6', 'neo-javaee7-wp': '1.21.13'] - def neo = new ToolDescriptor('SAP Cloud Platform Console Client', 'NEO_HOME', 'neoHome', '/tools/', 'neo.sh', neoVersions, 'version') + def neo = new ToolDescriptor('SAP Cloud Platform Console Client', 'NEO_HOME', 'neoHome', '/tools/', 'neo.sh', null, 'version') neo.verifyVersion(this, [neoHome: home]) return case 'cm':