Skip to content

Commit

Permalink
Provide JenkinsRule.restart() implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
strangelookingnerd committed Jan 17, 2024
1 parent f4793e4 commit 3b50059
Show file tree
Hide file tree
Showing 6 changed files with 130 additions and 1 deletion.
37 changes: 36 additions & 1 deletion src/main/java/org/jvnet/hudson/test/JenkinsRule.java
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,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 @@ -170,7 +171,6 @@
import jenkins.security.MasterToSlaveCallable;
import net.sf.json.JSON;
import net.sf.json.JSONObject;
import org.acegisecurity.GrantedAuthorityImpl;
import org.apache.commons.beanutils.PropertyUtils;
import org.apache.commons.io.FileUtils;
import org.eclipse.jetty.http.HttpCompliance;
Expand Down Expand Up @@ -3039,6 +3039,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( null ));
Expand Down
30 changes: 30 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 @@ -27,6 +31,7 @@
import org.htmlunit.WebRequest;
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 @@ -421,4 +426,29 @@ 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));
}

}
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 @@ -8,6 +8,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;

@WithJenkins
class JenkinsRuleClassResolverTest {
Expand All @@ -20,4 +21,9 @@ void jenkinsRuleIsAccessible() throws IOException {
rule.createFreeStyleProject("job-0");
assertThat(rule.jenkins.getJobNames(), hasSize(1));
}

@Test
void instanceOf() {
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,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);
}
}

0 comments on commit 3b50059

Please sign in to comment.