2018-04-20 10:59:32 +02:00
|
|
|
package com.sap.piper
|
2018-03-09 18:58:11 +02:00
|
|
|
|
|
|
|
import org.junit.Before
|
|
|
|
import org.junit.Rule
|
|
|
|
import org.junit.Test
|
|
|
|
import org.junit.rules.ExpectedException
|
|
|
|
import org.junit.rules.RuleChain
|
2018-06-06 11:19:19 +02:00
|
|
|
import util.BasePiperTest
|
|
|
|
import util.JenkinsStepRule
|
2018-03-09 18:58:11 +02:00
|
|
|
import util.Rules
|
|
|
|
|
|
|
|
import hudson.AbortException
|
|
|
|
|
|
|
|
|
2018-06-06 11:19:19 +02:00
|
|
|
class EnvironmentUtilsTest extends BasePiperTest {
|
2018-03-09 18:58:11 +02:00
|
|
|
|
|
|
|
private ExpectedException thrown = new ExpectedException()
|
|
|
|
|
|
|
|
@Rule
|
2018-06-06 11:19:19 +02:00
|
|
|
public RuleChain rules = Rules
|
|
|
|
.getCommonRules(this)
|
|
|
|
.around(thrown)
|
2018-03-09 18:58:11 +02:00
|
|
|
|
|
|
|
@Test
|
|
|
|
void isEnvironmentVariableFailedTest() {
|
|
|
|
|
|
|
|
helper.registerAllowedMethod('sh', [Map], { Map m -> throw new AbortException('') })
|
|
|
|
|
|
|
|
thrown.expect(AbortException)
|
2018-04-10 18:23:44 +02:00
|
|
|
thrown.expectMessage("There was an error requesting the environment variable 'JAVA_HOME'.")
|
2018-03-09 18:58:11 +02:00
|
|
|
|
2018-06-06 11:19:19 +02:00
|
|
|
EnvironmentUtils.isEnvironmentVariable(nullScript, 'JAVA_HOME')
|
2018-03-09 18:58:11 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@Test
|
|
|
|
void isNotEnvironmentVariableTest() {
|
|
|
|
|
|
|
|
helper.registerAllowedMethod('sh', [Map], { Map m -> return '' })
|
|
|
|
|
2018-06-06 11:19:19 +02:00
|
|
|
def isEnvVar = EnvironmentUtils.isEnvironmentVariable(nullScript, 'JAVA_HOME')
|
2018-03-09 18:58:11 +02:00
|
|
|
|
|
|
|
assert isEnvVar == false
|
|
|
|
}
|
|
|
|
|
|
|
|
@Test
|
|
|
|
void isEnvironmentVariableTest() {
|
|
|
|
|
|
|
|
helper.registerAllowedMethod('sh', [Map], { Map m -> return '/env/java' })
|
|
|
|
|
2018-06-06 11:19:19 +02:00
|
|
|
def isEnvVar = EnvironmentUtils.isEnvironmentVariable(nullScript, 'JAVA_HOME')
|
2018-03-09 18:58:11 +02:00
|
|
|
|
|
|
|
assert isEnvVar == true
|
|
|
|
}
|
|
|
|
|
|
|
|
@Test
|
|
|
|
void getEnvironmentVariableFailedTest() {
|
|
|
|
|
|
|
|
helper.registerAllowedMethod('sh', [Map], { Map m -> throw new AbortException('') })
|
|
|
|
|
|
|
|
thrown.expect(AbortException)
|
|
|
|
thrown.expectMessage("There was an error requesting the environment variable 'JAVA_HOME'.")
|
|
|
|
|
2018-06-06 11:19:19 +02:00
|
|
|
EnvironmentUtils.getEnvironmentVariable(nullScript, 'JAVA_HOME')
|
2018-03-09 18:58:11 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@Test
|
|
|
|
void getEnvironmentVariableTest() {
|
|
|
|
|
|
|
|
helper.registerAllowedMethod('sh', [Map], { Map m -> return '/env/java' })
|
|
|
|
|
2018-06-06 11:19:19 +02:00
|
|
|
def envVar = EnvironmentUtils.getEnvironmentVariable(nullScript, 'JAVA_HOME')
|
2018-03-09 18:58:11 +02:00
|
|
|
|
|
|
|
assert envVar == '/env/java'
|
|
|
|
}
|
|
|
|
}
|