23 * questions. |
23 * questions. |
24 */ |
24 */ |
25 import java.io.InputStream; |
25 import java.io.InputStream; |
26 import java.io.OutputStream; |
26 import java.io.OutputStream; |
27 import java.net.ServerSocket; |
27 import java.net.ServerSocket; |
|
28 import java.net.InetAddress; |
28 import java.net.Socket; |
29 import java.net.Socket; |
29 import java.net.SocketAddress; |
30 import java.net.SocketAddress; |
30 import java.nio.file.Paths; |
31 import java.nio.file.Paths; |
|
32 import java.util.ArrayList; |
31 import java.util.List; |
33 import java.util.List; |
32 import jdk.jfr.Recording; |
34 import jdk.jfr.Recording; |
33 import jdk.jfr.consumer.RecordedEvent; |
35 import jdk.jfr.consumer.RecordedEvent; |
34 import jdk.jfr.consumer.RecordingFile; |
36 import jdk.jfr.consumer.RecordingFile; |
35 import jdk.test.lib.process.OutputAnalyzer; |
37 import jdk.test.lib.process.OutputAnalyzer; |
79 List<RecordedEvent> events = RecordingFile.readAllEvents(r.getDestination()); |
81 List<RecordedEvent> events = RecordingFile.readAllEvents(r.getDestination()); |
80 events.forEach(e -> log ("event = " + e)); |
82 events.forEach(e -> log ("event = " + e)); |
81 assertTrue(!events.isEmpty(), "No recorded network events"); |
83 assertTrue(!events.isEmpty(), "No recorded network events"); |
82 RecordedEvent e = events.get(0); |
84 RecordedEvent e = events.get(0); |
83 log(JFR_REPORTED_CONTAINER_HOSTNAME_TAG + e.getString("host")); |
85 log(JFR_REPORTED_CONTAINER_HOSTNAME_TAG + e.getString("host")); |
84 verifyIpAddress(e.getString("address")); |
86 |
|
87 // compare IP addresses |
|
88 boolean matchFound = false; |
|
89 InetAddress reportedByJfr = InetAddress.getByName(e.getString("address")); |
|
90 for (InetAddress ip : getLocalIp()) { |
|
91 if (ip.equals(reportedByJfr)) { |
|
92 matchFound = true; |
|
93 break; |
|
94 } |
|
95 } |
|
96 assertTrue(matchFound, "IP address match not found"); |
85 } |
97 } |
86 |
98 |
87 private static void verifyIpAddress(String eventIp) throws Exception { |
99 private static List<InetAddress> getLocalIp() throws Exception { |
88 ProcessBuilder pb = new ProcessBuilder("hostname", "--ip-address"); |
100 List<InetAddress> addrs = new ArrayList<>(); |
89 OutputAnalyzer out = new OutputAnalyzer(pb.start()); |
101 InetAddress localHost = InetAddress.getLocalHost(); |
90 out.shouldHaveExitValue(0); |
102 if (!localHost.isLoopbackAddress()) { |
91 log("hostname --ip-address returned: " + out.getOutput()); |
103 addrs.add(localHost); |
92 out.shouldContain(eventIp); |
104 } |
|
105 |
|
106 log("getLocalIp() returning:"); |
|
107 for (InetAddress addr : addrs) { |
|
108 log(addr.getHostName()); |
|
109 log(addr.getHostAddress()); |
|
110 } |
|
111 |
|
112 return addrs; |
93 } |
113 } |
94 |
114 |
95 private static void log(String msg) { |
115 private static void log(String msg) { |
96 System.out.println(msg); |
116 System.out.println(msg); |
97 } |
117 } |