diff --git a/test/groovy/PrepareDefaultValuesTest.groovy b/test/groovy/PrepareDefaultValuesTest.groovy new file mode 100644 index 000000000..0ae67d65e --- /dev/null +++ b/test/groovy/PrepareDefaultValuesTest.groovy @@ -0,0 +1,122 @@ +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 + +import util.BasePiperTest +import util.JenkinsLoggingRule +import util.JenkinsShellCallRule +import util.JenkinsStepRule; +import util.Rules + +public class PrepareDefaultValuesTest extends BasePiperTest { + + private JenkinsStepRule jsr = new JenkinsStepRule(this) + private JenkinsLoggingRule jlr = new JenkinsLoggingRule(this) + private ExpectedException thrown = ExpectedException.none() + + @Rule + public RuleChain ruleChain = Rules + .getCommonRules(this) + .around(thrown) + .around(jsr) + .around(jlr) + + @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 [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' + } + + @Test + public void testAssertNoLogMessageInCaseOfNoAdditionalConfigFiles() { + + jsr.step.call(script: nullScript) + + assert ! jlr.log.contains("Loading configuration file 'default_pipeline_environment.yml'") + } + + @Test + public void testAssertLogMessageInCaseOfMoreThanOneConfigFile() { + + jsr.step.call(script: nullScript, customDefaults: ['custom.yml']) + + assert jlr.log.contains("Loading configuration file 'default_pipeline_environment.yml'") + assert jlr.log.contains("Loading configuration file 'custom.yml'") + } +} diff --git a/vars/prepareDefaultValues.groovy b/vars/prepareDefaultValues.groovy index 117ebd4f4..35bd1478a 100644 --- a/vars/prepareDefaultValues.groovy +++ b/vars/prepareDefaultValues.groovy @@ -1,9 +1,24 @@ import com.sap.piper.DefaultValueCache +import com.sap.piper.MapUtils def call(Map parameters = [:]) { handlePipelineStepErrors (stepName: 'prepareDefaultValues', stepParameters: parameters) { - if(!DefaultValueCache.getInstance()) { - Map defaultValues = readYaml text: libraryResource('default_pipeline_environment.yml') + if(!DefaultValueCache.getInstance() || parameters.customDefaults) { + def defaultValues = [:] + def configFileList = ['default_pipeline_environment.yml'] + def customDefaults = parameters.customDefaults + + if(customDefaults in String) + customDefaults = [customDefaults] + if(customDefaults in List) + configFileList += customDefaults + for (def configFileName : configFileList){ + if(configFileList.size() > 1) echo "Loading configuration file '${configFileName}'" + def configuration = readYaml text: libraryResource(configFileName) + defaultValues = MapUtils.merge( + MapUtils.pruneNulls(defaultValues), + MapUtils.pruneNulls(configuration)) + } DefaultValueCache.createInstance(defaultValues) } } diff --git a/vars/setupCommonPipelineEnvironment.groovy b/vars/setupCommonPipelineEnvironment.groovy index 355ba4ca1..a74486a2e 100644 --- a/vars/setupCommonPipelineEnvironment.groovy +++ b/vars/setupCommonPipelineEnvironment.groovy @@ -4,7 +4,7 @@ def call(Map parameters = [:]) { def script = parameters.script - prepareDefaultValues script: script + prepareDefaultValues script: script, customDefaults: parameters.customDefaults String configFile = parameters.get('configFile')