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/PrepareDefaultValuesTest.groovy

127 lines
4.6 KiB
Groovy
Raw Normal View History

import org.junit.Before
2019-05-22 16:56:50 +02:00
import org.junit.Rule
2018-06-20 11:45:42 +02:00
import org.junit.Test
import org.junit.rules.ExpectedException
2019-05-22 16:56:50 +02:00
import org.junit.rules.RuleChain
2018-06-20 11:45:42 +02:00
import com.sap.piper.DefaultValueCache
import util.BasePiperTest
2018-07-04 11:56:53 +02:00
import util.JenkinsLoggingRule
import util.JenkinsReadYamlRule
2019-05-22 16:56:50 +02:00
import util.JenkinsStepRule
2018-06-20 11:45:42 +02:00
import util.Rules
public class PrepareDefaultValuesTest extends BasePiperTest {
2019-01-22 10:25:42 +02:00
private JenkinsStepRule stepRule = new JenkinsStepRule(this)
private JenkinsLoggingRule loggingRule = new JenkinsLoggingRule(this)
private ExpectedException thrown = ExpectedException.none()
2018-06-20 11:45:42 +02:00
@Rule
public RuleChain ruleChain = Rules
.getCommonRules(this)
.around(new JenkinsReadYamlRule(this))
.around(thrown)
2019-01-22 10:25:42 +02:00
.around(stepRule)
.around(loggingRule)
2018-06-20 11:45:42 +02:00
@Before
public void setup() {
helper.registerAllowedMethod("libraryResource", [String], { fileName ->
switch (fileName) {
case 'default_pipeline_environment.yml': return "default: 'config'"
case 'custom.yml': return "custom: 'myConfig'"
case 'not_found': throw new hudson.AbortException('No such library resource not_found could be found')
default: return "the:'end'"
}
})
}
@Test
public void testDefaultPipelineEnvironmentOnly() {
2019-01-22 10:25:42 +02:00
stepRule.step.prepareDefaultValues(script: nullScript)
assert DefaultValueCache.getInstance().getDefaultValues().size() == 1
assert DefaultValueCache.getInstance().getDefaultValues().default == 'config'
}
@Test
public void testReInitializeOnCustomConfig() {
def instance = DefaultValueCache.createInstance([key:'value'])
// existing instance is dropped in case a custom config is provided.
2019-01-22 10:25:42 +02:00
stepRule.step.prepareDefaultValues(script: nullScript, customDefaults: 'custom.yml')
// this check is for checking we have another instance
assert ! instance.is(DefaultValueCache.getInstance())
// some additional checks that the configuration represented by the new
// config is fine
assert DefaultValueCache.getInstance().getDefaultValues().size() == 2
assert DefaultValueCache.getInstance().getDefaultValues().default == 'config'
assert DefaultValueCache.getInstance().getDefaultValues().custom == 'myConfig'
}
@Test
public void testNoReInitializeWithoutCustomConfig() {
def instance = DefaultValueCache.createInstance([key:'value'])
2019-01-22 10:25:42 +02:00
stepRule.step.prepareDefaultValues(script: nullScript)
assert instance.is(DefaultValueCache.getInstance())
assert DefaultValueCache.getInstance().getDefaultValues().size() == 1
assert DefaultValueCache.getInstance().getDefaultValues().key == 'value'
}
@Test
public void testAttemptToLoadNonExistingConfigFile() {
// Behavior documented here based on reality check
thrown.expect(hudson.AbortException.class)
thrown.expectMessage('No such library resource not_found could be found')
2019-01-22 10:25:42 +02:00
stepRule.step.prepareDefaultValues(script: nullScript, customDefaults: 'not_found')
}
@Test
public void testDefaultPipelineEnvironmentWithCustomConfigReferencedAsString() {
2019-01-22 10:25:42 +02:00
stepRule.step.prepareDefaultValues(script: nullScript, customDefaults: 'custom.yml')
assert DefaultValueCache.getInstance().getDefaultValues().size() == 2
assert DefaultValueCache.getInstance().getDefaultValues().default == 'config'
assert DefaultValueCache.getInstance().getDefaultValues().custom == 'myConfig'
}
@Test
public void testDefaultPipelineEnvironmentWithCustomConfigReferencedAsList() {
2019-01-22 10:25:42 +02:00
stepRule.step.prepareDefaultValues(script: nullScript, customDefaults: ['custom.yml'])
assert DefaultValueCache.getInstance().getDefaultValues().size() == 2
assert DefaultValueCache.getInstance().getDefaultValues().default == 'config'
assert DefaultValueCache.getInstance().getDefaultValues().custom == 'myConfig'
}
2018-07-04 11:56:53 +02:00
@Test
public void testAssertNoLogMessageInCaseOfNoAdditionalConfigFiles() {
2019-01-22 10:25:42 +02:00
stepRule.step.prepareDefaultValues(script: nullScript)
2018-07-04 11:56:53 +02:00
assert ! loggingRule.log.contains("Loading configuration file 'default_pipeline_environment.yml'")
2018-07-04 11:56:53 +02:00
}
@Test
public void testAssertLogMessageInCaseOfMoreThanOneConfigFile() {
2019-01-22 10:25:42 +02:00
stepRule.step.prepareDefaultValues(script: nullScript, customDefaults: ['custom.yml'])
2018-07-04 11:56:53 +02:00
assert loggingRule.log.contains("Loading configuration file 'default_pipeline_environment.yml'")
assert loggingRule.log.contains("Loading configuration file 'custom.yml'")
2018-07-04 11:56:53 +02:00
}
2018-06-20 11:45:42 +02:00
}