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

Cf validate app name (#1759)

CF deployment will fail if you have an _ in the app name. Also in general names should only contain alphanumeric chars. Warn users if it does not.
This commit is contained in:
Florian Wilhelm 2020-07-06 11:45:36 +02:00 committed by GitHub
parent cd4e4eb3b4
commit 209d940522
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 130 additions and 0 deletions

View File

@ -1,4 +1,5 @@
import com.sap.piper.JenkinsUtils
import hudson.AbortException
import org.junit.After
import org.junit.Before
import org.junit.Rule
@ -26,6 +27,7 @@ import static org.hamcrest.Matchers.hasItem
import static org.hamcrest.Matchers.is
import static org.hamcrest.Matchers.not
import static org.hamcrest.Matchers.stringContainsInOrder
import static org.junit.Assert.assertEquals
import static org.junit.Assert.assertNotNull
import static org.junit.Assert.assertThat
import static org.junit.Assert.assertTrue
@ -1069,4 +1071,108 @@ class CloudFoundryDeployTest extends BasePiperTest {
}
@Test
void 'appName with underscores should throw an error'() {
String expected = "Your application name my_invalid_app_name contains a '_' (underscore) which is not allowed, only letters, dashes and numbers can be used. Please change the name to fit this requirement.\n" +
"For more details please visit https://docs.cloudfoundry.org/devguide/deploy-apps/deploy-app.html#basic-settings."
String actual = ""
helper.registerAllowedMethod('error', [String.class], {s -> actual = s})
stepRule.step.cloudFoundryDeploy([
script: nullScript,
juStabUtils: utils,
jenkinsUtilsStub: new JenkinsUtilsMock(),
cloudFoundry: [
org: 'irrelevant',
space: 'irrelevant',
appName: 'my_invalid_app_name'
],
cfCredentialsId: 'test_cfCredentialsId',
mtaPath: 'irrelevant'
])
assertEquals(expected, actual)
}
@Test
void 'appName with alpha-numeric chars and leading dash should throw an error'() {
String expected = "Your application name -my-Invalid-AppName123 contains a starts or ends with a '-' (dash) which is not allowed, only letters, dashes and numbers can be used. Please change the name to fit this requirement.\nFor more details please visit https://docs.cloudfoundry.org/devguide/deploy-apps/deploy-app.html#basic-settings."
String actual = ""
helper.registerAllowedMethod('error', [String.class], {s -> actual = s})
stepRule.step.cloudFoundryDeploy([
script: nullScript,
juStabUtils: utils,
jenkinsUtilsStub: new JenkinsUtilsMock(),
cloudFoundry: [
org: 'irrelevant',
space: 'irrelevant',
appName: '-my-Invalid-AppName123'
],
cfCredentialsId: 'test_cfCredentialsId',
mtaPath: 'irrelevant'
])
assertEquals(expected, actual)
}
@Test
void 'appName with alpha-numeric chars and trailing dash should throw an error'() {
String expected = "Your application name my-Invalid-AppName123- contains a starts or ends with a '-' (dash) which is not allowed, only letters, dashes and numbers can be used. Please change the name to fit this requirement.\nFor more details please visit https://docs.cloudfoundry.org/devguide/deploy-apps/deploy-app.html#basic-settings."
String actual = ""
helper.registerAllowedMethod('error', [String.class], {s -> actual = s})
stepRule.step.cloudFoundryDeploy([
script: nullScript,
juStabUtils: utils,
jenkinsUtilsStub: new JenkinsUtilsMock(),
cloudFoundry: [
org: 'irrelevant',
space: 'irrelevant',
appName: 'my-Invalid-AppName123-'
],
cfCredentialsId: 'test_cfCredentialsId',
mtaPath: 'irrelevant'
])
assertEquals(expected, actual)
}
@Test
void 'appName with alpha-numeric chars should work'() {
stepRule.step.cloudFoundryDeploy([
script: nullScript,
juStabUtils: utils,
jenkinsUtilsStub: new JenkinsUtilsMock(),
cloudFoundry: [
org: 'irrelevant',
space: 'irrelevant',
appName: 'myValidAppName123'
],
cfCredentialsId: 'test_cfCredentialsId',
mtaPath: 'irrelevant'
])
assertTrue(loggingRule.log.contains("cfAppName=myValidAppName123"))
}
@Test
void 'appName with alpha-numeric chars and dash should work'() {
stepRule.step.cloudFoundryDeploy([
script: nullScript,
juStabUtils: utils,
jenkinsUtilsStub: new JenkinsUtilsMock(),
cloudFoundry: [
org: 'irrelevant',
space: 'irrelevant',
appName: 'my-Valid-AppName123'
],
cfCredentialsId: 'test_cfCredentialsId',
mtaPath: 'irrelevant'
])
assertTrue(loggingRule.log.contains("cfAppName=my-Valid-AppName123"))
}
}

View File

@ -222,6 +222,30 @@ void call(Map parameters = [:]) {
//make sure that for further execution whole workspace, e.g. also downloaded artifacts are considered
config.stashContent = []
// validate cf app name to avoid a failing deployment due to invalid chars
if (config.cloudFoundry.appName) {
String appName = config.cloudFoundry.appName.toString()
boolean isValidCfAppName = appName.matches("^[a-zA-Z0-9][a-zA-Z0-9\\-]*[a-zA-Z0-9]\$")
if (!isValidCfAppName) {
echo "WARNING: Your application name $appName contains non-alphanumeric characters which may lead to errors in the future, as they are not supported by CloudFoundry.\n" +
"For more details please visit https://docs.cloudfoundry.org/devguide/deploy-apps/deploy-app.html#basic-settings"
// Underscore in the app name will lead to errors because cf uses the appname as part of the url which may not contain underscores
if (appName.contains("_")) {
error("Your application name $appName contains a '_' (underscore) which is not allowed, only letters, dashes and numbers can be used. " +
"Please change the name to fit this requirement.\n" +
"For more details please visit https://docs.cloudfoundry.org/devguide/deploy-apps/deploy-app.html#basic-settings.")
}
if (appName.startsWith("-") || appName.endsWith("-")) {
error("Your application name $appName contains a starts or ends with a '-' (dash) which is not allowed, only letters, dashes and numbers can be used. " +
"Please change the name to fit this requirement.\n" +
"For more details please visit https://docs.cloudfoundry.org/devguide/deploy-apps/deploy-app.html#basic-settings.")
}
}
}
boolean deployTriggered = false
boolean deploySuccess = true
try {