Skip to content

Commit

Permalink
Merge pull request #295 from jglick/withHost
Browse files Browse the repository at this point in the history
RealJenkinsRule.withHost
  • Loading branch information
jglick authored Apr 7, 2021
2 parents 0e9763d + 6b5a83b commit 4fd5baf
Showing 1 changed file with 27 additions and 3 deletions.
30 changes: 27 additions & 3 deletions src/main/java/org/jvnet/hudson/test/RealJenkinsRule.java
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,8 @@ public final class RealJenkinsRule implements TestRule {

private int timeout = Integer.getInteger("jenkins.test.timeout", new DisableOnDebug(null).isDebugging() ? 0 : 600);

private String host = "localhost";

private Process proc;

// TODO may need to be relaxed for Gradle-based plugins
Expand Down Expand Up @@ -184,6 +186,22 @@ public RealJenkinsRule withTimeout(int timeout) {
return this;
}

/**
* Sets a custom host name for the Jenkins root URL.
* <p>By default, this is just {@code localhost}.
* But you may wish to set it to something else that resolves to localhost,
* such as {@code some-id.127.0.0.1.nip.io}.
* This is particularly useful when running multiple copies of Jenkins (and/or other services) in one test case,
* since browser cookies are sensitive to host but not port and so otherwise {@link HttpServletRequest#getSession}
* might accidentally be shared across otherwise distinct services.
* <p>Calling this method does <em>not</em> change the fact that Jenkins will be configured to listen only on localhost for security reasons
* (so others in the same network cannot access your system under test, especially if it lacks authentication).
*/
public RealJenkinsRule withHost(String host) {
this.host = host;
return this;
}

@Override public Statement apply(final Statement base, Description description) {
this.description = description;
return new Statement() {
Expand Down Expand Up @@ -311,7 +329,7 @@ public void then(Step s) throws Throwable {
* Like {@link JenkinsRule#getURL} but does not require Jenkins to have been started yet.
*/
public URL getUrl() throws MalformedURLException {
return new URL("http://localhost:" + port + "/jenkins/");
return new URL("http://" + host + ":" + port + "/jenkins/");
}

private URL endpoint(String method) throws MalformedURLException {
Expand All @@ -329,7 +347,7 @@ public void startJenkins() throws Throwable {
"-Dhudson.Main.development=true",
"-DRealJenkinsRule.location=" + RealJenkinsRule.class.getProtectionDomain().getCodeSource().getLocation(),
"-DRealJenkinsRule.cp=" + cp,
"-DRealJenkinsRule.port=" + port,
"-DRealJenkinsRule.url=" + getUrl(),
"-DRealJenkinsRule.description=" + description,
"-DRealJenkinsRule.token=" + token));
if (new DisableOnDebug(null).isDebugging()) {
Expand Down Expand Up @@ -526,18 +544,24 @@ public HttpResponse doExit(@QueryParameter String token) throws IOException {
}

public static final class CustomJenkinsRule extends JenkinsRule implements AutoCloseable {

public CustomJenkinsRule() throws Exception {
this.jenkins = Jenkins.get();
localPort = Integer.getInteger("RealJenkinsRule.port");
jenkins.setNoUsageStatistics(true); // cannot use JenkinsRule._configureJenkinsForTest earlier because it tries to save config before loaded
JenkinsLocationConfiguration.get().setUrl(getURL().toString());
testDescription = Description.createSuiteDescription(System.getProperty("RealJenkinsRule.description"));
env = new TestEnvironment(this.testDescription);
env.pin();
}

@Override public URL getURL() throws IOException {
return new URL(System.getProperty("RealJenkinsRule.url"));
}

@Override public void close() throws Exception {
env.dispose();
}

}

// Copied from hudson.remoting
Expand Down

0 comments on commit 4fd5baf

Please sign in to comment.