1
0
mirror of https://github.com/SAP/jenkins-library.git synced 2024-12-12 10:55:20 +02:00

Merge remote-tracking branch 'github/master' into HEAD

This commit is contained in:
Marcus Holl 2018-10-09 16:57:26 +02:00
commit 203be37f75
18 changed files with 115 additions and 153 deletions

View File

@ -1,7 +1,6 @@
package com.sap.piper
import com.cloudbees.groovy.cps.NonCPS
import com.sap.piper.MapUtils
class ConfigurationMerger {
@NonCPS

View File

@ -37,3 +37,18 @@ String[] extractLogLines(String filter = '',
?.findAll { line -> line ==~ /${filter}/ }
}
static String handleTestRepository(Script steps, Map config){
def stashName = "testContent-${UUID.randomUUID()}".toString()
def options = [url: config.testRepository]
if (config.gitSshKeyCredentialsId)
options.put('credentialsId', config.gitSshKeyCredentialsId)
if (config.gitBranch)
options.put('branch', config.gitBranch)
// checkout test repository
steps.git options
// stash test content
steps.stash stashName
// return stash name
return stashName
}

View File

@ -1,7 +1,5 @@
package com.sap.piper.cm
import java.util.Map
import com.sap.piper.GitUtils
import hudson.AbortException

View File

@ -23,6 +23,7 @@ import static org.hamcrest.Matchers.hasItem
import static org.hamcrest.Matchers.hasItems
import static org.hamcrest.Matchers.not
import static org.hamcrest.Matchers.notNullValue
import static org.hamcrest.Matchers.stringContainsInOrder
import static org.hamcrest.Matchers.containsString
import static org.junit.Assert.assertThat
@ -90,10 +91,13 @@ class ArtifactSetVersionTest extends BasePiperTest {
assertEquals('testCommitId', jer.env.getGitCommitId())
assertThat(jscr.shell, hasItem("mvn --file 'pom.xml' --batch-mode -Dorg.slf4j.simpleLogger.log.org.apache.maven.cli.transfer.Slf4jMavenTransferListener=warn versions:set -DnewVersion=1.2.3-20180101010203_testCommitId -DgenerateBackupPoms=false"))
assertThat(jscr.shell, hasItems(containsString("git add ."),
containsString("git commit -m 'update version 1.2.3-20180101010203_testCommitId'"),
containsString('git tag build_1.2.3-20180101010203_testCommitId'),
containsString('git push myGitSshUrl build_1.2.3-20180101010203_testCommitId')))
assertThat(jscr.shell.join(), stringContainsInOrder([
"git add .",
"git commit -m 'update version 1.2.3-20180101010203_testCommitId'",
'git tag build_1.2.3-20180101010203_testCommitId',
'git push myGitSshUrl build_1.2.3-20180101010203_testCommitId',
]
))
}
@Test

View File

@ -7,6 +7,7 @@ import util.*
import static org.hamcrest.Matchers.hasItem
import static org.hamcrest.Matchers.is
import static org.hamcrest.Matchers.startsWith
import static org.junit.Assert.assertThat
class BatsExecuteTestsTest extends BasePiperTest {
@ -104,7 +105,7 @@ class BatsExecuteTestsTest extends BasePiperTest {
gitRepository = m
})
helper.registerAllowedMethod('stash', [String.class], {s ->
assertThat(s, is('batsTests'))
assertThat(s, startsWith('testContent-'))
})
jsr.step.batsExecuteTests(
@ -115,7 +116,7 @@ class BatsExecuteTestsTest extends BasePiperTest {
assertThat(gitRepository.size(), is(1))
assertThat(gitRepository.url, is('testRepo'))
assertThat(jder.dockerParams.stashContent, hasItem('batsTests'))
assertThat(jder.dockerParams.stashContent, hasItem(startsWith('testContent-')))
}
@Test
@ -125,7 +126,7 @@ class BatsExecuteTestsTest extends BasePiperTest {
gitRepository = m
})
helper.registerAllowedMethod('stash', [String.class], {s ->
assertThat(s, is('batsTests'))
assertThat(s, startsWith('testContent-'))
})
jsr.step.batsExecuteTests(

View File

@ -71,7 +71,7 @@ class DockerExecuteOnKubernetesTest extends BasePiperTest {
podLabel = options.label
options.containers.each { option ->
containersList.add(option.name)
imageList.add(option.image)
imageList.add(option.image.toString())
envList.add(option.envVars)
portList.add(option.ports)
if (option.command) {
@ -91,19 +91,19 @@ class DockerExecuteOnKubernetesTest extends BasePiperTest {
dockerImage: 'maven:3.5-jdk-8-alpine',
dockerOptions: '-it',
dockerVolumeBind: ['my_vol': '/my_vol'],
dockerEnvVars: ['http_proxy': 'http://proxy:8000'], dockerWorkspace: '/home/piper'){
bodyExecuted = true
dockerEnvVars: ['http_proxy': 'http://proxy:8000'], dockerWorkspace: '/home/piper'
){
bodyExecuted = true
}
assertTrue(containersList.contains('container-exec'))
assertTrue(imageList.contains('maven:3.5-jdk-8-alpine'))
assertTrue(envList.toString().contains('http_proxy'))
assertTrue(envList.toString().contains('http://proxy:8000'))
assertTrue(envList.toString().contains('/home/piper'))
assertTrue(bodyExecuted)
assertThat(containersList, hasItem('container-exec'))
assertThat(imageList, hasItem('maven:3.5-jdk-8-alpine'))
assertThat(envList.toString(), containsString('http_proxy'))
assertThat(envList.toString(), containsString('http://proxy:8000'))
assertThat(envList.toString(), containsString('/home/piper'))
assertThat(bodyExecuted, is(true))
assertThat(containerCommands.size(), is(1))
}
@Test
void testDockerExecuteOnKubernetesWithCustomContainerMap() throws Exception {
jsr.step.dockerExecuteOnKubernetes(script: nullScript,

View File

@ -31,6 +31,7 @@ class SeleniumExecuteTestsTest extends BasePiperTest {
@Before
void init() throws Exception {
bodyExecuted = false
helper.registerAllowedMethod('stash', [String.class], null)
helper.registerAllowedMethod('git', [Map.class], {m ->
gitMap = m
})

View File

@ -86,5 +86,4 @@ class SetupCommonPipelineEnvironmentTest extends BasePiperTest {
}
}
}
}

View File

@ -1,20 +1,26 @@
package com.sap.piper
import static org.hamcrest.Matchers.equalTo
import static org.hamcrest.Matchers.hasEntry
import static org.hamcrest.Matchers.hasItem
import static org.hamcrest.Matchers.is
import static org.hamcrest.Matchers.notNullValue
import static org.hamcrest.Matchers.startsWith
import org.junit.Before
import org.junit.Rule
import org.junit.Test
import org.junit.rules.ExpectedException
import org.junit.rules.RuleChain
import util.BasePiperTest
import util.JenkinsLoggingRule
import util.JenkinsShellCallRule
import util.Rules
import static org.junit.Assert.assertEquals
import static org.hamcrest.Matchers.equalTo
import static org.junit.Assert.assertTrue
import static org.junit.Assert.assertFalse
import static org.hamcrest.Matchers.is
import static org.hamcrest.Matchers.notNullValue
import static org.junit.Assert.assertNotNull
import static org.junit.Assert.assertNull
import static org.junit.Assert.assertThat
@ -26,11 +32,15 @@ class GitUtilsTest extends BasePiperTest {
@Autowired
GitUtils gitUtils
JenkinsShellCallRule jscr = new JenkinsShellCallRule(this)
ExpectedException thrown = ExpectedException.none()
private JenkinsLoggingRule jlr = new JenkinsLoggingRule(this)
private JenkinsShellCallRule jscr = new JenkinsShellCallRule(this)
private ExpectedException thrown = ExpectedException.none()
@Rule
public RuleChain ruleChain = Rules.getCommonRules(this).around(jscr).around(thrown)
public RuleChain ruleChain = Rules.getCommonRules(this)
.around(jlr)
.around(jscr)
.around(thrown)
@Before
void init() throws Exception {
@ -96,4 +106,24 @@ class GitUtilsTest extends BasePiperTest {
assertNotNull(log)
assertThat(log.size(),is(equalTo(0)))
}
@Test
void testHandleTestRepository() {
def result, gitMap, stashName, config = [
testRepository: 'repoUrl',
gitSshKeyCredentialsId: 'abc',
gitBranch: 'master'
]
helper.registerAllowedMethod('git', [Map.class], {m -> gitMap = m })
helper.registerAllowedMethod("stash", [String.class], { s -> stashName = s})
result = GitUtils.handleTestRepository(nullScript, config)
// asserts
assertThat(gitMap, hasEntry('url', config.testRepository))
assertThat(gitMap, hasEntry('credentialsId', config.gitSshKeyCredentialsId))
assertThat(gitMap, hasEntry('branch', config.gitBranch))
assertThat(stashName, startsWith('testContent-'))
assertThat(result, startsWith('testContent-'))
}
}

View File

@ -1,11 +1,12 @@
import com.sap.piper.Utils
import com.sap.piper.ConfigurationHelper
import com.sap.piper.GitUtils
import com.sap.piper.Utils
import groovy.text.SimpleTemplateEngine
import groovy.transform.Field
@Field String STEP_NAME = 'batsExecuteTests'
@Field Set STEP_CONFIG_KEYS = [
'dockerImage', //
'dockerImage',
'dockerWorkspace',
'envVars',
'failOnError',
@ -39,17 +40,9 @@ def call(Map parameters = [:]) {
script.commonPipelineEnvironment.setInfluxStepData('bats', false)
if (config.testRepository) {
def gitParameters = [url: config.testRepository]
if (config.gitSshKeyCredentialsId?.length()>0) gitParameters.credentialsId = config.gitSshKeyCredentialsId
if (config.gitBranch?.length()>0) gitParameters.branch = config.gitBranch
git gitParameters
stash 'batsTests'
config.stashContent = ['batsTests']
} else {
config.stashContent = utils.unstashAll(config.stashContent)
}
config.stashContent = config.testRepository
?[GitUtils.handleTestRepository(this, config)]
:utils.unstashAll(config.stashContent)
//resolve commonPipelineEnvironment references in envVars
config.envVarList = []

View File

@ -1,12 +1,12 @@
class commonPipelineEnvironment implements Serializable {
private Map configProperties = [:]
Map configProperties = [:]
//stores version of the artifact which is build during pipeline run
def artifactVersion
//stores the gitCommitId as well as additional git information for the build during pipeline run
private String gitCommitId
private String gitSshUrl
String gitCommitId
String gitSshUrl
//stores properties for a pipeline which build an artifact and then bundles it into a container
private Map appContainerProperties = [:]
@ -19,7 +19,7 @@ class commonPipelineEnvironment implements Serializable {
//influxCustomData represents measurement jenkins_custom_data in Influx. Metrics can be written into this map
private Map influxCustomData = [:]
private String mtarFilePath
String mtarFilePath
def reset() {
appContainerProperties = [:]
@ -45,22 +45,6 @@ class commonPipelineEnvironment implements Serializable {
return appContainerProperties[property]
}
def setArtifactVersion(version) {
artifactVersion = version
}
def getArtifactVersion() {
return artifactVersion
}
def setConfigProperties(map) {
configProperties = map
}
def getConfigProperties() {
return configProperties
}
def setConfigProperty(property, value) {
configProperties[property] = value
}
@ -72,22 +56,6 @@ class commonPipelineEnvironment implements Serializable {
return configProperties[property]
}
def setGitCommitId(commitId) {
gitCommitId = commitId
}
def getGitCommitId() {
return gitCommitId
}
def setGitSshUrl(url) {
gitSshUrl = url
}
def getGitSshUrl() {
return gitSshUrl
}
def getInfluxCustomData() {
return influxCustomData
}
@ -103,15 +71,6 @@ class commonPipelineEnvironment implements Serializable {
return influxCustomDataMap.step_data[dataKey]
}
def getMtarFilePath() {
return mtarFilePath
}
void setMtarFilePath(mtarFilePath) {
this.mtarFilePath = mtarFilePath
}
def setPipelineMeasurement (measurementName, value) {
influxCustomDataMap.pipeline_data[measurementName] = value
}

View File

@ -36,34 +36,14 @@ void call(Map parameters = [:], body) {
.mixinStageConfig(script.commonPipelineEnvironment, parameters.stageName ?: env.STAGE_NAME, STEP_CONFIG_KEYS)
.mixin(parameters, PARAMETER_KEYS)
.addIfEmpty('uniqueId', UUID.randomUUID().toString())
Map config = [:]
Map config = configHelper.use()
if (parameters.containerMap) {
config = configHelper.use()
executeOnPodWithCustomContainerList(config: config) { body() }
} else {
config = configHelper
.withMandatoryProperty('dockerImage')
.use()
executeOnPodWithSingleContainer(config: config) { body() }
}
}
}
void executeOnPodWithCustomContainerList(Map parameters, body) {
def config = parameters.config
podTemplate(getOptions(config)) {
node(config.uniqueId) {
//allow execution in dedicated container
if (config.containerName) {
container(name: config.containerName){
body()
}
} else {
body()
}
if (!parameters.containerMap) {
configHelper.withMandatoryProperty('dockerImage')
config.containerName = 'container-exec'
config.containerMap = ["${config.get('dockerImage')}": config.containerName]
}
executeOnPod(config, body)
}
}
@ -73,37 +53,39 @@ def getOptions(config) {
containers: getContainerList(config)]
}
void executeOnPodWithSingleContainer(Map parameters, body) {
Map containerMap = [:]
def config = parameters.config
containerMap[config.get('dockerImage').toString()] = 'container-exec'
config.containerMap = containerMap
void executeOnPod(Map config, Closure body) {
/*
* There could be exceptions thrown by
- The podTemplate
- The container method
- The body
* We use nested exception handling in this case.
* In the first 2 cases, the 'container' stash is not created because the inner try/finally is not reached.
* In the first 2 cases, the 'container' stash is not created because the inner try/finally is not reached.
* However, the workspace has not been modified and don't need to be restored.
* In case third case, we need to create the 'container' stash to bring the modified content back to the host.
*/
try {
stashWorkspace(config, 'workspace')
if (config.containerName)
stashWorkspace(config, 'workspace')
podTemplate(getOptions(config)) {
node(config.uniqueId) {
container(name: 'container-exec') {
try {
unstashWorkspace(config, 'workspace')
body()
} finally {
stashWorkspace(config, 'container')
if (config.containerName) {
container(name: config.containerName){
try {
unstashWorkspace(config, 'workspace')
body()
} finally {
stashWorkspace(config, 'container')
}
}
} else {
body()
}
}
}
} finally {
unstashWorkspace(config, 'container')
if (config.containerName)
unstashWorkspace(config, 'container')
}
}

View File

@ -1,8 +1,8 @@
import com.sap.piper.Utils
import com.sap.piper.ConfigurationHelper
import com.sap.piper.GitUtils
import com.sap.piper.Utils
import groovy.transform.Field
import groovy.text.SimpleTemplateEngine
import groovy.transform.Field
@Field String STEP_NAME = 'newmanExecute'
@Field Set STEP_CONFIG_KEYS = [
@ -35,16 +35,9 @@ def call(Map parameters = [:]) {
new Utils().pushToSWA([step: STEP_NAME], config)
if (config.testRepository) {
def gitParameters = [url: config.testRepository]
if (config.gitSshKeyCredentialsId) gitParameters.credentialsId = config.gitSshKeyCredentialsId
if (config.gitBranch) gitParameters.branch = config.gitBranch
git gitParameters
stash 'newmanContent'
config.stashContent = ['newmanContent']
} else {
config.stashContent = utils.unstashAll(config.stashContent)
}
config.stashContent = config.testRepository
?[GitUtils.handleTestRepository(this, config)]
:utils.unstashAll(config.stashContent)
List collectionList = findFiles(glob: config.newmanCollection)?.toList()
if (collectionList.isEmpty()) {

View File

@ -1,5 +1,5 @@
import com.sap.piper.Utils
import com.sap.piper.ConfigurationHelper
import com.sap.piper.GitUtils
import com.sap.piper.Utils
import com.sap.piper.k8s.ContainerMap
import groovy.transform.Field
@ -54,14 +54,9 @@ def call(Map parameters = [:], Closure body) {
sidecarVolumeBind: config.sidecarVolumeBind
) {
try {
if (config.testRepository) {
def gitParameters = [url: config.testRepository]
if (config.gitSshKeyCredentialsId) gitParameters.credentialsId = config.gitSshKeyCredentialsId
if (config.gitBranch) gitParameters.branch = config.gitBranch
git gitParameters
} else {
config.stashContent = utils.unstashAll(config.stashContent)
}
config.stashContent = config.testRepository
?[GitUtils.handleTestRepository(this, config)]
:utils.unstashAll(config.stashContent)
body()
} catch (err) {
if (config.failOnError) {

View File

@ -1,7 +1,6 @@
import com.cloudbees.groovy.cps.NonCPS
import com.sap.piper.ConfigurationHelper
import com.sap.piper.ConfigurationMerger
import com.sap.piper.MapUtils
import com.sap.piper.Utils
import groovy.transform.Field

View File

@ -1,9 +1,7 @@
import com.sap.piper.GitUtils
import com.sap.piper.Utils
import groovy.transform.Field
import com.sap.piper.ConfigurationHelper
import com.sap.piper.ConfigurationMerger
import com.sap.piper.cm.ChangeManagement
import com.sap.piper.cm.ChangeManagementException

View File

@ -1,9 +1,7 @@
import com.sap.piper.GitUtils
import com.sap.piper.Utils
import groovy.transform.Field
import com.sap.piper.ConfigurationHelper
import com.sap.piper.ConfigurationMerger
import com.sap.piper.cm.ChangeManagement
import com.sap.piper.cm.ChangeManagementException

View File

@ -1,9 +1,7 @@
import com.sap.piper.GitUtils
import com.sap.piper.Utils
import groovy.transform.Field
import com.sap.piper.ConfigurationHelper
import com.sap.piper.ConfigurationMerger
import com.sap.piper.cm.ChangeManagement
import com.sap.piper.cm.ChangeManagementException