1 /* 2 * Copyright (c) 2008, 2015, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. 8 * 9 * This code is distributed in the hope that it will be useful, but WITHOUT 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 12 * version 2 for more details (a copy is included in the LICENSE file that 13 * accompanied this code). 14 * 15 * You should have received a copy of the GNU General Public License version 16 * 2 along with this work; if not, write to the Free Software Foundation, 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 18 * 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 20 * or visit www.oracle.com if you need additional information or have any 21 * questions. 22 */ 23 24 /* 25 * @test 26 * @bug 4981215 27 * @summary Tests that the jvmstat counters published by the out-of-the-box 28 * management agent for the JMX connection details are correct. 29 * @author Luis-Miguel Alventosa 30 * @modules jdk.management.agent/jdk.internal.agent 31 * @run clean JvmstatCountersTest 32 * @run build JvmstatCountersTest 33 * @run main/othervm/timeout=600 -XX:+UsePerfData JvmstatCountersTest 1 34 * @run main/othervm/timeout=600 -XX:+UsePerfData -Dcom.sun.management.jmxremote JvmstatCountersTest 2 35 * @run main/othervm/timeout=600 -XX:+UsePerfData -Dcom.sun.management.jmxremote.port=0 -Dcom.sun.management.jmxremote.authenticate=false -Dcom.sun.management.jmxremote.ssl=false JvmstatCountersTest 3 36 * @run main/othervm/timeout=600 -XX:+UsePerfData JvmstatCountersTest 4 37 */ 38 39 import java.io.*; 40 import java.lang.management.*; 41 import java.util.*; 42 import javax.management.*; 43 import javax.management.remote.*; 44 import com.sun.tools.attach.*; 45 import jdk.internal.agent.ConnectorAddressLink; 46 47 public class JvmstatCountersTest { 48 49 public static void checkAddress(String address) throws IOException { 50 System.out.println("Address = " + address); 51 JMXServiceURL url = new JMXServiceURL(address); 52 JMXConnector jmxc = JMXConnectorFactory.connect(url); 53 MBeanServerConnection mbsc = jmxc.getMBeanServerConnection(); 54 System.out.println("MBean Count = " + mbsc.getMBeanCount()); 55 } 56 57 public static void checkKey(Map<String, String> data, int index, 58 String key, String expectedValue) throws Exception { 59 String counter = "sun.management.JMXConnectorServer." + index + "." + key; 60 if (!data.containsKey(counter)) { 61 System.out.println("Test FAILED! Missing counter " + counter); 62 throw new IllegalArgumentException("Test case failed"); 63 } 64 String value = data.get(counter); 65 if (key.equals("remoteAddress")) { 66 checkAddress(value); 67 } else if (!expectedValue.equals(value)) { 68 System.out.println("Test FAILED! Invalid counter " + 69 counter + "=" + value); 70 throw new IllegalArgumentException("Test case failed"); 71 } 72 System.out.println("OK: " + counter + "=" + value); 73 } 74 75 public static void main(String args[]) throws Exception { 76 String localAddress = ConnectorAddressLink.importFrom(0); 77 Map<String, String> remoteData = ConnectorAddressLink.importRemoteFrom(0); 78 final int testCase = Integer.parseInt(args[0]); 79 switch (testCase) { 80 case 1: 81 if (localAddress == null && remoteData.isEmpty()) { 82 System.out.println("Test PASSED! The OOTB management " + 83 "agent didn't publish any jvmstat counter."); 84 } else { 85 System.out.println("Test FAILED! The OOTB management " + 86 "agent unexpectedly published jvmstat counters."); 87 throw new IllegalArgumentException("Test case 1 failed"); 88 } 89 break; 90 case 2: 91 if (localAddress == null) { 92 System.out.println("Test FAILED! The OOTB management " + 93 "agent didn't publish the local connector."); 94 throw new IllegalArgumentException("Test case 2 failed"); 95 } 96 checkAddress(localAddress); 97 if (!remoteData.isEmpty()) { 98 System.out.println("Test FAILED! The OOTB management " + 99 "agent shouldn't publish the remote connector."); 100 throw new IllegalArgumentException("Test case 2 failed"); 101 } 102 System.out.println("Test PASSED! The OOTB management " + 103 "agent only publishes the local connector through " + 104 "a jvmstat counter."); 105 break; 106 case 3: 107 if (localAddress == null) { 108 System.out.println("Test FAILED! The OOTB management " + 109 "agent didn't publish the local connector."); 110 throw new IllegalArgumentException("Test case 3 failed"); 111 } 112 checkAddress(localAddress); 113 if (remoteData.isEmpty()) { 114 System.out.println("Test FAILED! The OOTB management " + 115 "agent didnn't publish the remote connector."); 116 throw new IllegalArgumentException("Test case 3 failed"); 117 } 118 for (String key : remoteData.keySet()) { 119 if (!isKeyAcceptable(key)) { 120 System.out.println("Test FAILED! The OOTB management " + 121 "agent shouldn't publish anything which isn't " + 122 "related to the remote connector (" + key + ")."); 123 throw new IllegalArgumentException("Test case 3 failed"); 124 } 125 } 126 checkKey(remoteData, 0, "remoteAddress", null); 127 checkKey(remoteData, 0, "authenticate", "false"); 128 checkKey(remoteData, 0, "ssl", "false"); 129 checkKey(remoteData, 0, "sslRegistry", "false"); 130 checkKey(remoteData, 0, "sslNeedClientAuth", "false"); 131 System.out.println("Test PASSED! The OOTB management " + 132 "agent publishes both the local and remote " + 133 "connector info through jvmstat counters."); 134 break; 135 case 4: 136 if (localAddress != null || !remoteData.isEmpty()) { 137 System.out.println("Test FAILED! The OOTB management " + 138 "agent unexpectedly published jvmstat counters."); 139 throw new IllegalArgumentException("Test case 4 failed"); 140 } 141 RuntimeMXBean rt = ManagementFactory.getRuntimeMXBean(); 142 String name = rt.getName(); 143 System.out.println("name = " + name); 144 String vmid = name.substring(0, name.indexOf("@")); 145 System.out.println("vmid = " + vmid); 146 VirtualMachine vm = VirtualMachine.attach(vmid); 147 Properties p = new Properties(); 148 p.put("com.sun.management.jmxremote.port", "0"); 149 p.put("com.sun.management.jmxremote.authenticate", "false"); 150 p.put("com.sun.management.jmxremote.ssl", "false"); 151 vm.startManagementAgent(p); 152 vm.startLocalManagementAgent(); 153 vm.detach(); 154 String localAddress2 = ConnectorAddressLink.importFrom(0); 155 if (localAddress2 == null) { 156 System.out.println("Test FAILED! The OOTB management " + 157 "agent didn't publish the local connector."); 158 throw new IllegalArgumentException("Test case 4 failed"); 159 } 160 checkAddress(localAddress2); 161 Map<String, String> remoteData2 = ConnectorAddressLink.importRemoteFrom(0); 162 if (remoteData2.isEmpty()) { 163 System.out.println("Test FAILED! The OOTB management " + 164 "agent didnn't publish the remote connector."); 165 throw new IllegalArgumentException("Test case 4 failed"); 166 } 167 for (String key : remoteData2.keySet()) { 168 if (!isKeyAcceptable(key)) { 169 System.out.println("Test FAILED! The OOTB management " + 170 "agent shouldn't publish anything which isn't " + 171 "related to the remote connector (" + key + ")."); 172 throw new IllegalArgumentException("Test case 4 failed"); 173 } 174 } 175 checkKey(remoteData2, 0, "remoteAddress", null); 176 checkKey(remoteData2, 0, "authenticate", "false"); 177 checkKey(remoteData2, 0, "ssl", "false"); 178 checkKey(remoteData2, 0, "sslRegistry", "false"); 179 checkKey(remoteData2, 0, "sslNeedClientAuth", "false"); 180 System.out.println("Test PASSED! The OOTB management agent " + 181 "publishes both the local and remote connector " + 182 "info through jvmstat counters when the agent is " + 183 "loaded through the Attach API."); 184 } 185 System.out.println("Bye! Bye!"); 186 } 187 188 private static boolean isKeyAcceptable(String key) { 189 return key.startsWith("sun.management.JMXConnectorServer.0.") || 190 key.startsWith("sun.management.JMXConnectorServer.remote.enabled"); 191 } 192 }