2020-05-12 13:50:18 +02:00
|
|
|
import com.sap.piper.DefaultValueCache
|
2017-12-06 13:03:06 +02:00
|
|
|
import org.junit.Before
|
2018-01-16 10:33:13 +02:00
|
|
|
import org.junit.Rule
|
2017-12-06 13:03:06 +02:00
|
|
|
import org.junit.Test
|
2020-05-12 13:50:18 +02:00
|
|
|
import org.junit.rules.ExpectedException
|
2018-01-26 15:55:15 +02:00
|
|
|
import org.junit.rules.RuleChain
|
2017-12-06 13:03:06 +02:00
|
|
|
import org.yaml.snakeyaml.Yaml
|
2018-06-06 11:19:19 +02:00
|
|
|
import util.BasePiperTest
|
2020-05-12 13:50:18 +02:00
|
|
|
import util.JenkinsReadFileRule
|
|
|
|
import util.JenkinsShellCallRule
|
2018-02-28 14:11:09 +02:00
|
|
|
import util.JenkinsStepRule
|
2020-03-17 10:19:09 +02:00
|
|
|
import util.JenkinsWriteFileRule
|
2019-07-31 12:22:26 +02:00
|
|
|
import util.Rules
|
2018-01-16 10:33:13 +02:00
|
|
|
|
2020-05-12 13:50:18 +02:00
|
|
|
import static org.hamcrest.Matchers.hasItem
|
2017-12-06 13:03:06 +02:00
|
|
|
import static org.junit.Assert.assertEquals
|
|
|
|
import static org.junit.Assert.assertNotNull
|
2020-05-12 13:50:18 +02:00
|
|
|
import static org.junit.Assert.assertThat
|
|
|
|
import static org.junit.Assert.assertTrue
|
2017-12-06 13:03:06 +02:00
|
|
|
|
2018-06-06 11:19:19 +02:00
|
|
|
class SetupCommonPipelineEnvironmentTest extends BasePiperTest {
|
2019-03-26 15:13:03 +02:00
|
|
|
|
2018-03-02 11:57:50 +02:00
|
|
|
def usedConfigFile
|
|
|
|
|
2019-01-22 10:25:42 +02:00
|
|
|
private JenkinsStepRule stepRule = new JenkinsStepRule(this)
|
2020-03-17 10:19:09 +02:00
|
|
|
private JenkinsWriteFileRule writeFileRule = new JenkinsWriteFileRule(this)
|
2020-05-12 13:50:18 +02:00
|
|
|
private ExpectedException thrown = ExpectedException.none()
|
|
|
|
private JenkinsShellCallRule shellRule = new JenkinsShellCallRule(this)
|
|
|
|
private JenkinsReadFileRule readFileRule = new JenkinsReadFileRule(this, "./")
|
2018-01-16 18:06:25 +02:00
|
|
|
|
2018-01-16 10:33:13 +02:00
|
|
|
@Rule
|
2018-02-28 14:11:09 +02:00
|
|
|
public RuleChain rules = Rules
|
|
|
|
.getCommonRules(this)
|
2019-01-22 10:25:42 +02:00
|
|
|
.around(stepRule)
|
2020-03-17 10:19:09 +02:00
|
|
|
.around(writeFileRule)
|
2020-05-12 13:50:18 +02:00
|
|
|
.around(thrown)
|
|
|
|
.around(shellRule)
|
|
|
|
.around(readFileRule)
|
|
|
|
|
2018-02-28 14:11:09 +02:00
|
|
|
|
2017-12-06 13:03:06 +02:00
|
|
|
@Before
|
2018-01-16 10:33:13 +02:00
|
|
|
void init() {
|
2019-03-26 15:13:03 +02:00
|
|
|
|
2017-12-06 13:03:06 +02:00
|
|
|
def examplePipelineConfig = new File('test/resources/test_pipeline_config.yml').text
|
|
|
|
|
2020-05-12 13:50:18 +02:00
|
|
|
helper.registerAllowedMethod("libraryResource", [String], { fileName ->
|
|
|
|
switch(fileName) {
|
|
|
|
case 'default_pipeline_environment.yml': return "default: 'config'"
|
|
|
|
case 'custom.yml': return "custom: 'myConfig'"
|
|
|
|
case 'notFound.yml': throw new hudson.AbortException('No such library resource notFound could be found')
|
|
|
|
default: return "the:'end'"
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
2017-12-06 13:03:06 +02:00
|
|
|
helper.registerAllowedMethod("readYaml", [Map], { Map parameters ->
|
|
|
|
Yaml yamlParser = new Yaml()
|
2019-07-31 12:22:26 +02:00
|
|
|
if (parameters.text) {
|
2017-12-06 13:03:06 +02:00
|
|
|
return yamlParser.load(parameters.text)
|
2020-05-12 13:50:18 +02:00
|
|
|
} else if(parameters.file) {
|
|
|
|
if(parameters.file == '.pipeline/default_pipeline_environment.yml') return [default: 'config']
|
|
|
|
else if (parameters.file == '.pipeline/custom.yml') return [custom: 'myConfig']
|
|
|
|
} else {
|
|
|
|
throw new IllegalArgumentException("Key 'text' and 'file' are both missing in map ${m}.")
|
2017-12-06 13:03:06 +02:00
|
|
|
}
|
|
|
|
usedConfigFile = parameters.file
|
|
|
|
return yamlParser.load(examplePipelineConfig)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
@Test
|
2018-09-27 15:45:28 +02:00
|
|
|
void testIsYamlConfigurationAvailable() throws Exception {
|
|
|
|
|
|
|
|
helper.registerAllowedMethod("fileExists", [String], { String path ->
|
|
|
|
return path.endsWith('.pipeline/config.yml')
|
|
|
|
})
|
|
|
|
|
2019-03-26 15:13:03 +02:00
|
|
|
stepRule.step.setupCommonPipelineEnvironment(script: nullScript)
|
2017-12-06 13:03:06 +02:00
|
|
|
|
|
|
|
assertEquals('.pipeline/config.yml', usedConfigFile)
|
2018-06-06 11:19:19 +02:00
|
|
|
assertNotNull(nullScript.commonPipelineEnvironment.configuration)
|
|
|
|
assertEquals('develop', nullScript.commonPipelineEnvironment.configuration.general.productiveBranch)
|
|
|
|
assertEquals('my-maven-docker', nullScript.commonPipelineEnvironment.configuration.steps.mavenExecute.dockerImage)
|
2017-12-06 13:03:06 +02:00
|
|
|
}
|
2019-07-31 12:22:26 +02:00
|
|
|
|
|
|
|
@Test
|
|
|
|
void testWorksAlsoWithYamlFileEnding() throws Exception {
|
|
|
|
|
|
|
|
helper.registerAllowedMethod("fileExists", [String], { String path ->
|
|
|
|
return path.endsWith('.pipeline/config.yaml')
|
|
|
|
})
|
|
|
|
|
|
|
|
stepRule.step.setupCommonPipelineEnvironment(script: nullScript)
|
|
|
|
|
|
|
|
assertEquals('.pipeline/config.yaml', usedConfigFile)
|
|
|
|
assertNotNull(nullScript.commonPipelineEnvironment.configuration)
|
|
|
|
assertEquals('develop', nullScript.commonPipelineEnvironment.configuration.general.productiveBranch)
|
|
|
|
assertEquals('my-maven-docker', nullScript.commonPipelineEnvironment.configuration.steps.mavenExecute.dockerImage)
|
|
|
|
}
|
2020-05-12 13:50:18 +02:00
|
|
|
|
|
|
|
@Test
|
|
|
|
public void testAttemptToLoadNonExistingConfigFile() {
|
|
|
|
|
|
|
|
helper.registerAllowedMethod("fileExists", [String], { String path ->
|
|
|
|
switch(path) {
|
|
|
|
case 'default_pipeline_environment.yml': return false
|
|
|
|
case 'custom.yml': return false
|
|
|
|
case 'notFound.yml': return false
|
|
|
|
default: return true
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
helper.registerAllowedMethod("handlePipelineStepErrors", [Map,Closure], { Map map, Closure closure ->
|
|
|
|
closure()
|
|
|
|
})
|
|
|
|
|
|
|
|
// Behavior documented here based on reality check
|
|
|
|
thrown.expect(hudson.AbortException.class)
|
|
|
|
thrown.expectMessage('No such library resource notFound could be found')
|
|
|
|
|
|
|
|
stepRule.step.setupCommonPipelineEnvironment(
|
|
|
|
script: nullScript,
|
|
|
|
customDefaults: 'notFound.yml'
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
@Test
|
|
|
|
void testAttemptToLoadFileFromURL() {
|
|
|
|
helper.registerAllowedMethod("fileExists", [String], {String path ->
|
|
|
|
switch (path) {
|
|
|
|
case 'default_pipeline_environment.yml': return false
|
|
|
|
default: return true
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
String customDefaultUrl = "https://url-to-my-config.com/my-config.yml"
|
|
|
|
boolean urlRequested = false
|
|
|
|
|
|
|
|
helper.registerAllowedMethod("httpRequest", [Map], {Map parameters ->
|
|
|
|
switch (parameters.url) {
|
|
|
|
case customDefaultUrl:
|
|
|
|
urlRequested = true
|
|
|
|
return [status: 200, content: "custom: 'myRemoteConfig'"]
|
|
|
|
default:
|
|
|
|
throw new IllegalArgumentException('wrong URL requested')
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
helper.registerAllowedMethod("readYaml", [Map], { Map parameters ->
|
|
|
|
Yaml yamlParser = new Yaml()
|
|
|
|
if (parameters.text) {
|
|
|
|
return yamlParser.load(parameters.text)
|
|
|
|
} else if (parameters.file) {
|
|
|
|
if (parameters.file == '.pipeline/config-with-custom-defaults.yml') {
|
|
|
|
return [customDefaults: "${customDefaultUrl}"]
|
|
|
|
}
|
|
|
|
if (parameters.file == '.pipeline/custom_default_from_url_0.yml') {
|
|
|
|
return [custom: 'myRemoteConfig']
|
|
|
|
}
|
|
|
|
}
|
|
|
|
throw new IllegalArgumentException("Unexpected invocation of readYaml step")
|
|
|
|
})
|
|
|
|
|
|
|
|
stepRule.step.setupCommonPipelineEnvironment(
|
|
|
|
script: nullScript,
|
|
|
|
customDefaults: 'custom.yml',
|
|
|
|
configFile: '.pipeline/config-with-custom-defaults.yml',
|
|
|
|
)
|
|
|
|
assertEquals("custom: 'myRemoteConfig'", writeFileRule.files['.pipeline/custom_default_from_url_0.yml'])
|
|
|
|
assertEquals('myRemoteConfig', DefaultValueCache.instance.defaultValues['custom'])
|
|
|
|
}
|
2017-12-06 13:03:06 +02:00
|
|
|
}
|
2019-03-26 15:13:03 +02:00
|
|
|
|