1
0
mirror of https://github.com/SAP/jenkins-library.git synced 2025-02-21 19:48:53 +02:00

[refactoring] Rule handling

Starting point for that refactoring: it turned out that the tests
was not independent. The DefaultValueCache which is a singleton
keeps the status over various tests. Success of test execution depends
on the order test execution.

We have now
  * a dedicated rule for resetting the default value cache
  * JenkinsConfiguration rule (which already provided facilities for
    dealing with the configuration) has been replaced by a readYaml rule.
    From the PipelineUnit test framework we get already a handler for
    libraryResource, which is also part of the setup of the default
    values.
  * An auxiliar class which combines the
      * JenkinsSetupRule (registers the lib)
      * JenkinsReadYamlRule (provides facilities for Yaml parsing)
      * JenkinsResetDefaultValueCacheRule (cleans up the DefaultValueCache)
    into a rule chain. By using this rule chain we ensure that our
    setup OK (piper lib registered, and default config can be setup in
    a clean way).
This commit is contained in:
Marcus Holl 2018-01-26 14:55:15 +01:00
parent 0d87348c21
commit ef0b1bd9dd
12 changed files with 58 additions and 53 deletions

View File

@ -7,8 +7,7 @@ import org.junit.rules.RuleChain
import com.lesfurets.jenkins.unit.BasePipelineTest
import util.JenkinsLoggingRule
import util.JenkinsReadYamlRule
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 JenkinsReadYamlRule(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

@ -11,9 +11,8 @@ import org.junit.rules.TemporaryFolder
import com.lesfurets.jenkins.unit.BasePipelineTest
import util.JenkinsLoggingRule
import util.JenkinsReadYamlRule
import util.JenkinsSetupRule
import util.JenkinsShellCallRule
import util.Rules
public class MTABuildTest extends BasePipelineTest {
@ -23,13 +22,11 @@ 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 JenkinsReadYamlRule(this))
def currentDir

View File

@ -9,10 +9,8 @@ import com.lesfurets.jenkins.unit.BasePipelineTest
import static org.junit.Assert.assertEquals
import static org.junit.Assert.assertTrue
import util.JenkinsReadYamlRule
import util.JenkinsResetDefaultCacheRule
import util.JenkinsSetupRule
import util.JenkinsShellCallRule
import util.Rules
class MavenExecuteTest extends BasePipelineTest {
@ -21,10 +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 JenkinsReadYamlRule(this))
.around(new JenkinsResetDefaultCacheRule())
public RuleChain ruleChain = Rules.getCommonRules(this)
.around(jscr)
def mavenExecuteScript
def cpe

View File

@ -12,9 +12,8 @@ import org.junit.rules.RuleChain
import util.JenkinsLoggingRule
import util.JenkinsReadYamlRule
import util.JenkinsSetupRule
import util.JenkinsShellCallRule
import util.Rules
class NeoDeploymentTest extends BasePipelineTest {
@ -24,13 +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 JenkinsReadYamlRule(this))
public RuleChain ruleChain = Rules.getCommonRules(this)
.around(thrown)
.around(tmp)
.around(jlr)
.around(jscr)
def workspacePath
def warArchiveName

View File

@ -1,4 +1,4 @@
import util.JenkinsSetupRule
import util.Rules
import com.lesfurets.jenkins.unit.BasePipelineTest
@ -15,9 +15,8 @@ class PipelineExecuteTest extends BasePipelineTest {
private ExpectedException thrown = new ExpectedException().none()
@Rule
public RuleChain ruleChain = RuleChain.outerRule(thrown)
.around(new JenkinsSetupRule(this))
.around(new JenkinsReadYamlRule(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

@ -10,8 +10,7 @@ import org.junit.rules.TemporaryFolder
import com.lesfurets.jenkins.unit.BasePipelineTest
import util.JenkinsLoggingRule
import util.JenkinsReadYamlRule
import util.JenkinsSetupRule
import util.Rules
class ToolValidateTest extends BasePipelineTest {
@ -20,12 +19,10 @@ class ToolValidateTest extends BasePipelineTest {
private JenkinsLoggingRule jlr = new JenkinsLoggingRule(this)
@Rule
public RuleChain ruleChain =
RuleChain.outerRule(tmp)
.around(thrown)
.around(new JenkinsSetupRule(this))
.around(jlr)
.around(new JenkinsReadYamlRule(this))
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

@ -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())
}
}