Skip to content
Permalink
Browse files

Merge pull request #96 from batmat/JENKINS-50476

[JENKINS-50476] offer a way to assert that Jenkins won't start
  • Loading branch information...
batmat committed Apr 2, 2018
2 parents 2e8d1af + 61a2b95 commit 15a06ecccdcd996df444db6a2e08bd3a28607c45
Showing with 38 additions and 7 deletions.
  1. +38 −7 src/main/java/org/jvnet/hudson/test/RestartableJenkinsRule.java
@@ -2,6 +2,7 @@

import groovy.lang.Closure;
import hudson.FilePath;
import org.junit.Assert;
import org.junit.rules.MethodRule;
import org.junit.rules.TemporaryFolder;
import org.junit.runner.Description;
@@ -12,7 +13,9 @@
import java.io.IOException;
import java.nio.file.NoSuchFileException;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.Callable;

/**
@@ -34,7 +37,11 @@
public class RestartableJenkinsRule implements MethodRule {
public JenkinsRule j;
private Description description;
private final List<Statement> steps = new ArrayList<Statement>();

/**
* List of {@link Statement}. For each one, the boolean value says if Jenkins is expected to start or not.
*/
private final Map<Statement, Boolean> steps = new LinkedHashMap<>();

private TemporaryFolder tmp = new TemporaryFolder();

@@ -153,30 +160,43 @@ public void evaluate() throws Throwable {
});
}

public void thenDoesNotStart() {
addStep(new Statement() {
@Override
public void evaluate() throws Throwable {
throw new IllegalStateException("should have failed before reaching here.");
}
}, false);
}

public void addStep(final Statement step) {
steps.add(new Statement() {
addStep(step, true);
}

public void addStep(final Statement step, boolean expectedToStartCorrectly) {
steps.put(new Statement() {
@Override
public void evaluate() throws Throwable {
j.jenkins.getInjector().injectMembers(step);
j.jenkins.getInjector().injectMembers(target);
step.evaluate();
}
});
}, expectedToStartCorrectly);
}

/** Similar to {@link #addStep(Statement)} but we simulate a dirty shutdown after the step, rather than a clean one.
* See {@link #thenWithHardShutdown(Step)} for how this is done.
*/
public void addStepWithDirtyShutdown(final Statement step) {
steps.add(new Statement() {
steps.put(new Statement() {
@Override
public void evaluate() throws Throwable {
j.jenkins.getInjector().injectMembers(step);
j.jenkins.getInjector().injectMembers(target);
step.evaluate();
simulateAbruptShutdown();
}
});
}, true);
}

private void run() throws Throwable {
@@ -188,9 +208,20 @@ public File allocate() throws Exception {
};

// run each step inside its own JenkinsRule
for (Statement step : steps) {
for (Map.Entry<Statement, Boolean> entry : steps.entrySet()) {
Statement step = entry.getKey();
j = createJenkinsRule(description).with(loader);
j.apply(step,description).evaluate();
try {
j.apply(step, description).evaluate();
if (!entry.getValue()) {
Assert.fail("The current JenkinsRule should have failed to start Jenkins.");
}
} catch (Exception e) {
if(entry.getValue()) {
throw e;
}
// Failure ignored as requested
}
}
}

0 comments on commit 15a06ec

Please sign in to comment.
You can’t perform that action at this time.