1
0
mirror of https://github.com/SAP/jenkins-library.git synced 2024-12-12 10:55:20 +02:00

stashing tests (#4379)

Co-authored-by: Alexander Link <33052602+alxsap@users.noreply.github.com>

Co-authored-by: Alexander Link <33052602+alxsap@users.noreply.github.com>
This commit is contained in:
Marcus Holl 2023-06-01 15:49:07 +02:00 committed by GitHub
parent 83519eb771
commit de7027df40
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -10,6 +10,9 @@ import org.junit.rules.ExpectedException
import org.junit.rules.RuleChain
import static org.hamcrest.Matchers.is
import static org.hamcrest.Matchers.hasItem
import static org.hamcrest.Matchers.hasItems
import static org.hamcrest.Matchers.hasSize
import util.JenkinsLoggingRule
import util.JenkinsShellCallRule
@ -43,12 +46,197 @@ class UtilsTest extends BasePiperTest {
assertThat(result, is('0dad6c33b6246702132454f604dee80740f399ad'))
}
@Test
void testStashWithDefaults() {
Map stashProperties
def examinee = newExaminee(
stashClosure: { Map stashProps ->
stashProperties = stashProps
}
)
examinee.stash('foo')
assertThat(stashProperties, is([name: 'foo', includes: '**/*.*', excludes: '']))
}
@Test
void testStashWithIncludesAndExcludes() {
Map stashProperties
def examinee = newExaminee(
stashClosure: { Map stashProps ->
stashProperties = stashProps
}
)
examinee.stash('foo', '**/*.mtar', '**/target')
assert(stashProperties == [name: 'foo', includes: '**/*.mtar', excludes: '**/target'])
}
@Test
void testStashListStashesAllStashes() {
def stashes = [] as Set
def examinee = newExaminee(
stashClosure: { Map stash ->
stashes << stash
}
)
examinee.stashList(nullScript, [
[
name: 'foo',
includes: '*.foo',
excludes: 'target/foo/*'
],
[
name: 'bar',
includes: '*.bar',
excludes: 'target/bar/*'
]
])
assert stashes == [
[name: 'foo', includes: '*.foo', excludes: 'target/foo/*', allowEmpty: true],
[name: 'bar', includes: '*.bar', excludes: 'target/bar/*', allowEmpty: true]
] as Set
}
@Test
void testStashListDoesNotSwallowException() {
thrown.expect(RuntimeException.class)
thrown.expectMessage('something went wrong')
def examinee = newExaminee(
stashClosure: { Map stash ->
throw new RuntimeException('something went wrong')
}
)
examinee.stashList(nullScript, [
[
name: 'fail',
includes: '*.fail',
excludes: 'target/fail/*'
],
])
}
@Test
void testUnstashStageFilesUnstashesAllUnstashableStashes() {
// We do not fail in case a stash cannot be unstashed
// That might be barely OK for non-existing stashes, but there might also be
// real issues, e.g. related to permission issues when overwriting existing files
// maybe also from other stashes unstashed earlier.
// The behaviour wrt unstashable stashes should be improved. In case of issues
// with unstashing, we should throw an exception
boolean deleteDirCalled = false
def unstashed = []
def examinee = newExaminee(
unstashClosure: { def stashName ->
if(stashName == 'fail') {
throw new RuntimeException('something went wrong')
}
unstashed << stashName
}
)
nullScript.commonPipelineEnvironment.configuration.stageStashes = [
foo : [
unstash: ['stash-1', 'stash-2', 'fail', 'duplicate']
]
]
nullScript.metaClass.deleteDir = { deleteDirCalled = true }
def stashResult = examinee.unstashStageFiles(nullScript, 'foo', ['additional-stash', 'duplicate'])
assertThat(deleteDirCalled, is(true))
assertThat(unstashed, hasSize(5)) // should be 4 since we should not unstash 'duplicate' twice
assertThat(unstashed, hasItems('stash-1', 'stash-2', 'additional-stash', 'duplicate'))
// This is inconsistent. Above we can see only four different stashes has been unstashed ('duplicate' twice),
// but here we see that the stashResult contains six entries, also the 'fail' entry
// for which we throw an exception (... and duplicate twice).
// We should fix that and adjust the test accordingly with the fix.
assertThat(stashResult, hasSize(6))
assertThat(stashResult, hasItems('stash-1', 'stash-2', 'additional-stash', 'fail', 'duplicate'))
// cleanup the deleteDir method
nullScript.metaClass = null
}
@Test
void testUnstashAllSkipNull() {
def stashResult = utils.unstashAll(['a', null, 'b'])
assert stashResult == ['a', 'b']
}
@Test
void testUnstashSkipsFailedUnstashes() {
def examinee = newExaminee(
unstashClosure: { def stashName ->
if(stashName == 'fail') {
throw new RuntimeException('something went wrong')
}
}
)
def stashResult = examinee.unstashAll(['a', 'fail', 'b'])
assert stashResult == ['a', 'b']
}
@Test
void testUnstashAllSucceeds() {
def unstashed = [] as Set
def examinee = newExaminee(unstashClosure: { def stashName -> unstashed << stashName})
examinee.unstashAll(['a', 'b'])
assert(unstashed == ['a', 'b'] as Set)
}
@Test
void testUnstashFails() {
def logMessages = []
def examinee = newExaminee(
unstashClosure: {
def stashName -> throw new RuntimeException('something went wrong')
},
echoClosure: {
// coerce to java.lang.String, we might have GStrings.
// comparism with java.lang.String might fail.
message -> logMessages << message.toString()
}
)
def stashResult = examinee.unstash('a')
// in case unstash fails (maybe the stash does not exist, or we cannot unstash due to
// some colliding files in conjunction with file permissions) we emit a log message
// and continue silently instead of failing. In that case we get an empty array back
// instead an array containing the name of the unstashed stash.
assertThat(logMessages, hasItem('Unstash failed: a (something went wrong)'))
assert(stashResult == [])
}
private Utils newExaminee(Map parameters) {
def examinee = new Utils()
examinee.steps = [
stash: parameters.stashClosure ?: {},
unstash: parameters.unstashClosure ?: {},
]
examinee.echo = parameters.echoClosure ?: {}
return examinee
}
@Test
void testAppendNonExistingParameterToStringList() {
Map parameters = [:]