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

Add DownloadCacheUtils for handling download cache docker container (#1298)

This commit is contained in:
Florian Geckeler 2020-03-20 17:01:20 +01:00 committed by GitHub
parent f81fe916dc
commit b39080409c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 149 additions and 0 deletions

View File

@ -0,0 +1,10 @@
<settings xmlns="http://maven.apache.org/SETTINGS/1.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/SETTINGS/1.0.0 http://maven.apache.org/xsd/settings-1.0.0.xsd">
<mirrors>
<mirror>
<id>download-cache</id>
<name>download-cache</name>
<mirrorOf>central</mirrorOf>
<url>http://__HOSTNAME__:8081/repository/mvn-proxy/</url>
</mirror>
</mirrors>
</settings>

View File

@ -0,0 +1,49 @@
package com.sap.piper
class DownloadCacheUtils {
static boolean isEnabled(Script script) {
script.node('master') {
String network = script.env.DL_CACHE_NETWORK
String host = script.env.DL_CACHE_HOSTNAME
return (network.asBoolean() && host.asBoolean())
}
}
static String getDockerOptions(Script script) {
script.node('master') {
String dockerNetwork = script.env.DL_CACHE_NETWORK
if (!dockerNetwork) {
return ''
}
return " --network=$dockerNetwork"
}
}
static String getGlobalMavenSettingsForDownloadCache(Script script) {
String globalSettingsFilePath = '.pipeline/global_settings.xml'
if (script.fileExists(globalSettingsFilePath)) {
return globalSettingsFilePath
}
String hostname = ''
script.node('master') {
hostname = script.env.DL_CACHE_HOSTNAME // set by cx-server
}
if (!hostname) {
return ''
}
String mavenSettingsTemplate = script.libraryResource("mvn_download_cache_proxy_settings.xml")
String mavenSettings = mavenSettingsTemplate.replace('__HOSTNAME__', hostname)
if (!script.fileExists('.pipeline')) {
script.sh "mkdir .pipeline"
}
script.writeFile file: globalSettingsFilePath, text: mavenSettings
return globalSettingsFilePath
}
}

View File

@ -0,0 +1,90 @@
import com.sap.piper.DownloadCacheUtils
import org.junit.Before
import org.junit.Rule
import org.junit.Test
import org.junit.rules.RuleChain
import util.BasePiperTest
import util.JenkinsFileExistsRule
import util.Rules
import static org.junit.Assert.*
class DownloadCacheUtilsTest extends BasePiperTest {
private JenkinsFileExistsRule fileExistsRule = new JenkinsFileExistsRule(this, [])
@Rule
public RuleChain ruleChain = Rules
.getCommonRules(this)
.around(fileExistsRule)
@Before
void init() {
helper.registerAllowedMethod("libraryResource", [String.class], { path ->
File resource = new File(new File('resources'), path)
if (resource.exists()) {
return resource.getText()
}
return ''
})
helper.registerAllowedMethod('node', [String.class, Closure.class]) { s, body ->
body()
}
}
@Test
void 'isEnabled should return true if dl cache is enabled'() {
nullScript.env.DL_CACHE_HOSTNAME = 'cx-downloadcache'
nullScript.env.DL_CACHE_NETWORK = 'cx-network'
boolean actual = DownloadCacheUtils.isEnabled(nullScript)
assertTrue(actual)
}
@Test
void 'getDockerOptions should return docker network if configured'() {
nullScript.env.DL_CACHE_NETWORK = 'cx-network'
String expected = ' --network=cx-network'
String actual = DownloadCacheUtils.getDockerOptions(nullScript)
assertEquals(expected, actual)
}
@Test
void 'getGlobalMavenSettingsForDownloadCache should write file'() {
nullScript.env.DL_CACHE_HOSTNAME = 'cx-downloadcache'
boolean writeFileExecuted = false
helper.registerAllowedMethod('writeFile', [Map.class]) { Map m ->
writeFileExecuted = true
}
String expected = '.pipeline/global_settings.xml'
String actual = DownloadCacheUtils.getGlobalMavenSettingsForDownloadCache(nullScript)
assertEquals(expected, actual)
assertTrue(writeFileExecuted)
}
@Test
void 'getGlobalMavenSettingsForDownloadCache should return filePath if file already exists'() {
fileExistsRule.registerExistingFile('.pipeline/global_settings.xml')
nullScript.env.DL_CACHE_HOSTNAME = 'cx-downloadcache'
boolean writeFileExecuted = false
helper.registerAllowedMethod('writeFile', [Map.class]) { Map m ->
writeFileExecuted = true
}
String expected = '.pipeline/global_settings.xml'
String actual = DownloadCacheUtils.getGlobalMavenSettingsForDownloadCache(nullScript)
assertFalse(writeFileExecuted)
assertEquals(expected, actual)
}
@Test
void 'getGlobalMavenSettingsForDownloadCache should return empty string if dl cache not active'() {
String expected = ''
String actual = DownloadCacheUtils.getGlobalMavenSettingsForDownloadCache(nullScript)
assertEquals(expected, actual)
}
}