Skip to content

Commit

Permalink
Merge pull request #541 from Vlatombe/custom-port
Browse files Browse the repository at this point in the history
`RealJenkinsRule`: option to provide a custom port/listen address
  • Loading branch information
jglick committed Jan 11, 2023
2 parents bb34c68 + 1f1c29d commit 91779ee
Showing 1 changed file with 25 additions and 1 deletion.
26 changes: 25 additions & 1 deletion src/main/java/org/jvnet/hudson/test/RealJenkinsRule.java
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,8 @@ public final class RealJenkinsRule implements TestRule {
*/
private int port;

private String httpListenAddress = "127.0.0.1";

private File war;

private boolean includeTestClasspathPlugins = true;
Expand Down Expand Up @@ -306,6 +308,28 @@ public RealJenkinsRule withColor(PrefixedOutputStream.Color color) {
return this;
}

/**
* Provides a custom fixed port instead of a random one.
* @param port a custom port to use instead of a random one.
*/
public RealJenkinsRule withPort(int port) {
this.port = port;
return this;
}

/**
* Provides a custom interface to listen to.
* <p><em>Important:</em> for security reasons this should be overridden only in special scenarios,
* such as testing inside a Docker container.
* Otherwise a developer running tests could inadvertently expose a Jenkins service without password protection,
* allowing remote code execution.
* @param httpListenAddress network interface such as <pre>0.0.0.0</pre>. Defaults to <pre>127.0.0.1</pre>.
*/
public RealJenkinsRule withHttpListenAddress(String httpListenAddress) {
this.httpListenAddress = httpListenAddress;
return this;
}

/**
* The intended use case for this is to use the plugins bundled into the war {@link RealJenkinsRule#withWar(File)}
* instead of the plugins in the pom. A typical scenario for this feature is a test which does not live inside a
Expand Down Expand Up @@ -547,7 +571,7 @@ public void startJenkins() throws Throwable {
"-jar", war.getAbsolutePath(),
"--enable-future-java",
"--httpPort=" + port, // initially port=0. On subsequent runs, the port is set to the port used allocated randomly on the first run.
"--httpListenAddress=127.0.0.1",
"--httpListenAddress=" + httpListenAddress,
"--prefix=/jenkins"));
Map<String, String> env = new TreeMap<>();
env.put("JENKINS_HOME", home.getAbsolutePath());
Expand Down

0 comments on commit 91779ee

Please sign in to comment.