1 /*
   2  * Copyright (c) 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 /*
  25  * @test
  26  * @summary Basic test for jhsdb launcher
  27  * @library /test/lib/share/classes
  28  * @library /lib/testlibrary
  29  * @build jdk.testlibrary.*
  30  * @build jdk.test.lib.apps.*
  31  * @run main BasicLauncherTest
  32  */
  33 
  34 import static jdk.testlibrary.Asserts.assertTrue;
  35 import static jdk.testlibrary.Asserts.fail;
  36 
  37 import java.io.BufferedReader;
  38 import java.io.IOException;
  39 import java.io.InputStreamReader;
  40 import java.io.File;
  41 import java.util.ArrayList;
  42 import java.util.List;
  43 import java.util.Arrays;
  44 import jdk.testlibrary.JDKToolLauncher;
  45 import jdk.testlibrary.Utils;
  46 import jdk.testlibrary.OutputAnalyzer;
  47 import jdk.testlibrary.ProcessTools;
  48 import jdk.test.lib.apps.LingeredApp;
  49 import jdk.test.lib.hprof.HprofParser;
  50 import jdk.testlibrary.Platform;
  51 
  52 public class BasicLauncherTest {
  53 
  54     private final static String toolName = "jhsdb";
  55     private static LingeredApp theApp = null;
  56 
  57     /**
  58      *
  59      * @return exit code of tool
  60      */
  61     public static int launchCLHSDB()
  62         throws IOException {
  63 
  64         System.out.println("Starting LingeredApp");
  65         try {
  66             theApp = LingeredApp.startApp();
  67 
  68             System.out.println("Starting clhsdb against " + theApp.getPid());
  69             JDKToolLauncher launcher = JDKToolLauncher.createUsingTestJDK(toolName);
  70             launcher.addToolArg("clhsdb");
  71             launcher.addToolArg("--pid=" + Long.toString(theApp.getPid()));
  72 
  73             ProcessBuilder processBuilder = new ProcessBuilder(launcher.getCommand());
  74             processBuilder.redirectError(ProcessBuilder.Redirect.INHERIT);
  75             Process toolProcess = processBuilder.start();
  76             toolProcess.getOutputStream().write("quit\n".getBytes());
  77             toolProcess.getOutputStream().close();
  78 
  79             // By default child process output stream redirected to pipe, so we are reading it in foreground.
  80             BufferedReader reader = new BufferedReader(new InputStreamReader(toolProcess.getInputStream()));
  81 
  82             String line;
  83             while ((line = reader.readLine()) != null) {
  84                 System.out.println(line.trim());
  85             }
  86 
  87             toolProcess.waitFor();
  88 
  89             return toolProcess.exitValue();
  90         } catch (Exception ex) {
  91             throw new RuntimeException("Test ERROR " + ex, ex);
  92         } finally {
  93             LingeredApp.stopApp(theApp);
  94         }
  95     }
  96 
  97     /**
  98      *
  99      * @param vmArgs  - vm and java arguments to launch test app
 100      * @return exit code of tool
 101      */
 102     public static void launch(String expectedMessage, List<String> toolArgs)
 103         throws IOException {
 104 
 105         System.out.println("Starting LingeredApp");
 106         try {
 107             theApp = LingeredApp.startApp(Arrays.asList("-Xmx256m"));
 108 
 109             System.out.println("Starting " + toolName + " " + toolArgs.get(0) + " against " + theApp.getPid());
 110             JDKToolLauncher launcher = JDKToolLauncher.createUsingTestJDK(toolName);
 111 
 112             for (String cmd : toolArgs) {
 113                 launcher.addToolArg(cmd);
 114             }
 115 
 116             launcher.addToolArg("--pid=" + Long.toString(theApp.getPid()));
 117 
 118             ProcessBuilder processBuilder = new ProcessBuilder(launcher.getCommand());
 119             processBuilder.redirectError(ProcessBuilder.Redirect.INHERIT);
 120             OutputAnalyzer output = ProcessTools.executeProcess(processBuilder);;
 121             output.shouldContain(expectedMessage);
 122             output.shouldHaveExitValue(0);
 123 
 124         } catch (Exception ex) {
 125             throw new RuntimeException("Test ERROR " + ex, ex);
 126         } finally {
 127             LingeredApp.stopApp(theApp);
 128         }
 129     }
 130 
 131     public static void launch(String expectedMessage, String... toolArgs)
 132         throws IOException {
 133 
 134         launch(expectedMessage, Arrays.asList(toolArgs));
 135     }
 136 
 137     public static void testHeapDump() throws IOException {
 138         File dump = new File("jhsdb.jmap.dump." +
 139                              System.currentTimeMillis() + ".hprof");
 140         if (dump.exists()) {
 141             dump.delete();
 142         }
 143         dump.deleteOnExit();
 144 
 145         launch("heap written to", "jmap",
 146                "--binaryheap", "--dumpfile=" + dump.getAbsolutePath());
 147 
 148         assertTrue(dump.exists() && dump.isFile(),
 149                    "Could not create dump file " + dump.getAbsolutePath());
 150 
 151         try {
 152             HprofParser.parse(dump);
 153         } catch (Exception e) {
 154             e.printStackTrace();
 155             fail("Could not parse dump file " + dump.getAbsolutePath());
 156         }
 157     }
 158 
 159     public static void main(String[] args)
 160         throws IOException {
 161 
 162         if (!Platform.shouldSAAttach()) {
 163             // Silently skip the test if we don't have enough permissions to attach
 164             System.err.println("Error! Insufficient permissions to attach.");
 165             return;
 166         }
 167 
 168         launchCLHSDB();
 169 
 170         launch("No deadlocks found", "jstack");
 171         launch("compiler detected", "jmap");
 172         launch("Java System Properties", "jinfo");
 173         launch("java.threads", "jsnap");
 174 
 175         testHeapDump();
 176 
 177         // The test throws RuntimeException on error.
 178         // IOException is thrown if LingeredApp can't start because of some bad
 179         // environment condition
 180         System.out.println("Test PASSED");
 181     }
 182 }