1
0
mirror of https://github.com/SAP/jenkins-library.git synced 2025-01-04 04:07:16 +02:00
This commit is contained in:
Alejandra Ferreiro Vidal 2018-04-10 18:23:44 +02:00
parent 63ced4a0ea
commit 64b45a13c4
12 changed files with 92 additions and 112 deletions

View File

@ -6,15 +6,7 @@ import hudson.AbortException
class EnvironmentUtils implements Serializable {
def static isEnvironmentVariable(script, variable) {
def envVar
try {
envVar = script.sh returnStdout: true, script: """#!/bin/bash --login
echo \$$variable"""
} catch(AbortException e) {
throw new AbortException("The verification of the environment variable '$variable' failed. Reason: $e.message.")
}
if (envVar.trim()) return true
else return false
return !getEnvironmentVariable(script, variable).isEmpty()
}
def static getEnvironmentVariable(script, variable) {

View File

@ -9,8 +9,7 @@ class FileUtils implements Serializable {
static directoryOrFileExists(script, dirOrFile) {
if (!dirOrFile) throw new IllegalArgumentException("The parameter 'dirOrFile' can not be null or empty.")
def returnStatus = script.sh returnStatus: true, script: """
#!/bin/bash --login
set +x
if [ -d $dirOrFile ]; then
echo \"$dirOrFile exists.\"
exit 0
@ -22,32 +21,28 @@ class FileUtils implements Serializable {
exit 1
fi
"""
if (returnStatus == 0) return true
else return false
return returnStatus == 0
}
static isDirectory(script, dir) {
if (!dir) throw new IllegalArgumentException("The parameter 'dir' can not be null or empty.")
def returnStatus = script.sh returnStatus: true, script: """
#!/bin/bash --login
set +x
if [ -d $dir ]; then
echo \"$dir is a directory.\"
exit 0
else
echo \"$dir is not a directory.\"
exit 0
exit 1
fi
"""
if (returnStatus == 0) return true
else return false
return returnStatus == 0
}
static isDirectoryEmpty(script, dir) {
if (!dir) throw new IllegalArgumentException("The parameter 'dir' can not be null or empty.")
def returnStatus = script.sh returnStatus: true, script: """
#!/bin/bash --login
set +x
if [ -z "\$(ls -A $dir)" ]; then
echo "$dir is empty."
exit 1
@ -56,15 +51,13 @@ class FileUtils implements Serializable {
exit 0
fi
"""
if (returnStatus == 0) return false
else return true
return returnStatus == 1
}
static isFile(script, filePath) {
if (!filePath) throw new IllegalArgumentException("The parameter 'filePath' can not be null or empty.")
def returnStatus = script.sh returnStatus: true, script: """
#!/bin/bash --login
set +x
if [ -f $filePath ]; then
echo \"$filePath is a file.\"
exit 0
@ -73,8 +66,7 @@ class FileUtils implements Serializable {
exit 1
fi
"""
if (returnStatus == 0) return true
else return false
return returnStatus == 0
}
static validateDirectoryOrFileExists(script, dirOrFile) {

View File

@ -11,14 +11,14 @@ class JavaArchiveDescriptor extends ToolDescriptor {
final javaTool
final javaOptions
JavaArchiveDescriptor(name, environmentKey, stepConfigurationKey, executablePath, executableName, version, versionOption, javaTool, 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 getToolLocation(script, configuration, log = true) {
def home
if (EnvironmentUtils.isEnvironmentVariable(script, environmentKey)) {
@ -32,23 +32,24 @@ class JavaArchiveDescriptor extends ToolDescriptor {
home = ''
if (log) script.echo "$name expected on current working directory."
} else {
throw new AbortException(getConfigurationOptions())
throw new AbortException(getMessage())
}
return home
}
@Override
def getExecutable(script, configuration, log = true) {
def getToolExecutable(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
def javaArchive = getTool(script, configuration, log)
if (log) script.echo "Using $name '$javaArchive'."
def javaExecutable = javaTool.getToolExecutable(script, configuration, false)
def javaCall = "$javaExecutable -jar"
if (javaOptions) javaCall += " $javaOptions"
return "$javaCall $javaArchive"
}
@Override
def getConfigurationOptions() {
def getMessage() {
def configOptions = "Please, configure $name home. $name home can be set "
if (environmentKey) configOptions += "using the environment variable '$environmentKey'"
if (environmentKey && stepConfigurationKey) configOptions += ", or "

View File

@ -27,65 +27,62 @@ class ToolDescriptor implements Serializable {
this.versionOption = versionOption
}
def getHome(script, configuration, log = true) {
def getToolLocation(script, configuration, log = true) {
def home
def toolLocation
if (EnvironmentUtils.isEnvironmentVariable(script, environmentKey)) {
home = EnvironmentUtils.getEnvironmentVariable(script, environmentKey)
if (log) script.echo "$name home '$home' retrieved from environment."
toolLocation = EnvironmentUtils.getEnvironmentVariable(script, environmentKey)
if (log) script.echo "$name home '$toolLocation' retrieved from environment."
}
else if (configuration.containsKey(stepConfigurationKey)) {
home = configuration.get(stepConfigurationKey)
if (log) script.echo "$name home '$home' retrieved from configuration."
toolLocation = configuration.get(stepConfigurationKey)
if (log) script.echo "$name home '$toolLocation' retrieved from configuration."
} else if (isOnPath(script, configuration)){
home = ''
toolLocation = ''
if (log) script.echo "$name is on PATH."
} else {
throw new AbortException(getConfigurationOptions())
throw new AbortException(getMessage())
}
return home
return toolLocation
}
def getTool(script, configuration, log = true) {
def home = getHome(script, configuration, log)
def toolLocation = getToolLocation(script, configuration, log)
def path = "$executablePath"
def executable = "$executableName"
if (home) {
return "$home$path$executable"
if (toolLocation) {
return "$toolLocation$executablePath$executableName"
} else {
return "$executable"
return executableName
}
}
def getExecutable(script, configuration, log = true) {
def getToolExecutable(script, configuration, log = true) {
def executable = getTool(script, configuration, log)
if (log) script.echo "Using $name executable '$executable'."
if (log) script.echo "Using $name '$executable'."
return executable
}
def verify(script, configuration) {
verifyHome(script, configuration)
verifyTool(script, configuration)
verifyToolLocation(script, configuration)
verifyToolExecutable(script, configuration)
verifyVersion(script, configuration)
}
def verifyHome(script, configuration) {
def verifyToolLocation(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 toolLocation = getToolLocation(script, configuration)
if (toolLocation) {
script.echo "Verifying $name location '$toolLocation'."
FileUtils.validateDirectoryIsNotEmpty(script, toolLocation)
script.echo "Verification success. $name location '$toolLocation' exists."
}
}
def verifyTool(script, configuration) {
def verifyToolExecutable(script, configuration) {
def home = getHome(script, configuration, false)
def home = getToolLocation(script, configuration, false)
def tool = getTool(script, configuration, false)
if (home) {
script.echo "Verifying $name '$tool'."
@ -96,13 +93,14 @@ class ToolDescriptor implements Serializable {
def verifyVersion(script, configuration) {
def executable = getExecutable(script, configuration, false)
def executable = getToolExecutable(script, configuration, false)
script.echo "Verifying $name version $version or compatible version."
def toolVersion
try {
toolVersion = script.sh returnStdout: true, script: "$executable $versionOption"
toolVersion = script.sh returnStdout: true, script: """#!/bin/bash
$executable $versionOption"""
} catch(AbortException e) {
throw new AbortException("The verification of $name failed. Please check '$executable'. $e.message.")
}
@ -113,7 +111,7 @@ class ToolDescriptor implements Serializable {
script.echo "Verification success. $name version ${installedVersion.toString()} is installed."
}
def getConfigurationOptions() {
def getMessage() {
def configOptions = "Please, configure $name home. $name home can be set "
if (environmentKey) configOptions += "using the environment variable '$environmentKey', or "
if (stepConfigurationKey) configOptions += "using the configuration key '$stepConfigurationKey', or "
@ -123,17 +121,13 @@ class ToolDescriptor implements Serializable {
def isOnPath(script, configuration) {
def path
def exitStatus
try {
path = script.sh returnStdout: true, script: """#!/bin/bash --login
which $executableName"""
exitStatus = script.sh returnStatus: true, script: """set +x
which $executableName"""
} catch(AbortException e) {
def exitStatus = script.sh returnStatus: true, script: """#!/bin/bash --login
which $executableName"""
if (exitStatus == 1) return false
else throw new AbortException("The verification of $name failed. Script execution 'which $executableName' failed. $e.message.")
throw new AbortException("The verification of $name failed, while checking if it was on PATH. Reason: $e.message.")
}
if (path.trim()) return true
else return false
return exitStatus == 0
}
}

View File

@ -45,7 +45,6 @@ public class MtaBuildTest extends BasePipelineTest {
private static newDir
private static mtaYaml
@BeforeClass
static void createTestFiles() {
@ -131,18 +130,18 @@ 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 current working directory.")
assert jlr.log.contains("Using SAP Multitarget Application Archive Builder executable 'java -jar mta.jar'.")
assert jlr.log.contains("Using SAP Multitarget Application Archive Builder 'mta.jar'.")
}
@Test
void mtaJarLocationAsParameterTest() {
jsr.step.call(mtaJarLocation: "/mylocation/mta", buildTarget: 'NEO')
jsr.step.call(mtaJarLocation: '/mylocation/mta', buildTarget: 'NEO')
assert jscr.shell.find { c -> c.contains("-jar /mylocation/mta/mta.jar --mtar")}
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 'java -jar /mylocation/mta/mta.jar'.")
assert jlr.log.contains("Using SAP Multitarget Application Archive Builder '/mylocation/mta/mta.jar'.")
}
@ -189,21 +188,21 @@ 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 'java -jar /env/mta/mta.jar'.")
assert jlr.log.contains("Using SAP Multitarget Application Archive Builder '/env/mta/mta.jar'.")
}
@Test
void mtaJarLocationFromCustomStepConfigurationTest() {
jer.env.configuration = [steps:[mtaBuild:[mtaJarLocation: "/config/mta"]]]
jer.env.configuration = [steps:[mtaBuild:[mtaJarLocation: '/config/mta']]]
jsr.step.call(script: [commonPipelineEnvironment: jer.env],
buildTarget: 'NEO')
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 'java -jar /config/mta/mta.jar'.")
assert jlr.log.contains("Using SAP Multitarget Application Archive Builder '/config/mta/mta.jar'.")
}
@ -346,7 +345,7 @@ public class MtaBuildTest extends BasePipelineTest {
} else if(m.script.contains('MTA_JAR_LOCATION')) {
return '/env/mta'
} else if(m.script.contains('which java')) {
return '/path/java'
return 0
} else {
return 0
}
@ -359,7 +358,7 @@ public class MtaBuildTest extends BasePipelineTest {
} else if(m.script.contains('MTA_JAR_LOCATION')) {
return ''
} else if(m.script.contains('which java')) {
return '/path/java'
return 0
} else {
return 0
}
@ -372,7 +371,7 @@ public class MtaBuildTest extends BasePipelineTest {
} else if(m.script.contains('MTA_JAR_LOCATION')) {
return ''
} else if(m.script.contains('which java')) {
return '/path/java'
return 0
} else {
return 1
}

View File

@ -169,7 +169,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 is on PATH.')
assert jlr.log.contains("Using SAP Cloud Platform Console Client executable 'neo.sh'.")
assert jlr.log.contains("Using SAP Cloud Platform Console Client 'neo.sh'.")
}
@ -186,7 +186,7 @@ class NeoDeployTest extends BasePipelineTest {
assert jscr.shell.find{ c -> c = "\"/param/neo/tools/neo.sh\" deploy-mta" }
assert jlr.log.contains("SAP Cloud Platform Console Client home '/param/neo' retrieved from configuration.")
assert jlr.log.contains("Using SAP Cloud Platform Console Client executable '/param/neo/tools/neo.sh'.")
assert jlr.log.contains("Using SAP Cloud Platform Console Client '/param/neo/tools/neo.sh'.")
}
@ -199,7 +199,7 @@ class NeoDeployTest extends BasePipelineTest {
assert jscr.shell.find { c -> c.contains("\"/opt/neo/tools/neo.sh\" deploy-mta")}
assert jlr.log.contains("SAP Cloud Platform Console Client home '/opt/neo' retrieved from environment.")
assert jlr.log.contains("Using SAP Cloud Platform Console Client executable '/opt/neo/tools/neo.sh'.")
assert jlr.log.contains("Using SAP Cloud Platform Console Client '/opt/neo/tools/neo.sh'.")
}
@ -216,7 +216,7 @@ class NeoDeployTest extends BasePipelineTest {
assert jscr.shell.find { c -> c = "\"/config/neo/tools/neo.sh\" deploy-mta"}
assert jlr.log.contains("SAP Cloud Platform Console Client home '/config/neo' retrieved from configuration.")
assert jlr.log.contains("Using SAP Cloud Platform Console Client executable '/config/neo/tools/neo.sh'.")
assert jlr.log.contains("Using SAP Cloud Platform Console Client '/config/neo/tools/neo.sh'.")
}
@ -475,9 +475,9 @@ class NeoDeployTest extends BasePipelineTest {
} else if(m.script.contains('NEO_HOME')) {
return '/opt/neo'
} else if (m.script.contains('which java')) {
return ''
return 0
} else if (m.script.contains('which neo')) {
return ''
return 0
} else {
return 0
}
@ -490,9 +490,9 @@ class NeoDeployTest extends BasePipelineTest {
} else if(m.script.contains('NEO_HOME')) {
return ''
} else if (m.script.contains('which java')) {
return '/path/java'
return 0
} else if (m.script.contains('which neo')) {
return '/path/neo'
return 0
} else {
return 0
}

View File

@ -38,7 +38,7 @@ class EnvironmentUtilsTest extends BasePipelineTest {
helper.registerAllowedMethod('sh', [Map], { Map m -> throw new AbortException('') })
thrown.expect(AbortException)
thrown.expectMessage("The verification of the environment variable 'JAVA_HOME' failed.")
thrown.expectMessage("There was an error requesting the environment variable 'JAVA_HOME'.")
EnvironmentUtils.isEnvironmentVariable(script, 'JAVA_HOME')
}

View File

@ -60,7 +60,7 @@ class FileUtilsTest extends BasePipelineTest {
}
@Test
void validateDirectory_emptyParmaterTest() {
void validateDirectory_emptyParameterTest() {
thrown.expect(IllegalArgumentException)
thrown.expectMessage("The parameter 'dir' can not be null or empty.")

View File

@ -38,7 +38,7 @@ class JavaArchiveDescriptorTest extends BasePipelineTest {
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')
tool = new JavaArchiveDescriptor('SAP Multitarget Application Archive Builder', 'MTA_JAR_LOCATION', 'mtaJarLocation', '/', 'mta.jar', '1.0.6', '-v', java)
}
@Before
@ -56,7 +56,7 @@ class JavaArchiveDescriptorTest extends BasePipelineTest {
helper.registerAllowedMethod('sh', [Map], { Map m -> getEnvVars(m) })
def toolHome = tool.getHome(script, configuration)
def toolHome = tool.getToolLocation(script, configuration)
assert toolHome == '/env/mta'
assert jlr.log.contains("$tool.name home '/env/mta' retrieved from environment.")
@ -67,7 +67,7 @@ class JavaArchiveDescriptorTest extends BasePipelineTest {
configuration = [mtaJarLocation: '/config/mta']
def toolHome = tool.getHome(script, configuration)
def toolHome = tool.getToolLocation(script, configuration)
assert toolHome == '/config/mta'
assert jlr.log.contains("$tool.name home '/config/mta' retrieved from configuration.")
@ -76,7 +76,7 @@ class JavaArchiveDescriptorTest extends BasePipelineTest {
@Test
void getToolHomeFromCurrentWorkingDirectoryTest() {
def toolHome = tool.getHome(script, configuration)
def toolHome = tool.getToolLocation(script, configuration)
assert toolHome == ''
assert jlr.log.contains("$tool.name expected on current working directory.")
@ -97,10 +97,10 @@ class JavaArchiveDescriptorTest extends BasePipelineTest {
configuration = [mtaJarLocation: '/config/mta']
def toolExecutable = tool.getExecutable(script, configuration)
def toolExecutable = tool.getToolExecutable(script, configuration)
assert toolExecutable == 'java -jar /config/mta/mta.jar'
assert jlr.log.contains("Using $tool.name executable 'java -jar /config/mta/mta.jar'.")
assert jlr.log.contains("Using $tool.name '/config/mta/mta.jar'.")
}
@Test
@ -108,10 +108,10 @@ class JavaArchiveDescriptorTest extends BasePipelineTest {
helper.registerAllowedMethod('sh', [Map], { Map m -> getEnvVars(m) })
tool.verifyHome(script, configuration)
tool.verifyToolLocation(script, configuration)
assert jlr.log.contains("Verifying $tool.name home '/env/mta'.")
assert jlr.log.contains("Verification success. $tool.name home '/env/mta' exists.")
assert jlr.log.contains("Verifying $tool.name location '/env/mta'.")
assert jlr.log.contains("Verification success. $tool.name location '/env/mta' exists.")
}
@Test
@ -119,7 +119,7 @@ class JavaArchiveDescriptorTest extends BasePipelineTest {
helper.registerAllowedMethod('sh', [Map], { Map m -> getEnvVars(m) })
tool.verifyTool(script, configuration)
tool.verifyToolExecutable(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.")
@ -177,7 +177,7 @@ class JavaArchiveDescriptorTest extends BasePipelineTest {
} else if(m.script.contains('MTA_JAR_LOCATION')) {
return ''
} else if(m.script.contains('which java')) {
return '/path/java'
return 0
} else {
return 0
}

View File

@ -34,7 +34,7 @@ def call(Map parameters = [:]) {
def java = new ToolDescriptor('Java', 'JAVA_HOME', '', '/bin/', 'java', '1.8.0', '-version 2>&1')
java.verify(this, configuration)
def mta = new JavaArchiveDescriptor('SAP Multitarget Application Archive Builder', 'MTA_JAR_LOCATION', 'mtaJarLocation', '/', 'mta.jar', '1.0.6', '-v', java, '-jar')
def mta = new JavaArchiveDescriptor('SAP Multitarget Application Archive Builder', 'MTA_JAR_LOCATION', 'mtaJarLocation', '/', 'mta.jar', '1.0.6', '-v', java)
mta.verify(this, configuration)
def mtaYmlName = "${pwd()}/mta.yaml"
@ -60,7 +60,7 @@ def call(Map parameters = [:]) {
}
def mtarFileName = "${id}.mtar"
def mtaJar = mta.getExecutable(this, configuration)
def mtaJar = mta.getToolExecutable(this, configuration)
def buildTarget = configuration.buildTarget
sh """#!/bin/bash

View File

@ -144,7 +144,7 @@ def call(parameters = [:]) {
}
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 neoExecutable = neo.getToolExecutable(this, configuration)
def neoDeployScript
if (deployMode == 'mta') {

View File

@ -10,6 +10,8 @@ def call(Map parameters = [:]) {
handlePipelineStepErrors (stepName: 'toolValidate', stepParameters: parameters) {
echo '[WARNING][toolValidate] This step is deprecated, and it will be removed in future versions. Validation is automatically done inside the steps.'
def tool = parameters.tool
def home = parameters.home
@ -25,7 +27,7 @@ def call(Map parameters = [:]) {
return
case 'mta':
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')
def mta = new JavaArchiveDescriptor('SAP Multitarget Application Archive Builder', 'MTA_JAR_LOCATION', 'mtaJarLocation', '/', 'mta.jar', '1.0.6', '-v', java)
mta.verifyVersion(this, [mtaJarLocation: home])
return
case 'neo':