2018-02-07 14:17:33 +02:00
|
|
|
#!groovy
|
|
|
|
import org.junit.Before
|
|
|
|
import org.junit.Rule
|
|
|
|
import org.junit.Test
|
|
|
|
import org.junit.rules.ExpectedException
|
|
|
|
import org.junit.rules.RuleChain
|
2018-06-06 11:19:19 +02:00
|
|
|
import util.BasePiperTest
|
|
|
|
import util.JenkinsDockerExecuteRule
|
2018-03-06 14:28:57 +02:00
|
|
|
import util.JenkinsEnvironmentRule
|
|
|
|
import util.JenkinsLoggingRule
|
|
|
|
import util.JenkinsReadMavenPomRule
|
|
|
|
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
|
|
|
|
import static org.junit.Assert.assertThat
|
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-03-02 11:53:09 +02:00
|
|
|
def sshAgentList = []
|
|
|
|
|
2018-02-28 14:11:09 +02:00
|
|
|
private ExpectedException thrown = ExpectedException.none()
|
2018-06-06 11:19:19 +02:00
|
|
|
private JenkinsDockerExecuteRule jder = new JenkinsDockerExecuteRule(this)
|
2018-02-28 14:11:09 +02:00
|
|
|
private JenkinsLoggingRule jlr = new JenkinsLoggingRule(this)
|
|
|
|
private JenkinsShellCallRule jscr = new JenkinsShellCallRule(this)
|
|
|
|
private JenkinsWriteFileRule jwfr = new JenkinsWriteFileRule(this)
|
|
|
|
private JenkinsStepRule jsr = new JenkinsStepRule(this)
|
|
|
|
private JenkinsEnvironmentRule jer = new JenkinsEnvironmentRule(this)
|
2018-02-07 14:17:33 +02:00
|
|
|
|
|
|
|
@Rule
|
|
|
|
public RuleChain ruleChain = Rules
|
|
|
|
.getCommonRules(this)
|
|
|
|
.around(thrown)
|
|
|
|
.around(jlr)
|
|
|
|
.around(jscr)
|
|
|
|
.around(new JenkinsReadMavenPomRule(this, 'test/resources/MavenArtifactVersioning'))
|
|
|
|
.around(jwfr)
|
2018-06-06 11:19:19 +02:00
|
|
|
.around(jder)
|
2018-02-28 12:55:19 +02:00
|
|
|
.around(jsr)
|
|
|
|
.around(jer)
|
2018-02-07 14:17:33 +02:00
|
|
|
|
|
|
|
@Before
|
|
|
|
void init() throws Throwable {
|
2018-03-06 14:28:57 +02:00
|
|
|
dockerParameters = [:]
|
|
|
|
|
2018-02-07 14:17:33 +02:00
|
|
|
helper.registerAllowedMethod("sshagent", [List.class, Closure.class], { list, closure ->
|
|
|
|
sshAgentList = list
|
|
|
|
return closure()
|
|
|
|
})
|
|
|
|
|
|
|
|
jscr.setReturnValue('git rev-parse HEAD', 'testCommitId')
|
2018-03-02 17:35:35 +02:00
|
|
|
jscr.setReturnValue("date --universal +'%Y%m%d%H%M%S'", '20180101010203')
|
2018-02-07 14:17:33 +02:00
|
|
|
jscr.setReturnValue('git diff --quiet HEAD', 0)
|
2018-05-07 14:46:51 +02:00
|
|
|
jscr.setReturnValue('git rev-parse --is-inside-work-tree 1>/dev/null 2>&1', 0)
|
2018-02-07 14:17:33 +02:00
|
|
|
|
2018-03-06 14:28:57 +02:00
|
|
|
binding.setVariable('Jenkins', [instance: [pluginManager: [plugins: [new DockerExecuteTest.PluginMock()]]]])
|
|
|
|
|
2018-06-06 11:19:19 +02:00
|
|
|
helper.registerAllowedMethod('fileExists', [String.class], {true})
|
2018-02-07 14:17:33 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@Test
|
|
|
|
void testVersioning() {
|
2018-04-05 11:36:51 +02:00
|
|
|
jsr.step.call(script: jsr.step, juStabGitUtils: gitUtils, buildTool: 'maven', gitSshUrl: 'myGitSshUrl')
|
2018-02-07 14:17:33 +02:00
|
|
|
|
2018-02-28 12:55:19 +02:00
|
|
|
assertEquals('1.2.3-20180101010203_testCommitId', jer.env.getArtifactVersion())
|
|
|
|
assertEquals('testCommitId', jer.env.getGitCommitId())
|
2018-02-07 14:17:33 +02:00
|
|
|
|
2018-06-06 14:42:47 +02:00
|
|
|
assertThat(jscr.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"))
|
2018-06-06 11:19:19 +02:00
|
|
|
assertThat(jscr.shell, hasItem('git add .'))
|
|
|
|
assertThat(jscr.shell, hasItem("git commit -m 'update version 1.2.3-20180101010203_testCommitId'"))
|
|
|
|
assertThat(jscr.shell, hasItem("git remote set-url origin myGitSshUrl"))
|
|
|
|
assertThat(jscr.shell, hasItem("git tag build_1.2.3-20180101010203_testCommitId"))
|
|
|
|
assertThat(jscr.shell, hasItem("git push origin build_1.2.3-20180101010203_testCommitId"))
|
2018-02-07 14:17:33 +02:00
|
|
|
}
|
|
|
|
|
2018-03-05 10:04:53 +02:00
|
|
|
@Test
|
|
|
|
void testVersioningWithoutCommit() {
|
2018-04-05 11:36:51 +02:00
|
|
|
jsr.step.call(script: jsr.step, juStabGitUtils: gitUtils, buildTool: 'maven', commitVersion: false)
|
|
|
|
|
|
|
|
assertEquals('1.2.3-20180101010203_testCommitId', jer.env.getArtifactVersion())
|
2018-06-06 14:42:47 +02:00
|
|
|
assertThat(jscr.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"))
|
2018-04-05 11:36:51 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@Test
|
|
|
|
void testVersioningWithoutScript() {
|
|
|
|
jsr.step.call(juStabGitUtils: gitUtils, buildTool: 'maven', commitVersion: false)
|
2018-03-05 10:04:53 +02:00
|
|
|
|
|
|
|
assertEquals('1.2.3-20180101010203_testCommitId', jer.env.getArtifactVersion())
|
2018-06-06 14:42:47 +02:00
|
|
|
assertThat(jscr.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"))
|
2018-03-05 10:04:53 +02:00
|
|
|
}
|
|
|
|
|
2018-02-07 14:17:33 +02:00
|
|
|
@Test
|
|
|
|
void testVersioningCustomGitUserAndEMail() {
|
2018-04-05 11:36:51 +02:00
|
|
|
jsr.step.call(script: jsr.step, juStabGitUtils: gitUtils, buildTool: 'maven', gitSshUrl: 'myGitSshUrl', gitUserEMail: 'test@test.com', gitUserName: 'test')
|
2018-02-07 14:17:33 +02:00
|
|
|
|
2018-06-06 11:19:19 +02:00
|
|
|
assertThat(jscr.shell, hasItem("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() {
|
2018-04-05 11:36:51 +02:00
|
|
|
jsr.step.call(script: jsr.step, juStabGitUtils: gitUtils, buildTool: 'maven', timestamp: '2018')
|
2018-02-28 12:55:19 +02:00
|
|
|
assertEquals('1.2.3-2018_testCommitId', jer.env.getArtifactVersion())
|
2018-02-07 14:17:33 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@Test
|
|
|
|
void testVersioningNoBuildTool() {
|
|
|
|
thrown.expect(Exception)
|
|
|
|
thrown.expectMessage('ERROR - NO VALUE AVAILABLE FOR buildTool')
|
2018-04-05 11:36:51 +02:00
|
|
|
jsr.step.call(script: jsr.step, juStabGitUtils: gitUtils)
|
2018-02-07 14:17:33 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@Test
|
|
|
|
void testVersioningWithCustomTemplate() {
|
2018-04-05 11:36:51 +02:00
|
|
|
jsr.step.call(script: jsr.step, juStabGitUtils: gitUtils, buildTool: 'maven', versioningTemplate: '${version}-xyz')
|
2018-02-28 12:55:19 +02:00
|
|
|
assertEquals('1.2.3-xyz', jer.env.getArtifactVersion())
|
2018-02-07 14:17:33 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@Test
|
|
|
|
void testVersioningWithTypeAppContainer() {
|
2018-02-28 12:55:19 +02:00
|
|
|
jer.env.setArtifactVersion('1.2.3-xyz')
|
2018-04-05 11:36:51 +02:00
|
|
|
jsr.step.call(script: jsr.step, juStabGitUtils: gitUtils, buildTool: 'docker', artifactType: 'appContainer', dockerVersionSource: 'appVersion')
|
2018-02-28 12:55:19 +02:00
|
|
|
assertEquals('1.2.3-xyz', jer.env.getArtifactVersion())
|
2018-02-07 14:17:33 +02:00
|
|
|
assertEquals('1.2.3-xyz', jwfr.files['VERSION'])
|
|
|
|
}
|
|
|
|
|
|
|
|
void prepareObjectInterceptors(object) {
|
|
|
|
object.metaClass.invokeMethod = helper.getMethodInterceptor()
|
|
|
|
object.metaClass.static.invokeMethod = helper.getMethodInterceptor()
|
|
|
|
object.metaClass.methodMissing = helper.getMethodMissingInterceptor()
|
|
|
|
}
|
2018-03-06 14:28:57 +02:00
|
|
|
|
2018-02-07 14:17:33 +02:00
|
|
|
}
|