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  *
  31  * @modules java.management
  32  *          jdk.attach
  33  *          jdk.management.agent/jdk.internal.agent
  34  *
  35  * @run clean JvmstatCountersTest
  36  * @run build JvmstatCountersTest
  37  * @run main/othervm/timeout=600 -XX:+UsePerfData JvmstatCountersTest 1
  38  * @run main/othervm/timeout=600 -XX:+UsePerfData -Dcom.sun.management.jmxremote JvmstatCountersTest 2
  39  * @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
  40  * @run main/othervm/timeout=600 -XX:+UsePerfData JvmstatCountersTest 4
  41  */
  42 
  43 import java.io.*;
  44 import java.lang.management.*;
  45 import java.util.*;
  46 import javax.management.*;
  47 import javax.management.remote.*;
  48 import com.sun.tools.attach.*;
  49 import jdk.internal.agent.ConnectorAddressLink;
  50 
  51 public class JvmstatCountersTest {
  52 
  53     public static void checkAddress(String address) throws IOException {
  54         System.out.println("Address = " + address);
  55         JMXServiceURL url = new JMXServiceURL(address);
  56         JMXConnector jmxc = JMXConnectorFactory.connect(url);
  57         MBeanServerConnection mbsc = jmxc.getMBeanServerConnection();
  58         System.out.println("MBean Count = " + mbsc.getMBeanCount());
  59     }
  60 
  61     public static void checkKey(Map<String, String> data, int index,
  62             String key, String expectedValue) throws Exception {
  63         String counter = "sun.management.JMXConnectorServer." + index + "." + key;
  64         if (!data.containsKey(counter)) {
  65             System.out.println("Test FAILED! Missing counter " + counter);
  66             throw new IllegalArgumentException("Test case failed");
  67         }
  68         String value = data.get(counter);
  69         if (key.equals("remoteAddress")) {
  70             checkAddress(value);
  71         } else if (!expectedValue.equals(value)) {
  72             System.out.println("Test FAILED! Invalid counter " +
  73                     counter + "=" + value);
  74             throw new IllegalArgumentException("Test case failed");
  75         }
  76         System.out.println("OK: " + counter + "=" + value);
  77     }
  78 
  79     public static void main(String args[]) throws Exception {
  80         String localAddress = ConnectorAddressLink.importFrom(0);
  81         Map<String, String> remoteData = ConnectorAddressLink.importRemoteFrom(0);
  82         final int testCase = Integer.parseInt(args[0]);
  83         switch (testCase) {
  84             case 1:
  85                 if (localAddress == null && remoteData.isEmpty()) {
  86                     System.out.println("Test PASSED! The OOTB management " +
  87                             "agent didn't publish any jvmstat counter.");
  88                 } else {
  89                     System.out.println("Test FAILED! The OOTB management " +
  90                             "agent unexpectedly published jvmstat counters.");
  91                     throw new IllegalArgumentException("Test case 1 failed");
  92                 }
  93                 break;
  94             case 2:
  95                 if (localAddress == null) {
  96                     System.out.println("Test FAILED! The OOTB management " +
  97                             "agent didn't publish the local connector.");
  98                     throw new IllegalArgumentException("Test case 2 failed");
  99                 }
 100                 checkAddress(localAddress);
 101                 if (!remoteData.isEmpty()) {
 102                     System.out.println("Test FAILED! The OOTB management " +
 103                             "agent shouldn't publish the remote connector.");
 104                     throw new IllegalArgumentException("Test case 2 failed");
 105                 }
 106                 System.out.println("Test PASSED! The OOTB management " +
 107                         "agent only publishes the local connector through " +
 108                         "a jvmstat counter.");
 109                 break;
 110             case 3:
 111                 if (localAddress == null) {
 112                     System.out.println("Test FAILED! The OOTB management " +
 113                             "agent didn't publish the local connector.");
 114                     throw new IllegalArgumentException("Test case 3 failed");
 115                 }
 116                 checkAddress(localAddress);
 117                 if (remoteData.isEmpty()) {
 118                     System.out.println("Test FAILED! The OOTB management " +
 119                             "agent didnn't publish the remote connector.");
 120                     throw new IllegalArgumentException("Test case 3 failed");
 121                 }
 122                 for (String key : remoteData.keySet()) {
 123                     if (!isKeyAcceptable(key)) {
 124                         System.out.println("Test FAILED! The OOTB management " +
 125                                 "agent shouldn't publish anything which isn't " +
 126                                 "related to the remote connector (" + key + ").");
 127                         throw new IllegalArgumentException("Test case 3 failed");
 128                     }
 129                 }
 130                 checkKey(remoteData, 0, "remoteAddress", null);
 131                 checkKey(remoteData, 0, "authenticate", "false");
 132                 checkKey(remoteData, 0, "ssl", "false");
 133                 checkKey(remoteData, 0, "sslRegistry", "false");
 134                 checkKey(remoteData, 0, "sslNeedClientAuth", "false");
 135                 System.out.println("Test PASSED! The OOTB management " +
 136                         "agent publishes both the local and remote " +
 137                         "connector info through jvmstat counters.");
 138                 break;
 139             case 4:
 140                 if (localAddress != null || !remoteData.isEmpty()) {
 141                     System.out.println("Test FAILED! The OOTB management " +
 142                             "agent unexpectedly published jvmstat counters.");
 143                     throw new IllegalArgumentException("Test case 4 failed");
 144                 }
 145                 RuntimeMXBean rt = ManagementFactory.getRuntimeMXBean();
 146                 String name = rt.getName();
 147                 System.out.println("name = " + name);
 148                 String vmid = name.substring(0, name.indexOf("@"));
 149                 System.out.println("vmid = " + vmid);
 150                 VirtualMachine vm = VirtualMachine.attach(vmid);
 151                 Properties p = new Properties();
 152                 p.put("com.sun.management.jmxremote.port", "0");
 153                 p.put("com.sun.management.jmxremote.authenticate", "false");
 154                 p.put("com.sun.management.jmxremote.ssl", "false");
 155                 vm.startManagementAgent(p);
 156                 vm.startLocalManagementAgent();
 157                 vm.detach();
 158                 String localAddress2 = ConnectorAddressLink.importFrom(0);
 159                 if (localAddress2 == null) {
 160                     System.out.println("Test FAILED! The OOTB management " +
 161                             "agent didn't publish the local connector.");
 162                     throw new IllegalArgumentException("Test case 4 failed");
 163                 }
 164                 checkAddress(localAddress2);
 165                 Map<String, String> remoteData2 = ConnectorAddressLink.importRemoteFrom(0);
 166                 if (remoteData2.isEmpty()) {
 167                     System.out.println("Test FAILED! The OOTB management " +
 168                             "agent didnn't publish the remote connector.");
 169                     throw new IllegalArgumentException("Test case 4 failed");
 170                 }
 171                 for (String key : remoteData2.keySet()) {
 172                     if (!isKeyAcceptable(key)) {
 173                         System.out.println("Test FAILED! The OOTB management " +
 174                                 "agent shouldn't publish anything which isn't " +
 175                                 "related to the remote connector (" + key + ").");
 176                         throw new IllegalArgumentException("Test case 4 failed");
 177                     }
 178                 }
 179                 checkKey(remoteData2, 0, "remoteAddress", null);
 180                 checkKey(remoteData2, 0, "authenticate", "false");
 181                 checkKey(remoteData2, 0, "ssl", "false");
 182                 checkKey(remoteData2, 0, "sslRegistry", "false");
 183                 checkKey(remoteData2, 0, "sslNeedClientAuth", "false");
 184                 System.out.println("Test PASSED! The OOTB management agent " +
 185                         "publishes both the local and remote connector " +
 186                         "info through jvmstat counters when the agent is " +
 187                         "loaded through the Attach API.");
 188         }
 189         System.out.println("Bye! Bye!");
 190     }
 191 
 192     private static boolean isKeyAcceptable(String key) {
 193         return key.startsWith("sun.management.JMXConnectorServer.0.") ||
 194                key.startsWith("sun.management.JMXConnectorServer.remote.enabled");
 195     }
 196 }