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/ToolValidateTest.groovy

271 lines
8.3 KiB
Groovy
Raw Normal View History

2017-07-11 15:12:03 +02:00
import org.apache.commons.exec.*
import hudson.AbortException
2018-02-12 16:55:37 +02:00
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.JenkinsLoggingRule
import util.JenkinsReadYamlRule
2018-02-28 14:11:09 +02:00
import util.JenkinsStepRule
import util.Rules
2017-07-11 15:12:03 +02:00
class ToolValidateTest extends BasePiperTest {
2018-02-12 16:55:37 +02:00
private ExpectedException thrown = new ExpectedException().none()
private JenkinsLoggingRule jlr = new JenkinsLoggingRule(this)
2018-02-28 14:11:09 +02:00
private JenkinsStepRule jsr = new JenkinsStepRule(this)
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))
2018-02-28 14:11:09 +02:00
.around(thrown)
.around(jlr)
.around(jsr)
2017-07-11 15:12:03 +02:00
2018-03-09 18:58:11 +02:00
def home = 'home'
2017-07-11 15:12:03 +02:00
@Test
void nullHomeTest() {
2018-03-09 18:58:11 +02:00
2017-07-11 15:12:03 +02:00
thrown.expect(IllegalArgumentException)
thrown.expectMessage("The parameter 'home' can not be null or empty.")
jsr.step.toolValidate(tool: 'java')
2017-07-11 15:12:03 +02:00
}
@Test
void emptyHomeTest() {
2018-03-09 18:58:11 +02:00
2017-07-11 15:12:03 +02:00
thrown.expect(IllegalArgumentException)
thrown.expectMessage("The parameter 'home' can not be null or empty.")
jsr.step.toolValidate(tool: 'java', home: '')
2017-07-11 15:12:03 +02:00
}
@Test
void nullToolTest() {
2018-03-09 18:58:11 +02:00
helper.registerAllowedMethod('sh', [Map], { Map m -> return 0 })
2017-07-11 15:12:03 +02:00
thrown.expect(IllegalArgumentException)
thrown.expectMessage("The parameter 'tool' can not be null or empty.")
jsr.step.toolValidate(tool: null, home: home)
2017-07-11 15:12:03 +02:00
}
@Test
void emptyToolTest() {
2018-03-09 18:58:11 +02:00
helper.registerAllowedMethod('sh', [Map], { Map m -> return 0 })
2017-07-11 15:12:03 +02:00
thrown.expect(IllegalArgumentException)
thrown.expectMessage("The parameter 'tool' can not be null or empty.")
jsr.step.toolValidate(tool: '', home: home)
2017-07-11 15:12:03 +02:00
}
@Test
void invalidToolTest() {
2018-03-09 18:58:11 +02:00
helper.registerAllowedMethod('sh', [Map], { Map m -> return 0 })
2017-07-11 15:12:03 +02:00
thrown.expect(AbortException)
thrown.expectMessage("The tool 'test' is not supported.")
jsr.step.toolValidate(tool: 'test', home: home)
2017-07-11 15:12:03 +02:00
}
@Test
void unableToValidateJavaTest() {
2018-03-09 18:58:11 +02:00
2017-07-11 15:12:03 +02:00
thrown.expect(AbortException)
thrown.expectMessage('The verification of Java failed.')
2017-07-11 15:12:03 +02:00
helper.registerAllowedMethod('sh', [Map], { Map m -> getNoVersion(m) })
jsr.step.toolValidate(tool: 'java', home: home)
2017-07-11 15:12:03 +02:00
}
@Test
void unableToValidateMtaTest() {
2018-03-09 18:58:11 +02:00
2017-07-11 15:12:03 +02:00
thrown.expect(AbortException)
thrown.expectMessage('The verification of SAP Multitarget Application Archive Builder failed.')
2017-07-11 15:12:03 +02:00
helper.registerAllowedMethod('sh', [Map], { Map m -> getNoVersion(m) })
jsr.step.toolValidate(tool: 'mta', home: home)
2017-07-11 15:12:03 +02:00
}
@Test
void unableToValidateNeoTest() {
2018-03-09 18:58:11 +02:00
2017-07-11 15:12:03 +02:00
thrown.expect(AbortException)
thrown.expectMessage('The verification of SAP Cloud Platform Console Client failed.')
2017-07-11 15:12:03 +02:00
helper.registerAllowedMethod('sh', [Map], { Map m -> getNoVersion(m) })
jsr.step.toolValidate(tool: 'neo', home: home)
2017-07-11 15:12:03 +02:00
}
@Test
void unableToValidateCmTest() {
2018-03-09 18:58:11 +02:00
2017-07-11 15:12:03 +02:00
thrown.expect(AbortException)
thrown.expectMessage('The verification of Change Management Command Line Interface failed.')
2017-07-11 15:12:03 +02:00
helper.registerAllowedMethod('sh', [Map], { Map m -> getNoVersion(m) })
jsr.step.toolValidate(tool: 'cm', home: home)
2017-07-11 15:12:03 +02:00
}
@Test
void validateIncompatibleVersionJavaTest() {
2018-03-09 18:58:11 +02:00
2017-07-11 15:12:03 +02:00
thrown.expect(AbortException)
thrown.expectMessage('The installed version of Java is 1.7.0.')
helper.registerAllowedMethod('sh', [Map], { Map m -> getIncompatibleVersion(m) })
jsr.step.toolValidate(tool: 'java', home: home)
2017-07-11 15:12:03 +02:00
}
@Test
void validateIncompatibleVersionMtaTest() {
2018-03-09 18:58:11 +02:00
2017-07-11 15:12:03 +02:00
thrown.expect(AbortException)
thrown.expectMessage('The installed version of SAP Multitarget Application Archive Builder is 1.0.5.')
helper.registerAllowedMethod('sh', [Map], { Map m -> getIncompatibleVersion(m) })
jsr.step.toolValidate(tool: 'mta', home: home)
2017-07-11 15:12:03 +02:00
}
@Test
void validateCmIncompatibleVersionTest() {
2018-03-09 18:58:11 +02:00
2017-07-11 15:12:03 +02:00
thrown.expect(AbortException)
thrown.expectMessage('The installed version of Change Management Command Line Interface is 0.0.0.')
helper.registerAllowedMethod('sh', [Map], { Map m -> getIncompatibleVersion(m) })
binding.setVariable('tool', 'cm')
jsr.step.toolValidate(tool: 'cm', home: home)
2017-07-11 15:12:03 +02:00
}
@Test
void validateJavaTest() {
2018-03-09 18:58:11 +02:00
2017-07-11 15:12:03 +02:00
helper.registerAllowedMethod('sh', [Map], { Map m -> getVersion(m) })
jsr.step.toolValidate(tool: 'java', home: home)
2017-07-11 15:12:03 +02:00
assert jlr.log.contains('Verifying Java version 1.8.0 or compatible version.')
assert jlr.log.contains('Java version 1.8.0 is installed.')
2017-07-11 15:12:03 +02:00
}
@Test
void validateMtaTest() {
2018-03-09 18:58:11 +02:00
2017-07-11 15:12:03 +02:00
helper.registerAllowedMethod('sh', [Map], { Map m -> getVersion(m) })
jsr.step.toolValidate(tool: 'mta', home: home)
2017-07-11 15:12:03 +02:00
assert jlr.log.contains('Verifying SAP Multitarget Application Archive Builder version 1.0.6 or compatible version.')
assert jlr.log.contains('SAP Multitarget Application Archive Builder version 1.0.6 is installed.')
2017-07-11 15:12:03 +02:00
}
@Test
void validateNeoTest() {
2018-03-09 18:58:11 +02:00
2017-07-11 15:12:03 +02:00
helper.registerAllowedMethod('sh', [Map], { Map m -> getVersion(m) })
jsr.step.toolValidate(tool: 'neo', home: home)
2017-07-11 15:12:03 +02:00
}
@Test
void validateCmTest() {
2018-03-09 18:58:11 +02:00
2017-07-11 15:12:03 +02:00
helper.registerAllowedMethod('sh', [Map], { Map m -> getVersion(m) })
jsr.step.toolValidate(tool: 'cm', home: home)
2017-07-11 15:12:03 +02:00
assert jlr.log.contains('Verifying Change Management Command Line Interface version 0.0.1 or compatible version.')
assert jlr.log.contains('Change Management Command Line Interface version 0.0.1 is installed.')
2017-07-11 15:12:03 +02:00
}
2018-03-09 18:58:11 +02:00
private getToolHome(Map m) {
if(m.script.contains('JAVA_HOME')) {
return '/env/java'
} else if(m.script.contains('MTA_JAR_LOCATION')) {
return '/env/mta/mta.jar'
2018-03-09 18:58:11 +02:00
} else if(m.script.contains('NEO_HOME')) {
return '/env/neo'
} else if(m.script.contains('CM_CLI_HOME')) {
return '/env/cmclient'
} else {
return 0
}
}
2018-02-28 14:11:09 +02:00
private getNoVersion(Map m) {
2018-03-09 18:58:11 +02:00
if(m.script.contains('java -version')) {
throw new AbortException('script returned exit code 127')
} else if(m.script.contains('mta.jar -v')) {
throw new AbortException('script returned exit code 127')
} else if(m.script.contains('neo.sh version')) {
throw new AbortException('script returned exit code 127')
} else if(m.script.contains('cmclient -v')) {
throw new AbortException('script returned exit code 127')
} else {
return getToolHome(m)
}
2017-07-11 15:12:03 +02:00
}
private getVersion(Map m) {
2018-03-09 18:58:11 +02:00
2017-07-11 15:12:03 +02:00
if(m.script.contains('java -version')) {
return '''openjdk version \"1.8.0_121\"
OpenJDK Runtime Environment (build 1.8.0_121-8u121-b13-1~bpo8+1-b13)
OpenJDK 64-Bit Server VM (build 25.121-b13, mixed mode)'''
} else if(m.script.contains('mta.jar -v')) {
return '1.0.6'
} else if(m.script.contains('neo.sh version')) {
return '''SAP Cloud Platform Console Client
SDK version : 3.39.10
Runtime : neo-java-web'''
} else if(m.script.contains('cmclient -v')) {
return '0.0.1-beta-2 : fc9729964a6acf5c1cad9c6f9cd6469727625a8e'
2018-03-09 18:58:11 +02:00
} else {
return getToolHome(m)
2017-07-11 15:12:03 +02:00
}
}
private getIncompatibleVersion(Map m) {
2018-03-09 18:58:11 +02:00
2017-07-11 15:12:03 +02:00
if(m.script.contains('java -version')) {
return '''openjdk version \"1.7.0_121\"
OpenJDK Runtime Environment (build 1.7.0_121-8u121-b13-1~bpo8+1-b13)
OpenJDK 64-Bit Server VM (build 25.121-b13, mixed mode)'''
} else if(m.script.contains('mta.jar -v')) {
return '1.0.5'
} else if(m.script.contains('neo.sh version')) {
return '''SAP Cloud Platform Console Client
SDK version : 1.126.51
Runtime : neo-java-web'''
} else if(m.script.contains('cmclient -v')) {
return '0.0.0-beta-1 : fc9729964a6acf5c1cad9c6f9cd6469727625a8e'
2018-03-09 18:58:11 +02:00
} else {
return getToolHome(m)
2017-07-11 15:12:03 +02:00
}
}
}