|
24 | 24 |
|
25 | 25 | import java.net.InetAddress; |
26 | 26 | import java.net.NetworkInterface; |
| 27 | +import java.util.ArrayList; |
27 | 28 | import java.util.Enumeration; |
28 | 29 | import java.util.List; |
29 | 30 | import java.util.Locale; |
@@ -89,9 +90,7 @@ public void testInvalidRegionServerHostnameAbortsServer() throws Exception { |
89 | 90 |
|
90 | 91 | @Test |
91 | 92 | public void testRegionServerHostname() throws Exception { |
92 | | - Enumeration<NetworkInterface> netInterfaceList = NetworkInterface.getNetworkInterfaces(); |
93 | | - while (netInterfaceList.hasMoreElements()) { |
94 | | - NetworkInterface ni = netInterfaceList.nextElement(); |
| 93 | + for (NetworkInterface ni : getValidNetworkInterfaces()) { |
95 | 94 | Enumeration<InetAddress> addrList = ni.getInetAddresses(); |
96 | 95 | // iterate through host addresses and use each as hostname |
97 | 96 | while (addrList.hasMoreElements()) { |
@@ -205,4 +204,22 @@ public void testRegionServerHostnameReportedToMaster() throws Exception { |
205 | 204 | assertEquals(expectedRS, servers.size()); |
206 | 205 | } |
207 | 206 | } |
| 207 | + |
| 208 | + private boolean ignoreNetworkInterface(NetworkInterface networkInterface) throws Exception { |
| 209 | + return networkInterface == null || networkInterface.isLoopback() || networkInterface.isVirtual() |
| 210 | + || !networkInterface.isUp(); |
| 211 | + } |
| 212 | + |
| 213 | + private List<NetworkInterface> getValidNetworkInterfaces() throws Exception { |
| 214 | + List<NetworkInterface> validNetworkInterfaces = new ArrayList<>(); |
| 215 | + Enumeration<NetworkInterface> interfaces = NetworkInterface.getNetworkInterfaces(); |
| 216 | + while (interfaces.hasMoreElements()) { |
| 217 | + NetworkInterface networkInterface = interfaces.nextElement(); |
| 218 | + if (ignoreNetworkInterface(networkInterface)) { |
| 219 | + continue; |
| 220 | + } |
| 221 | + validNetworkInterfaces.add(networkInterface); |
| 222 | + } |
| 223 | + return validNetworkInterfaces; |
| 224 | + } |
208 | 225 | } |
0 commit comments