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