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/com/sap/piper/JenkinsUtilsTest.groovy
Oliver Nocon 9e539e68ee
Piper pipeline - enhance stages (#755)
* fill PRVoting stage with content
* add prVoting stage incl. tests
* add Build stage incl. Tests
* add docs pages
* add additional unit test stage
* add acceptance stage and additional documentation ...
* add release stage content
* add promote stage
* add Security stage, fix defaults for Promote stage
* fix issue in doc generation
2019-07-03 10:13:26 +02:00

113 lines
3.3 KiB
Groovy

package com.sap.piper
import org.jenkinsci.plugins.workflow.steps.MissingContextVariableException
import org.junit.Before
import org.junit.Rule
import org.junit.Test
import org.junit.rules.ExpectedException
import org.junit.rules.RuleChain
import util.BasePiperTest
import util.JenkinsLoggingRule
import util.JenkinsShellCallRule
import util.LibraryLoadingTestExecutionListener
import util.Rules
import static org.hamcrest.Matchers.*
import static org.junit.Assert.assertThat
class JenkinsUtilsTest extends BasePiperTest {
private JenkinsLoggingRule loggingRule = new JenkinsLoggingRule(this)
private JenkinsShellCallRule shellRule = new JenkinsShellCallRule(this)
@Rule
public RuleChain rules = Rules
.getCommonRules(this)
.around(shellRule)
.around(loggingRule)
JenkinsUtils jenkinsUtils
Object currentBuildMock
Object rawBuildMock
Object jenkinsInstanceMock
Object parentMock
Map triggerCause
@Before
void init() throws Exception {
jenkinsUtils = new JenkinsUtils() {
def getCurrentBuildInstance() {
return currentBuildMock
}
def getActiveJenkinsInstance() {
return jenkinsInstanceMock
}
}
LibraryLoadingTestExecutionListener.prepareObjectInterceptors(jenkinsUtils)
jenkinsInstanceMock = new Object()
LibraryLoadingTestExecutionListener.prepareObjectInterceptors(jenkinsInstanceMock)
parentMock = new Object() {
}
LibraryLoadingTestExecutionListener.prepareObjectInterceptors(parentMock)
rawBuildMock = new Object() {
def getParent() {
return parentMock
}
def getCause(type) {
return triggerCause
}
}
LibraryLoadingTestExecutionListener.prepareObjectInterceptors(rawBuildMock)
currentBuildMock = new Object() {
def number
def getRawBuild() {
return rawBuildMock
}
}
LibraryLoadingTestExecutionListener.prepareObjectInterceptors(currentBuildMock)
}
@Test
void testNodeAvailable() {
def result = jenkinsUtils.nodeAvailable()
assertThat(shellRule.shell, contains("echo 'Node is available!'"))
assertThat(result, is(true))
}
@Test
void testNoNodeAvailable() {
helper.registerAllowedMethod('sh', [String.class], {s ->
throw new MissingContextVariableException(String.class)
})
def result = jenkinsUtils.nodeAvailable()
assertThat(loggingRule.log, containsString('No node context available.'))
assertThat(result, is(false))
}
@Test
void testGetIssueCommentTriggerAction() {
triggerCause = [
comment: 'this is my test comment /n /piper test whatever',
triggerPattern: '.*/piper ([a-z]*).*'
]
assertThat(jenkinsUtils.getIssueCommentTriggerAction(), is('test'))
}
@Test
void testGetIssueCommentTriggerActionNoAction() {
triggerCause = [
comment: 'this is my test comment /n whatever',
triggerPattern: '.*/piper ([a-z]*).*'
]
assertThat(jenkinsUtils.getIssueCommentTriggerAction(), isEmptyOrNullString())
}
}