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