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