From fd33fdb223a5f712e336f7cc1f9ae8c41270c93b Mon Sep 17 00:00:00 2001 From: Stefan Spieker Date: Sun, 15 Dec 2024 11:24:18 +0100 Subject: [PATCH 1/4] documented the RestartableJenkinsRule --- content/doc/developer/testing/index.adoc | 44 +++++++++++++++++++++++- 1 file changed, 43 insertions(+), 1 deletion(-) diff --git a/content/doc/developer/testing/index.adoc b/content/doc/developer/testing/index.adoc index 323d21ea0e7e..ac72a1e30d66 100644 --- a/content/doc/developer/testing/index.adoc +++ b/content/doc/developer/testing/index.adoc @@ -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 From 0b9806834f4aa39552822dd8a29accbdbec6c657 Mon Sep 17 00:00:00 2001 From: Stefan Spieker Date: Sun, 15 Dec 2024 17:24:12 +0100 Subject: [PATCH 2/4] Apply suggestions from code review Co-authored-by: Kris Stern --- content/doc/developer/testing/index.adoc | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/content/doc/developer/testing/index.adoc b/content/doc/developer/testing/index.adoc index ac72a1e30d66..d3bb405dcb68 100644 --- a/content/doc/developer/testing/index.adoc +++ b/content/doc/developer/testing/index.adoc @@ -598,13 +598,13 @@ To use them in your plugin, please find documentation here: == 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. +Sometimes you might need to test more sophisticated things, as restarting Jenkins and want to verify that the changes are still present. === Testing Durable Pipeline Steps -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. +If you want to ensure that your plugin behaves correctly after a restart, there are methods to help you. +`RestartableJenkinsRule` is part of the Jenkins testing framework and provides an easy way to simulate restarting Jenkins in your tests without actually having to restart the Jenkins instance. +You can use the `then()` method to run one Jenkins session and then shut down. The following example shows how it can be used with a `FreeStyleJob` run which is verified after a Jenkins restart: [source,java] @@ -640,7 +640,7 @@ public class MyPluginTest { } ---- -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]. +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 From 45a83570148d0fc451c23bb5a44f6a904ee1b50c Mon Sep 17 00:00:00 2001 From: Stefan Spieker Date: Mon, 16 Dec 2024 17:26:42 +0100 Subject: [PATCH 3/4] Update content/doc/developer/testing/index.adoc Co-authored-by: Mark Waite --- content/doc/developer/testing/index.adoc | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/content/doc/developer/testing/index.adoc b/content/doc/developer/testing/index.adoc index 2982d3fdf48f..e27fca7cd2bc 100644 --- a/content/doc/developer/testing/index.adoc +++ b/content/doc/developer/testing/index.adoc @@ -650,7 +650,7 @@ Sometimes you might need to test more sophisticated things, as restarting Jenkin If you want to ensure that your plugin behaves correctly after a restart, there are methods to help you. `RestartableJenkinsRule` is part of the Jenkins testing framework and provides an easy way to simulate restarting Jenkins in your tests without actually having to restart the Jenkins instance. You can use the `then()` method to run one Jenkins session and then shut down. -The following example shows how it can be used with a `FreeStyleJob` run which is verified after a Jenkins restart: +The following example shows how it can be used with a `FreeStyleProject` build which is verified after a Jenkins restart: [source,java] ---- From c496e9d5e9fa0f028f75dd7d750b0ae99dcf56bc Mon Sep 17 00:00:00 2001 From: Stefan Spieker Date: Mon, 16 Dec 2024 17:30:23 +0100 Subject: [PATCH 4/4] renamed section --- content/doc/developer/testing/index.adoc | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/content/doc/developer/testing/index.adoc b/content/doc/developer/testing/index.adoc index e27fca7cd2bc..646a5edf4ba1 100644 --- a/content/doc/developer/testing/index.adoc +++ b/content/doc/developer/testing/index.adoc @@ -641,11 +641,7 @@ To use them in your plugin, please find documentation here: * link:https://github.com/jenkinsci/plugin-pom#running-benchmarks[Maven profile that runs benchmarks] * 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 restarting Jenkins and want to verify that the changes are still present. - -=== Testing Durable Pipeline Steps +== Testing Jenkins Restart If you want to ensure that your plugin behaves correctly after a restart, there are methods to help you. `RestartableJenkinsRule` is part of the Jenkins testing framework and provides an easy way to simulate restarting Jenkins in your tests without actually having to restart the Jenkins instance.