1
0
mirror of https://github.com/SAP/jenkins-library.git synced 2025-09-16 09:26:22 +02:00

Fix containers map and add test (#1769)

This commit is contained in:
Daniel Kurzynski
2020-07-07 12:07:44 +02:00
committed by GitHub
parent 42208b6774
commit 5749b47c75
3 changed files with 106 additions and 14 deletions

View File

@@ -1,6 +1,7 @@
package com.sap.piper.k8s
import com.sap.piper.API
import com.sap.piper.Utils
import groovy.json.JsonOutput
@API
@@ -23,26 +24,28 @@ class ContainerMap implements Serializable {
private static class PiperExecutionPreparator {
private boolean executionPrepared
private utils
public PiperExecutionPreparator(utils){
this.utils = utils
}
void prepareExecution(Script script) {
if (!executionPrepared) {
script.piperExecuteBin.prepareExecution(script)
script.piperExecuteBin.prepareExecution(script, utils)
executionPrepared = true
}
}
}
void initFromResource(Script script, String yamlResourceName, String buildTool) {
void initFromResource(Script script, String yamlResourceName, String buildTool, utils = new Utils()) {
Map containers = [:]
PiperExecutionPreparator piperPreparator = new PiperExecutionPreparator()
PiperExecutionPreparator piperPreparator = new PiperExecutionPreparator(utils)
Map stageToStepMapping
Map stepToMetaDataMapping
try {
Map yamlContents = script.readYaml(text: script.libraryResource(yamlResourceName))
Map stageToStepMapping = yamlContents.containerMaps as Map
Map stepToMetaDataMapping = yamlContents.stepMetadata as Map ?: [:]
stageToStepMapping.each { stageName, stepsList ->
containers[stageName] = getContainersForStage(script, stageName as String, stepsList as List,
stepToMetaDataMapping, buildTool, piperPreparator)
}
stageToStepMapping = yamlContents.containerMaps as Map
stepToMetaDataMapping = yamlContents.stepMetadata as Map ?: [:]
} catch (Exception e) {
script.error "Failed to parse container maps in '$yamlResourceName'. It is expected to contain " +
"the entries 'containerMaps' and optionally 'stepMetadata' in the root." +
@@ -51,6 +54,10 @@ class ContainerMap implements Serializable {
"metadata resource file." +
"Error: ${e.getMessage()}"
}
stageToStepMapping.each { stageName, stepsList ->
containers[stageName] = getContainersForStage(script, stageName as String, stepsList as List,
stepToMetaDataMapping, buildTool, piperPreparator)
}
setMap(containers)
}

View File

@@ -1,16 +1,60 @@
package com.sap.piper.k8s
import com.sap.piper.k8s.ContainerMap
import com.sap.piper.DefaultValueCache
import org.junit.Before
import org.junit.Rule
import org.junit.Test
import org.junit.rules.RuleChain
import util.*
import static org.junit.Assert.assertEquals
import static org.junit.Assert.assertNotNull
import static org.hamcrest.Matchers.hasItem
import static org.junit.Assert.*
class ContainerMapTest extends BasePiperTest {
String exampleConfigYaml = """
# Mapping of Go step names to their YAML metadata resource file
stepMetadata:
artifactPrepareVersion: versioning.yaml
containerMaps:
init:
- artifactPrepareVersion
- mavenExecute
"""
private JenkinsShellCallRule shellCallRule = new JenkinsShellCallRule(this)
@Rule
public RuleChain ruleChain = Rules
.getCommonRules(this)
.around(new JenkinsReadYamlRule(this))
.around(new JenkinsPiperExecuteBinRule(this))
.around(new JenkinsWriteFileRule(this))
.around(new JenkinsReadJsonRule(this))
.around(shellCallRule)
private List envs
class ContainerMapTest {
@Before
void setUp() {
void init() {
helper.registerAllowedMethod('libraryResource', [String.class], {
return exampleConfigYaml
})
helper.registerAllowedMethod('withEnv', [List.class, Closure.class], { List envs, Closure body ->
this.envs = envs.collect {it.toString()}
body()
})
DefaultValueCache.createInstance([
steps: [
mavenExecute: [
dockerImage: 'maven:3.5-jdk-8-alpine'
]
]
])
shellCallRule.setReturnValue('./piper getConfig --contextConfig --stepMetadata \'.pipeline/tmp/metadata/versioning.yaml\'', '{"dockerImage":"artifact-image"}')
}
@Test
@@ -28,4 +72,13 @@ class ContainerMapTest {
void testGetMap() {
assertNotNull(ContainerMap.instance.getMap())
}
@Test
void testInitFromResource(){
ContainerMap.instance.initFromResource(nullScript, 'containersMapYaml', 'maven', utils)
assertThat(envs, hasItem('STAGE_NAME=init'))
assertThat(envs, hasItem('PIPER_parametersJSON={"buildTool":"maven"}'))
assertEquals(['init': ['artifact-image':'artifactprepareversion', 'maven:3.5-jdk-8-alpine': 'mavenexecute']],ContainerMap.instance.getMap())
}
}

View File

@@ -0,0 +1,32 @@
package util
import com.lesfurets.jenkins.unit.BasePipelineTest
import org.junit.rules.TestRule
import org.junit.runner.Description
import org.junit.runners.model.Statement
class JenkinsPiperExecuteBinRule implements TestRule {
final BasePipelineTest testInstance
def env
JenkinsPiperExecuteBinRule(BasePipelineTest testInstance) {
this.testInstance = testInstance
}
@Override
Statement apply(Statement base, Description description) {
return new Statement() {
@Override
void evaluate() throws Throwable {
def piperExecuteBin = testInstance.loadScript('piperExecuteBin.groovy').piperExecuteBin
try {
testInstance?.nullScript.piperExecuteBin = piperExecuteBin
} catch (MissingPropertyException e) {
//kept for backward compatibility before all tests inherit from BasePiperTest
}
base.evaluate()
}
}
}
}