1
0
mirror of https://github.com/SAP/jenkins-library.git synced 2025-01-18 05:18:24 +02:00

Merge custom defaults with library defaults.

This commit is contained in:
Marcus Holl 2018-06-20 12:00:28 +02:00
parent c29fb02dd9
commit 22b6dd63f6
2 changed files with 83 additions and 20 deletions

View File

@ -1,5 +1,7 @@
import org.junit.Before
import org.junit.Rule;
import org.junit.Test
import org.junit.rules.ExpectedException
import org.junit.rules.RuleChain;
import com.sap.piper.DefaultValueCache
@ -11,25 +13,89 @@ import util.Rules
public class PrepareDefaultValuesTest extends BasePiperTest {
private JenkinsStepRule jsr = new JenkinsStepRule(this)
private ExpectedException thrown = ExpectedException.none()
@Rule
public RuleChain ruleChain = Rules
.getCommonRules(this)
.around(thrown)
.around(jsr)
@Test
public void testMerge() {
@Before
public void setup() {
helper.registerAllowedMethod("libraryResource", [String], { fileName-> return fileName })
helper.registerAllowedMethod("readYaml", [Map], { m ->
switch(m.text) {
case 'default_pipeline_environment.yml': return [a: 'x']
default: return [the:'end']
}
})
jsr.step.call(script: nullScript)
println DefaultValueCache.getInstance().getDefaultValues()
}
helper.registerAllowedMethod("libraryResource", [String], { fileName-> return fileName })
helper.registerAllowedMethod("readYaml", [Map], { m ->
switch(m.text) {
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() {
jsr.step.call(script: nullScript)
assert DefaultValueCache.getInstance().getDefaultValues().size() == 1
assert DefaultValueCache.getInstance().getDefaultValues().default == 'config'
}
@Test
public void testReInitializeOnCustomConfig() {
DefaultValueCache.createInstance([key:'value'])
// existing instance is dropped in case a custom config is provided.
jsr.step.call(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 testNoReInitializeWithoutCustomConfig() {
DefaultValueCache.createInstance([key:'value'])
jsr.step.call(script: nullScript)
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')
jsr.step.call(script: nullScript, customDefaults: 'not_found')
}
@Test
public void testDefaultPipelineEnvironmentWithCustomConfigReferencedAsString() {
jsr.step.call(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() {
jsr.step.call(script: nullScript, customDefaults: ['custom.yml'])
assert DefaultValueCache.getInstance().getDefaultValues().size() == 2
assert DefaultValueCache.getInstance().getDefaultValues().default == 'config'
assert DefaultValueCache.getInstance().getDefaultValues().custom == 'myConfig'
}
}

View File

@ -7,16 +7,13 @@ def call(Map parameters = [:]) {
def defaultValues = [:]
def configurationFiles = ['default_pipeline_environment.yml']
def customDefaults = parameters.customDefaults
if(customDefaults in String) // >> filename resolves to Map
if(customDefaults in String)
customDefaults = [].plus(customDefaults)
if(customDefaults in List)
configurationFiles += customDefaults
/*
if(defaults instanceof Map) // >> config map
defaults = [].plus(defaults)
*/
for (def configFileName : configurationFiles){
if(configurationFiles.size() > 1) echo "Loading configuration file '${}'"
def configuration = readYaml text: libraryResource(configFileName)
defaultValues = MapUtils.merge(
MapUtils.pruneNulls(defaultValues),