2018-06-26 12:49:51 +02:00
|
|
|
package com.sap.piper.cm
|
2018-06-25 13:59:25 +02:00
|
|
|
import static org.hamcrest.Matchers.allOf
|
|
|
|
import static org.hamcrest.Matchers.containsString
|
|
|
|
import static org.hamcrest.Matchers.equalTo
|
2018-06-25 16:56:38 +02:00
|
|
|
import static org.hamcrest.Matchers.hasItem
|
2018-06-25 13:59:25 +02:00
|
|
|
import static org.hamcrest.Matchers.is
|
2018-06-25 12:37:33 +02:00
|
|
|
import static org.hamcrest.Matchers.not
|
2018-06-25 13:59:25 +02:00
|
|
|
import static org.junit.Assert.assertThat
|
2019-01-29 12:06:36 +02:00
|
|
|
import static org.junit.Assert.assertEquals
|
2018-06-25 13:59:25 +02:00
|
|
|
|
|
|
|
import org.hamcrest.Matchers
|
|
|
|
import org.junit.Assert
|
2018-06-26 12:49:51 +02:00
|
|
|
import org.junit.Rule
|
|
|
|
import org.junit.Test
|
|
|
|
import org.junit.rules.ExpectedException
|
|
|
|
import org.junit.rules.RuleChain
|
|
|
|
|
|
|
|
import com.sap.piper.GitUtils
|
|
|
|
|
|
|
|
import util.BasePiperTest
|
2018-06-25 16:56:38 +02:00
|
|
|
import util.JenkinsLoggingRule
|
2018-06-29 10:11:46 +02:00
|
|
|
import util.JenkinsScriptLoaderRule
|
2018-06-25 13:59:25 +02:00
|
|
|
import util.JenkinsShellCallRule
|
2018-07-16 15:41:46 +02:00
|
|
|
import util.JenkinsCredentialsRule
|
2019-01-29 12:06:36 +02:00
|
|
|
import util.JenkinsDockerExecuteRule
|
2018-06-26 12:49:51 +02:00
|
|
|
import util.Rules
|
|
|
|
|
2018-06-29 09:34:46 +02:00
|
|
|
import hudson.AbortException
|
|
|
|
|
2018-06-26 12:49:51 +02:00
|
|
|
public class ChangeManagementTest extends BasePiperTest {
|
|
|
|
|
|
|
|
private ExpectedException thrown = ExpectedException.none()
|
|
|
|
|
2018-06-25 13:59:25 +02:00
|
|
|
private JenkinsShellCallRule script = new JenkinsShellCallRule(this)
|
2018-06-29 09:34:46 +02:00
|
|
|
private JenkinsLoggingRule logging = new JenkinsLoggingRule(this)
|
2019-01-29 12:06:36 +02:00
|
|
|
private JenkinsDockerExecuteRule dockerExecuteRule = new JenkinsDockerExecuteRule(this)
|
2018-06-25 13:59:25 +02:00
|
|
|
|
2018-06-26 12:49:51 +02:00
|
|
|
@Rule
|
2018-06-25 13:59:25 +02:00
|
|
|
public RuleChain rules = Rules.getCommonRules(this)
|
|
|
|
.around(thrown)
|
|
|
|
.around(script)
|
2018-06-25 16:56:38 +02:00
|
|
|
.around(logging)
|
2018-07-16 15:41:46 +02:00
|
|
|
.around(new JenkinsCredentialsRule(this).withCredentials('me','user','password'))
|
2019-01-29 12:06:36 +02:00
|
|
|
.around(dockerExecuteRule)
|
2018-06-26 12:49:51 +02:00
|
|
|
|
|
|
|
@Test
|
|
|
|
public void testRetrieveChangeDocumentIdOutsideGitWorkTreeTest() {
|
|
|
|
|
|
|
|
thrown.expect(ChangeManagementException)
|
2018-07-03 09:44:41 +02:00
|
|
|
thrown.expectMessage('Cannot retrieve ChangeDocumentId. ' +
|
2018-06-26 12:49:51 +02:00
|
|
|
'Not in a git work tree. ' +
|
2018-07-03 09:44:41 +02:00
|
|
|
'ChangeDocumentId is extracted from git commit messages.')
|
2018-06-26 12:49:51 +02:00
|
|
|
|
2018-06-21 15:16:13 +02:00
|
|
|
new ChangeManagement(nullScript, gitUtilsMock(false, new String[0])).getChangeDocumentId()
|
2018-06-26 12:49:51 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@Test
|
|
|
|
public void testRetrieveChangeDocumentIdNothingFound() {
|
|
|
|
|
|
|
|
thrown.expect(ChangeManagementException)
|
2018-07-03 09:44:41 +02:00
|
|
|
thrown.expectMessage('Cannot retrieve ChangeDocumentId from git commits.')
|
2018-06-26 12:49:51 +02:00
|
|
|
|
2018-06-21 15:16:13 +02:00
|
|
|
new ChangeManagement(nullScript, gitUtilsMock(true, new String[0])).getChangeDocumentId()
|
2018-06-26 12:49:51 +02:00
|
|
|
}
|
2018-06-21 15:16:13 +02:00
|
|
|
|
2018-06-22 13:52:17 +02:00
|
|
|
@Test
|
|
|
|
public void testRetrieveChangeDocumentIdReturnsArrayWithNullValue() {
|
|
|
|
|
|
|
|
thrown.expect(ChangeManagementException)
|
2018-07-03 09:44:41 +02:00
|
|
|
thrown.expectMessage('Cannot retrieve ChangeDocumentId from git commits.')
|
2018-06-22 13:52:17 +02:00
|
|
|
|
|
|
|
new ChangeManagement(nullScript, gitUtilsMock(true, (String[])[ null ])).getChangeDocumentId()
|
|
|
|
}
|
|
|
|
|
2018-06-21 15:32:47 +02:00
|
|
|
@Test
|
|
|
|
public void testRetrieveChangeDocumentNotUnique() {
|
|
|
|
|
|
|
|
thrown.expect(ChangeManagementException)
|
2018-07-03 09:44:41 +02:00
|
|
|
thrown.expectMessage('Multiple ChangeDocumentIds found')
|
2018-06-21 15:32:47 +02:00
|
|
|
|
|
|
|
String[] changeIds = [ 'a', 'b' ]
|
|
|
|
new ChangeManagement(nullScript, gitUtilsMock(true, changeIds)).getChangeDocumentId()
|
|
|
|
}
|
|
|
|
|
|
|
|
@Test
|
|
|
|
public void testRetrieveChangeDocumentSameChangeIdFoundTwice() {
|
|
|
|
|
|
|
|
String[] changeIds = [ 'a', 'a' ]
|
|
|
|
def changeID = new ChangeManagement(nullScript, gitUtilsMock(true, changeIds)).getChangeDocumentId()
|
|
|
|
|
|
|
|
assert changeID == 'a'
|
|
|
|
}
|
|
|
|
|
2018-06-25 13:59:25 +02:00
|
|
|
@Test
|
|
|
|
public void testIsChangeInDevelopmentReturnsTrueWhenChangeIsInDevelopent() {
|
|
|
|
|
|
|
|
script.setReturnValue(JenkinsShellCallRule.Type.REGEX, "cmclient.*is-change-in-development -cID '001'", 0)
|
2018-07-16 15:41:46 +02:00
|
|
|
boolean inDevelopment = new ChangeManagement(nullScript, null).isChangeInDevelopment('001', 'endpoint', 'me')
|
2018-06-25 13:59:25 +02:00
|
|
|
|
|
|
|
assertThat(inDevelopment, is(equalTo(true)))
|
|
|
|
assertThat(script.shell[0], allOf(containsString("cmclient"),
|
|
|
|
containsString("-u 'user'"),
|
|
|
|
containsString("-p 'password'"),
|
2018-06-25 09:16:42 +02:00
|
|
|
containsString("-e 'endpoint'"),
|
2018-06-25 13:59:25 +02:00
|
|
|
containsString('is-change-in-development'),
|
|
|
|
containsString("-cID '001'"),
|
|
|
|
containsString("-t SOLMAN")))
|
|
|
|
}
|
|
|
|
|
|
|
|
@Test
|
|
|
|
public void testIsChangeInDevelopmentReturnsFalseWhenChangeIsNotInDevelopent() {
|
|
|
|
|
|
|
|
script.setReturnValue(JenkinsShellCallRule.Type.REGEX, "cmclient.*is-change-in-development -cID '001'", 3)
|
|
|
|
|
2018-06-25 12:37:33 +02:00
|
|
|
boolean inDevelopment = new ChangeManagement(nullScript, null)
|
|
|
|
.isChangeInDevelopment('001',
|
|
|
|
'endpoint',
|
2018-07-16 15:41:46 +02:00
|
|
|
'me')
|
2018-06-25 13:59:25 +02:00
|
|
|
|
|
|
|
assertThat(inDevelopment, is(equalTo(false)))
|
|
|
|
}
|
|
|
|
|
|
|
|
@Test
|
|
|
|
public void testIsChangeInDevelopmentThrowsExceptionWhenCMClientReturnsUnexpectedExitCode() {
|
|
|
|
|
|
|
|
thrown.expect(ChangeManagementException)
|
2018-06-26 10:30:49 +02:00
|
|
|
thrown.expectMessage('Cannot retrieve status for change document \'001\'. Does this change exist? Return code from cmclient: 1.')
|
2018-06-25 13:59:25 +02:00
|
|
|
|
|
|
|
script.setReturnValue(JenkinsShellCallRule.Type.REGEX, "cmclient.*is-change-in-development -cID '001'", 1)
|
2018-07-16 15:41:46 +02:00
|
|
|
new ChangeManagement(nullScript, null).isChangeInDevelopment('001', 'endpoint', 'me')
|
2018-06-25 13:59:25 +02:00
|
|
|
}
|
2018-06-21 15:32:47 +02:00
|
|
|
|
2018-06-25 08:46:17 +02:00
|
|
|
@Test
|
2018-06-25 12:37:33 +02:00
|
|
|
public void testGetCommandLineWithoutCMClientOpts() {
|
2018-06-25 08:46:17 +02:00
|
|
|
String commandLine = new ChangeManagement(nullScript, null)
|
2018-09-24 14:06:48 +02:00
|
|
|
.getCMCommandLine(BackendType.SOLMAN,
|
2018-09-18 12:19:52 +02:00
|
|
|
'https://example.org/cm',
|
2018-06-25 08:46:17 +02:00
|
|
|
"me",
|
|
|
|
"topSecret",
|
|
|
|
"the-command",
|
|
|
|
["-key1", "val1", "-key2", "val2"])
|
|
|
|
commandLine = commandLine.replaceAll(' +', " ")
|
2018-06-25 12:37:33 +02:00
|
|
|
assertThat(commandLine, not(containsString("CMCLIENT_OPTS")))
|
2018-06-25 08:46:17 +02:00
|
|
|
assertThat(commandLine, containsString("cmclient -e 'https://example.org/cm' -u 'me' -p 'topSecret' -t SOLMAN the-command -key1 val1 -key2 val2"))
|
2018-07-16 15:41:46 +02:00
|
|
|
}
|
2018-06-25 08:46:17 +02:00
|
|
|
|
2018-06-25 12:37:33 +02:00
|
|
|
@Test
|
|
|
|
public void testGetCommandLineWithCMClientOpts() {
|
|
|
|
String commandLine = new ChangeManagement(nullScript, null)
|
2018-09-24 14:06:48 +02:00
|
|
|
.getCMCommandLine(BackendType.SOLMAN,
|
2018-09-18 12:19:52 +02:00
|
|
|
'https://example.org/cm',
|
2018-06-25 12:37:33 +02:00
|
|
|
"me",
|
|
|
|
"topSecret",
|
|
|
|
"the-command",
|
|
|
|
["-key1", "val1", "-key2", "val2"],
|
|
|
|
'-Djavax.net.debug=all')
|
|
|
|
commandLine = commandLine.replaceAll(' +', " ")
|
|
|
|
assertThat(commandLine, containsString('export CMCLIENT_OPTS="-Djavax.net.debug=all"'))
|
|
|
|
}
|
|
|
|
|
2018-06-29 09:34:46 +02:00
|
|
|
@Test
|
2018-09-25 09:12:07 +02:00
|
|
|
public void testCreateTransportRequestSOLMANSucceeds() {
|
2018-06-29 09:34:46 +02:00
|
|
|
|
|
|
|
script.setReturnValue(JenkinsShellCallRule.Type.REGEX, ".*cmclient.*create-transport -cID 001 -dID 002.*", '004')
|
2018-09-25 09:12:07 +02:00
|
|
|
def transportRequestId = new ChangeManagement(nullScript).createTransportRequestSOLMAN( '001', '002', '003', 'me')
|
|
|
|
|
|
|
|
// the check for the transportRequestID is sufficient. This checks implicit the command line since that value is
|
|
|
|
// returned only in case the shell call matches.
|
|
|
|
assert transportRequestId == '004'
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
@Test
|
|
|
|
public void testCreateTransportRequestCTSSucceeds() {
|
|
|
|
|
|
|
|
script.setReturnValue(JenkinsShellCallRule.Type.REGEX, 'cmclient.* -t CTS .*create-transport -tt W -ts XYZ -d "desc 123"$', '004')
|
|
|
|
def transportRequestId = new ChangeManagement(nullScript)
|
|
|
|
.createTransportRequestCTS(
|
|
|
|
'W', // transport type
|
|
|
|
'XYZ', // target system
|
|
|
|
'desc 123', // description
|
|
|
|
'https://example.org/cm',
|
|
|
|
'me')
|
2018-06-29 09:34:46 +02:00
|
|
|
|
|
|
|
// the check for the transportRequestID is sufficient. This checks implicit the command line since that value is
|
|
|
|
// returned only in case the shell call matches.
|
|
|
|
assert transportRequestId == '004'
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
@Test
|
|
|
|
public void testCreateTransportRequestFails() {
|
|
|
|
|
2018-06-29 10:11:46 +02:00
|
|
|
script.setReturnValue(JenkinsShellCallRule.Type.REGEX, '.*upload-file-to-transport.*', 1)
|
|
|
|
|
2018-06-29 09:34:46 +02:00
|
|
|
thrown.expect(ChangeManagementException)
|
2019-01-30 10:42:18 +02:00
|
|
|
thrown.expectMessage('Cannot upload file into transport request. Return code from cm client: 1.')
|
2018-06-29 10:11:46 +02:00
|
|
|
|
2018-12-14 16:24:03 +02:00
|
|
|
new ChangeManagement(nullScript).uploadFileToTransportRequestSOLMAN(
|
2018-09-24 14:06:48 +02:00
|
|
|
'001',
|
2018-06-29 10:11:46 +02:00
|
|
|
'002',
|
|
|
|
'XXX',
|
|
|
|
'/path',
|
|
|
|
'https://example.org/cm',
|
2018-07-16 15:41:46 +02:00
|
|
|
'me')
|
2018-06-29 10:11:46 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@Test
|
2018-09-24 14:06:48 +02:00
|
|
|
public void testUploadFileToTransportSucceedsSOLMAN() {
|
2018-06-29 10:11:46 +02:00
|
|
|
|
|
|
|
// the regex provided below is an implicit check that the command line is fine.
|
2018-09-24 14:06:48 +02:00
|
|
|
script.setReturnValue(JenkinsShellCallRule.Type.REGEX, 'upload-file-to-transport.*-cID 001 -tID 002 XXX "/path"', 0)
|
2018-06-29 10:11:46 +02:00
|
|
|
|
2018-12-14 16:24:03 +02:00
|
|
|
new ChangeManagement(nullScript).uploadFileToTransportRequestSOLMAN(
|
2018-09-24 14:06:48 +02:00
|
|
|
'001',
|
2018-06-29 10:11:46 +02:00
|
|
|
'002',
|
|
|
|
'XXX',
|
|
|
|
'/path',
|
|
|
|
'https://example.org/cm',
|
2018-07-16 15:41:46 +02:00
|
|
|
'me')
|
2018-06-29 09:34:46 +02:00
|
|
|
|
2018-06-29 10:11:46 +02:00
|
|
|
// no assert required here, since the regex registered above to the script rule is an implicit check for
|
|
|
|
// the command line.
|
2018-06-29 09:34:46 +02:00
|
|
|
}
|
2018-06-25 12:37:33 +02:00
|
|
|
|
2018-09-24 14:06:48 +02:00
|
|
|
@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)
|
|
|
|
|
2018-12-14 16:24:03 +02:00
|
|
|
new ChangeManagement(nullScript).uploadFileToTransportRequestCTS(
|
2018-09-24 14:06:48 +02:00
|
|
|
'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.
|
|
|
|
}
|
|
|
|
|
2018-07-13 15:13:58 +02:00
|
|
|
@Test
|
2019-01-29 12:06:36 +02:00
|
|
|
public void testUploadFileToTransportFailsSOLMAN() {
|
2018-07-13 15:13:58 +02:00
|
|
|
|
|
|
|
thrown.expect(ChangeManagementException)
|
2018-12-14 16:24:03 +02:00
|
|
|
thrown.expectMessage("Cannot upload file into transport request. " +
|
2019-01-30 10:42:18 +02:00
|
|
|
"Return code from cm client: 1.")
|
2018-07-13 15:13:58 +02:00
|
|
|
|
|
|
|
script.setReturnValue(JenkinsShellCallRule.Type.REGEX,, 'upload-file-to-transport', 1)
|
|
|
|
|
2018-12-14 16:24:03 +02:00
|
|
|
new ChangeManagement(nullScript).uploadFileToTransportRequestSOLMAN(
|
2018-09-24 14:06:48 +02:00
|
|
|
'001',
|
2018-07-13 15:13:58 +02:00
|
|
|
'002',
|
|
|
|
'XXX',
|
|
|
|
'/path',
|
|
|
|
'https://example.org/cm',
|
2018-07-16 15:41:46 +02:00
|
|
|
'me')
|
2018-07-13 15:13:58 +02:00
|
|
|
}
|
|
|
|
|
2018-07-19 11:18:52 +02:00
|
|
|
@Test
|
2018-09-25 10:44:53 +02:00
|
|
|
public void testReleaseTransportRequestSucceedsSOLMAN() {
|
2018-07-19 11:18:52 +02:00
|
|
|
|
|
|
|
// the regex provided below is an implicit check that the command line is fine.
|
2018-09-25 10:44:53 +02:00
|
|
|
script.setReturnValue(JenkinsShellCallRule.Type.REGEX, '-t SOLMAN release-transport.*-cID 001.*-tID 002', 0)
|
2018-07-19 11:18:52 +02:00
|
|
|
|
2018-09-25 10:44:53 +02:00
|
|
|
new ChangeManagement(nullScript).releaseTransportRequest(
|
|
|
|
BackendType.SOLMAN,
|
|
|
|
'001',
|
|
|
|
'002',
|
|
|
|
'https://example.org',
|
|
|
|
'me',
|
|
|
|
'openSesame')
|
|
|
|
|
|
|
|
// no assert required here, since the regex registered above to the script rule is an implicit check for
|
|
|
|
// the command line.
|
|
|
|
}
|
|
|
|
|
|
|
|
@Test
|
|
|
|
public void testReleaseTransportRequestSucceedsCTS() {
|
|
|
|
|
|
|
|
// the regex provided below is an implicit check that the command line is fine.
|
2018-09-25 11:12:04 +02:00
|
|
|
script.setReturnValue(JenkinsShellCallRule.Type.REGEX, '-t CTS export-transport.*-tID 002', 0)
|
2018-09-25 10:44:53 +02:00
|
|
|
|
|
|
|
new ChangeManagement(nullScript).releaseTransportRequest(
|
|
|
|
BackendType.CTS,
|
|
|
|
null,
|
2018-07-19 11:18:52 +02:00
|
|
|
'002',
|
|
|
|
'https://example.org',
|
|
|
|
'me',
|
|
|
|
'openSesame')
|
|
|
|
|
|
|
|
// no assert required here, since the regex registered above to the script rule is an implicit check for
|
|
|
|
// the command line.
|
|
|
|
}
|
|
|
|
|
2019-01-29 12:06:36 +02:00
|
|
|
@Test
|
|
|
|
public void testReleaseTransportRequestSucceedsRFC() {
|
|
|
|
|
|
|
|
// the regex provided below is an implicit check that the command line is fine.
|
|
|
|
// script.setReturnValue(JenkinsShellCallRule.Type.REGEX, '-tRFC releaseTransport.*-tID 002', 0)
|
|
|
|
|
|
|
|
new ChangeManagement(nullScript).releaseTransportRequest(
|
|
|
|
BackendType.RFC,
|
|
|
|
null,
|
|
|
|
'002',
|
|
|
|
'https://example.org',
|
|
|
|
'me',
|
|
|
|
'openSesame')
|
|
|
|
|
|
|
|
List stringDockerOptions = []
|
|
|
|
for(item in dockerExecuteRule.dockerParams.dockerOptions)
|
|
|
|
{
|
|
|
|
stringDockerOptions = stringDockerOptions.plus([item.toString()])
|
|
|
|
}
|
|
|
|
assertThat(stringDockerOptions, hasItem('--env ABAP_DEVELOPMENT_SERVER=https://example.org'))
|
|
|
|
assertThat(stringDockerOptions, hasItem('--env ABAP_DEVELOPMENT_USER=user'))
|
|
|
|
assertThat(stringDockerOptions, hasItem('--env ABAP_DEVELOPMENT_PASSWORD=password'))
|
|
|
|
assertThat(stringDockerOptions, hasItem('--env ABAP_DEVELOPMENT_CLIENT=001'))
|
|
|
|
assertThat(script.shell, hasItem('cts releaseTransport:002'))
|
|
|
|
}
|
|
|
|
|
2018-07-19 11:18:52 +02:00
|
|
|
@Test
|
2019-01-31 10:06:02 +02:00
|
|
|
public void testReleaseTransportRequestFailsSOLMAN() {
|
2018-07-19 11:18:52 +02:00
|
|
|
|
|
|
|
thrown.expect(ChangeManagementException)
|
|
|
|
thrown.expectMessage("Cannot release Transport Request '002'. Return code from cmclient: 1.")
|
|
|
|
|
|
|
|
// the regex provided below is an implicit check that the command line is fine.
|
|
|
|
script.setReturnValue(JenkinsShellCallRule.Type.REGEX, 'release-transport.*-cID 001.*-tID 002', 1)
|
|
|
|
|
2018-09-25 10:44:53 +02:00
|
|
|
new ChangeManagement(nullScript).releaseTransportRequest(
|
|
|
|
BackendType.SOLMAN,
|
|
|
|
'001',
|
2018-07-19 11:18:52 +02:00
|
|
|
'002',
|
|
|
|
'https://example.org',
|
|
|
|
'me',
|
|
|
|
'openSesame')
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2018-06-21 15:32:47 +02:00
|
|
|
private GitUtils gitUtilsMock(boolean insideWorkTree, String[] changeIds) {
|
|
|
|
return new GitUtils() {
|
|
|
|
public boolean insideWorkTree() {
|
|
|
|
return insideWorkTree
|
|
|
|
}
|
|
|
|
|
|
|
|
public String[] extractLogLines(
|
|
|
|
String filter = '',
|
|
|
|
String from = 'origin/master',
|
|
|
|
String to = 'HEAD',
|
|
|
|
String format = '%b') {
|
|
|
|
return changeIds
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2018-06-26 12:49:51 +02:00
|
|
|
}
|