2018-02-07 14:17:33 +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-07 15:49:51 +02:00
|
|
|
|
|
|
|
import com.sap.piper.GitUtils
|
|
|
|
|
2018-09-21 16:55:31 +02:00
|
|
|
import hudson.AbortException
|
2018-06-06 11:19:19 +02:00
|
|
|
import util.BasePiperTest
|
2019-07-12 17:01:33 +02:00
|
|
|
import util.JenkinsCredentialsRule
|
2018-06-06 11:19:19 +02:00
|
|
|
import util.JenkinsDockerExecuteRule
|
2018-03-06 14:28:57 +02:00
|
|
|
import util.JenkinsEnvironmentRule
|
|
|
|
import util.JenkinsLoggingRule
|
|
|
|
import util.JenkinsReadMavenPomRule
|
2018-08-31 10:22:43 +02:00
|
|
|
import util.JenkinsReadYamlRule
|
2018-03-06 14:28:57 +02:00
|
|
|
import util.JenkinsShellCallRule
|
|
|
|
import util.JenkinsStepRule
|
|
|
|
import util.JenkinsWriteFileRule
|
|
|
|
import util.Rules
|
2018-02-07 14:17:33 +02:00
|
|
|
|
2018-06-06 11:19:19 +02:00
|
|
|
import static org.hamcrest.Matchers.hasItem
|
2018-08-15 12:03:37 +02:00
|
|
|
import static org.hamcrest.Matchers.hasItems
|
2018-10-02 08:53:21 +02:00
|
|
|
import static org.hamcrest.Matchers.not
|
2018-08-15 12:03:37 +02:00
|
|
|
import static org.hamcrest.Matchers.notNullValue
|
2018-10-01 13:56:59 +02:00
|
|
|
import static org.hamcrest.Matchers.stringContainsInOrder
|
2019-07-16 14:52:45 +02:00
|
|
|
import static org.hamcrest.Matchers.allOf
|
2018-08-15 12:03:37 +02:00
|
|
|
import static org.hamcrest.Matchers.containsString
|
2019-07-16 15:33:57 +02:00
|
|
|
import static org.hamcrest.Matchers.emptyIterable
|
2018-06-06 11:19:19 +02:00
|
|
|
import static org.junit.Assert.assertThat
|
2018-08-15 12:03:37 +02:00
|
|
|
|
2019-07-16 14:52:45 +02:00
|
|
|
import org.hamcrest.Matchers
|
|
|
|
|
2018-02-07 14:17:33 +02:00
|
|
|
import static org.junit.Assert.assertEquals
|
|
|
|
|
2018-06-06 11:19:19 +02:00
|
|
|
class ArtifactSetVersionTest extends BasePiperTest {
|
2018-03-06 14:28:57 +02:00
|
|
|
Map dockerParameters
|
|
|
|
|
2018-09-07 15:49:51 +02:00
|
|
|
def GitUtils gitUtils = new GitUtils() {
|
2018-09-13 14:35:26 +02:00
|
|
|
boolean isWorkTreeDirty() {
|
|
|
|
return false
|
2018-09-07 15:49:51 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
String getGitCommitIdOrNull() {
|
|
|
|
return 'testCommitId'
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-03-02 11:53:09 +02:00
|
|
|
def sshAgentList = []
|
|
|
|
|
2018-02-28 14:11:09 +02:00
|
|
|
private ExpectedException thrown = ExpectedException.none()
|
2019-01-22 10:26:08 +02:00
|
|
|
private JenkinsDockerExecuteRule dockerExecuteRule = new JenkinsDockerExecuteRule(this)
|
2019-01-22 10:22:15 +02:00
|
|
|
private JenkinsLoggingRule loggingRule = new JenkinsLoggingRule(this)
|
2019-01-22 10:19:28 +02:00
|
|
|
private JenkinsShellCallRule shellRule = new JenkinsShellCallRule(this)
|
2019-01-22 10:29:15 +02:00
|
|
|
private JenkinsWriteFileRule writeFileRule = new JenkinsWriteFileRule(this)
|
2019-01-22 10:25:42 +02:00
|
|
|
private JenkinsStepRule stepRule = new JenkinsStepRule(this)
|
2019-01-22 10:27:45 +02:00
|
|
|
private JenkinsEnvironmentRule environmentRule = new JenkinsEnvironmentRule(this)
|
2019-07-12 17:01:33 +02:00
|
|
|
private JenkinsCredentialsRule jenkinsCredentialsRule = new JenkinsCredentialsRule(this)
|
2018-02-07 14:17:33 +02:00
|
|
|
|
|
|
|
@Rule
|
|
|
|
public RuleChain ruleChain = Rules
|
|
|
|
.getCommonRules(this)
|
2018-08-31 10:22:43 +02:00
|
|
|
.around(new JenkinsReadYamlRule(this))
|
2018-02-07 14:17:33 +02:00
|
|
|
.around(thrown)
|
2019-01-22 10:22:15 +02:00
|
|
|
.around(loggingRule)
|
2019-01-22 10:19:28 +02:00
|
|
|
.around(shellRule)
|
2018-08-31 14:13:11 +02:00
|
|
|
.around(new JenkinsReadMavenPomRule(this, 'test/resources/versioning/MavenArtifactVersioning'))
|
2019-01-22 10:29:15 +02:00
|
|
|
.around(writeFileRule)
|
2019-01-22 10:26:08 +02:00
|
|
|
.around(dockerExecuteRule)
|
2019-01-22 10:25:42 +02:00
|
|
|
.around(stepRule)
|
2019-07-12 17:01:33 +02:00
|
|
|
.around(jenkinsCredentialsRule)
|
2019-01-22 10:27:45 +02:00
|
|
|
.around(environmentRule)
|
2018-02-07 14:17:33 +02:00
|
|
|
|
|
|
|
@Before
|
|
|
|
void init() throws Throwable {
|
2018-03-06 14:28:57 +02:00
|
|
|
dockerParameters = [:]
|
|
|
|
|
2018-08-08 22:21:26 +02:00
|
|
|
nullScript.commonPipelineEnvironment.setArtifactVersion(null)
|
|
|
|
nullScript.commonPipelineEnvironment.setGitSshUrl('git@test.url')
|
|
|
|
|
2018-02-07 14:17:33 +02:00
|
|
|
helper.registerAllowedMethod("sshagent", [List.class, Closure.class], { list, closure ->
|
|
|
|
sshAgentList = list
|
|
|
|
return closure()
|
|
|
|
})
|
|
|
|
|
2019-04-02 15:59:33 +02:00
|
|
|
shellRule.setReturnValue("date --utc +'%Y%m%d%H%M%S'", '20180101010203')
|
2019-01-22 10:19:28 +02:00
|
|
|
shellRule.setReturnValue('git diff --quiet HEAD', 0)
|
2018-02-07 14:17:33 +02:00
|
|
|
|
2018-06-06 11:19:19 +02:00
|
|
|
helper.registerAllowedMethod('fileExists', [String.class], {true})
|
2018-02-07 14:17:33 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@Test
|
2019-07-12 17:01:33 +02:00
|
|
|
void testVersioningPushViaSSH() {
|
2019-01-22 10:25:42 +02:00
|
|
|
stepRule.step.artifactSetVersion(script: stepRule.step, juStabGitUtils: gitUtils, buildTool: 'maven', gitSshUrl: 'myGitSshUrl')
|
2018-02-07 14:17:33 +02:00
|
|
|
|
2019-01-22 10:27:45 +02:00
|
|
|
assertEquals('1.2.3-20180101010203_testCommitId', environmentRule.env.getArtifactVersion())
|
|
|
|
assertEquals('testCommitId', environmentRule.env.getGitCommitId())
|
2018-02-07 14:17:33 +02:00
|
|
|
|
2019-01-22 10:19:28 +02:00
|
|
|
assertThat(shellRule.shell, hasItem("mvn --file 'pom.xml' --batch-mode -Dorg.slf4j.simpleLogger.log.org.apache.maven.cli.transfer.Slf4jMavenTransferListener=warn versions:set -DnewVersion=1.2.3-20180101010203_testCommitId -DgenerateBackupPoms=false"))
|
|
|
|
assertThat(shellRule.shell.join(), stringContainsInOrder([
|
2018-10-01 13:56:59 +02:00
|
|
|
"git add .",
|
|
|
|
"git commit -m 'update version 1.2.3-20180101010203_testCommitId'",
|
|
|
|
'git tag build_1.2.3-20180101010203_testCommitId',
|
|
|
|
'git push myGitSshUrl build_1.2.3-20180101010203_testCommitId',
|
|
|
|
]
|
|
|
|
))
|
2018-02-07 14:17:33 +02:00
|
|
|
}
|
|
|
|
|
2019-07-16 15:33:57 +02:00
|
|
|
|
|
|
|
@Test
|
|
|
|
void testVersioningNoPush() {
|
|
|
|
|
|
|
|
stepRule.step.artifactSetVersion(
|
|
|
|
script: stepRule.step,
|
|
|
|
juStabGitUtils: gitUtils,
|
|
|
|
buildTool: 'maven',
|
|
|
|
gitPushMode: 'NONE')
|
|
|
|
|
|
|
|
assertThat(loggingRule.log, containsString('Git push to remote has been skipped.'))
|
|
|
|
assertThat(((Iterable)shellRule.shell).join(), not(containsString('push')))
|
|
|
|
}
|
|
|
|
|
2019-07-12 17:01:33 +02:00
|
|
|
@Test
|
|
|
|
void testVersioningPushViaHTTPS() {
|
|
|
|
|
|
|
|
jenkinsCredentialsRule.withCredentials('myGitRepoCredentials', 'me', 'topSecret')
|
|
|
|
|
|
|
|
stepRule.step.artifactSetVersion(
|
|
|
|
script: stepRule.step,
|
|
|
|
juStabGitUtils: gitUtils,
|
|
|
|
buildTool: 'maven',
|
|
|
|
gitCredentialsId: 'myGitRepoCredentials',
|
|
|
|
gitHttpsUrl: 'https://example.org/myGitRepo',
|
|
|
|
gitPushMode: 'HTTPS')
|
|
|
|
|
|
|
|
// closer version checks already performed in test 'testVersioningPushViaSSH', focusing on
|
|
|
|
// GIT related assertions here
|
|
|
|
|
|
|
|
assertThat(shellRule.shell, hasItem("mvn --file 'pom.xml' --batch-mode -Dorg.slf4j.simpleLogger.log.org.apache.maven.cli.transfer.Slf4jMavenTransferListener=warn versions:set -DnewVersion=1.2.3-20180101010203_testCommitId -DgenerateBackupPoms=false"))
|
|
|
|
assertThat(((Iterable)shellRule.shell).join(), stringContainsInOrder([
|
|
|
|
"git add .",
|
|
|
|
"git commit -m 'update version 1.2.3-20180101010203_testCommitId'",
|
|
|
|
'git tag build_1.2.3-20180101010203_testCommitId',
|
|
|
|
'git push https://me:topSecret@example.org/myGitRepo build_1.2.3-20180101010203_testCommitId',
|
|
|
|
]
|
|
|
|
))
|
|
|
|
}
|
|
|
|
|
2019-07-16 14:52:45 +02:00
|
|
|
@Test
|
|
|
|
void testVersioningPushViaHTTPDisableSSLCheck() {
|
|
|
|
|
|
|
|
jenkinsCredentialsRule.withCredentials('myGitRepoCredentials', 'me', 'topSecret')
|
|
|
|
|
|
|
|
stepRule.step.artifactSetVersion(
|
|
|
|
script: stepRule.step,
|
|
|
|
juStabGitUtils: gitUtils,
|
|
|
|
buildTool: 'maven',
|
|
|
|
gitCredentialsId: 'myGitRepoCredentials',
|
|
|
|
gitHttpsUrl: 'https://example.org/myGitRepo',
|
|
|
|
gitPushMode: 'HTTPS',
|
|
|
|
gitDisableSSLVerification: true)
|
|
|
|
|
|
|
|
// closer version checks already performed in test 'testVersioningPushViaSSH', focusing on
|
|
|
|
// GIT related assertions here
|
|
|
|
|
|
|
|
assertThat(((Iterable)shellRule.shell).join(), containsString('-c http.sslVerify=false'))
|
|
|
|
}
|
|
|
|
|
|
|
|
@Test
|
|
|
|
void testVersioningPushViaHTTPDebugMode() {
|
|
|
|
|
|
|
|
jenkinsCredentialsRule.withCredentials('myGitRepoCredentials', 'me', 'topSecret')
|
|
|
|
|
|
|
|
stepRule.step.artifactSetVersion(
|
|
|
|
script: stepRule.step,
|
|
|
|
juStabGitUtils: gitUtils,
|
|
|
|
buildTool: 'maven',
|
|
|
|
gitCredentialsId: 'myGitRepoCredentials',
|
|
|
|
gitHttpsUrl: 'https://example.org/myGitRepo',
|
|
|
|
gitPushMode: 'HTTPS',
|
|
|
|
debug: true)
|
|
|
|
|
|
|
|
// closer version checks already performed in test 'testVersioningPushViaSSH', focusing on
|
|
|
|
// GIT related assertions here
|
|
|
|
|
|
|
|
assertThat(((Iterable)shellRule.shell).join(), allOf(
|
|
|
|
containsString('GIT_CURL_VERBOSE=1'),
|
|
|
|
containsString('GIT_TRACE=1'),
|
|
|
|
containsString('--verbose'),
|
|
|
|
not(containsString('&>/dev/null'))))
|
|
|
|
}
|
|
|
|
|
2019-07-16 15:33:57 +02:00
|
|
|
@Test
|
|
|
|
void testVersioningPushViaHTTPSInDebugModeEncodingDoesNotRevealSecrets() {
|
|
|
|
|
|
|
|
loggingRule.expect('Debug flag set, but encoded username/password differs from unencoded version. Cannot provide debug output in this case.')
|
|
|
|
loggingRule.expect('Performing git push in quiet mode')
|
|
|
|
|
|
|
|
jenkinsCredentialsRule.withCredentials('myGitRepoCredentials', 'me', 'top@Secret')
|
|
|
|
|
|
|
|
stepRule.step.artifactSetVersion(
|
|
|
|
script: stepRule.step,
|
|
|
|
juStabGitUtils: gitUtils,
|
|
|
|
buildTool: 'maven',
|
|
|
|
gitCredentialsId: 'myGitRepoCredentials',
|
|
|
|
gitHttpsUrl: 'https://example.org/myGitRepo',
|
|
|
|
gitPushMode: 'HTTPS',
|
|
|
|
debug: true)
|
|
|
|
|
|
|
|
// closer version checks already performed in test 'testVersioningPushViaSSH', focusing on
|
|
|
|
// GIT related assertions here
|
|
|
|
|
|
|
|
assertThat(((Iterable)shellRule.shell).join(), stringContainsInOrder([
|
|
|
|
"git add .",
|
|
|
|
"git commit -m 'update version 1.2.3-20180101010203_testCommitId'",
|
|
|
|
'git tag build_1.2.3-20180101010203_testCommitId',
|
|
|
|
'#!/bin/bash -e git push --quiet https://me:top%40Secret@example.org/myGitRepo build_1.2.3-20180101010203_testCommitId &>/dev/null',
|
|
|
|
]
|
|
|
|
))
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2019-07-12 17:01:33 +02:00
|
|
|
@Test
|
|
|
|
void testVersioningPushViaHTTPSEncodingDoesNotRevealSecrets() {
|
|
|
|
|
|
|
|
// Credentials needs to be url encoded. In case that changes the secrets the credentials plugin
|
|
|
|
// doesn't hide the secrets anymore in the log. Hence we have to take care that the command is silent.
|
|
|
|
// Check for more details how that is handled in the step.
|
|
|
|
|
|
|
|
loggingRule.expect('Performing git push in quiet mode')
|
|
|
|
|
|
|
|
jenkinsCredentialsRule.withCredentials('myGitRepoCredentials', 'me', 'top@Secret')
|
|
|
|
|
|
|
|
stepRule.step.artifactSetVersion(
|
|
|
|
script: stepRule.step,
|
|
|
|
juStabGitUtils: gitUtils,
|
|
|
|
buildTool: 'maven',
|
|
|
|
gitCredentialsId: 'myGitRepoCredentials',
|
|
|
|
gitHttpsUrl: 'https://example.org/myGitRepo',
|
|
|
|
gitPushMode: 'HTTPS')
|
|
|
|
|
|
|
|
// closer version checks already performed in test 'testVersioningPushViaSSH', focusing on
|
|
|
|
// GIT related assertions here
|
|
|
|
|
|
|
|
assertThat(((Iterable)shellRule.shell).join(), stringContainsInOrder([
|
|
|
|
"git add .",
|
|
|
|
"git commit -m 'update version 1.2.3-20180101010203_testCommitId'",
|
|
|
|
'git tag build_1.2.3-20180101010203_testCommitId',
|
|
|
|
'#!/bin/bash -e git push --quiet https://me:top%40Secret@example.org/myGitRepo build_1.2.3-20180101010203_testCommitId &>/dev/null',
|
|
|
|
]
|
|
|
|
))
|
|
|
|
}
|
|
|
|
|
2018-03-05 10:04:53 +02:00
|
|
|
@Test
|
|
|
|
void testVersioningWithoutCommit() {
|
2019-01-22 10:25:42 +02:00
|
|
|
stepRule.step.artifactSetVersion(script: stepRule.step, juStabGitUtils: gitUtils, buildTool: 'maven', commitVersion: false)
|
2018-04-05 11:36:51 +02:00
|
|
|
|
2019-01-22 10:27:45 +02:00
|
|
|
assertEquals('1.2.3-20180101010203_testCommitId', environmentRule.env.getArtifactVersion())
|
2019-01-22 10:19:28 +02:00
|
|
|
assertThat(shellRule.shell, hasItem("mvn --file 'pom.xml' --batch-mode -Dorg.slf4j.simpleLogger.log.org.apache.maven.cli.transfer.Slf4jMavenTransferListener=warn versions:set -DnewVersion=1.2.3-20180101010203_testCommitId -DgenerateBackupPoms=false"))
|
|
|
|
assertThat(shellRule.shell, not(hasItem(containsString('commit'))))
|
2018-03-05 10:04:53 +02:00
|
|
|
}
|
|
|
|
|
2018-02-07 14:17:33 +02:00
|
|
|
@Test
|
|
|
|
void testVersioningCustomGitUserAndEMail() {
|
2019-01-22 10:25:42 +02:00
|
|
|
stepRule.step.artifactSetVersion(script: stepRule.step, juStabGitUtils: gitUtils, buildTool: 'maven', gitSshUrl: 'myGitSshUrl', gitUserEMail: 'test@test.com', gitUserName: 'test')
|
2018-02-07 14:17:33 +02:00
|
|
|
|
2019-01-22 10:19:28 +02:00
|
|
|
assertThat(shellRule.shell, hasItem(containsString("git -c user.email=\"test@test.com\" -c user.name=\"test\" commit -m 'update version 1.2.3-20180101010203_testCommitId'")))
|
2018-02-07 14:17:33 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@Test
|
|
|
|
void testVersioningWithTimestamp() {
|
2019-01-22 10:25:42 +02:00
|
|
|
stepRule.step.artifactSetVersion(script: stepRule.step, juStabGitUtils: gitUtils, buildTool: 'maven', timestamp: '2018')
|
2019-01-22 10:27:45 +02:00
|
|
|
assertEquals('1.2.3-2018_testCommitId', environmentRule.env.getArtifactVersion())
|
2018-02-07 14:17:33 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@Test
|
|
|
|
void testVersioningNoBuildTool() {
|
|
|
|
thrown.expect(Exception)
|
|
|
|
thrown.expectMessage('ERROR - NO VALUE AVAILABLE FOR buildTool')
|
2019-01-22 10:25:42 +02:00
|
|
|
stepRule.step.artifactSetVersion(script: stepRule.step, juStabGitUtils: gitUtils)
|
2018-02-07 14:17:33 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@Test
|
|
|
|
void testVersioningWithCustomTemplate() {
|
2019-01-22 10:25:42 +02:00
|
|
|
stepRule.step.artifactSetVersion(script: stepRule.step, juStabGitUtils: gitUtils, buildTool: 'maven', versioningTemplate: '${version}-xyz')
|
2019-01-22 10:27:45 +02:00
|
|
|
assertEquals('1.2.3-xyz', environmentRule.env.getArtifactVersion())
|
2018-02-07 14:17:33 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@Test
|
|
|
|
void testVersioningWithTypeAppContainer() {
|
2018-08-08 22:21:26 +02:00
|
|
|
nullScript.commonPipelineEnvironment.setAppContainerProperty('gitSshUrl', 'git@test.url')
|
2019-01-22 10:27:45 +02:00
|
|
|
environmentRule.env.setArtifactVersion('1.2.3-xyz')
|
2019-01-22 10:25:42 +02:00
|
|
|
stepRule.step.artifactSetVersion(script: stepRule.step, juStabGitUtils: gitUtils, buildTool: 'docker', artifactType: 'appContainer', dockerVersionSource: 'appVersion')
|
2019-01-22 10:27:45 +02:00
|
|
|
assertEquals('1.2.3-xyz', environmentRule.env.getArtifactVersion())
|
2019-01-22 10:29:15 +02:00
|
|
|
assertEquals('1.2.3-xyz', writeFileRule.files['VERSION'])
|
2018-02-07 14:17:33 +02:00
|
|
|
}
|
|
|
|
|
2018-08-08 22:21:26 +02:00
|
|
|
@Test
|
|
|
|
void testCredentialCompatibility() {
|
2019-01-22 10:25:42 +02:00
|
|
|
stepRule.step.artifactSetVersion (
|
2018-08-08 22:21:26 +02:00
|
|
|
script: nullScript,
|
|
|
|
buildTool: 'maven',
|
|
|
|
gitCredentialsId: 'testCredentials',
|
|
|
|
juStabGitUtils: gitUtils
|
|
|
|
)
|
|
|
|
assertThat(sshAgentList, hasItem('testCredentials'))
|
|
|
|
}
|
2018-02-07 14:17:33 +02:00
|
|
|
}
|