1
0
mirror of https://github.com/SAP/jenkins-library.git synced 2025-03-03 15:02:35 +02:00

Merge branch 'master' into pr/removeNeoCredentialsCheck

This commit is contained in:
Christopher Fenner 2019-01-30 10:46:06 +01:00 committed by GitHub
commit 1c9eed993b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 62 additions and 10 deletions

View File

@ -210,7 +210,7 @@ class Helper {
def _docu = []
docuLines.each { _docu << it }
_docu = Helper.trim(_docu)
step.description = _docu*.trim().join('\n')
step.description = _docu.join('\n')
} else {
def param = retrieveParameterName(line)
@ -245,6 +245,7 @@ class Helper {
if(_line.startsWith('/**')) _line = _line.replaceAll('^\\/\\*\\*', '') // start comment
if(_line.startsWith('*/')) _line = _line.replaceAll('^\\*/', '') // end comment
if(_line.startsWith('*')) _line = _line.replaceAll('^\\*', '') // continue comment
if(_line.startsWith(' ')) _line = _line.replaceAll('^\\s', '')
if(_line ==~ /.*@possibleValues.*/) {
mandatory = false // should be something like reset attributes
value = true
@ -270,7 +271,7 @@ class Helper {
}
if(! value && ! mandatory) {
docuLines << _line.trim()
docuLines << _line
}
}

View File

@ -5,11 +5,13 @@ import org.junit.Rule
import org.junit.Test
import org.junit.rules.ExpectedException
import org.junit.rules.RuleChain
import util.BasePiperTest
import util.JenkinsCredentialsRule
import util.JenkinsEnvironmentRule
import util.JenkinsDockerExecuteRule
import util.JenkinsLoggingRule
import util.JenkinsReadFileRule
import util.JenkinsShellCallRule
import util.JenkinsStepRule
import util.JenkinsWriteFileRule
@ -27,10 +29,12 @@ import static org.hamcrest.Matchers.containsString
class CloudFoundryDeployTest extends BasePiperTest {
private File tmpDir = File.createTempDir()
private ExpectedException thrown = ExpectedException.none()
private JenkinsLoggingRule loggingRule = new JenkinsLoggingRule(this)
private JenkinsShellCallRule shellRule = new JenkinsShellCallRule(this)
private JenkinsWriteFileRule writeFileRule = new JenkinsWriteFileRule(this)
private JenkinsReadFileRule readFileRule = new JenkinsReadFileRule(this, tmpDir.getAbsolutePath())
private JenkinsDockerExecuteRule dockerExecuteRule = new JenkinsDockerExecuteRule(this)
private JenkinsStepRule stepRule = new JenkinsStepRule(this)
private JenkinsEnvironmentRule environmentRule = new JenkinsEnvironmentRule(this)
@ -52,6 +56,7 @@ class CloudFoundryDeployTest extends BasePiperTest {
.around(loggingRule)
.around(shellRule)
.around(writeFileRule)
.around(readFileRule)
.around(dockerExecuteRule)
.around(environmentRule)
.around(new JenkinsCredentialsRule(this).withCredentials('test_cfCredentialsId', 'test_cf', '********'))
@ -301,7 +306,7 @@ class CloudFoundryDeployTest extends BasePiperTest {
assertThat(shellRule.shell, hasItem(containsString('cf login -u "test_cf" -p \'********\' -a https://api.cf.eu10.hana.ondemand.com -o "testOrg" -s "testSpace"')))
assertThat(shellRule.shell, hasItem(containsString("cf blue-green-deploy testAppName --delete-old-apps -f 'test.yml'")))
assertThat(shellRule.shell, not(hasItem(containsString("cf stop testAppName-old"))))
assertThat(shellRule.shell, not(hasItem(containsString("cf stop testAppName-old &>"))))
assertThat(shellRule.shell, hasItem(containsString("cf logout")))
}
@ -330,10 +335,47 @@ class CloudFoundryDeployTest extends BasePiperTest {
assertThat(shellRule.shell, hasItem(containsString('cf login -u "test_cf" -p \'********\' -a https://api.cf.eu10.hana.ondemand.com -o "testOrg" -s "testSpace"')))
assertThat(shellRule.shell, hasItem(containsString("cf blue-green-deploy testAppName -f 'test.yml'")))
assertThat(shellRule.shell, hasItem(containsString("cf stop testAppName-old")))
assertThat(shellRule.shell, hasItem(containsString("cf stop testAppName-old &>")))
assertThat(shellRule.shell, hasItem(containsString("cf logout")))
}
@Test
void testCfNativeBlueGreenKeepOldInstanceShouldThrowErrorOnStopError(){
// the name of the file which will be written contains a dynamically generated UUID
// we force randomUUID() to return 1 that we can use this file in the test
UUID.metaClass.static.randomUUID = { -> 1}
new File(tmpDir, '1-cfStopOutput.txt').write('any error message')
shellRule.setReturnValue(JenkinsShellCallRule.Type.REGEX, '^cf stop testAppName-old &> .*$', 1)
readYamlRule.registerYaml('test.yml', "applications: [[]]")
thrown.expect(hudson.AbortException)
thrown.expectMessage("Could not stop application testAppName-old. Error: any error message")
stepRule.step.cloudFoundryDeploy([
script: nullScript,
juStabUtils: utils,
jenkinsUtilsStub: new JenkinsUtilsMock(),
deployTool: 'cf_native',
deployType: 'blue-green',
keepOldInstance: true,
cfOrg: 'testOrg',
cfSpace: 'testSpace',
cfCredentialsId: 'test_cfCredentialsId',
cfAppName: 'testAppName',
cfManifest: 'test.yml'
])
assertThat(dockerExecuteRule.dockerParams, hasEntry('dockerImage', 's4sdk/docker-cf-cli'))
assertThat(dockerExecuteRule.dockerParams, hasEntry('dockerWorkspace', '/home/piper'))
assertThat(shellRule.shell, hasItem(containsString('cf login -u "test_cf" -p \'********\' -a https://api.cf.eu10.hana.ondemand.com -o "testOrg" -s "testSpace"')))
assertThat(shellRule.shell, hasItem(containsString("cf blue-green-deploy testAppName -f 'test.yml'")))
assertThat(shellRule.shell, hasItem(containsString("cf stop testAppName-old &> 1-cfStopOutput.txt")))
}
@Test
void testCfNativeStandardShouldNotStopInstance() {
@ -353,7 +395,7 @@ class CloudFoundryDeployTest extends BasePiperTest {
cfManifest: 'test.yml'
])
assertThat(shellRule.shell, not(hasItem(containsString("cf stop testAppName-old"))))
assertThat(shellRule.shell, not(hasItem(containsString("cf stop testAppName-old &>"))))
}
@Test

View File

@ -173,8 +173,8 @@ def deployCfNative (config) {
cf login -u \"${username}\" -p '${password}' -a ${config.cloudFoundry.apiEndpoint} -o \"${config.cloudFoundry.org}\" -s \"${config.cloudFoundry.space}\"
cf plugins
cf ${deployCommand} ${config.cloudFoundry.appName ?: ''} ${blueGreenDeployOptions} -f '${config.cloudFoundry.manifest}' ${config.smokeTest}
${stopOldAppIfRequired(config)}
"""
stopOldAppIfRunning(config)
sh "cf logout"
}
}
@ -196,11 +196,20 @@ private String deleteOptionIfRequired(Map config) {
}
}
private String stopOldAppIfRequired(Map config) {
private void stopOldAppIfRunning(Map config) {
String oldAppName = "${config.cloudFoundry.appName}-old"
String cfStopOutputFileName = "${UUID.randomUUID()}-cfStopOutput.txt"
if (config.keepOldInstance && config.deployType == 'blue-green') {
return "cf stop ${config.cloudFoundry.appName}-old"
} else {
return ''
int cfStopReturncode = sh (returnStatus: true, script: "cf stop $oldAppName &> $cfStopOutputFileName")
if (cfStopReturncode > 0) {
String cfStopOutput = readFile(file: cfStopOutputFileName)
if (!cfStopOutput.contains("$oldAppName not found")) {
error "Could not stop application $oldAppName. Error: $cfStopOutput"
}
}
}
}