github.com/ouraigua/jenkins-library@v0.0.0-20231028010029-fbeaf2f3aa9b/test/groovy/PrepareDefaultValuesTest.groovy (about) 1 import org.junit.Before 2 import org.junit.Rule 3 import org.junit.Test 4 import org.junit.rules.ExpectedException 5 import org.junit.rules.RuleChain 6 import com.sap.piper.DefaultValueCache 7 8 import util.BasePiperTest 9 import util.JenkinsLoggingRule 10 import util.JenkinsReadYamlRule 11 import util.JenkinsStepRule 12 import util.Rules 13 14 public class PrepareDefaultValuesTest extends BasePiperTest { 15 16 private JenkinsStepRule stepRule = new JenkinsStepRule(this) 17 private JenkinsLoggingRule loggingRule = new JenkinsLoggingRule(this) 18 private ExpectedException thrown = ExpectedException.none() 19 20 @Rule 21 public RuleChain ruleChain = Rules 22 .getCommonRules(this) 23 .around(new JenkinsReadYamlRule(this)) 24 .around(thrown) 25 .around(stepRule) 26 .around(loggingRule) 27 28 @Before 29 public void setup() { 30 31 helper.registerAllowedMethod("libraryResource", [String], { fileName -> 32 switch (fileName) { 33 case 'default_pipeline_environment.yml': return "default: 'config'" 34 case 'custom.yml': return "custom: 'myConfig'" 35 case 'not_found': throw new hudson.AbortException('No such library resource not_found could be found') 36 default: return "the:'end'" 37 } 38 }) 39 } 40 41 @Test 42 public void testDefaultPipelineEnvironmentOnly() { 43 44 stepRule.step.prepareDefaultValues(script: nullScript) 45 46 assert DefaultValueCache.getInstance().getDefaultValues().size() == 1 47 assert DefaultValueCache.getInstance().getDefaultValues().default == 'config' 48 } 49 50 @Test 51 public void testReInitializeOnCustomConfig() { 52 53 def instance = DefaultValueCache.createInstance([key:'value']) 54 55 // existing instance is dropped in case a custom config is provided. 56 stepRule.step.prepareDefaultValues(script: nullScript, customDefaults: 'custom.yml') 57 58 // this check is for checking we have another instance 59 assert ! instance.is(DefaultValueCache.getInstance()) 60 61 // some additional checks that the configuration represented by the new 62 // config is fine 63 assert DefaultValueCache.getInstance().getDefaultValues().size() == 2 64 assert DefaultValueCache.getInstance().getDefaultValues().default == 'config' 65 assert DefaultValueCache.getInstance().getDefaultValues().custom == 'myConfig' 66 } 67 68 @Test 69 public void testNoReInitializeWithoutCustomConfig() { 70 71 def instance = DefaultValueCache.createInstance([key:'value']) 72 73 stepRule.step.prepareDefaultValues(script: nullScript) 74 75 assert instance.is(DefaultValueCache.getInstance()) 76 assert DefaultValueCache.getInstance().getDefaultValues().size() == 1 77 assert DefaultValueCache.getInstance().getDefaultValues().key == 'value' 78 } 79 80 @Test 81 public void testAttemptToLoadNonExistingConfigFile() { 82 83 // Behavior documented here based on reality check 84 thrown.expect(hudson.AbortException.class) 85 thrown.expectMessage('No such library resource not_found could be found') 86 87 stepRule.step.prepareDefaultValues(script: nullScript, customDefaults: 'not_found') 88 } 89 90 @Test 91 public void testDefaultPipelineEnvironmentWithCustomConfigReferencedAsString() { 92 93 stepRule.step.prepareDefaultValues(script: nullScript, customDefaults: 'custom.yml') 94 95 assert DefaultValueCache.getInstance().getDefaultValues().size() == 2 96 assert DefaultValueCache.getInstance().getDefaultValues().default == 'config' 97 assert DefaultValueCache.getInstance().getDefaultValues().custom == 'myConfig' 98 } 99 100 @Test 101 public void testDefaultPipelineEnvironmentWithCustomConfigReferencedAsList() { 102 103 stepRule.step.prepareDefaultValues(script: nullScript, customDefaults: ['custom.yml']) 104 105 assert DefaultValueCache.getInstance().getDefaultValues().size() == 2 106 assert DefaultValueCache.getInstance().getDefaultValues().default == 'config' 107 assert DefaultValueCache.getInstance().getDefaultValues().custom == 'myConfig' 108 } 109 110 @Test 111 public void testAssertNoLogMessageInCaseOfNoAdditionalConfigFiles() { 112 113 stepRule.step.prepareDefaultValues(script: nullScript) 114 115 assert ! loggingRule.log.contains("Loading configuration file 'default_pipeline_environment.yml'") 116 } 117 118 @Test 119 public void testAssertLogMessageInCaseOfMoreThanOneConfigFile() { 120 121 stepRule.step.prepareDefaultValues(script: nullScript, customDefaults: ['custom.yml']) 122 123 assert loggingRule.log.contains("Loading configuration file 'default_pipeline_environment.yml'") 124 assert loggingRule.log.contains("Loading configuration file 'custom.yml'") 125 } 126 }