Skip to content

Commit e6646b6

Browse files
committed
Add remote host binding for JenkinsRule
The reason for this change is that the JenkinRule based tests were all running inside a docker container and i needed a way to bind it a non localhost address in order to access it from outside the running container. The host binding for the JenkinsRule is configurable with the `host` property. The default value is `localhost` this keeps the backward compatibility. The following code snippet shows how to bind the JenkinsRule instance ``` @rule public JenkinsRule j = new JenkinsRule() {{ host = null; } }; ``` to all network interfaces. This is similar to set the `host` value to `0.0.0.0`. The test case `JenkinsRuleHostIPTest` validates that the assignment to a specific host ip address is working correctly. The test case `JenkinsRuleAllHostInterfaceTest` validates that the assignment to all network interfaces is working correctly.
1 parent 9fecdc5 commit e6646b6

File tree

6 files changed

+161
-39
lines changed

6 files changed

+161
-39
lines changed

src/main/java/jenkins/benchmark/jmh/JmhBenchmarkState.java

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import jenkins.model.Jenkins;
77
import jenkins.model.JenkinsLocationConfiguration;
88
import org.apache.commons.lang3.mutable.MutableInt;
9+
import org.apache.commons.lang3.mutable.MutableObject;
910
import org.apache.commons.lang3.tuple.ImmutablePair;
1011
import org.eclipse.jetty.server.Server;
1112
import org.jvnet.hudson.test.JenkinsRule;
@@ -43,6 +44,7 @@ public abstract class JmhBenchmarkState implements RootAction {
4344

4445
private final TemporaryDirectoryAllocator temporaryDirectoryAllocator = new TemporaryDirectoryAllocator();
4546
private final MutableInt localPort = new MutableInt();
47+
private final MutableObject<String> host = new MutableObject<String>("localhost");
4648

4749
private Jenkins jenkins = null;
4850
private Server server = null;
@@ -87,8 +89,8 @@ public final void terminateJenkins() {
8789
}
8890

8991
private void launchInstance() throws Exception {
90-
ImmutablePair<Server, ServletContext> results = JenkinsRule._createWebServer(contextPath, localPort::setValue,
91-
getClass().getClassLoader(), localPort.getValue(), JenkinsRule::_configureUserRealm);
92+
ImmutablePair<Server, ServletContext> results = JenkinsRule._createWebServer(contextPath, host::setValue, localPort::setValue,
93+
getClass().getClassLoader(), host.getValue(), localPort.getValue(),JenkinsRule::_configureUserRealm);
9294

9395
server = results.left;
9496
ServletContext webServer = results.right;
@@ -104,7 +106,7 @@ private void launchInstance() throws Exception {
104106
}
105107

106108
private URL getJenkinsURL() throws MalformedURLException {
107-
return new URL("http://localhost:" + localPort.getValue() + contextPath + "/");
109+
return new URL("http://"+host.getValue()+":" + localPort.getValue() + contextPath + "/");
108110
}
109111

110112
/**

src/main/java/org/jvnet/hudson/test/JenkinsRule.java

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -280,6 +280,10 @@ public class JenkinsRule implements TestRule, MethodRule, RootAction {
280280
* TCP/IP port that the server is listening on.
281281
*/
282282
protected int localPort;
283+
/**
284+
* If null or 0.0.0.0, then bind to all interfaces.
285+
*/
286+
protected String host = "localhost";
283287
protected Server server;
284288

285289
/**
@@ -740,7 +744,7 @@ protected ServletContext createWebServer() throws Exception {
740744
*/
741745
protected ServletContext createWebServer(@CheckForNull BiConsumer<WebAppContext, Server> contextAndServerConsumer) throws Exception {
742746
ImmutablePair<Server, ServletContext> results = _createWebServer(contextPath,
743-
(x) -> localPort = x, getClass().getClassLoader(), localPort, this::configureUserRealm, contextAndServerConsumer);
747+
(x) -> host = x, (x) -> localPort = x, getClass().getClassLoader(), host, localPort, this::configureUserRealm, contextAndServerConsumer);
744748
server = results.left;
745749
LOGGER.log(Level.INFO, "Running on {0}", getURL());
746750
return results.right;
@@ -757,11 +761,11 @@ protected ServletContext createWebServer(@CheckForNull BiConsumer<WebAppContext,
757761
* @return ImmutablePair consisting of the {@link Server} and the {@link ServletContext}
758762
* @since 2.50
759763
*/
760-
public static ImmutablePair<Server, ServletContext> _createWebServer(String contextPath, Consumer<Integer> portSetter,
761-
ClassLoader classLoader, int localPort,
764+
public static ImmutablePair<Server, ServletContext> _createWebServer(String contextPath, Consumer<String> hostSetter, Consumer<Integer> portSetter,
765+
ClassLoader classLoader, String host, int localPort,
762766
Supplier<LoginService> loginServiceSupplier)
763767
throws Exception {
764-
return _createWebServer(contextPath, portSetter, classLoader, localPort, loginServiceSupplier, null);
768+
return _createWebServer(contextPath, hostSetter, portSetter, classLoader, host, localPort, loginServiceSupplier, null);
765769
}
766770
/**
767771
* Creates a web server on which Jenkins can run
@@ -775,8 +779,8 @@ public static ImmutablePair<Server, ServletContext> _createWebServer(String cont
775779
* @return ImmutablePair consisting of the {@link Server} and the {@link ServletContext}
776780
* @since 2.50
777781
*/
778-
public static ImmutablePair<Server, ServletContext> _createWebServer(String contextPath, Consumer<Integer> portSetter,
779-
ClassLoader classLoader, int localPort,
782+
public static ImmutablePair<Server, ServletContext> _createWebServer(String contextPath, Consumer<String> hostSetter, Consumer<Integer> portSetter,
783+
ClassLoader classLoader, String host, int localPort,
780784
Supplier<LoginService> loginServiceSupplier,
781785
@CheckForNull BiConsumer<WebAppContext, Server> contextAndServerConsumer)
782786
throws Exception {
@@ -797,7 +801,7 @@ public static ImmutablePair<Server, ServletContext> _createWebServer(String cont
797801
HttpConfiguration config = connector.getConnectionFactory(HttpConnectionFactory.class).getHttpConfiguration();
798802
// use a bigger buffer as Stapler traces can get pretty large on deeply nested URL
799803
config.setRequestHeaderSize(12 * 1024);
800-
connector.setHost("localhost");
804+
connector.setHost(host);
801805
if (System.getProperty("port") != null) {
802806
connector.setPort(Integer.parseInt(System.getProperty("port")));
803807
} else if (localPort != 0) {
@@ -810,6 +814,7 @@ public static ImmutablePair<Server, ServletContext> _createWebServer(String cont
810814
}
811815
server.start();
812816

817+
hostSetter.accept(connector.getHost());
813818
portSetter.accept(connector.getLocalPort());
814819

815820
ServletContext servletContext = context.getServletContext();
@@ -1003,7 +1008,14 @@ public void addGroups(String username, String... groups) {
10031008
* URL ends with '/'.
10041009
*/
10051010
public URL getURL() throws IOException {
1006-
return new URL("http://localhost:"+localPort+contextPath+"/");
1011+
return new URL(String.format("http://%s:%d%s/", getHost(), localPort, contextPath));
1012+
}
1013+
1014+
public String getHost() {
1015+
if (host == null) {
1016+
return "localhost";
1017+
}
1018+
return host;
10071019
}
10081020

10091021
public DumbSlave createSlave(EnvVars env) throws Exception {

src/test/java/org/jvnet/hudson/main/AppTest.java

Lines changed: 3 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -23,50 +23,25 @@
2323
*/
2424
package org.jvnet.hudson.main;
2525

26-
import static org.junit.Assert.assertTrue;
27-
28-
import hudson.model.FreeStyleBuild;
29-
import hudson.model.FreeStyleProject;
30-
import hudson.tasks.Shell;
31-
import hudson.tasks.BatchFile;
32-
import org.apache.commons.io.FileUtils;
3326
import org.junit.Rule;
3427
import org.junit.Test;
3528
import org.jvnet.hudson.test.JenkinsRule;
3629

37-
import java.io.IOException;
38-
import java.util.concurrent.ExecutionException;
39-
4030
/**
4131
* Experimenting with Hudson test suite.
4232
*/
43-
public class AppTest {
33+
public class AppTest extends BasicTestCase {
4434

4535
@Rule
4636
public JenkinsRule j = new JenkinsRule();
4737

4838
@Test
4939
public void test1() throws Exception {
50-
meat();
40+
meat(j);
5141
}
5242

5343
@Test
5444
public void test2() throws Exception {
55-
meat();
56-
}
57-
58-
private void meat() throws IOException, InterruptedException, ExecutionException {
59-
FreeStyleProject project = j.createFreeStyleProject();
60-
if(System.getProperty("os.name").contains("Windows")) {
61-
project.getBuildersList().add(new BatchFile("echo hello"));
62-
} else {
63-
project.getBuildersList().add(new Shell("echo hello"));
64-
}
65-
66-
FreeStyleBuild build = project.scheduleBuild2(0).get();
67-
System.out.println(build.getDisplayName()+" completed");
68-
69-
String s = FileUtils.readFileToString(build.getLogFile());
70-
assertTrue(s,s.contains("echo hello"));
45+
meat(j);
7146
}
7247
}
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
/*
2+
* The MIT License
3+
*
4+
* Copyright (c) 2004-2009, Sun Microsystems, Inc., Kohsuke Kawaguchi, Bruce Chapman
5+
*
6+
* Permission is hereby granted, free of charge, to any person obtaining a copy
7+
* of this software and associated documentation files (the "Software"), to deal
8+
* in the Software without restriction, including without limitation the rights
9+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10+
* copies of the Software, and to permit persons to whom the Software is
11+
* furnished to do so, subject to the following conditions:
12+
*
13+
* The above copyright notice and this permission notice shall be included in
14+
* all copies or substantial portions of the Software.
15+
*
16+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22+
* THE SOFTWARE.
23+
*/
24+
package org.jvnet.hudson.main;
25+
26+
import static org.junit.Assert.assertTrue;
27+
28+
import hudson.model.FreeStyleBuild;
29+
import hudson.model.FreeStyleProject;
30+
import hudson.tasks.Shell;
31+
import hudson.tasks.BatchFile;
32+
import org.apache.commons.io.FileUtils;
33+
import org.jvnet.hudson.test.JenkinsRule;
34+
35+
import java.io.IOException;
36+
import java.util.concurrent.ExecutionException;
37+
38+
/**
39+
* Experimenting with Hudson test suite.
40+
*/
41+
public class BasicTestCase {
42+
43+
protected void meat(JenkinsRule jenkinsRule) throws IOException, InterruptedException, ExecutionException {
44+
FreeStyleProject project = jenkinsRule.createFreeStyleProject();
45+
if(System.getProperty("os.name").contains("Windows")) {
46+
project.getBuildersList().add(new BatchFile("echo hello"));
47+
} else {
48+
project.getBuildersList().add(new Shell("echo hello"));
49+
}
50+
51+
FreeStyleBuild build = project.scheduleBuild2(0).get();
52+
System.out.println(build.getDisplayName()+" completed");
53+
54+
String s = FileUtils.readFileToString(build.getLogFile());
55+
assertTrue(s,s.contains("echo hello"));
56+
}
57+
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package org.jvnet.hudson.main;
2+
3+
import static org.junit.Assert.assertEquals;
4+
5+
import org.junit.Rule;
6+
import org.junit.Test;
7+
import org.jvnet.hudson.test.JenkinsRule;
8+
9+
import java.net.URL;
10+
import java.util.List;
11+
12+
public class JenkinsRuleAllHostInterfaceTest extends BasicTestCase {
13+
14+
@Rule public JenkinsRule j = new JenkinsRule() {
15+
{
16+
host = null;
17+
}
18+
};
19+
20+
@Test
21+
public void allInterfaceJenkins() throws Throwable {
22+
System.out.println("running in: " + j.jenkins.getRootUrl());
23+
URL jenkinsURL = new URL(j.jenkins.getRootUrl());
24+
25+
assertEquals("localhost", jenkinsURL.getHost());
26+
}
27+
28+
@Test
29+
public void allInterfaceJenkinsBuild() throws Throwable {
30+
meat(j);
31+
}
32+
}
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
package org.jvnet.hudson.main;
2+
3+
import static org.junit.Assert.assertEquals;
4+
5+
import org.junit.Rule;
6+
import org.junit.Test;
7+
import org.jvnet.hudson.test.JenkinsRule;
8+
9+
import java.net.InetAddress;
10+
import java.net.UnknownHostException;
11+
import java.net.URL;
12+
import java.util.List;
13+
14+
public class JenkinsRuleHostIPTest extends BasicTestCase {
15+
16+
private static String HOST_IP = getHostAddress();
17+
18+
private static String getHostAddress() {
19+
try {
20+
return InetAddress.getLocalHost().getHostAddress();
21+
} catch (UnknownHostException e) {
22+
throw new RuntimeException(e);
23+
}
24+
}
25+
26+
@Rule public JenkinsRule j = new JenkinsRule() {
27+
{
28+
host = HOST_IP;
29+
}
30+
};
31+
32+
@Test
33+
public void hostIpJenkins() throws Throwable {
34+
System.out.println("running in: " + j.jenkins.getRootUrl());
35+
URL jenkinsURL = new URL(j.jenkins.getRootUrl());
36+
37+
assertEquals(HOST_IP, jenkinsURL.getHost());
38+
}
39+
40+
@Test
41+
public void hostIpJenkinsBuild() throws Throwable {
42+
meat(j);
43+
}
44+
}

0 commit comments

Comments
 (0)