|
20 | 20 | package org.elasticsearch.common.network; |
21 | 21 |
|
22 | 22 | import org.elasticsearch.test.ESTestCase; |
| 23 | +import org.elasticsearch.test.hamcrest.OptionalMatchers; |
23 | 24 |
|
24 | 25 | import java.net.InetAddress; |
25 | 26 | import java.net.NetworkInterface; |
26 | | -import java.net.SocketException; |
27 | | -import java.util.Collections; |
| 27 | +import java.util.List; |
| 28 | +import java.util.Optional; |
28 | 29 |
|
29 | 30 | import static org.hamcrest.Matchers.containsString; |
| 31 | +import static org.hamcrest.Matchers.equalTo; |
30 | 32 |
|
31 | 33 | /** |
32 | 34 | * Tests for network utils. Please avoid using any methods that cause DNS lookups! |
@@ -80,27 +82,14 @@ public void testFilter() throws Exception { |
80 | 82 | assertArrayEquals(new InetAddress[] { InetAddress.getByName("::1") }, NetworkUtils.filterIPV6(addresses)); |
81 | 83 | } |
82 | 84 |
|
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())); |
104 | 93 | } |
105 | 94 | } |
106 | 95 |
|
|
0 commit comments