2018-03-13 10:31:01 +02:00
|
|
|
import org.junit.Before
|
2018-02-13 11:11:08 +02:00
|
|
|
import org.junit.BeforeClass
|
|
|
|
import org.junit.ClassRule
|
2018-03-08 10:07:25 +02:00
|
|
|
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
|
2018-01-16 10:33:13 +02:00
|
|
|
import org.junit.rules.RuleChain
|
2017-07-11 15:12:03 +02:00
|
|
|
import org.junit.rules.TemporaryFolder
|
2018-03-13 10:31:01 +02:00
|
|
|
import org.yaml.snakeyaml.parser.ParserException
|
2018-01-16 18:06:25 +02:00
|
|
|
|
|
|
|
import com.lesfurets.jenkins.unit.BasePipelineTest
|
|
|
|
|
2018-03-13 10:31:01 +02:00
|
|
|
import hudson.AbortException
|
|
|
|
import util.JenkinsEnvironmentRule
|
2018-01-16 10:33:13 +02:00
|
|
|
import util.JenkinsLoggingRule
|
2018-01-16 16:03:00 +02:00
|
|
|
import util.JenkinsShellCallRule
|
2018-02-28 14:12:03 +02:00
|
|
|
import util.JenkinsStepRule
|
2018-01-26 15:55:15 +02:00
|
|
|
import util.Rules
|
2018-01-16 10:33:13 +02:00
|
|
|
|
2018-02-28 14:12:03 +02:00
|
|
|
public class MtaBuildTest extends BasePipelineTest {
|
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()
|
|
|
|
|
2018-01-16 10:33:13 +02:00
|
|
|
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-02-28 14:12:03 +02: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 14:12:03 +02:00
|
|
|
public RuleChain ruleChain = Rules
|
|
|
|
.getCommonRules(this)
|
|
|
|
.around(thrown)
|
|
|
|
.around(jlr)
|
|
|
|
.around(jscr)
|
|
|
|
.around(jsr)
|
|
|
|
.around(jer)
|
2018-01-26 14:35:49 +02:00
|
|
|
|
2018-02-13 11:11:08 +02:00
|
|
|
private static currentDir
|
|
|
|
private static newDir
|
|
|
|
private static mtaYaml
|
2017-07-11 15:12:03 +02:00
|
|
|
|
2018-02-13 11:11:08 +02:00
|
|
|
@BeforeClass
|
|
|
|
static void createTestFiles() {
|
|
|
|
|
|
|
|
currentDir = "${tmp.getRoot()}"
|
|
|
|
mtaYaml = tmp.newFile('mta.yaml')
|
|
|
|
newDir = "$currentDir/newDir"
|
|
|
|
tmp.newFolder('newDir')
|
|
|
|
tmp.newFile('newDir/mta.yaml') << defaultMtaYaml()
|
|
|
|
}
|
|
|
|
|
2017-07-11 15:12:03 +02:00
|
|
|
@Before
|
2018-01-16 10:33:13 +02:00
|
|
|
void init() {
|
2018-02-13 11:11:08 +02:00
|
|
|
mtaYaml.text = defaultMtaYaml()
|
2017-07-11 15:12:03 +02:00
|
|
|
|
|
|
|
helper.registerAllowedMethod('pwd', [], { currentDir } )
|
2018-03-13 10:31:01 +02:00
|
|
|
|
|
|
|
helper.registerAllowedMethod('fileExists', [GString.class], { false })
|
2017-07-11 15:12:03 +02:00
|
|
|
|
|
|
|
binding.setVariable('PATH', '/usr/bin')
|
2018-02-21 13:48:14 +02:00
|
|
|
|
|
|
|
//
|
|
|
|
// 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 == 'mta')
|
|
|
|
toolMtaValidateCalled = true
|
2018-03-06 18:16:21 +02:00
|
|
|
|
2018-02-20 14:09:16 +02:00
|
|
|
if(m.tool == 'java')
|
|
|
|
toolJavaValidateCalled = true
|
2018-02-21 13:48:14 +02:00
|
|
|
})
|
2017-07-11 15:12:03 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2018-02-05 16:16:52 +02:00
|
|
|
@Test
|
2018-02-06 12:51:04 +02:00
|
|
|
void environmentPathTest() {
|
2018-02-05 16:16:52 +02:00
|
|
|
|
2018-02-28 14:12:03 +02:00
|
|
|
jsr.step.call(buildTarget: 'NEO')
|
2018-02-05 16:16:52 +02:00
|
|
|
|
2018-02-21 13:46:55 +02:00
|
|
|
assert jscr.shell.find { c -> c.contains('PATH=./node_modules/.bin:/usr/bin')}
|
2018-02-05 16:16:52 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2017-07-11 15:12:03 +02:00
|
|
|
@Test
|
2018-02-06 12:51:04 +02:00
|
|
|
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
|
|
|
|
2018-02-21 13:46:55 +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
|
2018-02-06 12:51:04 +02:00
|
|
|
void mtarFilePathFromCommonPipelineEnviromentTest() {
|
2017-07-11 15:12:03 +02:00
|
|
|
|
2018-02-28 14:12:03 +02:00
|
|
|
jsr.step.call(script: [commonPipelineEnvironment: jer.env],
|
2018-01-16 18:06:25 +02:00
|
|
|
buildTarget: 'NEO')
|
|
|
|
|
2018-02-28 14:12:03 +02:00
|
|
|
def mtarFilePath = jer.env.getMtarFilePath()
|
2017-07-11 15:12:03 +02:00
|
|
|
|
2018-02-05 18:22:03 +02:00
|
|
|
assert mtarFilePath == "$currentDir/com.mycompany.northwind.mtar"
|
2017-07-11 15:12:03 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
@Test
|
2018-02-06 12:51:04 +02:00
|
|
|
void mtaBuildWithSurroundingDirTest() {
|
2017-07-11 15:12:03 +02:00
|
|
|
|
2018-02-13 11:11:08 +02:00
|
|
|
helper.registerAllowedMethod('pwd', [], { newDir } )
|
2018-01-16 18:06:25 +02:00
|
|
|
|
2018-02-28 14:12:03 +02:00
|
|
|
def mtarFilePath = jsr.step.call(buildTarget: 'NEO')
|
2017-07-11 15:12:03 +02:00
|
|
|
|
2018-02-21 13:46:55 +02:00
|
|
|
assert jscr.shell.find { c -> c =~ /sed -ie "s\/\\\$\{timestamp\}\/`date \+%Y%m%d%H%M%S`\/g" ".*\/newDir\/mta.yaml"$/}
|
2017-07-11 15:12:03 +02:00
|
|
|
|
2018-02-13 11:11:08 +02:00
|
|
|
assert mtarFilePath == "$newDir/com.mycompany.northwind.mtar"
|
2017-07-11 15:12:03 +02:00
|
|
|
}
|
|
|
|
|
2018-02-05 18:15:46 +02:00
|
|
|
|
2017-07-11 15:12:03 +02:00
|
|
|
@Test
|
2018-02-05 16:30:21 +02:00
|
|
|
void mtaJarLocationNotSetTest() {
|
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
|
|
|
|
2018-02-21 13:46:55 +02:00
|
|
|
assert jscr.shell.find { c -> c.contains(' -jar mta.jar --mtar ')}
|
2017-07-11 15:12:03 +02:00
|
|
|
|
2018-02-05 16:39:18 +02:00
|
|
|
assert jlr.log.contains('[mtaBuild] Using MTA JAR from current working directory.')
|
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
|
|
|
|
2018-02-28 14:12:03 +02:00
|
|
|
jsr.step.call(mtaJarLocation: '/mylocation/mta', buildTarget: 'NEO')
|
2017-07-11 15:12:03 +02:00
|
|
|
|
2018-02-21 13:46:55 +02:00
|
|
|
assert jscr.shell.find { c -> c.contains(' -jar /mylocation/mta/mta.jar --mtar ')}
|
2017-07-11 15:12:03 +02:00
|
|
|
|
2018-02-08 14:18:04 +02:00
|
|
|
assert jlr.log.contains('[mtaBuild] MTA JAR "/mylocation/mta/mta.jar" retrieved from configuration.')
|
2017-07-11 15:12:03 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
@Test
|
2018-02-06 12:51:04 +02:00
|
|
|
void noMtaPresentTest() {
|
2018-02-05 17:57:00 +02:00
|
|
|
|
|
|
|
mtaYaml.delete()
|
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
|
2018-02-06 12:51:04 +02:00
|
|
|
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')
|
|
|
|
|
2018-02-05 17:57:00 +02:00
|
|
|
mtaYaml.text = 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
|
2018-02-06 12:51:04 +02:00
|
|
|
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 at: '")
|
|
|
|
|
2018-02-05 17:57:00 +02:00
|
|
|
mtaYaml.text = 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
|
|
|
|
2018-02-08 14:18:04 +02:00
|
|
|
binding.setVariable('env', [:])
|
|
|
|
binding.getVariable('env')['MTA_JAR_LOCATION'] = '/env/mta'
|
|
|
|
|
2018-02-28 14:12:03 +02:00
|
|
|
jsr.step.call(buildTarget: 'NEO')
|
2017-07-11 15:12:03 +02:00
|
|
|
|
2018-02-21 13:46:55 +02:00
|
|
|
assert jscr.shell.find { c -> c.contains('-jar /env/mta/mta.jar --mtar')}
|
2018-02-08 14:18:04 +02:00
|
|
|
assert jlr.log.contains('[mtaBuild] MTA JAR "/env/mta/mta.jar" retrieved from environment.')
|
2017-07-11 15:12:03 +02:00
|
|
|
}
|
|
|
|
|
2018-02-05 16:28:16 +02:00
|
|
|
|
|
|
|
@Test
|
2018-02-08 14:18:04 +02:00
|
|
|
void mtaJarLocationFromCustomStepConfigurationTest() {
|
2018-02-05 16:28:16 +02:00
|
|
|
|
2018-02-28 14:12:03 +02:00
|
|
|
jer.env.configuration = [steps:[mtaBuild:[mtaJarLocation: '/step/mta']]]
|
2018-02-08 14:18:04 +02:00
|
|
|
|
2018-02-28 14:12:03 +02:00
|
|
|
jsr.step.call(script: [commonPipelineEnvironment: jer.env],
|
2018-02-08 14:18:04 +02:00
|
|
|
buildTarget: 'NEO')
|
|
|
|
|
2018-02-21 13:46:55 +02:00
|
|
|
assert jscr.shell.find(){ c -> c.contains('-jar /step/mta/mta.jar --mtar')}
|
2018-02-19 20:27:53 +02:00
|
|
|
assert jlr.log.contains('[mtaBuild] MTA JAR "/step/mta/mta.jar" retrieved from configuration.')
|
2018-02-08 14:18:04 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
@Test
|
|
|
|
void buildTargetFromParametersTest() {
|
2018-02-05 16:28:16 +02:00
|
|
|
|
2018-02-28 14:12:03 +02:00
|
|
|
jsr.step.call(buildTarget: 'NEO')
|
2018-02-05 16:28:16 +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
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
@Test
|
|
|
|
void buildTargetFromCustomStepConfigurationTest() {
|
|
|
|
|
2018-02-28 14:12:03 +02:00
|
|
|
jer.env.configuration = [steps:[mtaBuild:[buildTarget: 'NEO']]]
|
2018-02-08 14:18:04 +02:00
|
|
|
|
2018-02-28 14:12:03 +02:00
|
|
|
jsr.step.call(script: [commonPipelineEnvironment: jer.env])
|
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
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
@Test
|
|
|
|
void buildTargetFromDefaultStepConfigurationTest() {
|
|
|
|
|
2018-02-28 14:12:03 +02:00
|
|
|
jer.env.defaultConfiguration = [steps:[mtaBuild:[buildTarget: 'NEO']]]
|
2018-02-08 14:18:04 +02:00
|
|
|
|
2018-02-28 14:12:03 +02:00
|
|
|
jsr.step.call(script: [commonPipelineEnvironment: jer.env])
|
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-05 16:28:16 +02:00
|
|
|
}
|
|
|
|
|
2018-03-08 10:07:25 +02:00
|
|
|
@Ignore('Tool validation disabled since it does not work properly in conjunction with slaves.')
|
2018-02-21 13:48:14 +02:00
|
|
|
@Test
|
|
|
|
void skipValidationInCaseMtarJarFileIsUsedFromWorkingDir() {
|
|
|
|
jscr.setReturnValue('ls mta.jar', 0)
|
|
|
|
jsr.step.call(script: [commonPipelineEnvironment: jer.env])
|
|
|
|
assert !toolMtaValidateCalled
|
|
|
|
}
|
|
|
|
|
2018-03-08 10:07:25 +02:00
|
|
|
@Ignore('Tool validation disabled since it does not work properly in conjunction with slaves.')
|
2018-02-21 13:48:14 +02:00
|
|
|
@Test
|
|
|
|
void performValidationInCaseMtarJarFileIsNotUsedFromWorkingDir() {
|
|
|
|
jscr.setReturnValue('ls mta.jar', 1)
|
|
|
|
jsr.step.call(script: [commonPipelineEnvironment: jer.env])
|
|
|
|
assert toolMtaValidateCalled
|
|
|
|
}
|
2018-02-05 16:28:16 +02:00
|
|
|
|
2018-03-08 10:07:25 +02:00
|
|
|
@Ignore('Tool validation disabled since it does not work properly in conjunction with slaves.')
|
2018-02-20 14:09:16 +02:00
|
|
|
@Test
|
|
|
|
void toolJavaValidateCalled() {
|
|
|
|
|
|
|
|
jsr.step.call(buildTarget: 'NEO')
|
|
|
|
|
|
|
|
assert toolJavaValidateCalled
|
|
|
|
}
|
|
|
|
|
2018-03-08 10:07:25 +02:00
|
|
|
@Ignore('Tool validation disabled since it does not work properly in conjunction with slaves.')
|
2018-02-20 14:09:16 +02:00
|
|
|
@Test
|
|
|
|
void toolValidateNotCalledWhenJavaHomeIsUnsetButJavaIsInPath() {
|
|
|
|
|
|
|
|
jscr.setReturnValue('which java', 0)
|
|
|
|
jsr.step.call(buildTarget: 'NEO')
|
2018-02-05 16:28:16 +02:00
|
|
|
|
2018-02-20 14:09:16 +02:00
|
|
|
assert !toolJavaValidateCalled
|
2018-02-22 12:13:34 +02:00
|
|
|
assert jlr.log.contains('Tool validation (java) skipped. JAVA_HOME not set, but java executable in path.')
|
2018-03-06 18:16:21 +02:00
|
|
|
}
|
|
|
|
|
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
|
|
|
|
'''
|
|
|
|
}
|
|
|
|
}
|