Browse Source

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.
Michael Basnight 6 years ago
parent
commit
55eded9a4f

+ 8 - 0
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);
+    }
 }

+ 16 - 1
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<InetAddress> inetAddressesToBind = Arrays.stream(InetAddressHelper.getAllAddresses())
+                        InetAddress[] allAddresses = InetAddressHelper.getAllAddresses();
+                        if (serverAddress instanceof Inet4Address) {
+                            allAddresses = InetAddressHelper.filterIPV4(allAddresses);
+                        } else {
+                            allAddresses = InetAddressHelper.filterIPV6(allAddresses);
+                        }
+                        final List<InetAddress> 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);