Skip to content

Commit

Permalink
IPV6 follow-up
Browse files Browse the repository at this point in the history
Reverts part of #742

Only leaves the relevant part which was to switch to the
URL(String,String,String,String) constructor.

The change from localhost to ip caused some test failures in Kubernetes
context (It is invalid to provide an IP address as part of an ingress
host name)
  • Loading branch information
Vlatombe committed May 31, 2024
1 parent dd51025 commit 589639c
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 4 deletions.
8 changes: 4 additions & 4 deletions src/main/java/org/jvnet/hudson/test/RealJenkinsRule.java
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,7 @@ public final class RealJenkinsRule implements TestRule {

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

private String host = httpListenAddress;
private String host = "localhost";

Process proc;

Expand Down Expand Up @@ -292,13 +292,13 @@ public RealJenkinsRule withTimeout(int timeout) {

/**
* Sets a custom host name for the Jenkins root URL.
* <p>By default, this is the same as the {@link #httpListenAddress}.
* But you may wish to set it to something else that resolves to the loopback address,
* <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 the loopback address for security reasons
* <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) {
Expand Down
11 changes: 11 additions & 0 deletions src/test/java/org/jvnet/hudson/test/RealJenkinsRuleTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.arrayContainingInAnyOrder;
import static org.hamcrest.Matchers.containsString;
import static org.hamcrest.Matchers.instanceOf;
import static org.hamcrest.Matchers.is;
import static org.hamcrest.Matchers.nullValue;
import static org.junit.Assert.assertEquals;
Expand All @@ -53,6 +54,8 @@
import java.io.IOException;
import java.io.UncheckedIOException;
import java.net.HttpURLConnection;
import java.net.Inet6Address;
import java.net.InetAddress;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.util.concurrent.atomic.AtomicReference;
Expand Down Expand Up @@ -92,6 +95,14 @@ private static void _smokes(JenkinsRule r) throws Throwable {
assertEquals(rr.getUrl().toExternalForm(), rr.runRemotely(RealJenkinsRuleTest::_getJenkinsUrlFromRemote));
}

@Test public void ipv6() throws Throwable {
// Use -Djava.net.preferIPv6Addresses=true if dualstack
assumeThat(InetAddress.getLoopbackAddress(), instanceOf(Inet6Address.class));
rr.withHost("::1").startJenkins();
var externalForm = rr.getUrl().toExternalForm();
assertEquals(externalForm, rr.runRemotely(RealJenkinsRuleTest::_getJenkinsUrlFromRemote));
}

@Test public void testThrowsException() {
assertThat(assertThrows(RealJenkinsRule.StepException.class, () -> rr.then(RealJenkinsRuleTest::throwsException)).getMessage(),
containsString("IllegalStateException: something is wrong"));
Expand Down

0 comments on commit 589639c

Please sign in to comment.