1
0
mirror of https://github.com/SAP/jenkins-library.git synced 2024-12-14 11:03:09 +02:00

Merge remote-tracking branch 'github/master' into HEAD

This commit is contained in:
Marcus Holl 2018-07-19 08:54:57 +02:00
commit 3effe404b0
17 changed files with 338 additions and 56 deletions

View File

@ -14,12 +14,22 @@ 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:

View File

@ -198,4 +198,5 @@ steps:
gitFrom: 'origin/master'
gitTo: 'HEAD'
gitChangeDocumentLabel: 'ChangeDocument\s?:'
gitTransportRequestLabel: 'TransportRequest\s?:'
gitFormat: '%b'

View File

@ -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}"

View File

@ -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,25 +19,47 @@ 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.")
}
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
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 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))) {

View File

@ -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() {

View File

@ -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, '')

View File

@ -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

View File

@ -45,8 +45,6 @@ public class TransportRequestCreateTest extends BasePiperTest {
}
})
helper.registerAllowedMethod('sh', [Map], { Map m -> return 0 })
nullScript.commonPipelineEnvironment.configuration = [steps:
[transportRequestCreate:
[

View File

@ -4,12 +4,16 @@ 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
import util.Rules
import hudson.AbortException
import hudson.scm.NullSCM
public class TransportRequestReleaseTest extends BasePiperTest {
@ -42,8 +46,6 @@ public class TransportRequestReleaseTest extends BasePiperTest {
}
})
helper.registerAllowedMethod('sh', [Map], { Map m -> return 0 })
nullScript.commonPipelineEnvironment.configuration = [steps:
[transportRequestRelease:
[
@ -57,19 +59,37 @@ public class TransportRequestReleaseTest extends BasePiperTest {
@Test
public void changeIdNotProvidedTest() {
ChangeManagement cm = new ChangeManagement(nullScript) {
String getChangeDocumentId(String from,
String to,
String label,
String format) {
throw new ChangeManagementException('Cannot retrieve change documentId')
}
}
thrown.expect(IllegalArgumentException)
thrown.expectMessage("ERROR - NO VALUE AVAILABLE FOR changeDocumentId")
jsr.step.call(script: nullScript, transportRequestId: '001')
jsr.step.call(script: nullScript, transportRequestId: '001', cmUtils: cm)
}
@Test
public void transportRequestIdNotProvidedTest() {
thrown.expect(IllegalArgumentException)
thrown.expectMessage("ERROR - NO VALUE AVAILABLE FOR 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(IllegalArgumentException)
thrown.expectMessage("Transport request id not provided (parameter: 'transportRequestId' or via commit history).")
jsr.step.call(script: nullScript, changeDocumentId: '001', cmUtils: cm)
}
@Test

View File

@ -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,7 +49,7 @@ public class TransportRequestUploadFileTest extends BasePiperTest {
}
})
helper.registerAllowedMethod('sh', [Map], { Map m -> return 0 })
cmUtilReceivedParams.clear()
nullScript.commonPipelineEnvironment.configuration = [steps:
[transportRequestUploadFile:
@ -145,11 +149,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'.")
jsr.step.call(script: nullScript, changeDocumentId: '001', transportRequestId: '001', applicationId: 'app', filePath: '/path')
ChangeManagement cm = new ChangeManagement(nullScript) {
void uploadFileToTransportRequest(String changeId,
String transportRequestId,
String applicationId,
String filePath,
String endpoint,
String username,
String password,
String cmclientOpts) {
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'.")
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',
cmUtils: cm)
}
}

View File

@ -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")
}
}

View File

@ -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() {

View File

@ -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) {

View File

@ -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()

View File

@ -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} \

View File

@ -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':

View File

@ -14,7 +14,12 @@ import hudson.AbortException
@Field Set stepConfigurationKeys = [
'credentialsId',
'cmClientOpts',
'endpoint'
'endpoint',
'gitChangeDocumentLabel',
'gitFrom',
'gitTo',
'gitTransportRequestLabel',
'gitFormat'
]
@Field Set parameterKeys = stepConfigurationKeys.plus([
@ -30,17 +35,78 @@ def call(parameters = [:]) {
def script = parameters?.script ?: [commonPipelineEnvironment: commonPipelineEnvironment]
ChangeManagement cm = new ChangeManagement(script)
ChangeManagement cm = parameters.cmUtils ?: new ChangeManagement(script)
Map configuration = ConfigurationHelper
ConfigurationHelper configHelper = ConfigurationHelper
.loadStepDefaults(this)
.mixinGeneralConfig(script.commonPipelineEnvironment, generalConfigurationKeys)
.mixinStageConfig(script.commonPipelineEnvironment, parameters.stageName?:env.STAGE_NAME, stepConfigurationKeys)
.mixinStepConfig(script.commonPipelineEnvironment, stepConfigurationKeys)
.mixin(parameters, parameterKeys)
.withMandatoryProperty('changeDocumentId')
.withMandatoryProperty('transportRequestId')
.withMandatoryProperty('endpoint')
Map configuration = configHelper.use()
def transportRequestId = configuration.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()}."
}
}
def changeDocumentId = configuration.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()}."
}
}
configuration = configHelper
.mixin([transportRequestId: transportRequestId?.trim() ?: null,
changeDocumentId: changeDocumentId?.trim() ?: null], ['transportRequestId', 'changeDocumentId'] as Set)
.withMandatoryProperty('transportRequestId',
"Transport request id not provided (parameter: \'transportRequestId\' or via commit history).")
.withMandatoryProperty('changeDocumentId',
"Change document id not provided (parameter: \'changeDocumentId\' or via commit history).")
.use()
echo "[INFO] Closing transport request '${configuration.transportRequestId}' for change document '${configuration.changeDocumentId}'."