1
0
mirror of https://github.com/firstBitMarksistskaya/jenkins-lib.git synced 2025-02-15 15:10:23 +02:00

Конфиг по умолчанию, мерж конфигураций, интеграционные тесты

This commit is contained in:
Nikita Gryzlov 2020-04-03 15:17:12 +03:00
parent 9d1edee928
commit 6f21593e0b
No known key found for this signature in database
GPG Key ID: C1EAE411FEF0BF2F
6 changed files with 126 additions and 15 deletions

View File

@ -46,6 +46,7 @@ contributor(ctx) {
method(name: 'readProperties', type: 'Object', namedParams: [parameter(name: 'defaults', type: 'java.util.Map'), parameter(name: 'file', type: 'java.lang.String'), parameter(name: 'interpolate', type: 'java.lang.Boolean'), parameter(name: 'text', type: 'java.lang.String'),], doc: 'Read properties from files in the workspace or text.')
method(name: 'readFile', type: 'java.lang.String', params: [file: 'java.lang.String'], doc: 'Read file from workspace')
method(name: 'readFile', type: 'java.lang.String', namedParams: [parameter(name: 'file', type: 'java.lang.String'), parameter(name: 'encoding', type: 'java.lang.String'),], doc: 'Read file from workspace')
method(name: 'fileExists', type: 'Object', params: [file: 'java.lang.String'], doc: 'Verify if file exists in workspace')
method(name: 'readTrusted', type: 'Object', params: [path: 'java.lang.String'], doc: 'Read trusted file from SCM')
method(name: 'readYaml', type: 'Object', params: [:], doc: 'Read yaml from files in the workspace or text.')
method(name: 'readYaml', type: 'Object', namedParams: [parameter(name: 'file', type: 'java.lang.String'), parameter(name: 'text', type: 'java.lang.String'),], doc: 'Read yaml from files in the workspace or text.')

View File

@ -1,28 +1,46 @@
package ru.pulsar.jenkins.library.configuration
import com.cloudbees.groovy.cps.NonCPS
import com.fasterxml.jackson.databind.ObjectMapper
import org.apache.commons.beanutils.BeanUtils
import ru.pulsar.jenkins.library.IStepExecutor
import ru.pulsar.jenkins.library.ioc.ContextRegistry
class ConfigurationReader implements Serializable {
static JobConfiguration create(String config) {
private static ObjectMapper mapper = new ObjectMapper()
private static final String DEFAULT_CONFIGURATION_RESOURCE = 'globalConfiguration.json'
static JobConfiguration create() {
IStepExecutor steps = ContextRegistry.getContext().getStepExecutor()
def mapper = new ObjectMapper()
def globalConfig = steps.libraryResource 'globalConfiguration.json'
def globalConfig = steps.libraryResource DEFAULT_CONFIGURATION_RESOURCE
def globalConfiguration = mapper.readValue(globalConfig, JobConfiguration.class)
def jobConfiguration = mapper.readValue(config, JobConfiguration.class)
BeanUtils.describe(jobConfiguration).entrySet().stream()
.filter({ e -> e.getValue() != null })
.filter({ e -> e.getKey() != "class" })
.filter({ e -> e.getKey() != "metaClass" })
.forEach { e ->
BeanUtils.setProperty(globalConfiguration, e.getKey(), e.getValue());
}
return globalConfiguration
}
static JobConfiguration create(String config) {
def globalConfiguration = create()
def jobConfiguration = mapper.readValue(config, JobConfiguration.class)
return mergeConfigurations(globalConfiguration, jobConfiguration);
}
@NonCPS
private static JobConfiguration mergeConfigurations(
JobConfiguration baseConfiguration,
JobConfiguration configurationToMerge
) {
BeanUtils.describe(configurationToMerge).entrySet().stream()
.filter({ e -> e.getValue() != null })
.filter({ e -> e.getKey() != "class" })
.filter({ e -> e.getKey() != "metaClass" })
.forEach {e ->
BeanUtils.setProperty(baseConfiguration, e.getKey(), e.getValue());
}
return baseConfiguration;
}
}

View File

@ -2,6 +2,8 @@ package ru.pulsar.jenkins.library.configuration
import com.fasterxml.jackson.annotation.JsonIgnoreProperties
import com.fasterxml.jackson.annotation.JsonPropertyDescription
import org.apache.commons.lang3.builder.ReflectionToStringBuilder
import org.apache.commons.lang3.builder.ToStringStyle
@JsonIgnoreProperties(ignoreUnknown = true)
class JobConfiguration implements Serializable {
@ -10,4 +12,9 @@ class JobConfiguration implements Serializable {
@JsonPropertyDescription("Имя настроенной утилиты sonar-scanner.")
String sonarScannerToolName
@Override
String toString() {
return ReflectionToStringBuilder.toString(this, ToStringStyle.NO_CLASS_NAME_STYLE)
}
}

View File

@ -0,0 +1,77 @@
import org.apache.commons.io.IOUtils
import org.jenkinsci.plugins.workflow.cps.CpsFlowDefinition
import org.jenkinsci.plugins.workflow.job.WorkflowJob
import org.junit.Before
import org.junit.Rule
import org.junit.Test
import org.jvnet.hudson.test.JenkinsRule
import java.nio.charset.StandardCharsets
class jobConfigurationTest {
@Rule
public JenkinsRule rule = new JenkinsRule()
@Before
void configureGlobalGitLibraries() {
RuleBootstrapper.setup(rule)
}
@Test
void "jobConfiguration should not fail without file"() {
def pipeline = """
pipeline {
agent any
stages {
stage('test') {
steps {
echo jobConfiguration().toString()
}
}
}
}
""".stripIndent()
final CpsFlowDefinition flow = new CpsFlowDefinition(pipeline, true)
final WorkflowJob workflowJob = rule.createProject(WorkflowJob, 'project')
workflowJob.definition = flow
rule.buildAndAssertSuccess(workflowJob)
}
@Test
void "jobConfiguration should merge configurations"() {
def file = IOUtils.resourceToString(
'jobConfiguration.json',
StandardCharsets.UTF_8,
this.getClass().getClassLoader()
);
def writeFile = """
writeFile text: \"\"\"$file\"\"\", file: 'jobConfiguration.json'
"""
def pipeline = """
pipeline {
agent any
stages {
stage('test') {
steps {
$writeFile
echo jobConfiguration().toString()
}
}
}
}
""".stripIndent()
final CpsFlowDefinition flow = new CpsFlowDefinition(pipeline, true)
final WorkflowJob workflowJob = rule.createProject(WorkflowJob, 'project')
workflowJob.definition = flow
def run = rule.buildAndAssertSuccess(workflowJob)
rule.assertLogContains('v8version=8.3.12.1500', run)
rule.assertLogContains('sonarScannerToolName=sonar-scanner', run)
}
}

View File

@ -0,0 +1,3 @@
{
"v8version": "8.3.12.1500"
}

View File

@ -2,9 +2,14 @@ import ru.pulsar.jenkins.library.configuration.ConfigurationReader
import ru.pulsar.jenkins.library.configuration.JobConfiguration
import ru.pulsar.jenkins.library.ioc.ContextRegistry
JobConfiguration call(String path = "") {
JobConfiguration call(String path = "jobConfiguration.json") {
ContextRegistry.registerDefaultContext(this)
def config = readFile(path)
return ConfigurationReader.create(config)
if (fileExists(path)) {
def config = readFile(path)
return ConfigurationReader.create(config)
} else {
return ConfigurationReader.create()
}
}