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