1
0
mirror of https://github.com/SAP/jenkins-library.git synced 2024-12-14 11:03:09 +02:00
sap-jenkins-library/test/groovy/NeoDeployTest.groovy

519 lines
18 KiB
Groovy
Raw Normal View History

import com.sap.piper.Utils
2017-07-11 15:12:03 +02:00
import hudson.AbortException
import static org.hamcrest.Matchers.allOf
import static org.hamcrest.Matchers.containsString
import static org.hamcrest.Matchers.not
import org.hamcrest.Matchers
import org.hamcrest.BaseMatcher
import org.hamcrest.Description
import org.jenkinsci.plugins.credentialsbinding.impl.CredentialNotFoundException
import org.junit.Assert
2017-07-11 15:12:03 +02:00
import org.junit.Before
import org.junit.Rule
import org.junit.Test
import org.junit.rules.ExpectedException
import org.junit.rules.RuleChain
import util.BasePiperTest
import util.CommandLineMatcher
import util.JenkinsCredentialsRule
import util.JenkinsLockRule
import util.JenkinsLoggingRule
import util.JenkinsPropertiesRule
import util.JenkinsReadYamlRule
2018-01-16 16:03:00 +02:00
import util.JenkinsShellCallRule
import util.JenkinsShellCallRule.Type
2018-02-28 14:11:09 +02:00
import util.JenkinsStepRule
import util.JenkinsWithEnvRule
2019-03-25 17:42:23 +02:00
import util.JenkinsFileExistsRule
import util.Rules
2017-07-11 15:12:03 +02:00
class NeoDeployTest extends BasePiperTest {
2018-02-20 14:10:14 +02:00
private ExpectedException thrown = new ExpectedException().none()
private JenkinsLoggingRule loggingRule = new JenkinsLoggingRule(this)
2019-01-22 10:19:28 +02:00
private JenkinsShellCallRule shellRule = new JenkinsShellCallRule(this)
2019-01-22 10:25:42 +02:00
private JenkinsStepRule stepRule = new JenkinsStepRule(this)
private JenkinsLockRule lockRule = new JenkinsLockRule(this)
2019-03-25 17:42:23 +02:00
private JenkinsFileExistsRule fileExistsRule = new JenkinsFileExistsRule(this, ['warArchive.war', 'archive.mtar', 'war.properties'])
2017-07-11 15:12:03 +02:00
@Rule
2018-02-28 14:11:09 +02:00
public RuleChain ruleChain = Rules
.getCommonRules(this)
.around(new JenkinsReadYamlRule(this))
2019-03-26 15:16:01 +02:00
.around(new JenkinsPropertiesRule(this, warPropertiesFileName, warProperties))
2018-02-28 14:11:09 +02:00
.around(thrown)
.around(loggingRule)
2019-01-22 10:19:28 +02:00
.around(shellRule)
.around(new JenkinsCredentialsRule(this)
.withCredentials('myCredentialsId', 'anonymous', '********')
.withCredentials('CI_CREDENTIALS_ID', 'defaultUser', '********'))
2019-01-22 10:25:42 +02:00
.around(stepRule)
.around(lockRule)
.around(new JenkinsWithEnvRule(this))
2019-03-25 17:42:23 +02:00
.around(fileExistsRule)
2019-03-25 17:42:23 +02:00
private static warArchiveName = 'warArchive.war'
private static warPropertiesFileName = 'war.properties'
private static archiveName = 'archive.mtar'
2019-03-26 15:16:01 +02:00
private static warProperties
2017-07-11 15:12:03 +02:00
2019-03-25 17:42:23 +02:00
@Before
void init() {
2018-02-12 12:03:07 +02:00
2019-03-26 15:16:01 +02:00
warProperties = new Properties()
warProperties.put('account', 'trialuser123')
warProperties.put('host', 'test.deploy.host.com')
warProperties.put('application', 'testApp')
2017-12-13 11:05:19 +02:00
helper.registerAllowedMethod('dockerExecute', [Map, Closure], null)
2019-03-25 17:42:23 +02:00
helper.registerAllowedMethod('pwd', [], { return './' })
2017-07-11 15:12:03 +02:00
nullScript.commonPipelineEnvironment.configuration = [steps: [neoDeploy: [neo: [host: 'test.deploy.host.com', account: 'trialuser123']]]]
2017-07-11 15:12:03 +02:00
}
@Test
void straightForwardTestConfigViaParameters() {
2019-01-22 10:25:42 +02:00
stepRule.step.neoDeploy(script: nullScript,
source: archiveName,
neo:[credentialsId: 'myCredentialsId'],
utils: utils,
2018-02-28 14:11:09 +02:00
)
2019-01-22 10:19:28 +02:00
Assert.assertThat(shellRule.shell,
2019-03-07 13:53:25 +02:00
new CommandLineMatcher().hasProlog("neo.sh deploy-mta")
.hasSingleQuotedOption('host', 'test\\.deploy\\.host\\.com')
.hasSingleQuotedOption('account', 'trialuser123')
.hasOption('synchronous', '')
.hasSingleQuotedOption('user', 'anonymous')
.hasSingleQuotedOption('password', '\\*\\*\\*\\*\\*\\*\\*\\*')
.hasSingleQuotedOption('source', '.*'))
}
@Test
void straightForwardTestConfigViaConfiguration() {
nullScript.commonPipelineEnvironment.configuration = [steps: [
neoDeploy: [
neo: [
host: 'configuration-frwk.deploy.host.com',
account: 'configurationFrwkUser123'
],
source: archiveName
]
]]
2019-01-22 10:25:42 +02:00
stepRule.step.neoDeploy(script: nullScript,
neo:[credentialsId: 'myCredentialsId']
)
2019-01-22 10:19:28 +02:00
Assert.assertThat(shellRule.shell,
2019-03-07 13:53:25 +02:00
new CommandLineMatcher().hasProlog("neo.sh deploy-mta")
.hasSingleQuotedOption('host', 'configuration-frwk\\.deploy\\.host\\.com')
.hasSingleQuotedOption('account', 'configurationFrwkUser123')
.hasOption('synchronous', '')
.hasSingleQuotedOption('user', 'anonymous')
.hasSingleQuotedOption('password', '\\*\\*\\*\\*\\*\\*\\*\\*')
.hasSingleQuotedOption('source', archiveName))
}
2018-08-17 12:52:01 +02:00
@Test
void archivePathFromCPETest() {
2019-03-26 16:04:30 +02:00
2018-08-17 12:52:01 +02:00
nullScript.commonPipelineEnvironment.setMtarFilePath('archive.mtar')
2019-03-26 16:04:30 +02:00
2019-01-22 10:25:42 +02:00
stepRule.step.neoDeploy(script: nullScript)
2018-08-17 12:52:01 +02:00
2019-01-22 10:19:28 +02:00
Assert.assertThat(shellRule.shell,
2019-03-07 13:53:25 +02:00
new CommandLineMatcher().hasProlog("neo.sh deploy-mta")
.hasSingleQuotedOption('source', 'archive.mtar'))
2018-08-17 12:52:01 +02:00
}
@Test
void archivePathFromParamsHasHigherPrecedenceThanCPETest() {
2019-03-26 16:04:30 +02:00
2018-08-17 12:52:01 +02:00
nullScript.commonPipelineEnvironment.setMtarFilePath('archive2.mtar')
2019-03-26 16:04:30 +02:00
2019-01-22 10:25:42 +02:00
stepRule.step.neoDeploy(script: nullScript,
source: "archive.mtar")
2018-08-17 12:52:01 +02:00
2019-01-22 10:19:28 +02:00
Assert.assertThat(shellRule.shell,
2019-03-07 13:53:25 +02:00
new CommandLineMatcher().hasProlog("neo.sh deploy-mta")
.hasSingleQuotedOption('source', 'archive.mtar'))
2018-08-17 12:52:01 +02:00
}
2017-07-11 15:12:03 +02:00
@Test
void badCredentialsIdTest() {
thrown.expect(CredentialNotFoundException)
2017-07-11 15:12:03 +02:00
2019-01-22 10:25:42 +02:00
stepRule.step.neoDeploy(script: nullScript,
source: archiveName,
neo:[credentialsId: 'badCredentialsId']
)
2017-07-11 15:12:03 +02:00
}
@Test
void credentialsIdNotProvidedTest() {
2019-01-22 10:25:42 +02:00
stepRule.step.neoDeploy(script: nullScript,
source: archiveName
)
2017-07-11 15:12:03 +02:00
2019-01-22 10:19:28 +02:00
Assert.assertThat(shellRule.shell,
2019-03-07 13:53:25 +02:00
new CommandLineMatcher().hasProlog("neo.sh deploy-mta")
.hasSingleQuotedOption('host', 'test\\.deploy\\.host\\.com')
.hasSingleQuotedOption('account', 'trialuser123')
.hasOption('synchronous', '')
.hasSingleQuotedOption('user', 'defaultUser')
.hasSingleQuotedOption('password', '\\*\\*\\*\\*\\*\\*\\*\\*')
.hasSingleQuotedOption('source', '.*')
)
2017-07-11 15:12:03 +02:00
}
@Test
void wrongArchivePathProvidedTest() {
thrown.expect(AbortException)
thrown.expectMessage('File wrongArchiveName cannot be found')
2019-01-22 10:25:42 +02:00
stepRule.step.neoDeploy(script: nullScript,
source: 'wrongArchiveName')
2017-07-11 15:12:03 +02:00
}
@Test
void sanityChecksDeployModeMTATest() {
2017-07-11 15:12:03 +02:00
thrown.expect(Exception)
thrown.expectMessage(
allOf(
containsString('ERROR - NO VALUE AVAILABLE FOR:'),
containsString('neo/host'),
containsString('neo/account'),
containsString('source')))
nullScript.commonPipelineEnvironment.configuration = [:]
// deployMode mta is the default, but for the sake of transparency it is better to repeat it.
stepRule.step.neoDeploy(script: nullScript, deployMode: 'mta')
}
@Test
public void sanityChecksDeployModeWarPropertiesFileTest() {
thrown.expect(IllegalArgumentException)
// using this deploy mode 'account' and 'host' are provided by the properties file
thrown.expectMessage(
allOf(
containsString('ERROR - NO VALUE AVAILABLE FOR source'),
not(containsString('neo/host')),
not(containsString('neo/account'))))
nullScript.commonPipelineEnvironment.configuration = [:]
stepRule.step.neoDeploy(script: nullScript, deployMode: 'warPropertiesFile')
}
@Test
public void sanityChecksDeployModeWarParamsTest() {
thrown.expect(IllegalArgumentException)
thrown.expectMessage(
allOf(
containsString('ERROR - NO VALUE AVAILABLE FOR:'),
containsString('source'),
containsString('neo/application'),
containsString('neo/runtime'),
containsString('neo/runtimeVersion'),
containsString('neo/host'),
containsString('neo/account')))
2017-07-11 15:12:03 +02:00
nullScript.commonPipelineEnvironment.configuration = [:]
stepRule.step.neoDeploy(script: nullScript, deployMode: 'warParams')
2017-07-11 15:12:03 +02:00
}
@Test
void mtaDeployModeTest() {
stepRule.step.neoDeploy(script: nullScript, source: archiveName, deployMode: 'mta')
2019-01-22 10:19:28 +02:00
Assert.assertThat(shellRule.shell,
2019-03-07 13:53:25 +02:00
new CommandLineMatcher().hasProlog("neo.sh deploy-mta")
.hasSingleQuotedOption('host', 'test\\.deploy\\.host\\.com')
.hasSingleQuotedOption('account', 'trialuser123')
.hasOption('synchronous', '')
.hasSingleQuotedOption('user', 'defaultUser')
.hasSingleQuotedOption('password', '\\*\\*\\*\\*\\*\\*\\*\\*')
.hasSingleQuotedOption('source', '.*'))
}
@Test
void warFileParamsDeployModeTest() {
2019-01-22 10:25:42 +02:00
stepRule.step.neoDeploy(script: nullScript,
neo: [
application: 'testApp',
runtime: 'neo-javaee6-wp',
runtimeVersion: '2.125',
size: 'lite',
],
deployMode: 'warParams',
warAction: 'deploy',
source: warArchiveName)
2019-01-22 10:19:28 +02:00
Assert.assertThat(shellRule.shell,
2019-03-07 13:53:25 +02:00
new CommandLineMatcher().hasProlog("neo.sh deploy")
.hasSingleQuotedOption('host', 'test\\.deploy\\.host\\.com')
.hasSingleQuotedOption('account', 'trialuser123')
.hasSingleQuotedOption('application', 'testApp')
.hasSingleQuotedOption('runtime', 'neo-javaee6-wp')
.hasSingleQuotedOption('runtime-version', '2\\.125')
.hasSingleQuotedOption('size', 'lite')
.hasSingleQuotedOption('user', 'defaultUser')
.hasSingleQuotedOption('password', '\\*\\*\\*\\*\\*\\*\\*\\*')
.hasSingleQuotedOption('source', '.*\\.war'))
}
@Test
void warFileParamsDeployModeRollingUpdateTest() {
shellRule.setReturnValue(JenkinsShellCallRule.Type.REGEX, '.* status .*', 'Status: STARTED')
stepRule.step.neoDeploy(script: nullScript,
source: warArchiveName,
deployMode: 'warParams',
warAction: 'rolling-update',
neo: [
application: 'testApp',
runtime: 'neo-javaee6-wp',
runtimeVersion: '2.125',
size: 'lite'
]
)
Assert.assertThat(shellRule.shell,
2019-03-07 13:53:25 +02:00
new CommandLineMatcher().hasProlog("neo.sh rolling-update")
.hasSingleQuotedOption('host', 'test\\.deploy\\.host\\.com')
.hasSingleQuotedOption('account', 'trialuser123')
.hasSingleQuotedOption('application', 'testApp')
.hasSingleQuotedOption('runtime', 'neo-javaee6-wp')
.hasSingleQuotedOption('runtime-version', '2\\.125')
.hasSingleQuotedOption('size', 'lite')
.hasSingleQuotedOption('user', 'defaultUser')
.hasSingleQuotedOption('password', '\\*\\*\\*\\*\\*\\*\\*\\*')
.hasSingleQuotedOption('source', '.*\\.war'))
}
@Test
void warFirstTimeRollingUpdateTest() {
shellRule.setReturnValue(JenkinsShellCallRule.Type.REGEX, '.* status .*', 'ERROR: Application [testApp] not found')
stepRule.step.neoDeploy(script: nullScript,
source: warArchiveName,
deployMode: 'warParams',
warAction: 'rolling-update',
neo: [
application: 'testApp',
runtime: 'neo-javaee6-wp',
runtimeVersion: '2.125'
]
)
Assert.assertThat(shellRule.shell,
new CommandLineMatcher()
2019-03-07 13:53:25 +02:00
.hasProlog("neo.sh deploy")
.hasSingleQuotedOption('application', 'testApp'))
}
void warNotStartedRollingUpdateTest() {
shellRule.setReturnValue(JenkinsShellCallRule.Type.REGEX, '.* status .*', 'Status: STOPPED')
2019-01-22 10:25:42 +02:00
stepRule.step.neoDeploy(script: nullScript,
source: warArchiveName,
deployMode: 'warParams',
warAction: 'rolling-update',
neo: [
application: 'testApp',
runtime: 'neo-javaee6-wp',
runtimeVersion: '2.125'
]
)
2019-01-22 10:19:28 +02:00
Assert.assertThat(shellRule.shell,
new CommandLineMatcher()
.hasProlog("\"/opt/neo/tools/neo.sh\" deploy")
.hasSingleQuotedOption('application', 'testApp'))
}
@Test
void showLogsOnFailingDeployment() {
thrown.expect(Exception)
shellRule.failExecution(Type.REGEX, '.* deploy .*')
stepRule.step.neoDeploy(script: nullScript,
source: warArchiveName,
deployMode: 'warParams',
warAction: 'deploy',
neo: [
application: 'testApp',
runtime: 'neo-javaee6-wp',
runtimeVersion: '2.125'
]
)
Assert.assertThat(shellRule.shell,
new CommandLineMatcher().hasProlog("cat /var/log/neo/*"))
}
@Test
void warPropertiesFileDeployModeTest() {
2019-01-22 10:25:42 +02:00
stepRule.step.neoDeploy(script: nullScript,
source: warArchiveName,
deployMode: 'warPropertiesFile',
warAction: 'deploy',
neo: [
2019-03-26 15:16:01 +02:00
propertiesFile: warPropertiesFileName,
application: 'testApp',
runtime: 'neo-javaee6-wp',
runtimeVersion: '2.125',
size: 'lite'
]
)
2019-01-22 10:19:28 +02:00
Assert.assertThat(shellRule.shell,
2019-03-07 13:53:25 +02:00
new CommandLineMatcher().hasProlog("neo.sh deploy")
2019-03-26 15:16:01 +02:00
.hasArgument('war.properties')
.hasSingleQuotedOption('user', 'defaultUser')
.hasSingleQuotedOption('password', '\\*\\*\\*\\*\\*\\*\\*\\*')
.hasSingleQuotedOption('source', '.*\\.war'))
}
@Test
void warPropertiesFileDeployModeRollingUpdateTest() {
shellRule.setReturnValue(JenkinsShellCallRule.Type.REGEX, '.* status .*', 'Status: STARTED')
2019-01-22 10:25:42 +02:00
stepRule.step.neoDeploy(script: nullScript,
source: warArchiveName,
deployMode: 'warPropertiesFile',
warAction: 'rolling-update',
neo: [
2019-03-26 15:16:01 +02:00
propertiesFile: warPropertiesFileName,
application: 'testApp',
runtime: 'neo-javaee6-wp',
runtimeVersion: '2.125',
size: 'lite'
])
2019-01-22 10:19:28 +02:00
Assert.assertThat(shellRule.shell,
2019-03-07 13:53:25 +02:00
new CommandLineMatcher().hasProlog("neo.sh rolling-update")
2019-03-26 15:16:01 +02:00
.hasArgument('war.properties')
.hasSingleQuotedOption('user', 'defaultUser')
.hasSingleQuotedOption('password', '\\*\\*\\*\\*\\*\\*\\*\\*')
.hasSingleQuotedOption('source', '.*\\.war'))
}
@Test
void illegalDeployModeTest() {
thrown.expect(Exception)
thrown.expectMessage("Invalid deployMode = 'illegalMode'. Valid 'deployMode' values are: [mta, warParams, warPropertiesFile].")
2019-01-22 10:25:42 +02:00
stepRule.step.neoDeploy(script: nullScript,
source: warArchiveName,
deployMode: 'illegalMode',
warAction: 'deploy',
neo: [
application: 'testApp',
runtime: 'neo-javaee6-wp',
runtimeVersion: '2.125',
size: 'lite'
])
}
@Test
void illegalWARActionTest() {
thrown.expect(Exception)
thrown.expectMessage("Invalid warAction = 'illegalWARAction'. Valid 'warAction' values are: [deploy, rolling-update].")
2019-01-22 10:25:42 +02:00
stepRule.step.neoDeploy(script: nullScript,
source: warArchiveName,
deployMode: 'warParams',
warAction: 'illegalWARAction',
neo: [
application: 'testApp',
runtime: 'neo-javaee6-wp',
runtimeVersion: '2.125',
size: 'lite'
])
2017-07-11 15:12:03 +02:00
}
@Test
void dontSwallowExceptionWhenUnableToProvideLogsTest() {
thrown.expect(AbortException)
thrown.expectMessage('Something went wrong during neo deployment')
thrown.expect(new BaseMatcher() {
def expectedException = AbortException
def expectedText = 'Cannot provide logs.'
boolean matches(def ex) {
def suppressed = ex.getSuppressed()
return (suppressed.size() == 1 &&
suppressed[0] in expectedException &&
suppressed[0].message == expectedText)
}
void describeTo(Description d) {
d.appendText(" a suppressed ${expectedException} with message ${expectedText}.")
}
})
loggingRule.expect('Unable to provide the logs.')
helper.registerAllowedMethod('fileExists', [String],
{ f ->
f == 'archive.mtar'
}
)
helper.registerAllowedMethod('sh', [Map],
{ m ->
if(m.script.toString().contains('neo.sh deploy-mta'))
throw new AbortException('Something went wrong during neo deployment.')
}
)
helper.registerAllowedMethod("sh", [String],
{ cmd ->
if (cmd == 'cat logs/neo/*')
throw new AbortException('Cannot provide logs.')
}
)
stepRule.step.neoDeploy(script: nullScript,
source: archiveName,
neo:[credentialsId: 'myCredentialsId'],
deployMode: 'mta',
utils: utils,
)
}
2017-07-11 15:12:03 +02:00
}