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

499 lines
16 KiB
Groovy
Raw Normal View History

import com.sap.piper.Utils
2017-07-11 15:12:03 +02:00
import hudson.AbortException
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.BeforeClass
import org.junit.ClassRule
2017-07-11 15:12:03 +02:00
import org.junit.Rule
import org.junit.Test
import org.junit.rules.ExpectedException
import org.junit.rules.RuleChain
import org.junit.rules.TemporaryFolder
import util.BasePiperTest
import util.CommandLineMatcher
import util.JenkinsCredentialsRule
import util.JenkinsLockRule
import util.JenkinsLoggingRule
import util.JenkinsPropertiesRule
import util.JenkinsReadYamlRule
2018-01-16 15:03:00 +01:00
import util.JenkinsShellCallRule
import util.JenkinsShellCallRule.Type
2018-02-28 13:11:09 +01:00
import util.JenkinsStepRule
import util.JenkinsWithEnvRule
import util.Rules
2017-07-11 15:12:03 +02:00
class NeoDeployTest extends BasePiperTest {
2018-02-20 13:10:14 +01:00
def toolJavaValidateCalled = false
2018-02-12 11:03:07 +01:00
@ClassRule
public static TemporaryFolder tmp = new TemporaryFolder()
private ExpectedException thrown = new ExpectedException().none()
private JenkinsLoggingRule loggingRule = new JenkinsLoggingRule(this)
2019-01-22 09:19:28 +01:00
private JenkinsShellCallRule shellRule = new JenkinsShellCallRule(this)
2019-01-22 09:25:42 +01:00
private JenkinsStepRule stepRule = new JenkinsStepRule(this)
private JenkinsLockRule lockRule = new JenkinsLockRule(this)
2017-07-11 15:12:03 +02:00
@Rule
2018-02-28 13:11:09 +01:00
public RuleChain ruleChain = Rules
.getCommonRules(this)
.around(new JenkinsReadYamlRule(this))
.around(new JenkinsPropertiesRule(this, propertiesFileName, configProperties))
2018-02-28 13:11:09 +01:00
.around(thrown)
.around(loggingRule)
2019-01-22 09:19:28 +01:00
.around(shellRule)
.around(new JenkinsCredentialsRule(this)
.withCredentials('myCredentialsId', 'anonymous', '********')
.withCredentials('CI_CREDENTIALS_ID', 'defaultUser', '********'))
2019-01-22 09:25:42 +01:00
.around(stepRule)
.around(lockRule)
.around(new JenkinsWithEnvRule(this))
2018-02-12 11:03:07 +01:00
private static workspacePath
private static warArchiveName
private static propertiesFileName
private static archiveName
private static configProperties
2017-07-11 15:12:03 +02:00
2018-02-12 11:03:07 +01:00
@BeforeClass
static void createTestFiles() {
2017-07-11 15:12:03 +02:00
2018-02-12 10:52:13 +01:00
workspacePath = "${tmp.getRoot()}"
warArchiveName = 'warArchive.war'
propertiesFileName = 'config.properties'
2018-02-12 11:03:07 +01:00
archiveName = 'archive.mtar'
configProperties = new Properties()
configProperties.put('account', 'trialuser123')
configProperties.put('host', 'test.deploy.host.com')
configProperties.put('application', 'testApp')
2018-02-13 15:06:09 +01:00
tmp.newFile(warArchiveName) << 'dummy war archive'
tmp.newFile(propertiesFileName) << 'dummy properties file'
tmp.newFile(archiveName) << 'dummy archive'
2018-02-12 11:03:07 +01:00
}
@Before
void init() {
2017-07-11 15:12:03 +02:00
2017-12-13 10:05:19 +01:00
helper.registerAllowedMethod('dockerExecute', [Map, Closure], null)
helper.registerAllowedMethod('fileExists', [String], { s -> return new File(workspacePath, s).exists() })
helper.registerAllowedMethod('pwd', [], { return workspacePath })
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() {
boolean notifyOldConfigFrameworkUsed = true
def utils = new Utils() {
void pushToSWA(Map parameters, Map config) {
notifyOldConfigFrameworkUsed = parameters.stepParam4
}
}
2019-01-22 09:25:42 +01:00
stepRule.step.neoDeploy(script: nullScript,
source: archiveName,
neo:[credentialsId: 'myCredentialsId'],
utils: utils,
2018-02-28 13:11:09 +01:00
)
2019-01-22 09:19:28 +01:00
Assert.assertThat(shellRule.shell,
2019-03-07 12:53:25 +01: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', '.*'))
assert !notifyOldConfigFrameworkUsed
}
@Test
void straightForwardTestConfigViaConfiguration() {
nullScript.commonPipelineEnvironment.configuration = [steps: [
neoDeploy: [
neo: [
host: 'configuration-frwk.deploy.host.com',
account: 'configurationFrwkUser123'
],
source: archiveName
]
]]
2019-01-22 09:25:42 +01:00
stepRule.step.neoDeploy(script: nullScript,
neo:[credentialsId: 'myCredentialsId']
)
2019-01-22 09:19:28 +01:00
Assert.assertThat(shellRule.shell,
2019-03-07 12:53:25 +01: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() {
nullScript.commonPipelineEnvironment.setMtarFilePath('archive.mtar')
2019-01-22 09:25:42 +01:00
stepRule.step.neoDeploy(script: nullScript)
2018-08-17 12:52:01 +02:00
2019-01-22 09:19:28 +01:00
Assert.assertThat(shellRule.shell,
2019-03-07 12:53:25 +01:00
new CommandLineMatcher().hasProlog("neo.sh deploy-mta")
.hasSingleQuotedOption('source', 'archive.mtar'))
2018-08-17 12:52:01 +02:00
}
@Test
void archivePathFromParamsHasHigherPrecedenceThanCPETest() {
nullScript.commonPipelineEnvironment.setMtarFilePath('archive2.mtar')
2019-01-22 09:25:42 +01:00
stepRule.step.neoDeploy(script: nullScript,
source: "archive.mtar")
2018-08-17 12:52:01 +02:00
2019-01-22 09:19:28 +01:00
Assert.assertThat(shellRule.shell,
2019-03-07 12:53:25 +01: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 09:25:42 +01: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 09:25:42 +01:00
stepRule.step.neoDeploy(script: nullScript,
source: archiveName
)
2017-07-11 15:12:03 +02:00
2019-01-22 09:19:28 +01:00
Assert.assertThat(shellRule.shell,
2019-03-07 12:53:25 +01: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 archiveNotProvidedTest() {
thrown.expect(Exception)
thrown.expectMessage('ERROR - NO VALUE AVAILABLE FOR source')
2017-07-11 15:12:03 +02:00
2019-01-22 09:25:42 +01:00
stepRule.step.neoDeploy(script: nullScript)
2017-07-11 15:12:03 +02:00
}
@Test
void wrongArchivePathProvidedTest() {
thrown.expect(AbortException)
thrown.expectMessage('File wrongArchiveName cannot be found')
2019-01-22 09:25:42 +01:00
stepRule.step.neoDeploy(script: nullScript,
source: 'wrongArchiveName')
2017-07-11 15:12:03 +02:00
}
@Test
void scriptNotProvidedTest() {
thrown.expect(Exception)
thrown.expectMessage('ERROR - NO VALUE AVAILABLE FOR neo/host')
2017-07-11 15:12:03 +02:00
nullScript.commonPipelineEnvironment.configuration = [:]
stepRule.step.neoDeploy(script: nullScript, source: archiveName)
2017-07-11 15:12:03 +02:00
}
@Test
void mtaDeployModeTest() {
stepRule.step.neoDeploy(script: nullScript, source: archiveName, deployMode: 'mta')
2019-01-22 09:19:28 +01:00
Assert.assertThat(shellRule.shell,
2019-03-07 12:53:25 +01: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 09:25:42 +01: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 09:19:28 +01:00
Assert.assertThat(shellRule.shell,
2019-03-07 12:53:25 +01: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 12:53:25 +01: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 12:53:25 +01:00
.hasProlog("neo.sh deploy")
.hasSingleQuotedOption('application', 'testApp'))
}
void warNotStartedRollingUpdateTest() {
shellRule.setReturnValue(JenkinsShellCallRule.Type.REGEX, '.* status .*', 'Status: STOPPED')
2019-01-22 09:25:42 +01: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 09:19:28 +01: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 09:25:42 +01:00
stepRule.step.neoDeploy(script: nullScript,
source: warArchiveName,
deployMode: 'warPropertiesFile',
warAction: 'deploy',
neo: [
propertiesFile: propertiesFileName,
application: 'testApp',
runtime: 'neo-javaee6-wp',
runtimeVersion: '2.125',
size: 'lite'
]
)
2019-01-22 09:19:28 +01:00
Assert.assertThat(shellRule.shell,
2019-03-07 12:53:25 +01:00
new CommandLineMatcher().hasProlog("neo.sh deploy")
.hasArgument("config.properties")
.hasSingleQuotedOption('user', 'defaultUser')
.hasSingleQuotedOption('password', '\\*\\*\\*\\*\\*\\*\\*\\*')
.hasSingleQuotedOption('source', '.*\\.war'))
}
@Test
void warPropertiesFileDeployModeRollingUpdateTest() {
shellRule.setReturnValue(JenkinsShellCallRule.Type.REGEX, '.* status .*', 'Status: STARTED')
2019-01-22 09:25:42 +01:00
stepRule.step.neoDeploy(script: nullScript,
source: warArchiveName,
deployMode: 'warPropertiesFile',
warAction: 'rolling-update',
neo: [
propertiesFile: propertiesFileName,
application: 'testApp',
runtime: 'neo-javaee6-wp',
runtimeVersion: '2.125',
size: 'lite'
])
2019-01-22 09:19:28 +01:00
Assert.assertThat(shellRule.shell,
2019-03-07 12:53:25 +01:00
new CommandLineMatcher().hasProlog("neo.sh rolling-update")
.hasArgument('config.properties')
.hasSingleQuotedOption('user', 'defaultUser')
.hasSingleQuotedOption('password', '\\*\\*\\*\\*\\*\\*\\*\\*')
.hasSingleQuotedOption('source', '.*\\.war'))
}
@Test
void applicationNameNotProvidedTest() {
thrown.expect(Exception)
thrown.expectMessage('ERROR - NO VALUE AVAILABLE FOR neo/application')
2019-01-22 09:25:42 +01:00
stepRule.step.neoDeploy(script: nullScript,
source: warArchiveName,
deployMode: 'warParams',
neo: [
runtime: 'neo-javaee6-wp',
runtimeVersion: '2.125'
]
)
}
@Test
void runtimeNotProvidedTest() {
thrown.expect(Exception)
thrown.expectMessage('ERROR - NO VALUE AVAILABLE FOR neo/runtime')
2019-01-22 09:25:42 +01:00
stepRule.step.neoDeploy(script: nullScript,
source: warArchiveName,
neo: [
application: 'testApp',
runtimeVersion: '2.125'
],
deployMode: 'warParams')
}
@Test
void runtimeVersionNotProvidedTest() {
thrown.expect(Exception)
thrown.expectMessage('ERROR - NO VALUE AVAILABLE FOR neo/runtimeVersion')
2019-01-22 09:25:42 +01:00
stepRule.step.neoDeploy(script: nullScript,
source: warArchiveName,
neo: [
application: 'testApp',
runtime: 'neo-javaee6-wp'
],
deployMode: 'warParams')
}
@Test
void illegalDeployModeTest() {
thrown.expect(Exception)
thrown.expectMessage("Invalid deployMode = 'illegalMode'. Valid 'deployMode' values are: [mta, warParams, warPropertiesFile].")
2019-01-22 09:25:42 +01: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 09:25:42 +01: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
}
}