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

496 lines
18 KiB
Groovy
Raw Normal View History

2017-07-11 15:12:03 +02:00
import hudson.AbortException
2017-07-11 15:12:03 +02:00
import org.junit.rules.TemporaryFolder
import com.lesfurets.jenkins.unit.BasePipelineTest
2018-02-12 11:03:07 +01:00
import org.junit.BeforeClass
import org.junit.ClassRule
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.JenkinsLoggingRule
2018-01-16 15:03:00 +01:00
import util.JenkinsShellCallRule
2018-02-28 13:11:09 +01:00
import util.JenkinsStepRule
import util.JenkinsEnvironmentRule
import util.Rules
2017-07-11 15:12:03 +02:00
2018-02-28 13:11:09 +01:00
class NeoDeployTest extends BasePipelineTest {
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 jlr = new JenkinsLoggingRule(this)
2018-01-16 15:03:00 +01:00
private JenkinsShellCallRule jscr = new JenkinsShellCallRule(this)
2018-02-28 13:11:09 +01:00
private JenkinsStepRule jsr = new JenkinsStepRule(this)
private JenkinsEnvironmentRule jer = new JenkinsEnvironmentRule(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(thrown)
.around(jlr)
.around(jscr)
.around(jsr)
.around(jer)
def toolNeoValidateCalled = false
2018-02-12 11:03:07 +01:00
private static workspacePath
private static warArchiveName
private static propertiesFileName
private static archiveName
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'
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() })
2017-07-11 15:12:03 +02:00
helper.registerAllowedMethod('usernamePassword', [Map], { m -> return m })
helper.registerAllowedMethod('withCredentials', [List, Closure], { l, c ->
if(l[0].credentialsId == 'myCredentialsId') {
binding.setProperty('username', 'anonymous')
binding.setProperty('password', '********')
} else if(l[0].credentialsId == 'CI_CREDENTIALS_ID') {
binding.setProperty('username', 'defaultUser')
binding.setProperty('password', '********')
}
try {
c()
} finally {
binding.setProperty('username', null)
binding.setProperty('password', null)
}
})
2018-02-12 13:44:02 +01:00
binding.setVariable('env', ['NEO_HOME':'/opt/neo'])
2017-07-11 15:12:03 +02:00
2018-02-28 13:11:09 +01:00
jer.env.configuration = [steps:[neoDeploy: [host: 'test.deploy.host.com', account: 'trialuser123']]]
//
// needs to be after loading the scripts. Here we have a different behaviour
// for usual steps and for steps contained in the shared lib itself.
//
// toolValidate mocked here since we are not interested in testing
// toolValidate here. This is expected to be done in a test class for
// toolValidate.
//
helper.registerAllowedMethod('toolValidate', [Map], { m ->
if(m.tool == 'neo')
toolNeoValidateCalled = true
if(m.tool == 'java')
toolJavaValidateCalled = true
})
2017-07-11 15:12:03 +02:00
}
@Test
void straightForwardTestConfigViaConfigProperties() {
2017-07-11 15:12:03 +02:00
2018-02-28 13:11:09 +01:00
jer.env.setConfigProperty('DEPLOY_HOST', 'test.deploy.host.com')
jer.env.setConfigProperty('CI_DEPLOY_ACCOUNT', 'trialuser123')
jer.env.configuration = [:]
2018-02-28 13:11:09 +01:00
jsr.step.call(script: [commonPipelineEnvironment: jer.env],
archivePath: archiveName,
neoCredentialsId: 'myCredentialsId'
)
2017-07-11 15:12:03 +02:00
assert jscr.shell.find { c -> c =~ /#!\/bin\/bash "\/opt\/neo\/tools\/neo\.sh" deploy-mta --host 'test\.deploy\.host\.com' --account 'trialuser123' --synchronous --user 'anonymous' --password '\*\*\*\*\*\*\*\*' --source ".*"/}
2017-07-11 15:12:03 +02:00
}
@Test
void straightForwardTestConfigViaConfiguration() {
2018-02-28 13:11:09 +01:00
jsr.step.call(script: [commonPipelineEnvironment: jer.env],
archivePath: archiveName,
neoCredentialsId: 'myCredentialsId'
2018-02-28 13:11:09 +01:00
)
assert jscr.shell.find { c -> c =~ /#!\/bin\/bash "\/opt\/neo\/tools\/neo\.sh" deploy-mta --host 'test\.deploy\.host\.com' --account 'trialuser123' --synchronous --user 'anonymous' --password '\*\*\*\*\*\*\*\*' --source ".*"/}
}
@Test
void straightForwardTestConfigViaConfigurationAndViaConfigProperties() {
2018-02-28 13:11:09 +01:00
jer.env.setConfigProperty('DEPLOY_HOST', 'configProperties.deploy.host.com')
jer.env.setConfigProperty('CI_DEPLOY_ACCOUNT', 'configPropsUser123')
2018-02-28 13:11:09 +01:00
jer.env.configuration = [steps:[neoDeploy: [host: 'configuration-frwk.deploy.host.com',
account: 'configurationFrwkUser123']]]
2018-02-28 13:11:09 +01:00
jsr.step.call(script: [commonPipelineEnvironment: jer.env],
archivePath: archiveName,
neoCredentialsId: 'myCredentialsId'
)
assert jscr.shell.find { c -> c =~ /#!\/bin\/bash "\/opt\/neo\/tools\/neo\.sh" deploy-mta --host 'configuration-frwk\.deploy\.host\.com' --account 'configurationFrwkUser123' --synchronous --user 'anonymous' --password '\*\*\*\*\*\*\*\*' --source ".*"/}
}
2017-07-11 15:12:03 +02:00
@Test
void badCredentialsIdTest() {
thrown.expect(MissingPropertyException)
thrown.expectMessage('No such property: username')
2017-07-11 15:12:03 +02:00
2018-02-28 13:11:09 +01:00
jsr.step.call(script: [commonPipelineEnvironment: jer.env],
archivePath: archiveName,
neoCredentialsId: 'badCredentialsId'
)
2017-07-11 15:12:03 +02:00
}
@Test
void credentialsIdNotProvidedTest() {
2018-02-28 13:11:09 +01:00
jsr.step.call(script: [commonPipelineEnvironment: jer.env],
archivePath: archiveName
)
2017-07-11 15:12:03 +02:00
assert jscr.shell.find { c -> c =~ /#!\/bin\/bash "\/opt\/neo\/tools\/neo\.sh" deploy-mta --host 'test\.deploy\.host\.com' --account 'trialuser123' --synchronous --user 'defaultUser' --password '\*\*\*\*\*\*\*\*' --source ".*"/ }
2017-07-11 15:12:03 +02:00
}
@Test
void neoHomeNotSetTest() {
2018-02-12 13:44:02 +01:00
binding.setVariable('env', [:])
2018-02-28 13:11:09 +01:00
jsr.step.call(script: [commonPipelineEnvironment: jer.env],
archivePath: archiveName
)
2017-07-11 15:12:03 +02:00
assert jscr.shell.find { c -> c.contains('which neo.sh') }
assert jscr.shell.find { c -> c.contains('"neo.sh" deploy-mta') }
2018-02-12 15:21:59 +01:00
assert jlr.log.contains('Using Neo executable from PATH.')
2017-07-11 15:12:03 +02:00
}
@Test
void neoHomeAsParameterTest() {
2018-02-28 13:11:09 +01:00
jsr.step.call(script: [commonPipelineEnvironment: jer.env],
archivePath: archiveName,
neoCredentialsId: 'myCredentialsId',
neoHome: '/etc/neo'
)
2017-07-11 15:12:03 +02:00
assert jscr.shell.find{ c -> c = '"/etc/neo/tools/neo.sh" deploy-mta' }
2018-02-12 15:21:59 +01:00
assert jlr.log.contains('[neoDeploy] Neo executable "/etc/neo/tools/neo.sh" retrieved from configuration.')
2017-07-11 15:12:03 +02:00
}
2018-02-12 15:14:13 +01:00
@Test
void neoHomeFromEnvironmentTest() {
2018-02-28 13:11:09 +01:00
jsr.step.call(script: [commonPipelineEnvironment: jer.env],
2018-02-12 15:14:13 +01:00
archivePath: archiveName
)
assert jscr.shell.find { c -> c.contains('"/opt/neo/tools/neo.sh" deploy-mta')}
2018-02-12 15:14:13 +01:00
assert jlr.log.contains('[neoDeploy] Neo executable "/opt/neo/tools/neo.sh" retrieved from environment.')
}
2018-02-16 17:28:17 +01:00
@Test
void neoHomeFromCustomStepConfigurationTest() {
2018-02-28 13:11:09 +01:00
jer.env.configuration = [steps:[neoDeploy: [host: 'test.deploy.host.com', account: 'trialuser123', neoHome: '/step/neo']]]
2018-02-16 17:28:17 +01:00
2018-02-28 13:11:09 +01:00
jsr.step.call(script: [commonPipelineEnvironment: jer.env],
2018-02-16 17:28:17 +01:00
archivePath: archiveName
)
assert jscr.shell.find { c -> c = '"/step/neo/tools/neo.sh" deploy-mta'}
2018-02-16 17:28:17 +01:00
assert jlr.log.contains('[neoDeploy] Neo executable "/step/neo/tools/neo.sh" retrieved from configuration.')
}
2017-07-11 15:12:03 +02:00
@Test
void archiveNotProvidedTest() {
thrown.expect(Exception)
thrown.expectMessage('Archive path not configured (parameter "archivePath").')
2017-07-11 15:12:03 +02:00
2018-02-28 13:11:09 +01:00
jsr.step.call(script: [commonPipelineEnvironment: jer.env])
2017-07-11 15:12:03 +02:00
}
@Test
void wrongArchivePathProvidedTest() {
thrown.expect(AbortException)
2018-02-13 15:06:09 +01:00
thrown.expectMessage('Archive cannot be found')
2017-07-11 15:12:03 +02:00
2018-02-28 13:11:09 +01:00
jsr.step.call(script: [commonPipelineEnvironment: jer.env],
2018-02-12 11:03:07 +01:00
archivePath: 'wrongArchiveName')
2017-07-11 15:12:03 +02:00
}
@Test
void scriptNotProvidedTest() {
thrown.expect(Exception)
thrown.expectMessage('ERROR - NO VALUE AVAILABLE FOR host')
2017-07-11 15:12:03 +02:00
2018-02-28 13:11:09 +01:00
jer.env.configuration = [:]
2018-02-28 13:11:09 +01:00
jsr.step.call(archivePath: archiveName)
2017-07-11 15:12:03 +02:00
}
@Test
void mtaDeployModeTest() {
2018-02-28 13:11:09 +01:00
jsr.step.call(script: [commonPipelineEnvironment: jer.env], archivePath: archiveName, deployMode: 'mta')
assert jscr.shell.find { c -> c =~ /#!\/bin\/bash "\/opt\/neo\/tools\/neo\.sh" deploy-mta --host 'test\.deploy\.host\.com' --account 'trialuser123' --synchronous --user 'defaultUser' --password '\*\*\*\*\*\*\*\*' --source ".*"/}
}
@Test
void warFileParamsDeployModeTest() {
2018-02-28 13:11:09 +01:00
jsr.step.call(script: [commonPipelineEnvironment: jer.env],
applicationName: 'testApp',
runtime: 'neo-javaee6-wp',
runtimeVersion: '2.125',
deployMode: 'warParams',
vmSize: 'lite',
warAction: 'deploy',
archivePath: warArchiveName)
assert jscr.shell.find { c -> c =~ /#!\/bin\/bash "\/opt\/neo\/tools\/neo\.sh" deploy --host 'test\.deploy\.host\.com' --account 'trialuser123' --application 'testApp' --runtime 'neo-javaee6-wp' --runtime-version '2\.125' --size 'lite' --user 'defaultUser' --password '\*\*\*\*\*\*\*\*' --source ".*\.war"/}
}
@Test
void warFileParamsDeployModeRollingUpdateTest() {
2018-02-28 13:11:09 +01:00
jsr.step.call(script: [commonPipelineEnvironment: jer.env],
archivePath: warArchiveName,
deployMode: 'warParams',
applicationName: 'testApp',
runtime: 'neo-javaee6-wp',
runtimeVersion: '2.125',
warAction: 'rolling-update',
vmSize: 'lite')
assert jscr.shell.find { c -> c =~ /#!\/bin\/bash "\/opt\/neo\/tools\/neo\.sh" rolling-update --host 'test\.deploy\.host\.com' --account 'trialuser123' --application 'testApp' --runtime 'neo-javaee6-wp' --runtime-version '2\.125' --size 'lite' --user 'defaultUser' --password '\*\*\*\*\*\*\*\*' --source ".*\.war"/}
}
@Test
void warPropertiesFileDeployModeTest() {
2018-02-28 13:11:09 +01:00
jsr.step.call(script: [commonPipelineEnvironment: jer.env],
archivePath: warArchiveName,
deployMode: 'warPropertiesFile',
propertiesFile: propertiesFileName,
applicationName: 'testApp',
runtime: 'neo-javaee6-wp',
runtimeVersion: '2.125',
warAction: 'deploy',
vmSize: 'lite')
assert jscr.shell.find { c -> c =~ /#!\/bin\/bash "\/opt\/neo\/tools\/neo\.sh" deploy .*\.properties --user 'defaultUser' --password '\*\*\*\*\*\*\*\*' --source ".*\.war"/}
}
@Test
void warPropertiesFileDeployModeRollingUpdateTest() {
2018-02-28 13:11:09 +01:00
jsr.step.call(script: [commonPipelineEnvironment: jer.env],
archivePath: warArchiveName,
deployMode: 'warPropertiesFile',
propertiesFile: propertiesFileName,
applicationName: 'testApp',
runtime: 'neo-javaee6-wp',
runtimeVersion: '2.125',
warAction: 'rolling-update',
vmSize: 'lite')
assert jscr.shell.find { c -> c =~ /#!\/bin\/bash "\/opt\/neo\/tools\/neo\.sh" rolling-update .*\.properties --user 'defaultUser' --password '\*\*\*\*\*\*\*\*' --source ".*\.war"/}
}
@Test
void applicationNameNotProvidedTest() {
thrown.expect(Exception)
thrown.expectMessage('ERROR - NO VALUE AVAILABLE FOR applicationName')
2018-02-28 13:11:09 +01:00
jsr.step.call(script: [commonPipelineEnvironment: jer.env],
archivePath: warArchiveName,
deployMode: 'warParams',
runtime: 'neo-javaee6-wp',
runtimeVersion: '2.125'
)
}
@Test
void runtimeNotProvidedTest() {
thrown.expect(Exception)
thrown.expectMessage('ERROR - NO VALUE AVAILABLE FOR runtime')
2018-02-28 13:11:09 +01:00
jsr.step.call(script: [commonPipelineEnvironment: jer.env],
archivePath: warArchiveName,
applicationName: 'testApp',
deployMode: 'warParams',
runtimeVersion: '2.125')
}
@Test
void runtimeVersionNotProvidedTest() {
thrown.expect(Exception)
thrown.expectMessage('ERROR - NO VALUE AVAILABLE FOR runtimeVersion')
2018-02-28 13:11:09 +01:00
jsr.step.call(script: [commonPipelineEnvironment: jer.env],
archivePath: warArchiveName,
applicationName: 'testApp',
deployMode: 'warParams',
runtime: 'neo-javaee6-wp')
}
@Test
void illegalDeployModeTest() {
thrown.expect(Exception)
thrown.expectMessage("[neoDeploy] Invalid deployMode = 'illegalMode'. Valid 'deployMode' values are: 'mta', 'warParams' and 'warPropertiesFile'")
2018-02-28 13:11:09 +01:00
jsr.step.call(script: [commonPipelineEnvironment: jer.env],
archivePath: warArchiveName,
deployMode: 'illegalMode',
applicationName: 'testApp',
runtime: 'neo-javaee6-wp',
runtimeVersion: '2.125',
warAction: 'deploy',
vmSize: 'lite')
}
@Test
void illegalVMSizeTest() {
thrown.expect(Exception)
thrown.expectMessage("[neoDeploy] Invalid vmSize = 'illegalVM'. Valid 'vmSize' values are: 'lite', 'pro', 'prem' and 'prem-plus'.")
2018-02-28 13:11:09 +01:00
jsr.step.call(script: [commonPipelineEnvironment: jer.env],
archivePath: warArchiveName,
deployMode: 'warParams',
applicationName: 'testApp',
runtime: 'neo-javaee6-wp',
runtimeVersion: '2.125',
warAction: 'deploy',
vmSize: 'illegalVM')
}
@Test
void illegalWARActionTest() {
thrown.expect(Exception)
thrown.expectMessage("[neoDeploy] Invalid warAction = 'illegalWARAction'. Valid 'warAction' values are: 'deploy' and 'rolling-update'.")
2018-02-28 13:11:09 +01:00
jsr.step.call(script: [commonPipelineEnvironment: jer.env],
archivePath: warArchiveName,
deployMode: 'warParams',
applicationName: 'testApp',
runtime: 'neo-javaee6-wp',
runtimeVersion: '2.125',
warAction: 'illegalWARAction',
vmSize: 'lite')
2017-07-11 15:12:03 +02:00
}
@Test
void deployHostProvidedAsDeprecatedParameterTest() {
2018-02-12 11:03:07 +01:00
2018-02-28 13:11:09 +01:00
jer.env.setConfigProperty('CI_DEPLOY_ACCOUNT', 'configPropsUser123')
2018-02-28 13:11:09 +01:00
jsr.step.call(script: [commonPipelineEnvironment: jer.env],
archivePath: archiveName,
deployHost: "my.deploy.host.com"
)
assert jlr.log.contains("[WARNING][neoDeploy] Deprecated parameter 'deployHost' is used. This will not work anymore in future versions. Use parameter 'host' instead.")
}
@Test
void deployAccountProvidedAsDeprecatedParameterTest() {
2018-02-12 11:03:07 +01:00
2018-02-28 13:11:09 +01:00
jer.env.setConfigProperty('CI_DEPLOY_ACCOUNT', 'configPropsUser123')
2018-02-28 13:11:09 +01:00
jsr.step.call(script: [commonPipelineEnvironment: jer.env],
archivePath: archiveName,
host: "my.deploy.host.com",
deployAccount: "myAccount"
)
assert jlr.log.contains("Deprecated parameter 'deployAccount' is used. This will not work anymore in future versions. Use parameter 'account' instead.")
}
@Test
void skipValidationWhenNeoToolsetIsInPathButNeoHomeNotProvidedViaConfigNorEnvironment() {
binding.setVariable('env', [:])
jscr.setReturnValue('which neo.sh', 0)
jsr.step.call(script: [commonPipelineEnvironment: jer.env],
archivePath: archiveName,
neoCredentialsId: 'myCredentialsId'
)
assert !toolNeoValidateCalled
}
@Test
void performValidationWhenNeoToolsetIsNotInPathAndNeoHomeNotProvidedViaConfigNorEnvironment() {
binding.setVariable('env', [:])
jscr.setReturnValue('which neo.sh', 1)
jsr.step.call(script: [commonPipelineEnvironment: jer.env],
archivePath: archiveName,
neoCredentialsId: 'myCredentialsId'
)
assert toolNeoValidateCalled
}
2018-02-20 13:10:14 +01:00
@Test
void toolJavaValidateCalled() {
jsr.step.call(script: [commonPipelineEnvironment: jer.env],
archivePath: archiveName,
neoCredentialsId: 'myCredentialsId')
assert toolJavaValidateCalled
}
@Test
void toolValidateSkippedIfJavaHomeNotSetButJavaInPath() {
jscr.setReturnValue('which java', 0)
jsr.step.envProps = [:] // make sure we are not confused by JAVA_HOME in current env props.
jsr.step.call(script: [commonPipelineEnvironment: jer.env],
archivePath: archiveName,
neoCredentialsId: 'myCredentialsId')
assert ! toolJavaValidateCalled
assert jlr.log.contains('Skipping tool validate check (java). Java executable in path, but no JAVA_HOME found.')
}
2017-07-11 15:12:03 +02:00
}