* Follow up to #42109: * Adjust test to only check that interface lookup by name works not actually lookup IPs which is brittle since virtual interfaces can be destroyed/created by Docker while the tests are running Co-authored-by: Jason Tedor <jason@tedor.me>
This commit is contained in:
parent
1dde6ba1db
commit
be42b2c70c
|
@ -200,10 +200,14 @@ public abstract class NetworkUtils {
|
||||||
static InetAddress[] getAllAddresses() throws IOException {
|
static InetAddress[] getAllAddresses() throws IOException {
|
||||||
return filterAllAddresses(address -> true, "no up-and-running addresses found");
|
return filterAllAddresses(address -> true, "no up-and-running addresses found");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static Optional<NetworkInterface> maybeGetInterfaceByName(List<NetworkInterface> networkInterfaces, String name) {
|
||||||
|
return networkInterfaces.stream().filter(netIf -> name.equals(netIf.getName())).findFirst();
|
||||||
|
}
|
||||||
|
|
||||||
/** Returns addresses for the given interface (it must be marked up) */
|
/** Returns addresses for the given interface (it must be marked up) */
|
||||||
static InetAddress[] getAddressesForInterface(String name) throws SocketException {
|
static InetAddress[] getAddressesForInterface(String name) throws SocketException {
|
||||||
Optional<NetworkInterface> networkInterface = getInterfaces().stream().filter((netIf) -> name.equals(netIf.getName())).findFirst();
|
Optional<NetworkInterface> networkInterface = maybeGetInterfaceByName(getInterfaces(), name);
|
||||||
|
|
||||||
if (networkInterface.isPresent() == false) {
|
if (networkInterface.isPresent() == false) {
|
||||||
throw new IllegalArgumentException("No interface named '" + name + "' found, got " + getInterfaces());
|
throw new IllegalArgumentException("No interface named '" + name + "' found, got " + getInterfaces());
|
||||||
|
|
|
@ -20,12 +20,15 @@
|
||||||
package org.elasticsearch.common.network;
|
package org.elasticsearch.common.network;
|
||||||
|
|
||||||
import org.elasticsearch.test.ESTestCase;
|
import org.elasticsearch.test.ESTestCase;
|
||||||
|
import org.elasticsearch.test.hamcrest.OptionalMatchers;
|
||||||
|
|
||||||
import java.net.InetAddress;
|
import java.net.InetAddress;
|
||||||
import java.net.NetworkInterface;
|
import java.net.NetworkInterface;
|
||||||
import java.util.Collections;
|
import java.util.List;
|
||||||
|
import java.util.Optional;
|
||||||
|
|
||||||
import static org.hamcrest.Matchers.containsString;
|
import static org.hamcrest.Matchers.containsString;
|
||||||
|
import static org.hamcrest.Matchers.equalTo;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Tests for network utils. Please avoid using any methods that cause DNS lookups!
|
* Tests for network utils. Please avoid using any methods that cause DNS lookups!
|
||||||
|
@ -79,23 +82,14 @@ public class NetworkUtilsTests extends ESTestCase {
|
||||||
assertArrayEquals(new InetAddress[] { InetAddress.getByName("::1") }, NetworkUtils.filterIPV6(addresses));
|
assertArrayEquals(new InetAddress[] { InetAddress.getByName("::1") }, NetworkUtils.filterIPV6(addresses));
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
// test that selecting by name is possible
|
||||||
* Test that selecting by name is possible and properly matches the addresses on all interfaces and virtual
|
public void testMaybeGetInterfaceByName() throws Exception {
|
||||||
* interfaces.
|
final List<NetworkInterface> networkInterfaces = NetworkUtils.getInterfaces();
|
||||||
*
|
for (NetworkInterface netIf : networkInterfaces) {
|
||||||
* Note that to avoid that this test fails when interfaces are down or they do not have addresses assigned to them,
|
final Optional<NetworkInterface> maybeNetworkInterface =
|
||||||
* they are ignored.
|
NetworkUtils.maybeGetInterfaceByName(networkInterfaces, netIf.getName());
|
||||||
*/
|
assertThat(maybeNetworkInterface, OptionalMatchers.isPresent());
|
||||||
public void testAddressInterfaceLookup() throws Exception {
|
assertThat(maybeNetworkInterface.get().getName(), equalTo(netIf.getName()));
|
||||||
for (NetworkInterface netIf : NetworkUtils.getInterfaces()) {
|
|
||||||
if (!netIf.isUp() || Collections.list(netIf.getInetAddresses()).isEmpty()) {
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
|
|
||||||
String name = netIf.getName();
|
|
||||||
InetAddress[] expectedAddresses = Collections.list(netIf.getInetAddresses()).toArray(new InetAddress[0]);
|
|
||||||
InetAddress[] foundAddresses = NetworkUtils.getAddressesForInterface(name);
|
|
||||||
assertArrayEquals(expectedAddresses, foundAddresses);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue