Skip to content

Commit

Permalink
documented the RestartableJenkinsRule
Browse files Browse the repository at this point in the history
  • Loading branch information
StefanSpieker committed Dec 15, 2024
1 parent a3108f7 commit fd33fdb
Showing 1 changed file with 43 additions and 1 deletion.
44 changes: 43 additions & 1 deletion content/doc/developer/testing/index.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -597,8 +597,50 @@ To use them in your plugin, please find documentation here:
* link:https://github.com/jenkins-infra/pipeline-library#runbenchmarks[Running benchmark through Jenkinsfile]

== Further Pipeline Testing

Sometimes you might need to test more sophisticated things, as a restarting Jenkins and want to verify that the changes are still present.

=== Testing Durable Pipeline Steps
TODO: RestartableJenkinsRule.

If you want to ensure that your plugin behaves correctly after a restart, there are methods helping you.
`RestartableJenkinsRule` is part of the Jenkins testing framework and provides an easy way to simulate restarting Jenkins in your tests without actually restarting the Jenkins instance.
You can use the `then()` method to run one Jenkins session and shut down.
The following example shows how it can be used with a `FreeStyleJob` run which is verified after a Jenkins restart:

[source,java]
----
import hudson.model.FreeStyleProject;
import org.jvnet.hudson.test.RestartableJenkinsRule;
import org.junit.Rule;
import org.junit.Test;
public class MyPluginTest {
@Rule
public RestartableJenkinsRule rr = new RestartableJenkinsRule();
@Test
public void testPluginAfterRestart() {
rr.then(r -> {
// Set up the test environment
// e.g., create a job, configure your plugin, etc.
FreeStyleProject project = r.createFreeStyleProject("myJob");
// let's execute one build
project.scheduleBuild2(0).get();
});
// now we want to verify things after the restart
rr.then(r -> {
// get first project
FreeStyleProject p = (FreeStyleProject) r.getInstance().getAllItems().get(0);
// verify that it is still successfully
r.assertBuildStatusSuccess(p.getBuild("1"));
});
}
}
----

For more detailed information you can take a look at the link:https://javadoc.jenkins.io/component/jenkins-test-harness/org/jvnet/hudson/test/RestartableJenkinsRule.html[`RestartableJenkinsRule` JavaDoc].

== Further Patterns
=== Custom builder
Expand Down

0 comments on commit fd33fdb

Please sign in to comment.