github.com/ouraigua/jenkins-library@v0.0.0-20231028010029-fbeaf2f3aa9b/test/groovy/com/sap/piper/UtilsTest.groovy (about) 1 package com.sap.piper 2 3 import org.junit.Rule 4 import org.junit.Before 5 import org.junit.Test 6 import static org.junit.Assert.assertEquals 7 import static org.junit.Assert.assertThat 8 import static org.junit.Assert.assertTrue 9 import org.junit.rules.ExpectedException 10 import org.junit.rules.RuleChain 11 12 import static org.hamcrest.Matchers.is 13 import static org.hamcrest.Matchers.hasItem 14 import static org.hamcrest.Matchers.hasItems 15 import static org.hamcrest.Matchers.hasSize 16 17 import util.JenkinsLoggingRule 18 import util.JenkinsShellCallRule 19 import util.BasePiperTest 20 import util.Rules 21 22 class UtilsTest extends BasePiperTest { 23 private ExpectedException thrown = ExpectedException.none() 24 private JenkinsLoggingRule loggingRule = new JenkinsLoggingRule(this) 25 private JenkinsShellCallRule shellRule = new JenkinsShellCallRule(this) 26 27 @Rule 28 public RuleChain rules = Rules 29 .getCommonRules(this) 30 .around(thrown) 31 .around(shellRule) 32 .around(loggingRule) 33 34 private parameters 35 36 @Before 37 void setup() { 38 parameters = [:] 39 } 40 41 @Test 42 void testGenerateSHA1() { 43 def result = utils.generateSha1('ContinuousDelivery') 44 // asserts 45 // generated with "echo -n 'ContinuousDelivery' | sha1sum | sed 's/ -//'" 46 assertThat(result, is('0dad6c33b6246702132454f604dee80740f399ad')) 47 } 48 49 @Test 50 void testStashWithDefaults() { 51 Map stashProperties 52 53 def examinee = newExaminee( 54 stashClosure: { Map stashProps -> 55 stashProperties = stashProps 56 } 57 ) 58 examinee.stash('foo') 59 60 assertThat(stashProperties, is([name: 'foo', includes: '**/*.*', excludes: ''])) 61 } 62 63 @Test 64 void testStashWithIncludesAndExcludes() { 65 Map stashProperties 66 67 def examinee = newExaminee( 68 stashClosure: { Map stashProps -> 69 stashProperties = stashProps 70 } 71 ) 72 73 examinee.stash('foo', '**/*.mtar', '**/target') 74 75 assert(stashProperties == [name: 'foo', includes: '**/*.mtar', excludes: '**/target']) 76 } 77 78 @Test 79 void testStashListStashesAllStashes() { 80 def stashes = [] as Set 81 def examinee = newExaminee( 82 stashClosure: { Map stash -> 83 stashes << stash 84 } 85 ) 86 87 examinee.stashList(nullScript, [ 88 [ 89 name: 'foo', 90 includes: '*.foo', 91 excludes: 'target/foo/*' 92 ], 93 [ 94 name: 'bar', 95 includes: '*.bar', 96 excludes: 'target/bar/*' 97 ] 98 ]) 99 100 assert stashes == [ 101 [name: 'foo', includes: '*.foo', excludes: 'target/foo/*', allowEmpty: true], 102 [name: 'bar', includes: '*.bar', excludes: 'target/bar/*', allowEmpty: true] 103 ] as Set 104 } 105 106 @Test 107 void testStashListDoesNotSwallowException() { 108 109 thrown.expect(RuntimeException.class) 110 thrown.expectMessage('something went wrong') 111 112 def examinee = newExaminee( 113 stashClosure: { Map stash -> 114 throw new RuntimeException('something went wrong') 115 } 116 ) 117 118 examinee.stashList(nullScript, [ 119 [ 120 name: 'fail', 121 includes: '*.fail', 122 excludes: 'target/fail/*' 123 ], 124 ]) 125 } 126 127 @Test 128 void testUnstashStageFilesUnstashesAllUnstashableStashes() { 129 130 // We do not fail in case a stash cannot be unstashed 131 // That might be barely OK for non-existing stashes, but there might also be 132 // real issues, e.g. related to permission issues when overwriting existing files 133 // maybe also from other stashes unstashed earlier. 134 // The behaviour wrt unstashable stashes should be improved. In case of issues 135 // with unstashing, we should throw an exception 136 137 boolean deleteDirCalled = false 138 def unstashed = [] 139 def examinee = newExaminee( 140 unstashClosure: { def stashName -> 141 if(stashName == 'fail') { 142 throw new RuntimeException('something went wrong') 143 } 144 unstashed << stashName 145 } 146 ) 147 148 nullScript.commonPipelineEnvironment.configuration.stageStashes = [ 149 foo : [ 150 unstash: ['stash-1', 'stash-2', 'fail', 'duplicate'] 151 ] 152 ] 153 154 nullScript.metaClass.deleteDir = { deleteDirCalled = true } 155 156 def stashResult = examinee.unstashStageFiles(nullScript, 'foo', ['additional-stash', 'duplicate']) 157 158 assertThat(deleteDirCalled, is(true)) 159 160 assertThat(unstashed, hasSize(5)) // should be 4 since we should not unstash 'duplicate' twice 161 assertThat(unstashed, hasItems('stash-1', 'stash-2', 'additional-stash', 'duplicate')) 162 163 // This is inconsistent. Above we can see only four different stashes has been unstashed ('duplicate' twice), 164 // but here we see that the stashResult contains six entries, also the 'fail' entry 165 // for which we throw an exception (... and duplicate twice). 166 // We should fix that and adjust the test accordingly with the fix. 167 assertThat(stashResult, hasSize(6)) 168 assertThat(stashResult, hasItems('stash-1', 'stash-2', 'additional-stash', 'fail', 'duplicate')) 169 170 // cleanup the deleteDir method 171 nullScript.metaClass = null 172 } 173 174 @Test 175 void testUnstashAllSkipNull() { 176 def stashResult = utils.unstashAll(['a', null, 'b']) 177 assert stashResult == ['a', 'b'] 178 } 179 180 @Test 181 void testUnstashSkipsFailedUnstashes() { 182 183 def examinee = newExaminee( 184 unstashClosure: { def stashName -> 185 if(stashName == 'fail') { 186 throw new RuntimeException('something went wrong') 187 } 188 } 189 ) 190 191 def stashResult = examinee.unstashAll(['a', 'fail', 'b']) 192 193 assert stashResult == ['a', 'b'] 194 } 195 196 197 @Test 198 void testUnstashAllSucceeds() { 199 def unstashed = [] as Set 200 def examinee = newExaminee(unstashClosure: { def stashName -> unstashed << stashName}) 201 202 examinee.unstashAll(['a', 'b']) 203 204 assert(unstashed == ['a', 'b'] as Set) 205 } 206 207 @Test 208 void testUnstashFails() { 209 def logMessages = [] 210 def examinee = newExaminee( 211 unstashClosure: { 212 def stashName -> throw new RuntimeException('something went wrong') 213 }, 214 echoClosure: { 215 // coerce to java.lang.String, we might have GStrings. 216 // comparism with java.lang.String might fail. 217 message -> logMessages << message.toString() 218 } 219 ) 220 def stashResult = examinee.unstash('a') 221 222 // in case unstash fails (maybe the stash does not exist, or we cannot unstash due to 223 // some colliding files in conjunction with file permissions) we emit a log message 224 // and continue silently instead of failing. In that case we get an empty array back 225 // instead an array containing the name of the unstashed stash. 226 assertThat(logMessages, hasItem('Unstash failed: a (something went wrong)')) 227 assert(stashResult == []) 228 } 229 230 private Utils newExaminee(Map parameters) { 231 def examinee = new Utils() 232 examinee.steps = [ 233 stash: parameters.stashClosure ?: {}, 234 unstash: parameters.unstashClosure ?: {}, 235 ] 236 examinee.echo = parameters.echoClosure ?: {} 237 return examinee 238 } 239 240 private def newExamineeRememberingLastStashProperties() { 241 Map stashProperties = [:] 242 Utils examinee = newExaminee( 243 stashClosure: { Map stashProps -> 244 stashProperties.clear() 245 stashProperties << stashProps 246 } 247 ) 248 return [examinee, stashProperties] 249 } 250 251 @Test 252 void testAppendNonExistingParameterToStringList() { 253 Map parameters = [:] 254 List result = Utils.appendParameterToStringList([], parameters, 'non-existing') 255 assertTrue(result.isEmpty()) 256 } 257 258 @Test 259 void testAppendStringParameterToStringList() { 260 Map parameters = ['param': 'string'] 261 List result = Utils.appendParameterToStringList([], parameters, 'param') 262 assertEquals(1, result.size()) 263 } 264 265 @Test 266 void testAppendListParameterToStringList() { 267 Map parameters = ['param': ['string2', 'string3']] 268 List result = Utils.appendParameterToStringList(['string1'], parameters, 'param') 269 assertEquals(['string1', 'string2', 'string3'], result) 270 } 271 272 @Test 273 void testAppendEmptyListParameterToStringList() { 274 Map parameters = ['param': []] 275 List result = Utils.appendParameterToStringList(['string'], parameters, 'param') 276 assertEquals(['string'], result) 277 } 278 279 @Test 280 void testStash_noParentheses() { 281 final def (Utils examinee, Map stashProperties) = newExamineeRememberingLastStashProperties() 282 283 examinee.stash 'test' 284 285 assertEquals([name: 'test', includes: '**/*.*', excludes: ''], stashProperties) 286 } 287 288 @Test 289 void testStashAndLog_noParentheses() { 290 final def (Utils examinee, Map stashProperties) = newExamineeRememberingLastStashProperties() 291 292 examinee.stash name: 'test' 293 294 assertEquals([name: 'test', includes: '**/*.*', excludes: ''], stashProperties) 295 } 296 297 @Test 298 void testStash_simpleSignature1Param() { 299 final def (Utils examinee, Map stashProperties) = newExamineeRememberingLastStashProperties() 300 Map expected = [name: 'test', includes: '**/*.*', excludes: ''] 301 302 examinee.stash('test') 303 assertEquals(expected, stashProperties) 304 305 examinee.stash(name: 'test') 306 assertEquals(expected, stashProperties) 307 } 308 309 @Test 310 void testStash_simpleSignature2Params() { 311 final def (Utils examinee, Map stashProperties) = newExamineeRememberingLastStashProperties() 312 Map expected = [name: 'test', includes: 'includesX', excludes: ''] 313 314 examinee.stash('test', 'includesX') 315 assertEquals(expected, stashProperties) 316 317 examinee.stash(name: 'test', includes: 'includesX') 318 assertEquals(expected, stashProperties) 319 } 320 321 @Test 322 void testStash_simpleSignature3Params() { 323 final def (Utils examinee, Map stashProperties) = newExamineeRememberingLastStashProperties() 324 Map expected = [name: 'test', includes: 'includesX', excludes: 'excludesX'] 325 326 examinee.stash('test', 'includesX', 'excludesX') 327 assertEquals(expected, stashProperties) 328 329 examinee.stash(name: 'test', includes: 'includesX', excludes: 'excludesX') 330 assertEquals(expected, stashProperties) 331 } 332 333 @Test 334 void testStash_simpleSignature4Params() { 335 final def (Utils examinee, Map stashProperties) = newExamineeRememberingLastStashProperties() 336 Map expected = [name: 'test', includes: 'includesX', excludes: 'excludesX', useDefaultExcludes: false] 337 338 examinee.stash('test', 'includesX', 'excludesX', false) 339 assertEquals(expected, stashProperties) 340 341 examinee.stash(name: 'test', includes: 'includesX', excludes: 'excludesX', useDefaultExcludes: false) 342 assertEquals(expected, stashProperties) 343 } 344 345 @Test 346 void testStash_simpleSignature5Params() { 347 final def (Utils examinee, Map stashProperties) = newExamineeRememberingLastStashProperties() 348 Map expected = [name: 'test', includes: 'includesX', excludes: 'excludesX', useDefaultExcludes: false, allowEmpty: true] 349 350 examinee.stash('test', 'includesX', 'excludesX', false, true) 351 assertEquals(expected, stashProperties) 352 353 examinee.stash(name: 'test', includes: 'includesX', excludes: 'excludesX', useDefaultExcludes: false, allowEmpty: true) 354 assertEquals(expected, stashProperties) 355 } 356 357 @Test 358 void testStash_explicitDefaults() { 359 final def (Utils examinee, Map stashProperties) = newExamineeRememberingLastStashProperties() 360 Map expected = [name: 'test', includes: 'includesX', excludes: 'excludesX'] 361 362 examinee.stash('test', 'includesX', 'excludesX', true, false) 363 assertEquals(expected, stashProperties) 364 365 examinee.stash(name: 'test', includes: 'includesX', excludes: 'excludesX', useDefaultExcludes: true, allowEmpty: false) 366 assertEquals(expected, stashProperties) 367 } 368 369 @Test(expected = IllegalArgumentException.class) 370 void testStashAndLog_noName_fails() { 371 final def (Utils examinee, Map stashProperties) = newExamineeRememberingLastStashProperties() 372 373 examinee.stash([:]) 374 375 assertEquals([includes: 'includesX'], stashProperties) 376 } 377 378 @Test 379 void testStashAndLog_includes() { 380 final def (Utils examinee, Map stashProperties) = newExamineeRememberingLastStashProperties() 381 382 examinee.stash(name: 'test', includes: 'includesX') 383 384 assertEquals([name: 'test', includes: 'includesX', excludes: ''], stashProperties) 385 } 386 387 @Test 388 void testStashAndLog_excludes() { 389 final def (Utils examinee, Map stashProperties) = newExamineeRememberingLastStashProperties() 390 391 examinee.stash(name: 'test', excludes: 'excludesX') 392 393 assertEquals([name: 'test', includes: '**/*.*', excludes: 'excludesX'], stashProperties) 394 } 395 396 @Test 397 void testStashAndLog_useDefaultExcludes() { 398 final def (Utils examinee, Map stashProperties) = newExamineeRememberingLastStashProperties() 399 400 examinee.stash(name: 'test', useDefaultExcludes: true) 401 assertEquals([name: 'test', includes: '**/*.*', excludes: ''], stashProperties) 402 403 examinee.stash(name: 'test', useDefaultExcludes: false) 404 assertEquals([name: 'test', includes: '**/*.*', excludes: '', useDefaultExcludes: false], stashProperties) 405 } 406 407 @Test 408 void testStashAndLog_allowEmpty() { 409 final def (Utils examinee, Map stashProperties) = newExamineeRememberingLastStashProperties() 410 411 examinee.stash(name: 'test', allowEmpty: true) 412 assertEquals([name: 'test', includes: '**/*.*', excludes: '', allowEmpty: true], stashProperties) 413 414 examinee.stash(name: 'test', allowEmpty: false) 415 assertEquals([name: 'test', includes: '**/*.*', excludes: ''], stashProperties) 416 } 417 418 }