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

381 lines
11 KiB
Groovy
Raw Normal View History

import org.junit.Before
2018-02-13 11:11:08 +02:00
import org.junit.BeforeClass
import org.junit.ClassRule
import org.junit.Ignore
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
2017-07-11 15:12:03 +02:00
import org.junit.rules.TemporaryFolder
import org.yaml.snakeyaml.parser.ParserException
import hudson.AbortException
import util.BasePiperTest
2018-09-04 11:32:54 +02:00
import util.JenkinsDockerExecuteRule
import util.JenkinsLoggingRule
import util.JenkinsReadYamlRule
2018-01-16 16:03:00 +02:00
import util.JenkinsShellCallRule
2018-02-28 14:12:03 +02:00
import util.JenkinsStepRule
import util.Rules
public class MtaBuildTest extends BasePiperTest {
2018-02-21 13:48:14 +02:00
def toolMtaValidateCalled = false
2018-02-20 14:09:16 +02:00
def toolJavaValidateCalled = false
2018-02-21 13:48:14 +02:00
2018-02-13 11:11:08 +02:00
@ClassRule
public static TemporaryFolder tmp = new TemporaryFolder()
private ExpectedException thrown = new ExpectedException()
private JenkinsLoggingRule jlr = new JenkinsLoggingRule(this)
2018-01-16 16:03:00 +02:00
private JenkinsShellCallRule jscr = new JenkinsShellCallRule(this)
2018-09-04 11:32:54 +02:00
private JenkinsDockerExecuteRule jder = new JenkinsDockerExecuteRule(this)
2018-02-28 14:12:03 +02:00
private JenkinsStepRule jsr = new JenkinsStepRule(this)
private JenkinsReadYamlRule jryr = new JenkinsReadYamlRule(this).registerYaml('mta.yaml', defaultMtaYaml() )
2017-07-11 15:12:03 +02:00
@Rule
2018-02-28 14:12:03 +02:00
public RuleChain ruleChain = Rules
.getCommonRules(this)
.around(jryr)
2018-02-28 14:12:03 +02:00
.around(thrown)
.around(jlr)
.around(jscr)
2018-09-04 11:32:54 +02:00
.around(jder)
2018-02-28 14:12:03 +02:00
.around(jsr)
2018-02-13 11:11:08 +02:00
@BeforeClass
static void createTestFiles() {
}
2017-07-11 15:12:03 +02:00
@Before
void init() {
2017-07-11 15:12:03 +02:00
helper.registerAllowedMethod('fileExists', [String], { s -> false })
helper.registerAllowedMethod('sh', [Map], { Map m -> getVersionWithoutEnvVars(m) })
2017-07-11 15:12:03 +02:00
binding.setVariable('PATH', '/usr/bin')
}
@Test
void environmentPathTest() {
2018-02-28 14:12:03 +02:00
jsr.step.call(buildTarget: 'NEO')
2018-02-21 13:46:55 +02:00
assert jscr.shell.find { c -> c.contains('PATH=./node_modules/.bin:/usr/bin')}
}
2017-07-11 15:12:03 +02:00
@Test
void sedTest() {
2017-07-11 15:12:03 +02:00
2018-02-28 14:12:03 +02:00
jsr.step.call(buildTarget: 'NEO')
2017-07-11 15:12:03 +02:00
assert jscr.shell.find { c -> c =~ /sed -ie "s\/\\\$\{timestamp\}\/`date \+%Y%m%d%H%M%S`\/g" "mta.yaml"$/}
2017-07-11 15:12:03 +02:00
}
@Test
void mtarFilePathFromCommonPipelineEnviromentTest() {
2017-07-11 15:12:03 +02:00
jsr.step.call(script: nullScript,
buildTarget: 'NEO')
def mtarFilePath = nullScript.commonPipelineEnvironment.getMtarFilePath()
2017-07-11 15:12:03 +02:00
assert mtarFilePath == "com.mycompany.northwind.mtar"
2017-07-11 15:12:03 +02:00
}
@Test
2018-02-05 16:30:21 +02:00
void mtaJarLocationAsParameterTest() {
2017-07-11 15:12:03 +02:00
jsr.step.call(mtaJarLocation: '/mylocation/mta/mta.jar', buildTarget: 'NEO')
2017-07-11 15:12:03 +02:00
2018-04-10 18:23:44 +02:00
assert jscr.shell.find { c -> c.contains('-jar /mylocation/mta/mta.jar --mtar')}
2017-07-11 15:12:03 +02:00
assert jlr.log.contains("SAP Multitarget Application Archive Builder file '/mylocation/mta/mta.jar' retrieved from configuration.")
2018-04-10 18:23:44 +02:00
assert jlr.log.contains("Using SAP Multitarget Application Archive Builder '/mylocation/mta/mta.jar'.")
2017-07-11 15:12:03 +02:00
}
@Test
void noMtaPresentTest() {
jryr.registerYaml('mta.yaml', { throw new FileNotFoundException() })
2017-07-11 15:12:03 +02:00
thrown.expect(FileNotFoundException)
2018-02-28 14:12:03 +02:00
jsr.step.call(buildTarget: 'NEO')
2017-07-11 15:12:03 +02:00
}
@Test
void badMtaTest() {
2018-02-05 18:15:46 +02:00
2017-07-11 15:12:03 +02:00
thrown.expect(ParserException)
thrown.expectMessage('while parsing a block mapping')
jryr.registerYaml('mta.yaml', badMtaYaml())
2017-07-11 15:12:03 +02:00
2018-02-28 14:12:03 +02:00
jsr.step.call(buildTarget: 'NEO')
2017-07-11 15:12:03 +02:00
}
@Test
void noIdInMtaTest() {
2018-02-05 18:15:46 +02:00
2017-07-11 15:12:03 +02:00
thrown.expect(AbortException)
thrown.expectMessage("Property 'ID' not found in mta.yaml file.")
2017-07-11 15:12:03 +02:00
jryr.registerYaml('mta.yaml', noIdMtaYaml() )
2017-07-11 15:12:03 +02:00
2018-02-28 14:12:03 +02:00
jsr.step.call(buildTarget: 'NEO')
2017-07-11 15:12:03 +02:00
}
@Test
2018-02-08 14:18:04 +02:00
void mtaJarLocationFromEnvironmentTest() {
2018-02-05 18:15:46 +02:00
helper.registerAllowedMethod('sh', [Map], { Map m -> getVersionWithEnvVars(m) })
2018-02-08 14:18:04 +02:00
2018-02-28 14:12:03 +02:00
jsr.step.call(buildTarget: 'NEO')
2017-07-11 15:12:03 +02:00
assert jscr.shell.find { c -> c.contains("-jar /env/mta/mta.jar --mtar")}
assert jlr.log.contains("SAP Multitarget Application Archive Builder file '/env/mta/mta.jar' retrieved from environment.")
2018-04-10 18:23:44 +02:00
assert jlr.log.contains("Using SAP Multitarget Application Archive Builder '/env/mta/mta.jar'.")
2017-07-11 15:12:03 +02:00
}
@Test
2018-02-08 14:18:04 +02:00
void mtaJarLocationFromCustomStepConfigurationTest() {
nullScript.commonPipelineEnvironment.configuration = [steps:[mtaBuild:[mtaJarLocation: '/config/mta/mta.jar']]]
2018-02-08 14:18:04 +02:00
jsr.step.call(script: nullScript,
2018-02-08 14:18:04 +02:00
buildTarget: 'NEO')
assert jscr.shell.find(){ c -> c.contains("-jar /config/mta/mta.jar --mtar")}
assert jlr.log.contains("SAP Multitarget Application Archive Builder file '/config/mta/mta.jar' retrieved from configuration.")
2018-04-10 18:23:44 +02:00
assert jlr.log.contains("Using SAP Multitarget Application Archive Builder '/config/mta/mta.jar'.")
2018-02-08 14:18:04 +02:00
}
@Test
void mtaJarLocationFromDefaultStepConfigurationTest() {
jsr.step.call(script: nullScript,
buildTarget: 'NEO')
assert jscr.shell.find(){ c -> c.contains("-jar mta.jar --mtar")}
assert jlr.log.contains("SAP Multitarget Application Archive Builder file 'mta.jar' retrieved from configuration.")
assert jlr.log.contains("Using SAP Multitarget Application Archive Builder 'mta.jar'.")
}
2018-02-08 14:18:04 +02:00
@Test
void buildTargetFromParametersTest() {
2018-02-28 14:12:03 +02:00
jsr.step.call(buildTarget: 'NEO')
2018-02-21 13:46:55 +02:00
assert jscr.shell.find { c -> c.contains('java -jar mta.jar --mtar com.mycompany.northwind.mtar --build-target=NEO build')}
2018-02-08 14:18:04 +02:00
}
@Test
void buildTargetFromCustomStepConfigurationTest() {
nullScript.commonPipelineEnvironment.configuration = [steps:[mtaBuild:[buildTarget: 'NEO']]]
2018-02-08 14:18:04 +02:00
jsr.step.call(script: nullScript)
2018-02-08 14:18:04 +02:00
2018-02-21 13:46:55 +02:00
assert jscr.shell.find(){ c -> c.contains('java -jar mta.jar --mtar com.mycompany.northwind.mtar --build-target=NEO build')}
2018-02-08 14:18:04 +02:00
}
2018-09-04 11:32:54 +02:00
@Test
void canConfigureDockerImage() {
jsr.step.call(script: nullScript, dockerImage: 'mta-docker-image:latest')
assert 'mta-docker-image:latest' == jder.dockerParams.dockerImage
}
@Test
void canConfigureDockerOptions() {
jsr.step.call(script: nullScript, dockerOptions: 'something')
assert 'something' == jder.dockerParams.dockerOptions
}
2018-02-08 14:18:04 +02:00
@Test
void buildTargetFromDefaultStepConfigurationTest() {
nullScript.commonPipelineEnvironment.defaultConfiguration = [steps:[mtaBuild:[buildTarget: 'NEO']]]
2018-02-08 14:18:04 +02:00
jsr.step.call(script: nullScript)
2018-02-08 14:18:04 +02:00
2018-02-21 13:46:55 +02:00
assert jscr.shell.find { c -> c.contains('java -jar mta.jar --mtar com.mycompany.northwind.mtar --build-target=NEO build')}
}
2018-04-17 17:35:10 +02:00
@Test
void extensionFromParametersTest() {
jsr.step.call(buildTarget: 'NEO', extension: 'param_extension')
assert jscr.shell.find { c -> c.contains('java -jar mta.jar --mtar com.mycompany.northwind.mtar --build-target=NEO --extension=param_extension build')}
}
@Test
void extensionFromCustomStepConfigurationTest() {
nullScript.commonPipelineEnvironment.configuration = [steps:[mtaBuild:[buildTarget: 'NEO', extension: 'config_extension']]]
2018-04-17 17:35:10 +02:00
jsr.step.call(script: nullScript)
2018-04-17 17:35:10 +02:00
assert jscr.shell.find(){ c -> c.contains('java -jar mta.jar --mtar com.mycompany.northwind.mtar --build-target=NEO --extension=config_extension build')}
}
2018-02-13 11:11:08 +02:00
private static defaultMtaYaml() {
2017-07-11 15:12:03 +02:00
return '''
_schema-version: "2.0.0"
ID: "com.mycompany.northwind"
version: 1.0.0
2018-02-05 17:45:24 +02:00
2017-07-11 15:12:03 +02:00
parameters:
hcp-deployer-version: "1.0.0"
2018-02-05 17:45:24 +02:00
2017-07-11 15:12:03 +02:00
modules:
- name: "fiorinorthwind"
type: html5
path: .
parameters:
version: 1.0.0-${timestamp}
build-parameters:
builder: grunt
build-result: dist
'''
}
2018-02-05 18:16:53 +02:00
private badMtaYaml() {
2017-07-11 15:12:03 +02:00
return '''
_schema-version: "2.0.0
ID: "com.mycompany.northwind"
version: 1.0.0
2018-02-05 17:45:24 +02:00
2017-07-11 15:12:03 +02:00
parameters:
hcp-deployer-version: "1.0.0"
2018-02-05 17:45:24 +02:00
2017-07-11 15:12:03 +02:00
modules:
- name: "fiorinorthwind"
type: html5
path: .
parameters:
version: 1.0.0-${timestamp}
build-parameters:
builder: grunt
build-result: dist
'''
}
2018-02-05 18:16:53 +02:00
private noIdMtaYaml() {
2017-07-11 15:12:03 +02:00
return '''
_schema-version: "2.0.0"
version: 1.0.0
2018-02-05 17:45:24 +02:00
2017-07-11 15:12:03 +02:00
parameters:
hcp-deployer-version: "1.0.0"
2018-02-05 17:45:24 +02:00
2017-07-11 15:12:03 +02:00
modules:
- name: "fiorinorthwind"
type: html5
path: .
parameters:
version: 1.0.0-${timestamp}
build-parameters:
builder: grunt
build-result: dist
'''
}
private getVersionWithEnvVars(Map m) {
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 {
return getEnvVars(m)
}
}
private getVersionWithoutEnvVars(Map m) {
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 {
return getNoEnvVars(m)
}
}
2018-03-20 16:14:02 +02:00
private getVersionWithoutEnvVarsAndNotInCurrentDir(Map m) {
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 {
return getNoEnvVarsAndNotInCurrentDir(m)
}
}
private getEnvVars(Map m) {
if(m.script.contains('JAVA_HOME')) {
2018-03-19 19:29:41 +02:00
return ''
} else if(m.script.contains('MTA_JAR_LOCATION')) {
return '/env/mta/mta.jar'
2018-03-20 16:14:02 +02:00
} else if(m.script.contains('which java')) {
2018-04-10 18:23:44 +02:00
return 0
} else {
return 0
}
}
private getNoEnvVars(Map m) {
if(m.script.contains('JAVA_HOME')) {
return ''
} else if(m.script.contains('MTA_JAR_LOCATION')) {
return ''
2018-03-20 16:14:02 +02:00
} else if(m.script.contains('which java')) {
2018-04-10 18:23:44 +02:00
return 0
} else {
return 0
}
}
2018-03-20 16:14:02 +02:00
private getNoEnvVarsAndNotInCurrentDir(Map m) {
if(m.script.contains('JAVA_HOME')) {
return ''
} else if(m.script.contains('MTA_JAR_LOCATION')) {
return ''
} else if(m.script.contains('which java')) {
2018-04-10 18:23:44 +02:00
return 0
2018-03-20 16:14:02 +02:00
} else {
return 1
}
}
2017-07-11 15:12:03 +02:00
}