From 8c78fc096d85c2de36a5eec83e58e2362340d949 Mon Sep 17 00:00:00 2001 From: Michael Basnight Date: Tue, 12 Mar 2019 11:00:02 -0500 Subject: [PATCH] More lenient socket binding in LDAP tests (#39864) The LDAP tests attempt to bind all interfaces, but if for some reason an interface can't be bound the tests will stall until the suite times out. This modifies the tests to be a bit more lenient and allow some binding to fail so long as at least one succeeds. This allows the test to continue even in more antagonistic environments. --- .../common/network/InetAddressHelper.java | 8 ++++++++ .../SessionFactoryLoadBalancingTests.java | 17 ++++++++++++++++- 2 files changed, 24 insertions(+), 1 deletion(-) diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/common/network/InetAddressHelper.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/common/network/InetAddressHelper.java index 4c52cfb5c4c..3c852a3ae40 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/common/network/InetAddressHelper.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/common/network/InetAddressHelper.java @@ -19,4 +19,12 @@ public class InetAddressHelper { public static InetAddress[] getAllAddresses() throws SocketException { return NetworkUtils.getAllAddresses(); } + + public static InetAddress[] filterIPV4(InetAddress[] addresses){ + return NetworkUtils.filterIPV4(addresses); + } + + public static InetAddress[] filterIPV6(InetAddress[] addresses){ + return NetworkUtils.filterIPV6(addresses); + } } diff --git a/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/authc/ldap/support/SessionFactoryLoadBalancingTests.java b/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/authc/ldap/support/SessionFactoryLoadBalancingTests.java index cd159f69c48..2614a1dc3d9 100644 --- a/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/authc/ldap/support/SessionFactoryLoadBalancingTests.java +++ b/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/authc/ldap/support/SessionFactoryLoadBalancingTests.java @@ -31,6 +31,8 @@ import org.junit.After; import org.junit.Before; import java.io.IOException; +import java.net.ConnectException; +import java.net.Inet4Address; import java.net.InetAddress; import java.net.InetSocketAddress; import java.net.NoRouteToHostException; @@ -322,7 +324,13 @@ public class SessionFactoryLoadBalancingTests extends LdapTestCase { try { final boolean allSocketsOpened = awaitBusy(() -> { try { - final List inetAddressesToBind = Arrays.stream(InetAddressHelper.getAllAddresses()) + InetAddress[] allAddresses = InetAddressHelper.getAllAddresses(); + if (serverAddress instanceof Inet4Address) { + allAddresses = InetAddressHelper.filterIPV4(allAddresses); + } else { + allAddresses = InetAddressHelper.filterIPV6(allAddresses); + } + final List inetAddressesToBind = Arrays.stream(allAddresses) .filter(addr -> openedSockets.stream().noneMatch(s -> addr.equals(s.getLocalAddress()))) .filter(addr -> blacklistedAddress.contains(addr) == false) .collect(Collectors.toList()); @@ -334,8 +342,15 @@ public class SessionFactoryLoadBalancingTests extends LdapTestCase { } catch (NoRouteToHostException e) { logger.debug(new ParameterizedMessage("blacklisting address [{}] due to:", localAddress), e); blacklistedAddress.add(localAddress); + } catch (ConnectException e) { + logger.debug(new ParameterizedMessage("blacklisting address [{}] due to:", localAddress), e); + blacklistedAddress.add(localAddress); } } + if (openedSockets.size() == 0) { + logger.debug("Could not open any sockets from the available addresses"); + return false; + } return true; } catch (IOException e) { logger.debug(new ParameterizedMessage("caught exception while opening socket on [{}]", portToBind), e);