github.com/ouraigua/jenkins-library@v0.0.0-20231028010029-fbeaf2f3aa9b/test/groovy/ArtifactSetVersionTest.groovy (about) 1 import org.junit.After 2 import org.junit.Before 3 import org.junit.Rule 4 import org.junit.Test 5 import org.junit.rules.ExpectedException 6 import org.junit.rules.RuleChain 7 8 import com.sap.piper.GitUtils 9 import com.sap.piper.Utils 10 11 import hudson.AbortException 12 import util.BasePiperTest 13 import util.JenkinsCredentialsRule 14 import util.JenkinsDockerExecuteRule 15 import util.JenkinsEnvironmentRule 16 import util.JenkinsLoggingRule 17 import util.JenkinsMavenExecuteRule 18 import util.JenkinsReadMavenPomRule 19 import util.JenkinsReadYamlRule 20 import util.JenkinsShellCallRule 21 import util.JenkinsStepRule 22 import util.JenkinsWriteFileRule 23 import util.Rules 24 25 import static org.hamcrest.Matchers.hasItem 26 import static org.hamcrest.Matchers.hasItems 27 import static org.hamcrest.Matchers.not 28 import static org.hamcrest.Matchers.notNullValue 29 import static org.hamcrest.Matchers.stringContainsInOrder 30 import static org.hamcrest.Matchers.allOf 31 import static org.hamcrest.Matchers.containsString 32 import static org.hamcrest.Matchers.emptyIterable 33 import static org.junit.Assert.assertThat 34 35 import org.hamcrest.Matchers 36 37 import static org.junit.Assert.assertEquals 38 39 class ArtifactSetVersionTest extends BasePiperTest { 40 Map dockerParameters 41 42 def GitUtils gitUtils = new GitUtils() { 43 boolean isWorkTreeDirty() { 44 return false 45 } 46 47 String getGitCommitIdOrNull() { 48 return 'testCommitId' 49 } 50 } 51 52 def sshAgentList = [] 53 54 private ExpectedException thrown = ExpectedException.none() 55 private JenkinsDockerExecuteRule dockerExecuteRule = new JenkinsDockerExecuteRule(this) 56 private JenkinsLoggingRule loggingRule = new JenkinsLoggingRule(this) 57 private JenkinsShellCallRule shellRule = new JenkinsShellCallRule(this) 58 private JenkinsMavenExecuteRule mvnExecuteRule = new JenkinsMavenExecuteRule(this) 59 private JenkinsWriteFileRule writeFileRule = new JenkinsWriteFileRule(this) 60 private JenkinsStepRule stepRule = new JenkinsStepRule(this) 61 private JenkinsEnvironmentRule environmentRule = new JenkinsEnvironmentRule(this) 62 private JenkinsCredentialsRule jenkinsCredentialsRule = new JenkinsCredentialsRule(this) 63 64 @Rule 65 public RuleChain ruleChain = Rules 66 .getCommonRules(this) 67 .around(new JenkinsReadYamlRule(this)) 68 .around(thrown) 69 .around(loggingRule) 70 .around(shellRule) 71 .around(new JenkinsReadMavenPomRule(this, 'test/resources/versioning/MavenArtifactVersioning')) 72 .around(writeFileRule) 73 .around(dockerExecuteRule) 74 .around(stepRule) 75 .around(jenkinsCredentialsRule) 76 .around(environmentRule) 77 .around(mvnExecuteRule) 78 79 @Before 80 void init() throws Throwable { 81 dockerParameters = [:] 82 String version = '1.2.3' 83 84 nullScript.commonPipelineEnvironment.setArtifactVersion(null) 85 nullScript.commonPipelineEnvironment.setGitSshUrl('git@test.url') 86 87 helper.registerAllowedMethod("sshagent", [List.class, Closure.class], { list, closure -> 88 sshAgentList = list 89 return closure() 90 }) 91 92 mvnExecuteRule.setReturnValue([ 93 'pomPath': 'pom.xml', 94 'goals': ['org.apache.maven.plugins:maven-help-plugin:3.1.0:evaluate'], 95 'defines': ['-Dexpression=project.version', '-DforceStdout', '-q'], 96 ], version) 97 98 mvnExecuteRule.setReturnValue([ 99 'pomPath': 'snapshot/pom.xml', 100 'goals': ['org.apache.maven.plugins:maven-help-plugin:3.1.0:evaluate'], 101 'defines': ['-Dexpression=project.version', '-DforceStdout', '-q'], 102 ], version) 103 104 shellRule.setReturnValue("date --utc +'%Y%m%d%H%M%S'", '20180101010203') 105 shellRule.setReturnValue('git diff --quiet HEAD', 0) 106 107 helper.registerAllowedMethod('fileExists', [String.class], {true}) 108 109 Utils.metaClass.echo = { def m -> } 110 } 111 112 @After 113 public void tearDown() { 114 Utils.metaClass = null 115 } 116 117 @Test 118 void testVersioningPushViaSSH() { 119 stepRule.step.artifactSetVersion(script: stepRule.step, juStabGitUtils: gitUtils, buildTool: 'maven', gitSshUrl: 'myGitSshUrl') 120 121 assertEquals('1.2.3-20180101010203_testCommitId', environmentRule.env.getArtifactVersion()) 122 assertEquals('testCommitId', environmentRule.env.getGitCommitId()) 123 124 assertEquals(new JenkinsMavenExecuteRule.Execution([ 125 pomPath: 'pom.xml', 126 goals: ['org.codehaus.mojo:versions-maven-plugin:2.7:set'], 127 defines: ['-DnewVersion=1.2.3-20180101010203_testCommitId', '-DgenerateBackupPoms=false'] 128 ]), mvnExecuteRule.executions[1]) 129 130 assertThat(shellRule.shell.join(), stringContainsInOrder([ 131 "git add .", 132 "git commit -m 'update version 1.2.3-20180101010203_testCommitId'", 133 'git tag build_1.2.3-20180101010203_testCommitId', 134 'git push myGitSshUrl build_1.2.3-20180101010203_testCommitId', 135 ] 136 )) 137 } 138 139 @Test 140 void testVersioningNoPush() { 141 142 stepRule.step.artifactSetVersion( 143 script: stepRule.step, 144 juStabGitUtils: gitUtils, 145 buildTool: 'maven', 146 gitPushMode: 'NONE') 147 148 assertThat(loggingRule.log, containsString('Git push to remote has been skipped.')) 149 assertThat(((Iterable)shellRule.shell).join(), not(containsString('push'))) 150 } 151 152 @Test 153 void testVersioningPushViaHTTPS() { 154 155 jenkinsCredentialsRule.withCredentials('myGitRepoCredentials', 'me', 'topSecret') 156 157 stepRule.step.artifactSetVersion( 158 script: stepRule.step, 159 juStabGitUtils: gitUtils, 160 buildTool: 'maven', 161 gitHttpsCredentialsId: 'myGitRepoCredentials', 162 gitHttpsUrl: 'https://example.org/myGitRepo', 163 gitPushMode: 'HTTPS') 164 165 // closer version checks already performed in test 'testVersioningPushViaSSH', focusing on 166 // GIT related assertions here 167 168 assertEquals(new JenkinsMavenExecuteRule.Execution([ 169 pomPath: 'pom.xml', 170 goals: ['org.codehaus.mojo:versions-maven-plugin:2.7:set'], 171 defines: ['-DnewVersion=1.2.3-20180101010203_testCommitId', '-DgenerateBackupPoms=false'] 172 ]), mvnExecuteRule.executions[1]) 173 assertThat(((Iterable)shellRule.shell).join(), stringContainsInOrder([ 174 "git add .", 175 "git commit -m 'update version 1.2.3-20180101010203_testCommitId'", 176 'git tag build_1.2.3-20180101010203_testCommitId', 177 'git push https://me:topSecret@example.org/myGitRepo build_1.2.3-20180101010203_testCommitId', 178 ] 179 )) 180 } 181 182 @Test 183 void testVersioningPushViaHTTPDisableSSLCheck() { 184 185 jenkinsCredentialsRule.withCredentials('myGitRepoCredentials', 'me', 'topSecret') 186 187 stepRule.step.artifactSetVersion( 188 script: stepRule.step, 189 juStabGitUtils: gitUtils, 190 buildTool: 'maven', 191 gitHttpsCredentialsId: 'myGitRepoCredentials', 192 gitHttpsUrl: 'https://example.org/myGitRepo', 193 gitPushMode: 'HTTPS', 194 gitDisableSslVerification: true) 195 196 // closer version checks already performed in test 'testVersioningPushViaSSH', focusing on 197 // GIT related assertions here 198 199 assertThat(((Iterable)shellRule.shell).join(), containsString('-c http.sslVerify=false')) 200 } 201 202 @Test 203 void testVersioningPushViaHTTPVerboseMode() { 204 205 jenkinsCredentialsRule.withCredentials('myGitRepoCredentials', 'me', 'topSecret') 206 207 stepRule.step.artifactSetVersion( 208 script: stepRule.step, 209 juStabGitUtils: gitUtils, 210 buildTool: 'maven', 211 gitHttpsCredentialsId: 'myGitRepoCredentials', 212 gitHttpsUrl: 'https://example.org/myGitRepo', 213 gitPushMode: 'HTTPS', 214 verbose: true) 215 216 // closer version checks already performed in test 'testVersioningPushViaSSH', focusing on 217 // GIT related assertions here 218 219 assertThat(((Iterable)shellRule.shell).join(), allOf( 220 containsString('GIT_CURL_VERBOSE=1'), 221 containsString('GIT_TRACE=1'), 222 containsString('--verbose'), 223 not(containsString('&>/dev/null')))) 224 } 225 226 @Test 227 void testVersioningPushViaHTTPSInDebugModeEncodingDoesNotRevealSecrets() { 228 229 loggingRule.expect('Verbose flag set, but encoded username/password differs from unencoded version. Cannot provide verbose output in this case.') 230 loggingRule.expect('Performing git push in quiet mode') 231 232 jenkinsCredentialsRule.withCredentials('myGitRepoCredentials', 'me', 'top@Secret') 233 234 stepRule.step.artifactSetVersion( 235 script: stepRule.step, 236 juStabGitUtils: gitUtils, 237 buildTool: 'maven', 238 gitHttpsCredentialsId: 'myGitRepoCredentials', 239 gitHttpsUrl: 'https://example.org/myGitRepo', 240 gitPushMode: 'HTTPS', 241 verbose: true) 242 243 // closer version checks already performed in test 'testVersioningPushViaSSH', focusing on 244 // GIT related assertions here 245 246 assertThat(((Iterable)shellRule.shell).join(), stringContainsInOrder([ 247 "git add .", 248 "git commit -m 'update version 1.2.3-20180101010203_testCommitId'", 249 'git tag build_1.2.3-20180101010203_testCommitId', 250 '#!/bin/bash -e git push --quiet https://me:top%40Secret@example.org/myGitRepo build_1.2.3-20180101010203_testCommitId &>/dev/null', 251 ] 252 )) 253 } 254 255 256 @Test 257 void testVersioningPushViaHTTPSEncodingDoesNotRevealSecrets() { 258 259 // Credentials needs to be url encoded. In case that changes the secrets the credentials plugin 260 // doesn't hide the secrets anymore in the log. Hence we have to take care that the command is silent. 261 // Check for more details how that is handled in the step. 262 263 loggingRule.expect('Performing git push in quiet mode') 264 265 jenkinsCredentialsRule.withCredentials('myGitRepoCredentials', 'me', 'top@Secret') 266 267 stepRule.step.artifactSetVersion( 268 script: stepRule.step, 269 juStabGitUtils: gitUtils, 270 buildTool: 'maven', 271 gitHttpsCredentialsId: 'myGitRepoCredentials', 272 gitHttpsUrl: 'https://example.org/myGitRepo', 273 gitPushMode: 'HTTPS') 274 275 // closer version checks already performed in test 'testVersioningPushViaSSH', focusing on 276 // GIT related assertions here 277 278 assertThat(((Iterable)shellRule.shell).join(), stringContainsInOrder([ 279 "git add .", 280 "git commit -m 'update version 1.2.3-20180101010203_testCommitId'", 281 'git tag build_1.2.3-20180101010203_testCommitId', 282 '#!/bin/bash -e git push --quiet https://me:top%40Secret@example.org/myGitRepo build_1.2.3-20180101010203_testCommitId &>/dev/null', 283 ] 284 )) 285 } 286 287 @Test 288 void testVersioningWithoutCommit() { 289 stepRule.step.artifactSetVersion(script: stepRule.step, juStabGitUtils: gitUtils, buildTool: 'maven', commitVersion: false) 290 291 assertEquals('1.2.3-20180101010203_testCommitId', environmentRule.env.getArtifactVersion()) 292 assertEquals(new JenkinsMavenExecuteRule.Execution([ 293 pomPath: 'pom.xml', 294 goals: ['org.codehaus.mojo:versions-maven-plugin:2.7:set'], 295 defines: ['-DnewVersion=1.2.3-20180101010203_testCommitId', '-DgenerateBackupPoms=false'] 296 ]), mvnExecuteRule.executions[1]) 297 assertThat(shellRule.shell, not(hasItem(containsString('commit')))) 298 } 299 300 @Test 301 void testVersioningCustomGitUserAndEMail() { 302 stepRule.step.artifactSetVersion(script: stepRule.step, juStabGitUtils: gitUtils, buildTool: 'maven', gitSshUrl: 'myGitSshUrl', gitUserEMail: 'test@test.com', gitUserName: 'test') 303 304 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'"))) 305 } 306 307 @Test 308 void testVersioningWithTimestamp() { 309 stepRule.step.artifactSetVersion(script: stepRule.step, juStabGitUtils: gitUtils, buildTool: 'maven', timestamp: '2018') 310 assertEquals('1.2.3-2018_testCommitId', environmentRule.env.getArtifactVersion()) 311 } 312 313 @Test 314 void testVersioningNoBuildTool() { 315 thrown.expect(Exception) 316 thrown.expectMessage('ERROR - NO VALUE AVAILABLE FOR buildTool') 317 stepRule.step.artifactSetVersion(script: stepRule.step, juStabGitUtils: gitUtils) 318 } 319 320 @Test 321 void testVersioningWithCustomTemplate() { 322 stepRule.step.artifactSetVersion(script: stepRule.step, juStabGitUtils: gitUtils, buildTool: 'maven', versioningTemplate: '${version}-xyz') 323 assertEquals('1.2.3-xyz', environmentRule.env.getArtifactVersion()) 324 } 325 326 @Test 327 void testVersioningWithTypeAppContainer() { 328 nullScript.commonPipelineEnvironment.setAppContainerProperty('gitSshUrl', 'git@test.url') 329 environmentRule.env.setArtifactVersion('1.2.3-xyz') 330 stepRule.step.artifactSetVersion(script: stepRule.step, juStabGitUtils: gitUtils, buildTool: 'docker', artifactType: 'appContainer', dockerVersionSource: 'appVersion') 331 assertEquals('1.2.3-xyz', environmentRule.env.getArtifactVersion()) 332 assertEquals('1.2.3-xyz', writeFileRule.files['VERSION']) 333 } 334 335 @Test 336 void testCredentialCompatibility() { 337 stepRule.step.artifactSetVersion ( 338 script: nullScript, 339 buildTool: 'maven', 340 gitCredentialsId: 'testCredentials', 341 juStabGitUtils: gitUtils 342 ) 343 assertThat(sshAgentList, hasItem('testCredentials')) 344 } 345 }