From 60d1b007a8cb3068ae5ffee4ebb6f1fc48e9d6d9 Mon Sep 17 00:00:00 2001 From: Alejandra Ferreiro Vidal Date: Mon, 19 Mar 2018 18:29:41 +0100 Subject: [PATCH] add JavaArchiveDescriptor --- .../piper/tools/JavaArchiveDescriptor.groovy | 44 ++++ src/com/sap/piper/tools/Tool.groovy | 23 -- src/com/sap/piper/tools/ToolDescriptor.groovy | 113 +++++++++ src/com/sap/piper/tools/ToolUtils.groovy | 41 ---- src/com/sap/piper/tools/ToolVerifier.groovy | 57 ----- test/groovy/MTABuildTest.groovy | 12 +- test/groovy/NeoDeployTest.groovy | 2 +- .../tools/JavaArchiveDescriptorTest.groovy | 214 ++++++++++++++++++ .../com/sap/piper/tools/ToolUtilsTest.groovy | 92 -------- .../sap/piper/tools/ToolVerifierTest.groovy | 143 ------------ vars/mtaBuild.groovy | 17 +- vars/neoDeploy.groovy | 14 +- vars/toolValidate.groovy | 22 +- 13 files changed, 404 insertions(+), 390 deletions(-) create mode 100644 src/com/sap/piper/tools/JavaArchiveDescriptor.groovy delete mode 100644 src/com/sap/piper/tools/Tool.groovy create mode 100644 src/com/sap/piper/tools/ToolDescriptor.groovy delete mode 100644 src/com/sap/piper/tools/ToolUtils.groovy delete mode 100644 src/com/sap/piper/tools/ToolVerifier.groovy create mode 100644 test/groovy/com/sap/piper/tools/JavaArchiveDescriptorTest.groovy delete mode 100644 test/groovy/com/sap/piper/tools/ToolUtilsTest.groovy delete mode 100644 test/groovy/com/sap/piper/tools/ToolVerifierTest.groovy diff --git a/src/com/sap/piper/tools/JavaArchiveDescriptor.groovy b/src/com/sap/piper/tools/JavaArchiveDescriptor.groovy new file mode 100644 index 000000000..e2912f610 --- /dev/null +++ b/src/com/sap/piper/tools/JavaArchiveDescriptor.groovy @@ -0,0 +1,44 @@ +package com.sap.piper.tools + +import com.sap.piper.EnvironmentUtils + + +class JavaArchiveDescriptor extends ToolDescriptor { + + final javaTool + final javaOptions + + JavaArchiveDescriptor(name, environmentKey, stepConfigurationKey, executablePath, executableName, version, versionOption, javaTool, javaOptions) { + super(name, environmentKey, stepConfigurationKey, executablePath, executableName, version, versionOption) + this.javaTool = javaTool + this.javaOptions = javaOptions + } + + @Override + def getHome(script, configuration, log = true) { + + def home + if (EnvironmentUtils.isEnvironmentVariable(script, environmentKey)) { + home = EnvironmentUtils.getEnvironmentVariable(script, environmentKey) + if (log) script.echo "$name home '$home' retrieved from environment." + } + else if (configuration.containsKey(stepConfigurationKey)) { + home = configuration.get(stepConfigurationKey) + if (log) script.echo "$name home '$home' retrieved from configuration." + } else { + home = '' + if (log) script.echo "$name expected on current working directory." + } + return home + } + + @Override + def getExecutable(script, configuration, log = true) { + + def tool = getTool(script, configuration, log) + def javaExecutable = javaTool.getExecutable(script, configuration, false) + def executable = "$javaExecutable $javaOptions $tool" + if (log) script.echo "Using $name executable '$executable'." + return executable + } +} diff --git a/src/com/sap/piper/tools/Tool.groovy b/src/com/sap/piper/tools/Tool.groovy deleted file mode 100644 index f981b0ad0..000000000 --- a/src/com/sap/piper/tools/Tool.groovy +++ /dev/null @@ -1,23 +0,0 @@ -package com.sap.piper.tools - - -class Tool implements Serializable { - - final name - final environmentKey - final stepConfigurationKey - final executablePath - final executableName - final version - final versionOption - - Tool(name, environmentKey, stepConfigurationKey, executablePath, executableName, version, versionOption) { - this.name = name - this.environmentKey = environmentKey - this.stepConfigurationKey = stepConfigurationKey - this.executablePath = executablePath - this.executableName = executableName - this.version = version - this.versionOption = versionOption - } -} diff --git a/src/com/sap/piper/tools/ToolDescriptor.groovy b/src/com/sap/piper/tools/ToolDescriptor.groovy new file mode 100644 index 000000000..71fd14483 --- /dev/null +++ b/src/com/sap/piper/tools/ToolDescriptor.groovy @@ -0,0 +1,113 @@ +package com.sap.piper.tools + +import com.sap.piper.EnvironmentUtils +import com.sap.piper.FileUtils +import com.sap.piper.Version + +import hudson.AbortException + + +class ToolDescriptor implements Serializable { + + final name + final environmentKey + final stepConfigurationKey + final executablePath + final executableName + final version + final versionOption + + ToolDescriptor(name, environmentKey, stepConfigurationKey, executablePath, executableName, version, versionOption) { + this.name = name + this.environmentKey = environmentKey + this.stepConfigurationKey = stepConfigurationKey + this.executablePath = executablePath + this.executableName = executableName + this.version = version + this.versionOption = versionOption + } + + def getHome(script, configuration, log = true) { + + def home + if (EnvironmentUtils.isEnvironmentVariable(script, environmentKey)) { + home = EnvironmentUtils.getEnvironmentVariable(script, environmentKey) + if (log) script.echo "$name home '$home' retrieved from environment." + } + else if (configuration.containsKey(stepConfigurationKey)) { + home = configuration.get(stepConfigurationKey) + if (log) script.echo "$name home '$home' retrieved from configuration." + } else { + home = '' + if (log) script.echo "$name expected on PATH." + } + return home + } + + def getTool(script, configuration, log = true) { + + def home = getHome(script, configuration, log) + + def path = "$executablePath" + def executable = "$executableName" + + if (home) { + return "$home$path$executable" + } else { + return "$executable" + } + } + + def getExecutable(script, configuration, log = true) { + def executable = getTool(script, configuration, log) + if (log) script.echo "Using $name executable '$executable'." + return executable + } + + def verify(script, configuration) { + + verifyHome(script, configuration) + verifyTool(script, configuration) + verifyVersion(script, configuration) + } + + def verifyHome(script, configuration) { + + def home = getHome(script, configuration) + if (home) { + script.echo "Verifying $name home '$home'." + FileUtils.validateDirectoryIsNotEmpty(script, home) + script.echo "Verification success. $name home '$home' exists." + } + } + + def verifyTool(script, configuration) { + + def home = getHome(script, configuration, false) + def tool = getTool(script, configuration, false) + if (home) { + script.echo "Verifying $name '$tool'." + FileUtils.validateFile(script, tool) + script.echo "Verification success. $name '$tool' exists." + } + } + + def verifyVersion(script, configuration) { + + def executable = getExecutable(script, configuration, false) + + script.echo "Verifying $name version $version or compatible version." + + def toolVersion + try { + toolVersion = script.sh returnStdout: true, script: "$executable $versionOption" + } catch(AbortException e) { + throw new AbortException("The verification of $name failed. Please check '$executable'. $e.message.") + } + def installedVersion = new Version(toolVersion) + if (!installedVersion.isCompatibleVersion(new Version(version))) { + throw new AbortException("The installed version of $name is ${installedVersion.toString()}. Please install version $version or a compatible version.") + } + script.echo "Verification success. $name version ${installedVersion.toString()} is installed." + } +} diff --git a/src/com/sap/piper/tools/ToolUtils.groovy b/src/com/sap/piper/tools/ToolUtils.groovy deleted file mode 100644 index 15e6acabb..000000000 --- a/src/com/sap/piper/tools/ToolUtils.groovy +++ /dev/null @@ -1,41 +0,0 @@ -package com.sap.piper.tools - -import com.sap.piper.EnvironmentUtils - - -class ToolUtils implements Serializable { - - def static getToolHome(tool, script, configuration, log = true) { - - def home - if (EnvironmentUtils.isEnvironmentVariable(script, tool.environmentKey)) { - home = EnvironmentUtils.getEnvironmentVariable(script, tool.environmentKey) - if (log) script.echo "$tool.name home '$home' retrieved from environment." - } - else if (configuration.containsKey(tool.stepConfigurationKey)) { - home = configuration.get(tool.stepConfigurationKey) - if (log) script.echo "$tool.name home '$home' retrieved from configuration." - } else { - home = '' - if (log) script.echo "$tool.name expected on PATH or current working directory." - } - return home - } - - def static getToolExecutable(tool, script, configuration, log = true) { - - def home = getToolHome(tool, script, configuration, log) - - def path = "$tool.executablePath" - def executable = "$tool.executableName" - def toolExecutable - - if (home) { - toolExecutable = "$home$path$executable" - } else { - toolExecutable = "$executable" - } - if (log) script.echo "Using $tool.name executable '$toolExecutable'." - return toolExecutable - } -} diff --git a/src/com/sap/piper/tools/ToolVerifier.groovy b/src/com/sap/piper/tools/ToolVerifier.groovy deleted file mode 100644 index ae9332878..000000000 --- a/src/com/sap/piper/tools/ToolVerifier.groovy +++ /dev/null @@ -1,57 +0,0 @@ -package com.sap.piper.tools - -import com.sap.piper.EnvironmentUtils -import com.sap.piper.FileUtils -import com.sap.piper.Version -import com.sap.piper.tools.ToolUtils - -import hudson.AbortException - - -class ToolVerifier implements Serializable { - - def static verifyToolHome(tool, script, configuration) { - - def home = ToolUtils.getToolHome(tool, script, configuration) - if (home) { - script.echo "Verifying $tool.name home '$home'." - FileUtils.validateDirectoryIsNotEmpty(script, home) - script.echo "Verification success. $tool.name home '$home' exists." - } - } - - def static verifyToolExecutable(tool, script, configuration) { - - def home = ToolUtils.getToolHome(tool, script, configuration, false) - def executable = ToolUtils.getToolExecutable(tool, script, configuration) - if (home) { - script.echo "Verifying $tool.name executable '$executable'." - FileUtils.validateFile(script, executable) - script.echo "Verification success. $tool.name executable '$executable' exists." - } - } - - def static verifyToolVersion(tool, script, configuration) { - - def executable = ToolUtils.getToolExecutable(tool, script, configuration, false) - if (tool.name == 'SAP Multitarget Application Archive Builder'){ - def java = new Tool('Java', 'JAVA_HOME', '', '/bin/', 'java', '1.8.0', '-version 2>&1') - def javaExecutable = ToolUtils.getToolExecutable(java, script, configuration) - executable = "$javaExecutable -jar $executable" - } - - script.echo "Verifying $tool.name version $tool.version or compatible version." - - def toolVersion - try { - toolVersion = script.sh returnStdout: true, script: "$executable $tool.versionOption" - } catch(AbortException e) { - throw new AbortException("The verification of $tool.name failed. Please check '$executable'. $e.message.") - } - def version = new Version(toolVersion) - if (!version.isCompatibleVersion(new Version(tool.version))) { - throw new AbortException("The installed version of $tool.name is ${version.toString()}. Please install version $tool.version or a compatible version.") - } - script.echo "Verification success. $tool.name version ${version.toString()} is installed." - } -} diff --git a/test/groovy/MTABuildTest.groovy b/test/groovy/MTABuildTest.groovy index 8fdc76eea..f16bf27e5 100644 --- a/test/groovy/MTABuildTest.groovy +++ b/test/groovy/MTABuildTest.groovy @@ -118,8 +118,8 @@ public class MtaBuildTest extends BasePipelineTest { assert jscr.shell.find { c -> c.contains(' -jar mta.jar --mtar ')} - assert jlr.log.contains("SAP Multitarget Application Archive Builder expected on PATH or current working directory.") - assert jlr.log.contains("Using SAP Multitarget Application Archive Builder executable 'mta.jar'.") + assert jlr.log.contains("SAP Multitarget Application Archive Builder expected on current working directory.") + assert jlr.log.contains("Using SAP Multitarget Application Archive Builder executable 'java -jar mta.jar'.") } @@ -131,7 +131,7 @@ public class MtaBuildTest extends BasePipelineTest { assert jscr.shell.find { c -> c.contains("-jar /mylocation/mta/mta.jar --mtar")} assert jlr.log.contains("SAP Multitarget Application Archive Builder home '/mylocation/mta' retrieved from configuration.") - assert jlr.log.contains("Using SAP Multitarget Application Archive Builder executable '/mylocation/mta/mta.jar'.") + assert jlr.log.contains("Using SAP Multitarget Application Archive Builder executable 'java -jar /mylocation/mta/mta.jar'.") } @@ -178,7 +178,7 @@ public class MtaBuildTest extends BasePipelineTest { assert jscr.shell.find { c -> c.contains("-jar /env/mta/mta.jar --mtar")} assert jlr.log.contains("SAP Multitarget Application Archive Builder home '/env/mta' retrieved from environment.") - assert jlr.log.contains("Using SAP Multitarget Application Archive Builder executable '/env/mta/mta.jar'.") + assert jlr.log.contains("Using SAP Multitarget Application Archive Builder executable 'java -jar /env/mta/mta.jar'.") } @@ -192,7 +192,7 @@ public class MtaBuildTest extends BasePipelineTest { assert jscr.shell.find(){ c -> c.contains("-jar /config/mta/mta.jar --mtar")} assert jlr.log.contains("SAP Multitarget Application Archive Builder home '/config/mta' retrieved from configuration.") - assert jlr.log.contains("Using SAP Multitarget Application Archive Builder executable '/config/mta/mta.jar'.") + assert jlr.log.contains("Using SAP Multitarget Application Archive Builder executable 'java -jar /config/mta/mta.jar'.") } @@ -318,7 +318,7 @@ public class MtaBuildTest extends BasePipelineTest { private getEnvVars(Map m) { if(m.script.contains('JAVA_HOME')) { - return '/env/java' + return '' } else if(m.script.contains('MTA_JAR_LOCATION')) { return '/env/mta' } else { diff --git a/test/groovy/NeoDeployTest.groovy b/test/groovy/NeoDeployTest.groovy index 5436e7e91..86fb43298 100644 --- a/test/groovy/NeoDeployTest.groovy +++ b/test/groovy/NeoDeployTest.groovy @@ -168,7 +168,7 @@ class NeoDeployTest extends BasePipelineTest { ) assert jscr.shell.find { c -> c.contains('"neo.sh" deploy-mta') } - assert jlr.log.contains('SAP Cloud Platform Console Client expected on PATH or current working directory.') + assert jlr.log.contains('SAP Cloud Platform Console Client expected on PATH.') assert jlr.log.contains("Using SAP Cloud Platform Console Client executable 'neo.sh'.") } diff --git a/test/groovy/com/sap/piper/tools/JavaArchiveDescriptorTest.groovy b/test/groovy/com/sap/piper/tools/JavaArchiveDescriptorTest.groovy new file mode 100644 index 000000000..4ee7697b5 --- /dev/null +++ b/test/groovy/com/sap/piper/tools/JavaArchiveDescriptorTest.groovy @@ -0,0 +1,214 @@ +package com.sap.piper.tools + +import org.junit.BeforeClass +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 +import util.Rules + +import com.lesfurets.jenkins.unit.BasePipelineTest + +import com.sap.piper.tools.ToolDescriptor +import com.sap.piper.tools.JavaArchiveDescriptor + +import hudson.AbortException + + +class JavaArchiveDescriptorTest extends BasePipelineTest { + + private ExpectedException thrown = new ExpectedException() + private JenkinsLoggingRule jlr = new JenkinsLoggingRule(this) + + @Rule + public RuleChain rules = Rules.getCommonRules(this) + .around(thrown) + .around(jlr) + + private static tool + private static configuration + + private script + + + @BeforeClass + static void init() { + + def java = new ToolDescriptor('Java', 'JAVA_HOME', '', '/bin/', 'java', '1.8.0', '-version 2>&1') + tool = new JavaArchiveDescriptor('SAP Multitarget Application Archive Builder', 'MTA_JAR_LOCATION', 'mtaJarLocation', '/', 'mta.jar', '1.0.6', '-v', java, '-jar') + } + + @Before + void setup() { + + helper.registerAllowedMethod('sh', [Map], { Map m -> getNoEnvVars(m) }) + + script = loadScript('mtaBuild.groovy').mtaBuild + + configuration = [:] + } + + @Test + void getToolHomeFromEnvironmentTest() { + + helper.registerAllowedMethod('sh', [Map], { Map m -> getEnvVars(m) }) + + def toolHome = tool.getHome(script, configuration) + + assert toolHome == '/env/mta' + assert jlr.log.contains("$tool.name home '/env/mta' retrieved from environment.") + } + + @Test + void getToolHomeFromConfigurationTest() { + + configuration = [mtaJarLocation: '/config/mta'] + + def toolHome = tool.getHome(script, configuration) + + assert toolHome == '/config/mta' + assert jlr.log.contains("$tool.name home '/config/mta' retrieved from configuration.") + } + + @Test + void getToolHomeFromCurrentWorkingDirectoryTest() { + + def toolHome = tool.getHome(script, configuration) + + assert toolHome == '' + assert jlr.log.contains("$tool.name expected on current working directory.") + } + + @Test + void getToolTest() { + + configuration = [mtaJarLocation: '/config/mta'] + + def toolExecutable = tool.getTool(script, configuration) + + assert toolExecutable == '/config/mta/mta.jar' + } + + @Test + void getToolExecutableTest() { + + configuration = [mtaJarLocation: '/config/mta'] + + def toolExecutable = tool.getExecutable(script, configuration) + + assert toolExecutable == 'java -jar /config/mta/mta.jar' + assert jlr.log.contains("Using $tool.name executable 'java -jar /config/mta/mta.jar'.") + } + + @Test + void verifyToolHomeTest() { + + helper.registerAllowedMethod('sh', [Map], { Map m -> getEnvVars(m) }) + + tool.verifyHome(script, configuration) + + assert jlr.log.contains("Verifying $tool.name home '/env/mta'.") + assert jlr.log.contains("Verification success. $tool.name home '/env/mta' exists.") + } + + @Test + void verifyToolExecutableTest() { + + helper.registerAllowedMethod('sh', [Map], { Map m -> getEnvVars(m) }) + + tool.verifyTool(script, configuration) + + assert jlr.log.contains("Verifying $tool.name '/env/mta/mta.jar'.") + assert jlr.log.contains("Verification success. $tool.name '/env/mta/mta.jar' exists.") + } + + @Test + void verifyToolVersionTest() { + + helper.registerAllowedMethod('sh', [Map], { Map m -> getVersion(m) }) + + tool.verifyVersion(script, configuration) + + assert jlr.log.contains("Verifying $tool.name version $tool.version or compatible version.") + assert jlr.log.contains("Verification success. $tool.name version $tool.version is installed.") + } + + @Test + void verifyToolVersion_FailedTest() { + + thrown.expect(AbortException) + thrown.expectMessage("The verification of $tool.name failed. Please check 'java -jar mta.jar'. script returned exit code 127.") + + helper.registerAllowedMethod('sh', [Map], { Map m -> getVersionFailed(m) }) + + tool.verifyVersion(script, configuration) + } + + @Test + void verifyToolVersion_IncompatibleVersionTest() { + + thrown.expect(AbortException) + thrown.expectMessage("The installed version of $tool.name is 1.0.5. Please install version $tool.version or a compatible version.") + + helper.registerAllowedMethod('sh', [Map], { Map m -> getIncompatibleVersion(m) }) + + tool.verifyVersion(script, configuration) + } + + + private getEnvVars(Map m) { + + if(m.script.contains('JAVA_HOME')) { + return '/env/java' + } else if(m.script.contains('MTA_JAR_LOCATION')) { + return '/env/mta' + } else { + return 0 + } + } + + private getNoEnvVars(Map m) { + + if(m.script.contains('JAVA_HOME')) { + return '' + } else if(m.script.contains('MTA_JAR_LOCATION')) { + return '' + } else { + return 0 + } + } + + private getVersion(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 '' + } + } + + private getVersionFailed(Map m) { + + if(m.script.contains('java -version') || m.script.contains('mta.jar -v')) { + throw new AbortException('script returned exit code 127') + } else { + return '' + } + } + + private getIncompatibleVersion(Map m) { + + if(m.script.contains('java -version') || m.script.contains('mta.jar -v')) { + return '1.0.5' + } else { + return '' + } + } +} diff --git a/test/groovy/com/sap/piper/tools/ToolUtilsTest.groovy b/test/groovy/com/sap/piper/tools/ToolUtilsTest.groovy deleted file mode 100644 index 64dec357d..000000000 --- a/test/groovy/com/sap/piper/tools/ToolUtilsTest.groovy +++ /dev/null @@ -1,92 +0,0 @@ -package com.sap.piper.tools - -import org.junit.BeforeClass -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 -import util.Rules - -import com.lesfurets.jenkins.unit.BasePipelineTest - -import com.sap.piper.tools.Tool -import com.sap.piper.tools.ToolUtils - - -class ToolUtilsTest extends BasePipelineTest { - - private ExpectedException thrown = new ExpectedException() - private JenkinsLoggingRule jlr = new JenkinsLoggingRule(this) - - @Rule - public RuleChain rules = Rules.getCommonRules(this) - .around(thrown) - .around(jlr) - - private script - - private static tool - private static configuration - - - @BeforeClass - static void init() { - - tool = new Tool('SAP Multitarget Application Archive Builder', 'MTA_JAR_LOCATION', 'mtaJarLocation', '/', 'mta.jar', '1.0.6', '-v') - } - - @Before - void setup() { - - helper.registerAllowedMethod('sh', [Map], { Map m -> return '' }) - - script = loadScript('mtaBuild.groovy').mtaBuild - - configuration = [:] - } - - @Test - void getToolHomeFromEnvironmentTest() { - - helper.registerAllowedMethod('sh', [Map], { Map m -> return '/env/mta' }) - - def toolHome = ToolUtils.getToolHome(tool, script, configuration) - - assert toolHome == '/env/mta' - assert jlr.log.contains("$tool.name home '/env/mta' retrieved from environment.") - } - - @Test - void getToolHomeFromConfigurationTest() { - - configuration = [mtaJarLocation: '/config/mta'] - - def toolHome = ToolUtils.getToolHome(tool, script, configuration) - - assert toolHome == '/config/mta' - assert jlr.log.contains("$tool.name home '/config/mta' retrieved from configuration.") - } - - @Test - void getToolHomeFromCurrentWorkingDirectoryTest() { - - def toolHome = ToolUtils.getToolHome(tool, script, configuration) - - assert toolHome == '' - assert jlr.log.contains("$tool.name expected on PATH or current working directory.") - } - - @Test - void getToolExecutableTest() { - - configuration = [mtaJarLocation: '/config/mta'] - - def toolExecutable = ToolUtils.getToolExecutable(tool, script, configuration) - - assert toolExecutable == '/config/mta/mta.jar' - assert jlr.log.contains("Using $tool.name executable '/config/mta/mta.jar'.") - } -} diff --git a/test/groovy/com/sap/piper/tools/ToolVerifierTest.groovy b/test/groovy/com/sap/piper/tools/ToolVerifierTest.groovy deleted file mode 100644 index 8581a997a..000000000 --- a/test/groovy/com/sap/piper/tools/ToolVerifierTest.groovy +++ /dev/null @@ -1,143 +0,0 @@ -package com.sap.piper.tools - -import org.junit.BeforeClass -import org.junit.Before -import org.junit.Rule -import org.junit.Test -import org.junit.rules.ExpectedException -import org.junit.rules.TemporaryFolder -import org.junit.rules.RuleChain - -import util.JenkinsLoggingRule -import util.Rules - -import com.lesfurets.jenkins.unit.BasePipelineTest - -import com.sap.piper.tools.Tool -import com.sap.piper.tools.ToolVerifier - -import hudson.AbortException - - -class ToolVerifierTest extends BasePipelineTest { - - private ExpectedException thrown = new ExpectedException().none() - private JenkinsLoggingRule jlr = new JenkinsLoggingRule(this) - - @Rule - public RuleChain ruleChain = Rules.getCommonRules(this) - .around(thrown) - .around(jlr) - - private script - private static configuration - private static tool - - - @BeforeClass - static void init() { - - configuration = [mtaJarLocation: 'home'] - tool = new Tool('SAP Multitarget Application Archive Builder', 'MTA_JAR_LOCATION', 'mtaJarLocation', '/', 'mta.jar', '1.0.6', '-v') - } - - @Before - void setup() { - - script = loadScript('mtaBuild.groovy').mtaBuild - } - - - @Test - void verifyToolHomeTest() { - - helper.registerAllowedMethod('sh', [Map], { Map m -> getToolHome(m) }) - - ToolVerifier.verifyToolHome(tool, script, configuration) - - assert jlr.log.contains("Verifying $tool.name home '/env/mta'.") - assert jlr.log.contains("Verification success. $tool.name home '/env/mta' exists.") - } - - @Test - void verifyToolExecutableTest() { - - helper.registerAllowedMethod('sh', [Map], { Map m -> getToolHome(m) }) - - ToolVerifier.verifyToolExecutable(tool, script, configuration) - - assert jlr.log.contains("Verifying $tool.name executable '/env/mta/mta.jar'.") - assert jlr.log.contains("Verification success. $tool.name executable '/env/mta/mta.jar' exists.") - } - - @Test - void verifyToolVersionTest() { - - helper.registerAllowedMethod('sh', [Map], { Map m -> getVersion(m) }) - - ToolVerifier.verifyToolVersion(tool, script, configuration) - - assert jlr.log.contains("Verifying $tool.name version $tool.version or compatible version.") - assert jlr.log.contains("Verification success. $tool.name version $tool.version is installed.") - } - - @Test - void verifyToolVersion_FailedTest() { - - thrown.expect(AbortException) - thrown.expectMessage("The verification of $tool.name failed.") - - helper.registerAllowedMethod('sh', [Map], { Map m -> getVersionFailed(m) }) - - ToolVerifier.verifyToolVersion(tool, script, configuration) - } - - @Test - void verifyToolVersion_IncompatibleVersionTest() { - - thrown.expect(AbortException) - thrown.expectMessage("The installed version of $tool.name is 1.0.5.") - - helper.registerAllowedMethod('sh', [Map], { Map m -> getIncompatibleVersion(m) }) - - ToolVerifier.verifyToolVersion(tool, script, configuration) - } - - - private getToolHome(Map m) { - - if(m.script.contains('MTA_JAR_LOCATION')) { - return '/env/mta' - } else { - return 0 - } - } - - private getVersion(Map m) { - - if(m.script.contains('mta.jar -v')) { - return '1.0.6' - } else { - return '' - } - } - - private getVersionFailed(Map m) { - - if(m.script.contains('mta.jar -v')) { - throw new AbortException('script returned exit code 127') - } else { - return '' - } - } - - private getIncompatibleVersion(Map m) { - - if(m.script.contains('mta.jar -v')) { - return '1.0.5' - } else { - return '' - } - } -} - diff --git a/vars/mtaBuild.groovy b/vars/mtaBuild.groovy index f07f41e58..9d9402cdc 100644 --- a/vars/mtaBuild.groovy +++ b/vars/mtaBuild.groovy @@ -1,8 +1,7 @@ import com.sap.piper.ConfigurationMerger import com.sap.piper.MtaUtils -import com.sap.piper.tools.Tool -import com.sap.piper.tools.ToolVerifier -import com.sap.piper.tools.ToolUtils +import com.sap.piper.tools.JavaArchiveDescriptor +import com.sap.piper.tools.ToolDescriptor def call(Map parameters = [:]) { @@ -32,11 +31,11 @@ def call(Map parameters = [:]) { parameters, parameterKeys, stepConfigurationKeys) - def mta = new Tool('SAP Multitarget Application Archive Builder', 'MTA_JAR_LOCATION', 'mtaJarLocation', '/', 'mta.jar', '1.0.6', '-v') - ToolVerifier.verifyToolVersion(mta, this, configuration) + def java = new ToolDescriptor('Java', 'JAVA_HOME', '', '/bin/', 'java', '1.8.0', '-version 2>&1') + java.verify(this, configuration) - def java = new Tool('Java', 'JAVA_HOME', '', '/bin/', 'java', '1.8.0', '-version 2>&1') - ToolVerifier.verifyToolVersion(java, this, configuration) + def mta = new JavaArchiveDescriptor('SAP Multitarget Application Archive Builder', 'MTA_JAR_LOCATION', 'mtaJarLocation', '/', 'mta.jar', '1.0.6', '-v', java, '-jar') + mta.verify(this, configuration) def mtaYmlName = "${pwd()}/mta.yaml" def applicationName = configuration.applicationName @@ -51,7 +50,7 @@ def call(Map parameters = [:]) { } def mtaYaml = readYaml file: "${pwd()}/mta.yaml" - + //[Q]: Why not yaml.dump()? [A]: This reformats the whole file. sh "sed -ie \"s/\\\${timestamp}/`date +%Y%m%d%H%M%S`/g\" \"${pwd()}/mta.yaml\"" @@ -61,7 +60,7 @@ def call(Map parameters = [:]) { } def mtarFileName = "${id}.mtar" - def mtaJar = ToolUtils.getToolExecutable(mta, this, configuration) + def mtaJar = mta.getExecutable(this, configuration) def buildTarget = configuration.buildTarget sh """#!/bin/bash diff --git a/vars/neoDeploy.groovy b/vars/neoDeploy.groovy index 3501f7d03..c649fe380 100644 --- a/vars/neoDeploy.groovy +++ b/vars/neoDeploy.groovy @@ -3,9 +3,7 @@ import com.sap.piper.Utils import com.sap.piper.ConfigurationLoader import com.sap.piper.ConfigurationMerger import com.sap.piper.ConfigurationType -import com.sap.piper.tools.Tool -import com.sap.piper.tools.ToolVerifier -import com.sap.piper.tools.ToolUtils +import com.sap.piper.tools.ToolDescriptor def call(parameters = [:]) { @@ -145,8 +143,8 @@ def call(parameters = [:]) { deployAccount = utils.getMandatoryParameter(configuration, 'account') } - def neo = new Tool('SAP Cloud Platform Console Client', 'NEO_HOME', 'neoHome', '/tools/', 'neo.sh', '3.39.10', 'version') - def neoExecutable = ToolUtils.getToolExecutable(neo, this, configuration) + def neo = new ToolDescriptor('SAP Cloud Platform Console Client', 'NEO_HOME', 'neoHome', '/tools/', 'neo.sh', '3.39.10', 'version') + def neoExecutable = neo.getExecutable(this, configuration) def neoDeployScript if (deployMode == 'mta') { @@ -191,10 +189,10 @@ def call(parameters = [:]) { dockerEnvVars: configuration.get('dockerEnvVars'), dockerOptions: configuration.get('dockerOptions')) { - ToolVerifier.verifyToolVersion(neo, this, configuration) + neo.verify(this, configuration) - def java = new Tool('Java', 'JAVA_HOME', '', '/bin/', 'java', '1.8.0', '-version 2>&1') - ToolVerifier.verifyToolVersion(java, this, configuration) + def java = new ToolDescriptor('Java', 'JAVA_HOME', '', '/bin/', 'java', '1.8.0', '-version 2>&1') + java.verify(this, configuration) sh """${neoDeployScript} \ ${commonDeployParams} diff --git a/vars/toolValidate.groovy b/vars/toolValidate.groovy index 317318974..2103d6ab6 100644 --- a/vars/toolValidate.groovy +++ b/vars/toolValidate.groovy @@ -1,7 +1,8 @@ import com.sap.piper.FileUtils import com.sap.piper.Version -import com.sap.piper.tools.Tool -import com.sap.piper.tools.ToolVerifier +import com.sap.piper.tools.JavaArchiveDescriptor +import com.sap.piper.tools.ToolDescriptor + import hudson.AbortException @@ -19,20 +20,21 @@ def call(Map parameters = [:]) { switch(tool) { case 'java': - def java = new Tool('Java', 'JAVA_HOME', '', '/bin/', 'java', '1.8.0', '-version 2>&1') - ToolVerifier.verifyToolVersion(java, this, [:]) + def java = new ToolDescriptor('Java', 'JAVA_HOME', '', '/bin/', 'java', '1.8.0', '-version 2>&1') + java.verifyVersion(this, [:]) return case 'mta': - def mta = new Tool('SAP Multitarget Application Archive Builder', 'MTA_JAR_LOCATION', 'mtaJarLocation', '/', 'mta.jar', '1.0.6', '-v') - ToolVerifier.verifyToolVersion(mta, this, [mtaJarLocation: home]) + def java = new ToolDescriptor('Java', 'JAVA_HOME', '', '/bin/', 'java', '1.8.0', '-version 2>&1') + def mta = new JavaArchiveDescriptor('SAP Multitarget Application Archive Builder', 'MTA_JAR_LOCATION', 'mtaJarLocation', '/', 'mta.jar', '1.0.6', '-v', java, '-jar') + mta.verifyVersion(this, [mtaJarLocation: home]) return case 'neo': - def neo = new Tool('SAP Cloud Platform Console Client', 'NEO_HOME', 'neoHome', '/tools/', 'neo.sh', '3.39.10', 'version') - ToolVerifier.verifyToolVersion(neo, this, [neoHome: home]) + def neo = new ToolDescriptor('SAP Cloud Platform Console Client', 'NEO_HOME', 'neoHome', '/tools/', 'neo.sh', '3.39.10', 'version') + neo.verifyVersion(this, [neoHome: home]) return case 'cm': - def cmCli = new Tool('Change Management Command Line Interface', 'CM_CLI_HOME', 'cmCliHome', '/bin/', 'cmclient', '0.0.1', '-v') - ToolVerifier.verifyToolVersion(cmCli, this, [cmCliHome: home]) + def cmCli = new ToolDescriptor('Change Management Command Line Interface', 'CM_CLI_HOME', 'cmCliHome', '/bin/', 'cmclient', '0.0.1', '-v') + cmCli.verifyVersion(this, [cmCliHome: home]) return default: throw new AbortException("The tool \'$tool\' is not supported. The following tools are supported: java, mta, neo and cm.")