1 /*
   2  * Copyright (c) 2013, 2014, 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 import java.io.File;
  25 import java.lang.reflect.Method;
  26 import java.lang.reflect.Modifier;
  27 import java.util.ArrayList;
  28 import java.util.List;
  29 import java.util.concurrent.TimeUnit;
  30 import java.util.concurrent.atomic.AtomicReference;
  31 
  32 /**
  33  * @test
  34  * @library /lib/testlibrary
  35  * @bug 5016507 6173612 6319776 6342019 6484550 8004926
  36  * @summary Start a managed VM and test that a management tool can connect
  37  *          without connection or username/password details.
  38  *          TestManager will attempt a connection to the address obtained from
  39  *          both agent properties and jvmstat buffer.
  40  * @build jdk.testlibrary.* TestManager TestApplication
  41  * @run main/othervm/timeout=300 -XX:+UsePerfData LocalManagementTest
  42  */
  43 
  44 import jdk.testlibrary.ProcessTools;
  45 
  46 public class LocalManagementTest {
  47     private static final String TEST_CLASSPATH = System.getProperty("test.class.path");
  48     private static final String TEST_JDK = System.getProperty("test.jdk");
  49 
  50     public static void main(String[] args) throws Exception {
  51         int failures = 0;
  52         for(Method m : LocalManagementTest.class.getDeclaredMethods()) {
  53             if (Modifier.isStatic(m.getModifiers()) &&
  54                 m.getName().startsWith("test")) {
  55                 m.setAccessible(true);
  56                 try {
  57                     System.out.println(m.getName());
  58                     System.out.println("==========");
  59                     Boolean rslt = (Boolean)m.invoke(null);
  60                     if (!rslt) {
  61                         System.err.println(m.getName() + " failed");
  62                         failures++;
  63                     }
  64                 } catch (Exception e) {
  65                     e.printStackTrace();
  66                     failures++;
  67                 }
  68             }
  69         }
  70         if (failures > 0) {
  71             throw new Error("Test failed");
  72         }
  73     }
  74 
  75     @SuppressWarnings("unused")
  76     private static boolean test1() throws Exception {
  77         return doTest("1", "-Dcom.sun.management.jmxremote");
  78     }
  79 
  80     /**
  81      * no args (blank) - manager should attach and start agent
  82      */
  83     @SuppressWarnings("unused")
  84     private static boolean test3() throws Exception {
  85         return doTest("3", null);
  86     }
  87 
  88     /**
  89      * use DNS-only name service
  90      */
  91     @SuppressWarnings("unused")
  92     private static boolean test5() throws Exception {
  93         return doTest("5", "-Dsun.net.spi.namservice.provider.1=\"dns,sun\"");
  94     }
  95 
  96     private static boolean doTest(String testId, String arg) throws Exception {
  97         List<String> args = new ArrayList<>();
  98         args.add("-cp");
  99         args.add(TEST_CLASSPATH);
 100 
 101         if (arg != null) {
 102             args.add(arg);
 103         }
 104         args.add("TestApplication");
 105         ProcessBuilder server = ProcessTools.createJavaProcessBuilder(
 106             args.toArray(new String[args.size()])
 107         );
 108 
 109         Process serverPrc = null, clientPrc = null;
 110         try {
 111             final AtomicReference<String> port = new AtomicReference<>();
 112 
 113             serverPrc = ProcessTools.startProcess(
 114                 "TestApplication(" + testId + ")",
 115                 server,
 116                 (String line) -> {
 117                     if (line.startsWith("port:")) {
 118                          port.set(line.split("\\:")[1]);
 119                     } else if (line.startsWith("waiting")) {
 120                         return true;
 121                     }
 122                     return false;
 123                 },
 124                 5,
 125                 TimeUnit.SECONDS
 126             );
 127 
 128             System.out.println("Attaching test manager:");
 129             System.out.println("=========================");
 130             System.out.println("  PID           : " + serverPrc.getPid());
 131             System.out.println("  shutdown port : " + port.get());
 132 
 133             ProcessBuilder client = ProcessTools.createJavaProcessBuilder(
 134                 "-cp",
 135                 TEST_CLASSPATH,
 136                 "TestManager",
 137                 String.valueOf(serverPrc.getPid()),
 138                 port.get(),
 139                 "true"
 140             );
 141 
 142             clientPrc = ProcessTools.startProcess(
 143                 "TestManager",
 144                 client,
 145                 (String line) -> line.startsWith("Starting TestManager for PID"),
 146                 10,
 147                 TimeUnit.SECONDS
 148             );
 149 
 150             int clientExitCode = clientPrc.waitFor();
 151             int serverExitCode = serverPrc.waitFor();
 152             return clientExitCode == 0 && serverExitCode == 0;
 153         } finally {
 154             if (clientPrc != null) {
 155                 System.out.println("Stopping process " + clientPrc);
 156                 clientPrc.destroy();
 157                 clientPrc.waitFor();
 158             }
 159             if (serverPrc != null) {
 160                 System.out.println("Stopping process " + serverPrc);
 161                 serverPrc.destroy();
 162                 serverPrc.waitFor();
 163             }
 164         }
 165     }
 166 }