github.com/ouraigua/jenkins-library@v0.0.0-20231028010029-fbeaf2f3aa9b/test/groovy/DownloadCacheUtilsTest.groovy (about) 1 import com.sap.piper.BuildTool 2 import com.sap.piper.DownloadCacheUtils 3 import org.junit.After 4 import org.junit.Before 5 import org.junit.Rule 6 import org.junit.Test 7 import org.junit.rules.RuleChain 8 import util.BasePiperTest 9 import util.JenkinsFileExistsRule 10 import util.JenkinsWriteFileRule 11 import util.Rules 12 13 import static org.junit.Assert.* 14 15 class DownloadCacheUtilsTest extends BasePiperTest { 16 private JenkinsFileExistsRule fileExistsRule = new JenkinsFileExistsRule(this, []) 17 18 @Rule 19 public RuleChain ruleChain = Rules 20 .getCommonRules(this) 21 .around(fileExistsRule) 22 .around(new JenkinsWriteFileRule(this)) 23 24 @Before 25 void init() { 26 DownloadCacheUtils.metaClass.static.networkName = {return } 27 DownloadCacheUtils.metaClass.static.hostname = { return } 28 helper.registerAllowedMethod("libraryResource", [String.class], { path -> 29 File resource = new File(new File('resources'), path) 30 if (resource.exists()) { 31 return resource.getText() 32 } 33 return '' 34 }) 35 helper.registerAllowedMethod('node', [String.class, Closure.class]) { s, body -> 36 body() 37 } 38 } 39 40 @After 41 void after(){ 42 DownloadCacheUtils.metaClass.static.networkName = {return } 43 DownloadCacheUtils.metaClass.static.hostname = { return } 44 } 45 46 @Test 47 void 'isEnabled should return true if dl cache is enabled'() { 48 DownloadCacheUtils.metaClass.static.networkName = { 49 return 'cx-network' 50 } 51 DownloadCacheUtils.metaClass.static.hostname = { 52 return 'cx-downloadcache' 53 } 54 boolean actual = DownloadCacheUtils.isEnabled(nullScript) 55 56 assertTrue(actual) 57 } 58 59 @Test 60 void 'getDockerOptions should return docker network if configured'() { 61 DownloadCacheUtils.metaClass.static.networkName = { 62 return 'cx-network' 63 } 64 String expected = '--network=cx-network' 65 String actual = DownloadCacheUtils.getDockerOptions(nullScript) 66 67 assertEquals(expected, actual) 68 } 69 70 @Test 71 void 'getGlobalMavenSettingsForDownloadCache should write file'() { 72 DownloadCacheUtils.metaClass.static.hostname = { 73 return 'cx-downloadcache' 74 } 75 boolean writeFileExecuted = false 76 77 helper.registerAllowedMethod('writeFile', [Map.class]) { Map m -> 78 writeFileExecuted = true 79 } 80 String expected = '.pipeline/global_settings.xml' 81 String actual = DownloadCacheUtils.getGlobalMavenSettingsForDownloadCache(nullScript) 82 83 assertEquals(expected, actual) 84 assertTrue(writeFileExecuted) 85 } 86 87 @Test 88 void 'getGlobalMavenSettingsForDownloadCache should return filePath if file already exists'() { 89 fileExistsRule.registerExistingFile('.pipeline/global_settings.xml') 90 DownloadCacheUtils.metaClass.static.hostname = { 91 return 'cx-downloadcache' 92 } 93 boolean writeFileExecuted = false 94 95 helper.registerAllowedMethod('writeFile', [Map.class]) { Map m -> 96 writeFileExecuted = true 97 } 98 99 String expected = '.pipeline/global_settings.xml' 100 String actual = DownloadCacheUtils.getGlobalMavenSettingsForDownloadCache(nullScript) 101 assertFalse(writeFileExecuted) 102 assertEquals(expected, actual) 103 } 104 105 @Test 106 void 'getGlobalMavenSettingsForDownloadCache should return empty string if dl cache not active'() { 107 String expected = '' 108 String actual = DownloadCacheUtils.getGlobalMavenSettingsForDownloadCache(nullScript) 109 110 assertEquals(expected, actual) 111 } 112 113 @Test 114 void 'injectDownloadCacheInParameters should not change the parameters if dl cache not active'() { 115 Map newParameters = DownloadCacheUtils.injectDownloadCacheInParameters(nullScript, [:], BuildTool.MAVEN) 116 assertTrue(newParameters.isEmpty()) 117 } 118 119 @Test 120 void 'injectDownloadCacheInParameters should set docker options and global settings for maven'() { 121 DownloadCacheUtils.metaClass.static.hostname = { 122 return 'cx-downloadcache' 123 } 124 DownloadCacheUtils.metaClass.static.networkName = { 125 return 'cx-network' 126 } 127 128 Map expected = [ 129 dockerOptions: ['--network=cx-network'], 130 globalSettingsFile: '.pipeline/global_settings.xml' 131 ] 132 133 Map actual = DownloadCacheUtils.injectDownloadCacheInParameters(nullScript, [:], BuildTool.MAVEN) 134 135 assertEquals(expected, actual) 136 } 137 138 @Test 139 void 'injectDownloadCacheInParameters should set docker options, global settings and npm default registry for mta'() { 140 DownloadCacheUtils.metaClass.static.hostname = { 141 return 'cx-downloadcache' 142 } 143 DownloadCacheUtils.metaClass.static.networkName = { 144 return 'cx-network' 145 } 146 147 Map expected = [ 148 dockerOptions: ['--network=cx-network'], 149 globalSettingsFile: '.pipeline/global_settings.xml', 150 defaultNpmRegistry: 'http://cx-downloadcache:8081/repository/npm-proxy/' 151 ] 152 153 Map actual = DownloadCacheUtils.injectDownloadCacheInParameters(nullScript, [:], BuildTool.MTA) 154 155 assertEquals(expected, actual) 156 } 157 158 @Test 159 void 'injectDownloadCacheInParameters should set docker options and default npm config for npm'() { 160 DownloadCacheUtils.metaClass.static.hostname = { 161 return 'cx-downloadcache' 162 } 163 DownloadCacheUtils.metaClass.static.networkName = { 164 return 'cx-network' 165 } 166 167 Map expected = [ 168 dockerOptions: ['--network=cx-network'], 169 defaultNpmRegistry: 'http://cx-downloadcache:8081/repository/npm-proxy/' 170 ] 171 172 Map actual = DownloadCacheUtils.injectDownloadCacheInParameters(nullScript, [:], BuildTool.NPM) 173 174 assertEquals(expected, actual) 175 } 176 177 @Test 178 void 'injectDownloadCacheInParameters should append docker options'() { 179 DownloadCacheUtils.metaClass.static.hostname = { 180 return 'cx-downloadcache' 181 } 182 DownloadCacheUtils.metaClass.static.networkName = { 183 return 'cx-network' 184 } 185 186 List expectedDockerOptions = ['--test', '--network=cx-network'] 187 188 189 190 Map actual = DownloadCacheUtils.injectDownloadCacheInParameters(nullScript, [dockerOptions: '--test'], BuildTool.MAVEN) 191 192 assertEquals(expectedDockerOptions, actual.dockerOptions) 193 } 194 }