Skip to content

Commit 43ec1d8

Browse files
Fix NetworkUtilsTests (#43295)
* 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 <[email protected]>
1 parent 1836d6f commit 43ec1d8

File tree

2 files changed

+18
-25
lines changed

2 files changed

+18
-25
lines changed

server/src/main/java/org/elasticsearch/common/network/NetworkUtils.java

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -200,10 +200,14 @@ static InetAddress[] getGlobalAddresses() throws IOException {
200200
static InetAddress[] getAllAddresses() throws IOException {
201201
return filterAllAddresses(address -> true, "no up-and-running addresses found");
202202
}
203-
203+
204+
static Optional<NetworkInterface> maybeGetInterfaceByName(List<NetworkInterface> networkInterfaces, String name) {
205+
return networkInterfaces.stream().filter(netIf -> name.equals(netIf.getName())).findFirst();
206+
}
207+
204208
/** Returns addresses for the given interface (it must be marked up) */
205209
static InetAddress[] getAddressesForInterface(String name) throws SocketException {
206-
Optional<NetworkInterface> networkInterface = getInterfaces().stream().filter((netIf) -> name.equals(netIf.getName())).findFirst();
210+
Optional<NetworkInterface> networkInterface = maybeGetInterfaceByName(getInterfaces(), name);
207211

208212
if (networkInterface.isPresent() == false) {
209213
throw new IllegalArgumentException("No interface named '" + name + "' found, got " + getInterfaces());

server/src/test/java/org/elasticsearch/common/network/NetworkUtilsTests.java

Lines changed: 12 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -20,13 +20,15 @@
2020
package org.elasticsearch.common.network;
2121

2222
import org.elasticsearch.test.ESTestCase;
23+
import org.elasticsearch.test.hamcrest.OptionalMatchers;
2324

2425
import java.net.InetAddress;
2526
import java.net.NetworkInterface;
26-
import java.net.SocketException;
27-
import java.util.Collections;
27+
import java.util.List;
28+
import java.util.Optional;
2829

2930
import static org.hamcrest.Matchers.containsString;
31+
import static org.hamcrest.Matchers.equalTo;
3032

3133
/**
3234
* Tests for network utils. Please avoid using any methods that cause DNS lookups!
@@ -80,27 +82,14 @@ public void testFilter() throws Exception {
8082
assertArrayEquals(new InetAddress[] { InetAddress.getByName("::1") }, NetworkUtils.filterIPV6(addresses));
8183
}
8284

83-
/**
84-
* Test that selecting by name is possible and properly matches the addresses on all interfaces and virtual
85-
* interfaces.
86-
*
87-
* Note that to avoid that this test fails when interfaces are down or they do not have addresses assigned to them,
88-
* they are ignored.
89-
*/
90-
public void testAddressInterfaceLookup() throws Exception {
91-
for (NetworkInterface netIf : NetworkUtils.getInterfaces()) {
92-
try {
93-
if (!netIf.isUp() || Collections.list(netIf.getInetAddresses()).isEmpty()) {
94-
continue;
95-
}
96-
} catch (SocketException e) {
97-
throw new AssertionError("Failed to check if interface [" + netIf + "] is up", e);
98-
}
99-
100-
String name = netIf.getName();
101-
InetAddress[] expectedAddresses = Collections.list(netIf.getInetAddresses()).toArray(new InetAddress[0]);
102-
InetAddress[] foundAddresses = NetworkUtils.getAddressesForInterface(name);
103-
assertArrayEquals(expectedAddresses, foundAddresses);
85+
// test that selecting by name is possible
86+
public void testMaybeGetInterfaceByName() throws Exception {
87+
final List<NetworkInterface> networkInterfaces = NetworkUtils.getInterfaces();
88+
for (NetworkInterface netIf : networkInterfaces) {
89+
final Optional<NetworkInterface> maybeNetworkInterface =
90+
NetworkUtils.maybeGetInterfaceByName(networkInterfaces, netIf.getName());
91+
assertThat(maybeNetworkInterface, OptionalMatchers.isPresent());
92+
assertThat(maybeNetworkInterface.get().getName(), equalTo(netIf.getName()));
10493
}
10594
}
10695

0 commit comments

Comments
 (0)