2017-07-11 15:12:03 +02:00
|
|
|
import hudson.AbortException
|
|
|
|
import org.jenkinsci.plugins.pipeline.utility.steps.shaded.org.yaml.snakeyaml.Yaml
|
|
|
|
import org.jenkinsci.plugins.pipeline.utility.steps.shaded.org.yaml.snakeyaml.parser.ParserException
|
|
|
|
import org.junit.Before
|
|
|
|
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-01-16 18:06:25 +02:00
|
|
|
|
|
|
|
import com.lesfurets.jenkins.unit.BasePipelineTest
|
|
|
|
|
2018-01-16 10:33:13 +02:00
|
|
|
import util.JenkinsLoggingRule
|
2018-01-16 16:03:00 +02:00
|
|
|
import util.JenkinsShellCallRule
|
2018-01-26 15:55:15 +02:00
|
|
|
import util.Rules
|
2018-01-16 10:33:13 +02:00
|
|
|
|
2018-01-16 18:06:25 +02:00
|
|
|
public class MTABuildTest extends BasePipelineTest {
|
2017-07-11 15:12:03 +02:00
|
|
|
|
2018-01-16 10:33:13 +02:00
|
|
|
private ExpectedException thrown = new ExpectedException()
|
|
|
|
private TemporaryFolder tmp = new TemporaryFolder()
|
|
|
|
private JenkinsLoggingRule jlr = new JenkinsLoggingRule(this)
|
2018-01-16 16:03:00 +02:00
|
|
|
private JenkinsShellCallRule jscr = new JenkinsShellCallRule(this)
|
2017-07-11 15:12:03 +02:00
|
|
|
|
|
|
|
@Rule
|
2018-01-26 15:55:15 +02:00
|
|
|
public RuleChain ruleChain = Rules.getCommonRules(this)
|
|
|
|
.around(thrown)
|
2018-01-16 10:33:13 +02:00
|
|
|
.around(tmp)
|
|
|
|
.around(jlr)
|
2018-01-16 16:03:00 +02:00
|
|
|
.around(jscr)
|
2018-01-26 14:35:49 +02:00
|
|
|
|
2017-07-11 15:12:03 +02:00
|
|
|
|
|
|
|
def currentDir
|
2018-02-05 17:57:00 +02:00
|
|
|
def mtaYaml
|
2017-07-11 15:12:03 +02:00
|
|
|
|
2018-01-16 18:06:25 +02:00
|
|
|
def mtaBuildScript
|
|
|
|
def cpe
|
2017-07-11 15:12:03 +02:00
|
|
|
|
|
|
|
@Before
|
2018-01-16 10:33:13 +02:00
|
|
|
void init() {
|
2017-11-21 15:27:05 +02:00
|
|
|
|
2017-07-11 15:12:03 +02:00
|
|
|
currentDir = tmp.newFolder().toURI().getPath()[0..-2] //omit final '/'
|
2018-02-05 17:57:00 +02:00
|
|
|
mtaYaml = new File("${currentDir}/mta.yaml")
|
|
|
|
mtaYaml << defaultMtaYaml()
|
2017-07-11 15:12:03 +02:00
|
|
|
|
|
|
|
helper.registerAllowedMethod('readYaml', [Map], {
|
|
|
|
m ->
|
|
|
|
return new Yaml().load((m.file as File).text)
|
|
|
|
})
|
|
|
|
helper.registerAllowedMethod('pwd', [], { currentDir } )
|
|
|
|
|
|
|
|
binding.setVariable('PATH', '/usr/bin')
|
|
|
|
|
2018-02-05 16:39:18 +02:00
|
|
|
mtaBuildScript = loadScript('mtaBuild.groovy').mtaBuild
|
2018-01-16 18:06:25 +02:00
|
|
|
cpe = loadScript('commonPipelineEnvironment.groovy').commonPipelineEnvironment
|
2017-07-11 15:12:03 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2018-02-05 16:16:52 +02:00
|
|
|
@Test
|
|
|
|
public void environmentPathTest(){
|
|
|
|
|
|
|
|
mtaBuildScript.call(buildTarget: 'NEO')
|
|
|
|
|
2018-02-05 16:39:18 +02:00
|
|
|
assert jscr.shell[1].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-05 17:57:00 +02:00
|
|
|
public void sedTest(){
|
2017-07-11 15:12:03 +02:00
|
|
|
|
2018-02-05 12:54:53 +02:00
|
|
|
mtaBuildScript.call(buildTarget: 'NEO')
|
2017-07-11 15:12:03 +02:00
|
|
|
|
2018-01-16 16:03:00 +02:00
|
|
|
assert jscr.shell[0] =~ /sed -ie "s\/\\\$\{timestamp\}\/`date \+%Y%m%d%H%M%S`\/g" ".*\/mta.yaml"$/
|
2017-07-11 15:12:03 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
@Test
|
|
|
|
public void mtarFilePathFromCommonPipelineEnviromentTest(){
|
|
|
|
|
2018-01-16 18:06:25 +02:00
|
|
|
mtaBuildScript.call(script: [commonPipelineEnvironment: cpe],
|
|
|
|
buildTarget: 'NEO')
|
|
|
|
|
|
|
|
def mtarFilePath = cpe.getMtarFilePath()
|
2017-07-11 15:12:03 +02:00
|
|
|
|
|
|
|
assert mtarFilePath == "${currentDir}/com.mycompany.northwind.mtar"
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
@Test
|
|
|
|
public void mtaBuildWithSurroundingDirTest(){
|
|
|
|
|
|
|
|
def newDirName = 'newDir'
|
2018-01-31 18:17:01 +02:00
|
|
|
def newDirPath = "${currentDir}/${newDirName}"
|
|
|
|
def newDir = new File(newDirPath)
|
2018-01-16 18:06:25 +02:00
|
|
|
|
|
|
|
newDir.mkdirs()
|
|
|
|
new File(newDir, 'mta.yaml') << defaultMtaYaml()
|
2017-07-11 15:12:03 +02:00
|
|
|
|
2018-01-31 18:17:01 +02:00
|
|
|
helper.registerAllowedMethod('pwd', [], { newDirPath } )
|
2018-01-16 18:06:25 +02:00
|
|
|
|
2018-02-05 12:49:53 +02:00
|
|
|
def mtarFilePath = mtaBuildScript.call(buildTarget: 'NEO')
|
2017-07-11 15:12:03 +02:00
|
|
|
|
2018-01-16 16:03:00 +02:00
|
|
|
assert jscr.shell[0] =~ /sed -ie "s\/\\\$\{timestamp\}\/`date \+%Y%m%d%H%M%S`\/g" ".*\/newDir\/mta.yaml"$/
|
2017-07-11 15:12:03 +02:00
|
|
|
|
2018-01-16 18:06:25 +02:00
|
|
|
assert mtarFilePath == "${currentDir}/${newDirName}/com.mycompany.northwind.mtar"
|
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-05 12:54:53 +02:00
|
|
|
mtaBuildScript.call(buildTarget: 'NEO')
|
2017-07-11 15:12:03 +02:00
|
|
|
|
2018-01-16 16:03:00 +02:00
|
|
|
assert jscr.shell[1].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-05 12:54:53 +02:00
|
|
|
mtaBuildScript.call(mtaJarLocation: '/mylocation/mta', buildTarget: 'NEO')
|
2017-07-11 15:12:03 +02:00
|
|
|
|
2018-01-16 18:06:25 +02:00
|
|
|
assert jscr.shell[1].contains(' -jar /mylocation/mta/mta.jar --mtar ')
|
2017-07-11 15:12:03 +02:00
|
|
|
|
2018-02-05 16:39:18 +02:00
|
|
|
assert jlr.log.contains('[mtaBuild] MTA JAR "/mylocation/mta/mta.jar" retrieved from parameters.')
|
2017-07-11 15:12:03 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
@Test
|
|
|
|
public 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-05 12:49:53 +02:00
|
|
|
mtaBuildScript.call(buildTarget: 'NEO')
|
2017-07-11 15:12:03 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
@Test
|
|
|
|
public void badMtaTest(){
|
|
|
|
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-05 12:49:53 +02:00
|
|
|
mtaBuildScript.call(buildTarget: 'NEO')
|
2017-07-11 15:12:03 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
@Test
|
|
|
|
public void noIdInMtaTest(){
|
|
|
|
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-05 12:49:53 +02:00
|
|
|
mtaBuildScript.call(buildTarget: 'NEO')
|
2017-07-11 15:12:03 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
@Test
|
|
|
|
public void noBuildTargetTest(){
|
|
|
|
thrown.expect(Exception)
|
2018-02-05 16:39:18 +02:00
|
|
|
thrown.expectMessage('ERROR - NO VALUE AVAILABLE FOR buildTarget')
|
2017-07-11 15:12:03 +02:00
|
|
|
|
2018-02-05 12:49:53 +02:00
|
|
|
mtaBuildScript.call()
|
2017-07-11 15:12:03 +02:00
|
|
|
}
|
|
|
|
|
2018-02-05 16:28:16 +02:00
|
|
|
|
|
|
|
@Test
|
|
|
|
void mtaJarLocationFromEnvironmentTest(){
|
|
|
|
|
|
|
|
binding.setVariable('env', [:])
|
|
|
|
binding.getVariable('env')['MTA_JAR_LOCATION'] = '/env/mta'
|
|
|
|
|
|
|
|
mtaBuildScript.call(buildTarget: 'NEO')
|
|
|
|
|
|
|
|
assert jscr.shell[1].contains('-jar /env/mta/mta.jar --mtar')
|
|
|
|
assert jlr.log.contains('[mtaBuild] MTA JAR "/env/mta/mta.jar" retrieved from environment.')
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2017-07-11 15:12:03 +02:00
|
|
|
private defaultMtaYaml(){
|
|
|
|
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
|
|
|
|
'''
|
|
|
|
}
|
|
|
|
|
|
|
|
private badMtaYaml(){
|
|
|
|
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
|
|
|
|
'''
|
|
|
|
}
|
|
|
|
|
|
|
|
private noIdMtaYaml(){
|
|
|
|
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
|
|
|
|
'''
|
|
|
|
}
|
|
|
|
}
|