8222769: [TESTBUG] TestJFRNetworkEvents should not rely on hostname command
authormseledtsov
Wed, 01 May 2019 13:25:54 -0700
changeset 54671 41339a468716
parent 54670 a38438fcbbd2
child 54672 a43d6467317d
8222769: [TESTBUG] TestJFRNetworkEvents should not rely on hostname command Summary: Using InetAddress.getLocalHost() Reviewed-by: egahlin, lmesnik Contributed-by: Severin Gehwolf <sgehwolf@redhat.com>
test/hotspot/jtreg/containers/docker/JfrNetwork.java
--- a/test/hotspot/jtreg/containers/docker/JfrNetwork.java	Wed May 01 13:07:30 2019 -0700
+++ b/test/hotspot/jtreg/containers/docker/JfrNetwork.java	Wed May 01 13:25:54 2019 -0700
@@ -25,9 +25,11 @@
 import java.io.InputStream;
 import java.io.OutputStream;
 import java.net.ServerSocket;
+import java.net.InetAddress;
 import java.net.Socket;
 import java.net.SocketAddress;
 import java.nio.file.Paths;
+import java.util.ArrayList;
 import java.util.List;
 import jdk.jfr.Recording;
 import jdk.jfr.consumer.RecordedEvent;
@@ -81,15 +83,33 @@
         assertTrue(!events.isEmpty(), "No recorded network events");
         RecordedEvent e = events.get(0);
         log(JFR_REPORTED_CONTAINER_HOSTNAME_TAG + e.getString("host"));
-        verifyIpAddress(e.getString("address"));
+
+        // compare IP addresses
+        boolean matchFound = false;
+        InetAddress reportedByJfr = InetAddress.getByName(e.getString("address"));
+        for (InetAddress ip : getLocalIp()) {
+            if (ip.equals(reportedByJfr)) {
+                matchFound = true;
+                break;
+            }
+        }
+        assertTrue(matchFound, "IP address match not found");
     }
 
-    private static void verifyIpAddress(String eventIp) throws Exception {
-        ProcessBuilder pb = new ProcessBuilder("hostname", "--ip-address");
-        OutputAnalyzer out = new OutputAnalyzer(pb.start());
-        out.shouldHaveExitValue(0);
-        log("hostname --ip-address returned: " + out.getOutput());
-        out.shouldContain(eventIp);
+    private static List<InetAddress> getLocalIp() throws Exception {
+        List<InetAddress> addrs = new ArrayList<>();
+        InetAddress localHost = InetAddress.getLocalHost();
+        if (!localHost.isLoopbackAddress()) {
+            addrs.add(localHost);
+        }
+
+        log("getLocalIp() returning:");
+        for (InetAddress addr : addrs) {
+            log(addr.getHostName());
+            log(addr.getHostAddress());
+        }
+
+        return addrs;
     }
 
     private static void log(String msg) {