Skip to content

Commit

Permalink
Provide JenkinsRule.restart() implementation (#716)
Browse files Browse the repository at this point in the history
  • Loading branch information
strangelookingnerd authored Jan 8, 2025
1 parent cb93195 commit 46bda4f
Show file tree
Hide file tree
Showing 3 changed files with 112 additions and 0 deletions.
36 changes: 36 additions & 0 deletions src/main/java/org/jvnet/hudson/test/JenkinsRule.java
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@
import hudson.EnvVars;
import hudson.Extension;
import hudson.ExtensionList;
import hudson.FilePath;
import hudson.Functions;
import hudson.Launcher;
import hudson.Main;
Expand Down Expand Up @@ -3092,6 +3093,41 @@ public Description getTestDescription() {
return testDescription;
}

/**
* Restart the current instance with the same port and a copy of its {@code JENKINS_HOME}.
*/
public void restart() throws Throwable {
// create backup of current instance in new home
URL source = jenkins.getRootDir().toURI().toURL();
File copy = new TemporaryDirectoryAllocator().allocate();

if(source.getProtocol().equals("file")) {
File src = new File(source.toURI());
if (src.isDirectory()) {
new FilePath(src).copyRecursiveTo("**/*",new FilePath(copy));
} else if (src.getName().endsWith(".zip")) {
new FilePath(src).unzip(new FilePath(copy));
}
} else {
File tmp = File.createTempFile("hudson","zip");
try {
FileUtils.copyURLToFile(source,tmp);
new FilePath(tmp).unzip(new FilePath(copy));
} finally {
FileUtils.deleteQuietly(tmp);
}
}

// shutdown and cleanup current instance
after();

// init new instance with backup
withExistingHome(copy);

// startup new instance
before();
}

private NameValuePair getCrumbHeaderNVP() {
return new NameValuePair(jenkins.getCrumbIssuer().getDescriptor().getCrumbRequestField(),
jenkins.getCrumbIssuer().getCrumb((ServletRequest) null));
Expand Down
31 changes: 31 additions & 0 deletions src/test/java/org/jvnet/hudson/test/JenkinsRuleTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@

import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.containsString;
import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.not;
import static org.hamcrest.collection.IsCollectionWithSize.hasSize;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull;
Expand All @@ -14,6 +17,7 @@
import hudson.model.FreeStyleProject;
import hudson.model.RootAction;
import hudson.model.User;
import java.io.File;
import java.io.IOException;
import java.net.URL;
import java.nio.file.Files;
Expand All @@ -30,6 +34,7 @@
import org.htmlunit.util.WebConnectionWrapper;
import org.junit.Rule;
import org.junit.Test;
import org.junit.runner.Description;
import org.kohsuke.stapler.DataBoundConstructor;
import org.kohsuke.stapler.DataBoundSetter;
import org.kohsuke.stapler.HttpResponse;
Expand Down Expand Up @@ -424,6 +429,31 @@ public void waitForCompletion() throws Exception {
j.assertBuildStatusSuccess(j.waitForCompletion(b));
}

@Test
public void restart() throws Throwable {
// preserve relevant properties
URL previousUrl = j.getURL();
Description previousTestDescription = j.testDescription;
File previousRoot = j.jenkins.getRootDir();

// create some configuration
j.createFreeStyleProject();
assertThat(j.jenkins.getJobNames(), hasSize(1));

// restart the instance with same port and new JENKINS_HOME
j.restart();

// validate properties and configuration were preserved
assertThat(j.getURL(), equalTo(previousUrl));
assertThat(j.testDescription, equalTo(previousTestDescription));
assertThat(j.jenkins.getRootDir(), not(previousRoot));
assertThat(j.jenkins.getJobNames(), hasSize(1));

// validate restarted instance is working
j.createFreeStyleProject();
assertThat(j.jenkins.getJobNames(), hasSize(2));
}

@Test
public void mimeType() throws IOException {
JenkinsRule.WebClient wc = j.createWebClient();
Expand All @@ -441,4 +471,5 @@ public WebResponse getResponse(WebRequest request) throws IOException {
wc.getPage(j.getURL());
assertEquals("text/javascript", contentType.get());
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package org.jvnet.hudson.test.junit.jupiter;

import org.junit.jupiter.api.Test;
import org.jvnet.hudson.test.JenkinsRule;
import org.junit.runner.Description;
import java.io.File;
import java.net.URL;

import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.not;
import static org.hamcrest.collection.IsCollectionWithSize.hasSize;

/**
* Test {@link JUnit5JenkinsRule}.
*/
@WithJenkins
class JUnit5JenkinsRuleTest {

@Test
void restart(JenkinsRule rule) throws Throwable {
// preserve relevant properties
URL previousUrl = rule.getURL();
Description previousTestDescription = rule.getTestDescription();
File previousRoot = rule.jenkins.getRootDir();

// create some configuration
rule.createFreeStyleProject();
assertThat(rule.jenkins.getJobNames(), hasSize(1));

// restart the instance with same port and new JENKINS_HOME
rule.restart();

// validate properties and configuration were preserved
assertThat(rule.getURL(), equalTo(previousUrl));
assertThat(rule.getTestDescription(), equalTo(previousTestDescription));
assertThat(rule.jenkins.getRootDir(), not(previousRoot));
assertThat(rule.jenkins.getJobNames(), hasSize(1));

// validate restarted instance is working
rule.createFreeStyleProject();
assertThat(rule.jenkins.getJobNames(), hasSize(2));
}

}

0 comments on commit 46bda4f

Please sign in to comment.