1 /* 2 * Copyright (c) 2014, 2018, 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 com.sun.tools.attach.*; 25 26 import java.nio.file.Files; 27 import java.nio.file.Path; 28 import java.util.Properties; 29 import java.util.List; 30 import java.io.File; 31 32 import jdk.test.lib.thread.ProcessThread; 33 import jdk.testlibrary.OutputAnalyzer; 34 import jdk.testlibrary.ProcessTools; 35 36 /* 37 * @test 38 * @bug 8033104 39 * @summary Test to make sure attach and jvmstat works correctly when java.io.tmpdir is set 40 * 41 * @library /lib/testlibrary 42 * @library /test/lib 43 * @modules jdk.attach 44 * jdk.jartool/sun.tools.jar 45 * 46 * @run build jdk.testlibrary.* Application RunnerUtil 47 * @run main/timeout=200 TempDirTest 48 */ 49 50 /* 51 * This test runs with an extra long timeout since it takes a really long time with -Xcomp 52 * when starting many processes. 53 */ 54 55 public class TempDirTest { 56 57 private static long startTime; 58 59 public static void main(String args[]) throws Throwable { 60 61 startTime = System.currentTimeMillis(); 62 63 Path clientTmpDir = Files.createTempDirectory("TempDirTest-client"); 64 clientTmpDir.toFile().deleteOnExit(); 65 Path targetTmpDir = Files.createTempDirectory("TempDirTest-target"); 66 targetTmpDir.toFile().deleteOnExit(); 67 68 // run the test with all possible combinations of setting java.io.tmpdir 69 runExperiment(null, null); 70 runExperiment(clientTmpDir, null); 71 runExperiment(clientTmpDir, targetTmpDir); 72 runExperiment(null, targetTmpDir); 73 74 } 75 76 private static int counter = 0; 77 78 /* 79 * The actual test is in the nested class TestMain. 80 * The responsibility of this class is to: 81 * 1. Start the Application class in a separate process. 82 * 2. Find the pid and shutdown port of the running Application. 83 * 3. Launches the tests in nested class TestMain that will attach to the Application. 84 * 4. Shut down the Application. 85 */ 86 public static void runExperiment(Path clientTmpDir, Path targetTmpDir) throws Throwable { 87 88 System.out.print("### Running tests with overridden tmpdir for"); 89 System.out.print(" client: " + (clientTmpDir == null ? "no" : "yes")); 90 System.out.print(" target: " + (targetTmpDir == null ? "no" : "yes")); 91 System.out.println(" ###"); 92 93 long elapsedTime = (System.currentTimeMillis() - startTime) / 1000; 94 System.out.println("Started after " + elapsedTime + "s"); 95 96 final String pidFile = "TempDirTest.Application.pid-" + counter++; 97 ProcessThread processThread = null; 98 try { 99 String[] tmpDirArg = null; 100 if (targetTmpDir != null) { 101 tmpDirArg = new String[] {"-Djava.io.tmpdir=" + targetTmpDir}; 102 } 103 processThread = RunnerUtil.startApplication(tmpDirArg); 104 launchTests(processThread.getPid(), clientTmpDir); 105 } catch (Throwable t) { 106 System.out.println("TempDirTest got unexpected exception: " + t); 107 t.printStackTrace(); 108 throw t; 109 } finally { 110 // Make sure the Application process is stopped. 111 RunnerUtil.stopApplication(processThread); 112 } 113 114 elapsedTime = (System.currentTimeMillis() - startTime) / 1000; 115 System.out.println("Completed after " + elapsedTime + "s"); 116 117 } 118 119 /** 120 * Runs the actual tests in nested class TestMain. 121 * The reason for running the tests in a separate process 122 * is that we need to modify the class path and 123 * the -Djava.io.tmpdir property. 124 */ 125 private static void launchTests(long pid, Path clientTmpDir) throws Throwable { 126 final String sep = File.separator; 127 128 String classpath = 129 System.getProperty("test.class.path", ""); 130 131 String[] tmpDirArg = null; 132 if (clientTmpDir != null) { 133 tmpDirArg = new String [] {"-Djava.io.tmpdir=" + clientTmpDir}; 134 } 135 136 // Arguments : [-Djava.io.tmpdir=] -classpath cp TempDirTest$TestMain pid 137 String[] args = RunnerUtil.concat( 138 tmpDirArg, 139 new String[] { 140 "-classpath", 141 classpath, 142 "TempDirTest$TestMain", 143 Long.toString(pid) }); 144 OutputAnalyzer output = ProcessTools.executeTestJvm(args); 145 output.shouldHaveExitValue(0); 146 } 147 148 /** 149 * This is the actual test. It will attach to the running Application 150 * and perform a number of basic attach tests. 151 */ 152 public static class TestMain { 153 public static void main(String args[]) throws Exception { 154 String pid = args[0]; 155 156 // Test 1 - list method should list the target VM 157 System.out.println(" - Test: VirtualMachine.list"); 158 List<VirtualMachineDescriptor> l = VirtualMachine.list(); 159 boolean found = false; 160 for (VirtualMachineDescriptor vmd: l) { 161 if (vmd.id().equals(pid)) { 162 found = true; 163 break; 164 } 165 } 166 if (found) { 167 System.out.println(" - " + pid + " found."); 168 } else { 169 throw new RuntimeException(pid + " not found in VM list"); 170 } 171 172 // Test 2 - try to attach and verify connection 173 174 System.out.println(" - Attaching to application ..."); 175 VirtualMachine vm = VirtualMachine.attach(pid); 176 177 System.out.println(" - Test: system properties in target VM"); 178 Properties props = vm.getSystemProperties(); 179 String value = props.getProperty("attach.test"); 180 if (value == null || !value.equals("true")) { 181 throw new RuntimeException("attach.test property not set"); 182 } 183 System.out.println(" - attach.test property set as expected"); 184 } 185 } 186 }