github.com/ouraigua/jenkins-library@v0.0.0-20231028010029-fbeaf2f3aa9b/test/groovy/PiperLoadGlobalExtensionsTest.groovy (about) 1 import org.junit.Before 2 import org.junit.Rule 3 import org.junit.Test 4 import org.junit.rules.RuleChain 5 import util.BasePiperTest 6 import util.JenkinsFileExistsRule 7 import util.JenkinsReadFileRule 8 import util.JenkinsReadYamlRule 9 import util.JenkinsStepRule 10 import util.JenkinsWriteFileRule 11 import util.Rules 12 13 import static org.junit.Assert.assertEquals 14 import static org.junit.Assert.assertFalse 15 import static org.junit.Assert.assertNull 16 import static org.junit.Assert.assertTrue 17 18 class PiperLoadGlobalExtensionsTest extends BasePiperTest { 19 20 private Map checkoutParameters 21 private boolean checkoutCalled = false 22 private List filesRead = [] 23 private List fileWritten = [] 24 25 private JenkinsStepRule stepRule = new JenkinsStepRule(this) 26 private JenkinsReadYamlRule readYamlRule = new JenkinsReadYamlRule(this) 27 private JenkinsFileExistsRule fileExistsRule = new JenkinsFileExistsRule(this, []) 28 29 @Rule 30 public RuleChain ruleChain = Rules 31 .getCommonRules(this) 32 .around(stepRule) 33 .around(readYamlRule) 34 .around(fileExistsRule) 35 36 @Before 37 void init() { 38 helper.registerAllowedMethod("checkout", [Map.class], { map -> 39 checkoutParameters = map 40 checkoutCalled = true 41 }) 42 helper.registerAllowedMethod("readFile", [Map.class], { map -> 43 filesRead.add(map.file) 44 return "" 45 }) 46 helper.registerAllowedMethod("writeFile", [Map.class], { map -> 47 fileWritten.add(map.file) 48 }) 49 } 50 51 @Test 52 void testNotConfigured() throws Exception { 53 stepRule.step.piperLoadGlobalExtensions(script: nullScript) 54 assertFalse(checkoutCalled) 55 } 56 57 @Test 58 void testUrlConfigured() throws Exception { 59 60 nullScript.commonPipelineEnvironment.configuration = [ 61 general: [ 62 globalExtensionsRepository: 'https://my.git.example/foo/bar.git' 63 ] 64 ] 65 66 stepRule.step.piperLoadGlobalExtensions(script: nullScript) 67 assertTrue(checkoutCalled) 68 assertEquals('GitSCM', checkoutParameters.$class) 69 assertEquals(1, checkoutParameters.userRemoteConfigs.size()) 70 assertEquals([url: 'https://my.git.example/foo/bar.git'], checkoutParameters.userRemoteConfigs[0]) 71 } 72 73 @Test 74 void testVersionConfigured() throws Exception { 75 76 nullScript.commonPipelineEnvironment.configuration = [ 77 general: [ 78 globalExtensionsRepository: 'https://my.git.example/foo/bar.git', 79 globalExtensionsVersion: 'v35' 80 ] 81 ] 82 83 stepRule.step.piperLoadGlobalExtensions(script: nullScript) 84 assertTrue(checkoutCalled) 85 assertEquals(1, checkoutParameters.branches.size()) 86 assertEquals([name: 'v35'], checkoutParameters.branches[0]) 87 } 88 89 @Test 90 void testCredentialsConfigured() throws Exception { 91 92 nullScript.commonPipelineEnvironment.configuration = [ 93 general: [ 94 globalExtensionsRepository: 'https://my.git.example/foo/bar.git', 95 globalExtensionsRepositoryCredentialsId: 'my-credentials' 96 ] 97 ] 98 99 stepRule.step.piperLoadGlobalExtensions(script: nullScript) 100 assertTrue(checkoutCalled) 101 assertEquals(1, checkoutParameters.userRemoteConfigs.size()) 102 assertEquals([url: 'https://my.git.example/foo/bar.git', credentialsId: 'my-credentials'], checkoutParameters.userRemoteConfigs[0]) 103 } 104 105 @Test 106 void testExtensionConfigurationExists() throws Exception { 107 fileExistsRule.registerExistingFile('test/extension_configuration.yml') 108 109 nullScript.commonPipelineEnvironment.configuration = [ 110 general: [ 111 globalExtensionsDirectory: 'test', 112 globalExtensionsRepository: 'https://my.git.example/foo/bar.git' 113 ] 114 ] 115 116 Map prepareParameter = [:] 117 helper.registerAllowedMethod("prepareDefaultValues", [Map.class], { map -> 118 prepareParameter = map 119 }) 120 121 stepRule.step.piperLoadGlobalExtensions(script: nullScript, customDefaults: ['default.yml'], customDefaultsFromFiles: ['file1.yml']) 122 assertTrue(checkoutCalled) 123 124 //File copied 125 assertTrue(filesRead.contains('test/extension_configuration.yml')) 126 assertTrue(fileWritten.contains('.pipeline/extension_configuration.yml')) 127 128 assertEquals(2, prepareParameter.customDefaultsFromFiles.size()) 129 assertEquals('extension_configuration.yml', prepareParameter.customDefaultsFromFiles[0]) 130 assertEquals('file1.yml', prepareParameter.customDefaultsFromFiles[1]) 131 assertEquals(1, prepareParameter.customDefaults.size()) 132 assertEquals('default.yml', prepareParameter.customDefaults[0]) 133 } 134 135 @Test 136 void testLoadLibraries() throws Exception { 137 fileExistsRule.registerExistingFile('test/sharedLibraries.yml') 138 139 nullScript.commonPipelineEnvironment.configuration = [ 140 general: [ 141 globalExtensionsDirectory: 'test', 142 globalExtensionsRepository: 'https://my.git.example/foo/bar.git' 143 ] 144 ] 145 146 readYamlRule.registerYaml("test/sharedLibraries.yml", "[{name: my-extension-dependency, version: my-git-tag}]") 147 148 List libsLoaded = [] 149 helper.registerAllowedMethod("library", [String.class], { lib -> 150 libsLoaded.add(lib) 151 }) 152 153 stepRule.step.piperLoadGlobalExtensions(script: nullScript) 154 assertTrue(checkoutCalled) 155 assertEquals(1, libsLoaded.size()) 156 assertEquals("my-extension-dependency@my-git-tag", libsLoaded[0].toString()) 157 } 158 }