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 * @library /lib/testlibrary 36 * @bug 5016507 6173612 6319776 6342019 6484550 8004926 37 * @summary Start a managed VM and test that a management tool can connect 38 * without connection or username/password details. 39 * TestManager will attempt a connection to the address obtained from 40 * both agent properties and jvmstat buffer. 41 * @modules java.management/sun.management 42 * @build jdk.testlibrary.* TestManager TestApplication 43 * @run main/othervm/timeout=300 LocalManagementTest 44 */ 45 public class LocalManagementTest { 46 private static final String TEST_CLASSPATH = System.getProperty("test.class.path"); 47 48 public static void main(String[] args) throws Exception { 49 int failures = 0; 50 for(Method m : LocalManagementTest.class.getDeclaredMethods()) { 51 if (Modifier.isStatic(m.getModifiers()) && 52 m.getName().startsWith("test")) { 53 m.setAccessible(true); 54 try { 55 System.out.println(m.getName()); 56 System.out.println("=========="); 57 Boolean rslt = (Boolean)m.invoke(null); 58 if (!rslt) { 59 System.err.println(m.getName() + " failed"); 60 failures++; 61 } 62 } catch (Exception e) { 63 e.printStackTrace(); 64 failures++; 65 } 66 } 67 } 68 if (failures > 0) { 69 throw new Error("Test failed"); 70 } 71 } 72 73 @SuppressWarnings("unused") 74 private static boolean test1() throws Exception { 75 return doTest("1", "-Dcom.sun.management.jmxremote"); 76 } 77 78 /** 79 * no args (blank) - manager should attach and start agent 80 */ 81 @SuppressWarnings("unused") 82 private static boolean test3() throws Exception { 83 return doTest("3", null); 84 } 85 86 /** 87 * use DNS-only name service 88 */ 89 @SuppressWarnings("unused") 90 private static boolean test5() throws Exception { 91 return doTest("5", "-Dsun.net.spi.namservice.provider.1=\"dns,sun\""); 92 } 93 94 private static boolean doTest(String testId, String arg) throws Exception { 95 List<String> args = new ArrayList<>(); 96 args.add("-XX:+UsePerfData"); 97 args.addAll(Utils.getVmOptions()); 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 ); 125 126 System.out.println("Attaching test manager:"); 127 System.out.println("========================="); 128 System.out.println(" PID : " + serverPrc.getPid()); 129 System.out.println(" shutdown port : " + port.get()); 130 131 ProcessBuilder client = ProcessTools.createJavaProcessBuilder( 132 "-cp", 133 TEST_CLASSPATH, 134 "-XaddExports:java.management/sun.management=ALL-UNNAMED", 135 "TestManager", 136 String.valueOf(serverPrc.getPid()), 137 port.get(), 138 "true" 139 ); 140 141 clientPrc = ProcessTools.startProcess( 142 "TestManager", 143 client, 144 (String line) -> line.startsWith("Starting TestManager for PID") 145 ); 146 147 int clientExitCode = clientPrc.waitFor(); 148 int serverExitCode = serverPrc.waitFor(); 149 return clientExitCode == 0 && serverExitCode == 0; 150 } finally { 151 if (clientPrc != null) { 152 System.out.println("Stopping process " + clientPrc); 153 clientPrc.destroy(); 154 clientPrc.waitFor(); 155 } 156 if (serverPrc != null) { 157 System.out.println("Stopping process " + serverPrc); 158 serverPrc.destroy(); 159 serverPrc.waitFor(); 160 } 161 } 162 } 163 }