Tests: Ensure binding on localhost host is consistently ipv4/v6

The current netty multiport tests bind on localhost and then try to connect
to 127.0.0.1, which may fail, if localhost is resolved to ipv6 by default.

This randomly chooses between 127.0.0.1, localhost and ::1 (if available) for
binding and then uses this throughout the test.
This commit is contained in:
Alexander Reelsen 2015-08-31 09:16:55 +02:00
parent a17d7500d3
commit 00902207a6
1 changed files with 22 additions and 7 deletions

View File

@ -36,6 +36,7 @@ import org.elasticsearch.test.junit.rule.RepeatOnExceptionRule;
import org.elasticsearch.threadpool.ThreadPool; import org.elasticsearch.threadpool.ThreadPool;
import org.elasticsearch.transport.BindTransportException; import org.elasticsearch.transport.BindTransportException;
import org.elasticsearch.transport.TransportService; import org.elasticsearch.transport.TransportService;
import org.junit.Before;
import org.junit.Rule; import org.junit.Rule;
import org.junit.Test; import org.junit.Test;
@ -52,16 +53,30 @@ import static org.hamcrest.Matchers.is;
public class NettyTransportMultiPortTests extends ESTestCase { public class NettyTransportMultiPortTests extends ESTestCase {
private static final int MAX_RETRIES = 10; private static final int MAX_RETRIES = 10;
private String host;
@Rule @Rule
public RepeatOnExceptionRule repeatOnBindExceptionRule = new RepeatOnExceptionRule(logger, MAX_RETRIES, BindTransportException.class); public RepeatOnExceptionRule repeatOnBindExceptionRule = new RepeatOnExceptionRule(logger, MAX_RETRIES, BindTransportException.class);
@Before
public void setup() {
if (randomBoolean()) {
host = "localhost";
} else {
if (NetworkUtils.SUPPORTS_V6 && randomBoolean()) {
host = "::1";
} else {
host = "127.0.0.1";
}
}
}
@Test @Test
public void testThatNettyCanBindToMultiplePorts() throws Exception { public void testThatNettyCanBindToMultiplePorts() throws Exception {
int[] ports = getRandomPorts(3); int[] ports = getRandomPorts(3);
Settings settings = settingsBuilder() Settings settings = settingsBuilder()
.put("network.host", "127.0.0.1") .put("network.host", host)
.put("transport.tcp.port", ports[0]) .put("transport.tcp.port", ports[0])
.put("transport.profiles.default.port", ports[1]) .put("transport.profiles.default.port", ports[1])
.put("transport.profiles.client1.port", ports[2]) .put("transport.profiles.client1.port", ports[2])
@ -82,7 +97,7 @@ public class NettyTransportMultiPortTests extends ESTestCase {
int[] ports = getRandomPorts(2); int[] ports = getRandomPorts(2);
Settings settings = settingsBuilder() Settings settings = settingsBuilder()
.put("network.host", "127.0.0.1") .put("network.host", host)
.put("transport.tcp.port", ports[0]) .put("transport.tcp.port", ports[0])
.put("transport.profiles.client1.port", ports[1]) .put("transport.profiles.client1.port", ports[1])
.build(); .build();
@ -101,7 +116,7 @@ public class NettyTransportMultiPortTests extends ESTestCase {
int[] ports = getRandomPorts(1); int[] ports = getRandomPorts(1);
Settings settings = settingsBuilder() Settings settings = settingsBuilder()
.put("network.host", "127.0.0.1") .put("network.host", host)
.put("transport.tcp.port", ports[0]) .put("transport.tcp.port", ports[0])
.put("transport.profiles.client1.whatever", "foo") .put("transport.profiles.client1.whatever", "foo")
.build(); .build();
@ -119,7 +134,7 @@ public class NettyTransportMultiPortTests extends ESTestCase {
int[] ports = getRandomPorts(3); int[] ports = getRandomPorts(3);
Settings settings = settingsBuilder() Settings settings = settingsBuilder()
.put("network.host", "127.0.0.1") .put("network.host", host)
.put("transport.tcp.port", ports[0]) .put("transport.tcp.port", ports[0])
.put("transport.netty.port", ports[1]) .put("transport.netty.port", ports[1])
.put("transport.profiles.default.port", ports[2]) .put("transport.profiles.default.port", ports[2])
@ -140,7 +155,7 @@ public class NettyTransportMultiPortTests extends ESTestCase {
int[] ports = getRandomPorts(3); int[] ports = getRandomPorts(3);
Settings settings = settingsBuilder() Settings settings = settingsBuilder()
.put("network.host", "127.0.0.1") .put("network.host", host)
.put("transport.tcp.port", ports[0]) .put("transport.tcp.port", ports[0])
// mimics someone trying to define a profile for .local which is the profile for a node request to itself // mimics someone trying to define a profile for .local which is the profile for a node request to itself
.put("transport.profiles." + TransportService.DIRECT_RESPONSE_PROFILE + ".port", ports[1]) .put("transport.profiles." + TransportService.DIRECT_RESPONSE_PROFILE + ".port", ports[1])
@ -199,7 +214,7 @@ public class NettyTransportMultiPortTests extends ESTestCase {
private void assertConnectionRefused(int port) throws Exception { private void assertConnectionRefused(int port) throws Exception {
try { try {
trySocketConnection(new InetSocketTransportAddress(InetAddress.getByName("localhost"), port).address()); trySocketConnection(new InetSocketTransportAddress(InetAddress.getByName(host), port).address());
fail("Expected to get exception when connecting to port " + port); fail("Expected to get exception when connecting to port " + port);
} catch (IOException e) { } catch (IOException e) {
// expected // expected
@ -208,7 +223,7 @@ public class NettyTransportMultiPortTests extends ESTestCase {
} }
private void assertPortIsBound(int port) throws Exception { private void assertPortIsBound(int port) throws Exception {
assertPortIsBound("localhost", port); assertPortIsBound(host, port);
} }
private void assertPortIsBound(String host, int port) throws Exception { private void assertPortIsBound(String host, int port) throws Exception {