diff --git a/hbase-server/src/test/java/org/apache/hadoop/hbase/regionserver/TestRegionServerHostname.java b/hbase-server/src/test/java/org/apache/hadoop/hbase/regionserver/TestRegionServerHostname.java index 1acc926bc901..566b85b01e92 100644 --- a/hbase-server/src/test/java/org/apache/hadoop/hbase/regionserver/TestRegionServerHostname.java +++ b/hbase-server/src/test/java/org/apache/hadoop/hbase/regionserver/TestRegionServerHostname.java @@ -24,6 +24,7 @@ import java.net.InetAddress; import java.net.NetworkInterface; +import java.util.ArrayList; import java.util.Enumeration; import java.util.List; import java.util.Locale; @@ -89,9 +90,7 @@ public void testInvalidRegionServerHostnameAbortsServer() throws Exception { @Test public void testRegionServerHostname() throws Exception { - Enumeration netInterfaceList = NetworkInterface.getNetworkInterfaces(); - while (netInterfaceList.hasMoreElements()) { - NetworkInterface ni = netInterfaceList.nextElement(); + for (NetworkInterface ni : getValidNetworkInterfaces()) { Enumeration addrList = ni.getInetAddresses(); // iterate through host addresses and use each as hostname while (addrList.hasMoreElements()) { @@ -205,4 +204,22 @@ public void testRegionServerHostnameReportedToMaster() throws Exception { assertEquals(expectedRS, servers.size()); } } + + private boolean ignoreNetworkInterface(NetworkInterface networkInterface) throws Exception { + return networkInterface == null || networkInterface.isLoopback() || networkInterface.isVirtual() + || !networkInterface.isUp(); + } + + private List getValidNetworkInterfaces() throws Exception { + List validNetworkInterfaces = new ArrayList<>(); + Enumeration interfaces = NetworkInterface.getNetworkInterfaces(); + while (interfaces.hasMoreElements()) { + NetworkInterface networkInterface = interfaces.nextElement(); + if (ignoreNetworkInterface(networkInterface)) { + continue; + } + validNetworkInterfaces.add(networkInterface); + } + return validNetworkInterfaces; + } }