Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Provide JenkinsRule.restart() implementation #716

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -3281,6 +3282,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((javax.servlet.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));
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.collection.IsCollectionWithSize.hasSize;
import static org.hamcrest.collection.IsEmptyCollection.empty;
import static org.junit.jupiter.api.Assertions.assertInstanceOf;

import java.io.IOException;
import org.junit.jupiter.api.Test;
Expand All @@ -17,4 +18,9 @@ void jenkinsRuleIsAccessible(JenkinsRule rule) throws IOException {
rule.createFreeStyleProject("job-0");
assertThat(rule.jenkins.getJobNames(), hasSize(1));
}

@Test
void instanceOf(JenkinsRule rule) {
assertInstanceOf(JUnit5JenkinsRule.class, rule);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.collection.IsCollectionWithSize.hasSize;
import static org.hamcrest.collection.IsEmptyCollection.empty;
import static org.junit.jupiter.api.Assertions.assertInstanceOf;

import java.io.IOException;
import org.junit.jupiter.api.Test;
Expand All @@ -17,4 +18,10 @@ void jenkinsRuleIsAccessible(JenkinsRule rule) throws IOException {
rule.createFreeStyleProject("job-0");
assertThat(rule.jenkins.getJobNames(), hasSize(1));
}

@Test
@WithJenkins
void instanceOf(JenkinsRule rule) {
assertInstanceOf(JUnit5JenkinsRule.class, rule);
}
}
Loading