1
0
mirror of https://github.com/SAP/jenkins-library.git synced 2024-12-14 11:03:09 +02:00
sap-jenkins-library/test/groovy/PiperStageWrapperTest.groovy
Oliver Nocon e556fba950
add wrapper for stages contained in library (#341)
* add wrapper for stages contained in library

`piperStageWrapper` provides a wrapper for stages which we may include into the library.
It will take care about extension capabilities, locking, node handling, ... which should be a capability of every stage contained in the library.
2018-12-12 11:45:11 +01:00

119 lines
3.3 KiB
Groovy

#!groovy
import org.junit.Before
import org.junit.Rule
import org.junit.Test
import org.junit.rules.RuleChain
import util.BasePiperTest
import util.JenkinsLoggingRule
import util.JenkinsReadYamlRule
import util.JenkinsStepRule
import util.Rules
import static org.hamcrest.CoreMatchers.containsString
import static org.hamcrest.CoreMatchers.is
import static org.junit.Assert.assertThat
class PiperStageWrapperTest extends BasePiperTest {
private JenkinsLoggingRule jlr = new JenkinsLoggingRule(this)
private JenkinsStepRule jsr = new JenkinsStepRule(this)
private Map lockMap = [:]
private int countNodeUsage = 0
private String nodeLabel = ''
@Rule
public RuleChain rules = Rules
.getCommonRules(this)
.around(new JenkinsReadYamlRule(this))
.around(jlr)
.around(jsr)
@Before
void init() throws Exception {
helper.registerAllowedMethod('deleteDir', [], {return null})
helper.registerAllowedMethod('lock', [Map.class, Closure.class], {m, body ->
assertThat(m.resource.toString(), containsString('/10'))
lockMap = m
body()
})
helper.registerAllowedMethod('milestone', [Integer.class], {ordinal ->
assertThat(ordinal, is(10))
})
helper.registerAllowedMethod('node', [String.class, Closure.class], {s, body ->
nodeLabel = s
countNodeUsage++
body()
})
helper.registerAllowedMethod('fileExists', [String.class], {s ->
return false
})
}
@Test
void testDefault() {
def testInt = 1
jsr.step.piperStageWrapper(
script: nullScript,
juStabUtils: utils,
ordinal: 10,
stageName: 'test'
) {
testInt ++
}
assertThat(testInt, is(2))
assertThat(lockMap.size(), is(2))
assertThat(countNodeUsage, is(1))
}
@Test
void testNoLocking() {
def testInt = 1
jsr.step.piperStageWrapper(
script: nullScript,
juStabUtils: utils,
nodeLabel: 'testLabel',
ordinal: 10,
stageLocking: false,
stageName: 'test'
) {
testInt ++
}
assertThat(testInt, is(2))
assertThat(lockMap.size(), is(0))
assertThat(countNodeUsage, is(1))
assertThat(nodeLabel, is('testLabel'))
}
@Test
void testStageExit() {
helper.registerAllowedMethod('fileExists', [String.class], {s ->
return (s == '.pipeline/extensions/test.groovy')
})
helper.registerAllowedMethod('load', [String.class], {
return helper.loadScript('test/resources/stages/test.groovy')
})
def testInt = 1
jsr.step.piperStageWrapper(
script: nullScript,
juStabUtils: utils,
ordinal: 10,
stageName: 'test'
) {
testInt ++
}
assertThat(testInt, is(2))
assertThat(jlr.log, containsString('[piperStageWrapper] Running project interceptor \'.pipeline/extensions/test.groovy\' for test.'))
assertThat(jlr.log, containsString('Stage Name: test'))
assertThat(jlr.log, containsString('Config:'))
}
}