1
0
mirror of https://github.com/SAP/jenkins-library.git synced 2025-01-30 05:59:39 +02:00

Merge pull request #56 from marcusholl/pr/rules

Provide Rule chain for the common rules used as prerequisite in almost all tests
This commit is contained in:
Marcus Holl 2018-01-31 10:34:14 +01:00 committed by GitHub
commit c55673daea
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
14 changed files with 107 additions and 61 deletions

View File

@ -6,9 +6,8 @@ import org.junit.rules.RuleChain
import com.lesfurets.jenkins.unit.BasePipelineTest
import util.JenkinsConfigRule
import util.JenkinsLoggingRule
import util.JenkinsSetupRule
import util.Rules
import static org.junit.Assert.assertEquals
import static org.junit.Assert.assertTrue
@ -20,9 +19,8 @@ class DockerExecuteTest extends BasePipelineTest {
private JenkinsLoggingRule jlr = new JenkinsLoggingRule(this)
@Rule
public RuleChain ruleChain = RuleChain.outerRule(new JenkinsSetupRule(this))
.around(jlr)
.around(new JenkinsConfigRule(this))
public RuleChain ruleChain = RuleChain.outerRule(Rules.getCommonRules(this))
.around(jlr)
int whichDockerReturnValue = 0

View File

@ -1,14 +1,17 @@
#!groovy
import com.lesfurets.jenkins.unit.BasePipelineTest
import org.junit.Rule
import org.junit.Test
import util.JenkinsSetupRule
import static org.junit.Assert.assertTrue
import org.junit.rules.RuleChain
import util.Rules
class DurationMeasureTest extends BasePipelineTest {
@Rule
public JenkinsSetupRule setupRule = new JenkinsSetupRule(this)
public RuleChain rules = Rules.getCommonRules(this)
@Test
void testDurationMeasurement() throws Exception {

View File

@ -6,7 +6,7 @@ import org.junit.Rule
import org.junit.Test
import org.junit.rules.RuleChain
import util.JenkinsLoggingRule
import util.JenkinsSetupRule
import util.Rules
import static org.junit.Assert.assertTrue
import static org.junit.Assert.assertEquals
@ -21,13 +21,11 @@ class InfluxWriteDataTest extends BasePipelineTest {
def cpe
public JenkinsSetupRule setupRule = new JenkinsSetupRule(this)
public JenkinsLoggingRule loggingRule = new JenkinsLoggingRule(this)
@Rule
public RuleChain ruleChain =
RuleChain.outerRule(setupRule)
.around(loggingRule)
public RuleChain ruleChain = Rules.getCommonRules(this)
.around(loggingRule)
@Before
void init() throws Exception {

View File

@ -10,10 +10,9 @@ import org.junit.rules.TemporaryFolder
import com.lesfurets.jenkins.unit.BasePipelineTest
import util.JenkinsConfigRule
import util.JenkinsLoggingRule
import util.JenkinsSetupRule
import util.JenkinsShellCallRule
import util.Rules
public class MTABuildTest extends BasePipelineTest {
@ -23,13 +22,12 @@ public class MTABuildTest extends BasePipelineTest {
private JenkinsShellCallRule jscr = new JenkinsShellCallRule(this)
@Rule
public RuleChain ruleChain =
RuleChain.outerRule(thrown)
public RuleChain ruleChain = Rules.getCommonRules(this)
.around(thrown)
.around(tmp)
.around(new JenkinsSetupRule(this))
.around(jlr)
.around(jscr)
.around(new JenkinsConfigRule(this))
def currentDir
def otherDir

View File

@ -9,9 +9,8 @@ import com.lesfurets.jenkins.unit.BasePipelineTest
import static org.junit.Assert.assertEquals
import static org.junit.Assert.assertTrue
import util.JenkinsConfigRule
import util.JenkinsSetupRule
import util.JenkinsShellCallRule
import util.Rules
class MavenExecuteTest extends BasePipelineTest {
@ -20,9 +19,8 @@ class MavenExecuteTest extends BasePipelineTest {
private JenkinsShellCallRule jscr = new JenkinsShellCallRule(this)
@Rule
public RuleChain ruleChain = RuleChain.outerRule(new JenkinsSetupRule(this))
.around(jscr)
.around(new JenkinsConfigRule(this))
public RuleChain ruleChain = Rules.getCommonRules(this)
.around(jscr)
def mavenExecuteScript
def cpe

View File

@ -10,10 +10,10 @@ import org.junit.Test
import org.junit.rules.ExpectedException
import org.junit.rules.RuleChain
import util.JenkinsConfigRule
import util.JenkinsLoggingRule
import util.JenkinsSetupRule
import util.JenkinsShellCallRule
import util.Rules
class NeoDeploymentTest extends BasePipelineTest {
@ -23,12 +23,11 @@ class NeoDeploymentTest extends BasePipelineTest {
private JenkinsShellCallRule jscr = new JenkinsShellCallRule(this)
@Rule
public RuleChain ruleChain = RuleChain.outerRule(thrown)
.around(tmp)
.around(new JenkinsSetupRule(this))
.around(jlr)
.around(jscr)
.around(new JenkinsConfigRule(this))
public RuleChain ruleChain = Rules.getCommonRules(this)
.around(thrown)
.around(tmp)
.around(jlr)
.around(jscr)
def workspacePath
def warArchiveName

View File

@ -1,8 +1,4 @@
import hudson.AbortException
import util.JenkinsConfigRule
import util.JenkinsSetupRule
import org.junit.rules.TemporaryFolder
import util.Rules
import com.lesfurets.jenkins.unit.BasePipelineTest
@ -12,14 +8,15 @@ import org.junit.Test
import org.junit.rules.ExpectedException
import org.junit.rules.RuleChain
import util.JenkinsReadYamlRule
class PipelineExecuteTest extends BasePipelineTest {
private ExpectedException thrown = new ExpectedException().none()
@Rule
public RuleChain ruleChain = RuleChain.outerRule(thrown)
.around(new JenkinsSetupRule(this))
.around(new JenkinsConfigRule(this))
public RuleChain ruleChain = Rules.getCommonRules(this)
.around(thrown)
def pipelinePath
def checkoutParameters = [:]

View File

@ -1,11 +1,12 @@
import org.junit.Before
import org.junit.Rule
import org.junit.Test
import org.junit.rules.RuleChain
import org.yaml.snakeyaml.Yaml
import com.lesfurets.jenkins.unit.BasePipelineTest
import util.JenkinsSetupRule
import util.Rules
import static org.junit.Assert.assertEquals
import static org.junit.Assert.assertNotNull
@ -19,7 +20,7 @@ class SetupCommonPipelineEnvironmentTest extends BasePipelineTest {
def commonPipelineEnvironment
@Rule
public JenkinsSetupRule jsr = new JenkinsSetupRule(this)
public RuleChain rules = Rules.getCommonRules(this)
@Before
void init() {

View File

@ -9,24 +9,21 @@ import org.junit.rules.TemporaryFolder
import com.lesfurets.jenkins.unit.BasePipelineTest
import util.JenkinsConfigRule
import util.JenkinsLoggingRule
import util.JenkinsSetupRule
import util.Rules
class ToolValidateTest extends BasePipelineTest {
private ExpectedException thrown = new ExpectedException().none()
private TemporaryFolder tmp = new TemporaryFolder()
private JenkinsLoggingRule jlr = new JenkinsLoggingRule(this)
private JenkinsConfigRule jcr = new JenkinsConfigRule(this)
@Rule
public RuleChain ruleChain =
RuleChain.outerRule(tmp)
.around(thrown)
.around(new JenkinsSetupRule(this))
.around(jlr)
.around(jcr)
public RuleChain ruleChain = Rules.getCommonRules(this)
.around(tmp)
.around(thrown)
.around(jlr)
private notEmptyDir

View File

@ -6,8 +6,7 @@ import org.junit.Rule
import org.junit.Test
import org.junit.rules.ExpectedException
import org.junit.rules.RuleChain
import util.JenkinsLoggingRule
import util.JenkinsSetupRule
import util.Rules
import util.SharedLibraryCreator
import static org.hamcrest.Matchers.containsString
@ -21,7 +20,7 @@ class UtilsTest extends BasePipelineTest {
public ExpectedException exception = ExpectedException.none()
@Rule
public JenkinsSetupRule setUpRule = new JenkinsSetupRule(this, SharedLibraryCreator.lazyLoadedLibrary)
public RuleChain rules = Rules.getCommonRules(this, SharedLibraryCreator.lazyLoadedLibrary)
Utils utils

View File

@ -7,12 +7,12 @@ import org.junit.runner.Description
import org.junit.runners.model.Statement
import org.yaml.snakeyaml.Yaml
class JenkinsConfigRule implements TestRule {
class JenkinsReadYamlRule implements TestRule {
final BasePipelineTest testInstance
JenkinsConfigRule(BasePipelineTest testInstance) {
JenkinsReadYamlRule(BasePipelineTest testInstance) {
this.testInstance = testInstance
}
@ -25,11 +25,15 @@ class JenkinsConfigRule implements TestRule {
return new Statement() {
@Override
void evaluate() throws Throwable {
testInstance.helper.registerAllowedMethod("readYaml", [Map], { Map parameters ->
Yaml yamlParser = new Yaml()
return yamlParser.load(parameters.text)
testInstance.helper.registerAllowedMethod("readYaml", [Map], { Map m ->
if(m.text) {
return new Yaml().load(m.text)
} else if(m.file) {
throw new UnsupportedOperationException()
} else {
throw new IllegalArgumentException("Key 'text' is missing in map ${m}.")
}
})
DefaultValueCache.reset()
base.evaluate()
}

View File

@ -0,0 +1,34 @@
package util
import org.junit.rules.TestRule
import org.junit.runner.Description
import org.junit.runners.model.Statement
import com.lesfurets.jenkins.unit.BasePipelineTest
import com.sap.piper.DefaultValueCache
class JenkinsResetDefaultCacheRule implements TestRule {
JenkinsResetDefaultCacheRule() {
this(null)
}
//
// Actually not needed. Only provided for the sake of consistency
// with our other rules which comes with an constructor having the
// test case contained in the signature.
JenkinsResetDefaultCacheRule(BasePipelineTest testInstance) {
}
@Override
Statement apply(Statement base, Description description) {
return new Statement() {
@Override
void evaluate() throws Throwable {
DefaultValueCache.reset()
base.evaluate()
}
}
}
}

View File

@ -13,12 +13,13 @@ class JenkinsSetupRule implements TestRule {
final BasePipelineTest testInstance
JenkinsSetupRule(BasePipelineTest testInstance) {
this.testInstance = testInstance
this(testInstance, null)
}
JenkinsSetupRule(BasePipelineTest testInstance, LibraryConfiguration configuration) {
this.testInstance = testInstance
this.library = configuration
if(configuration)
this.library = configuration
}
@Override

View File

@ -0,0 +1,19 @@
package util;
import org.junit.rules.RuleChain;
import com.lesfurets.jenkins.unit.BasePipelineTest;
import com.lesfurets.jenkins.unit.global.lib.LibraryConfiguration
public class Rules {
public static RuleChain getCommonRules(BasePipelineTest testCase) {
return getCommonRules(testCase, null)
}
public static RuleChain getCommonRules(BasePipelineTest testCase, LibraryConfiguration libConfig) {
return RuleChain.outerRule(new JenkinsSetupRule(testCase, libConfig))
.around(new JenkinsReadYamlRule(testCase))
.around(new JenkinsResetDefaultCacheRule())
}
}