|
1 /* |
|
2 * Copyright (c) 2017, 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.io.IOException; |
|
25 import java.io.OutputStream; |
|
26 import java.util.List; |
|
27 import java.util.Map; |
|
28 |
|
29 import jdk.test.lib.apps.LingeredApp; |
|
30 import jdk.test.lib.Platform; |
|
31 import jdk.test.lib.JDKToolLauncher; |
|
32 import jdk.test.lib.process.OutputAnalyzer; |
|
33 |
|
34 /** |
|
35 * This is a framework to run 'jhsdb clhsdb' commands. |
|
36 * See open/test/hotspot/jtreg/serviceability/sa/ClhsdbLongConstant.java for |
|
37 * an example of how to write a test. |
|
38 */ |
|
39 |
|
40 public class ClhsdbLauncher { |
|
41 |
|
42 private Process toolProcess; |
|
43 |
|
44 public void ClhsdbLauncher() { |
|
45 toolProcess = null; |
|
46 } |
|
47 |
|
48 /** |
|
49 * |
|
50 * Launches 'jhsdb clhsdb' and attaches to the Lingered App process. |
|
51 * @param lingeredAppPid - pid of the Lingered App or one its sub-classes. |
|
52 */ |
|
53 private void attach(long lingeredAppPid) |
|
54 throws IOException { |
|
55 |
|
56 System.out.println("Starting clhsdb against " + lingeredAppPid); |
|
57 JDKToolLauncher launcher = JDKToolLauncher.createUsingTestJDK("jhsdb"); |
|
58 launcher.addToolArg("clhsdb"); |
|
59 launcher.addToolArg("--pid=" + Long.toString(lingeredAppPid)); |
|
60 |
|
61 ProcessBuilder processBuilder = new ProcessBuilder(launcher.getCommand()); |
|
62 processBuilder.redirectError(ProcessBuilder.Redirect.INHERIT); |
|
63 |
|
64 toolProcess = processBuilder.start(); |
|
65 } |
|
66 |
|
67 /** |
|
68 * |
|
69 * Runs 'jhsdb clhsdb' commands and checks for expected and unexpected strings. |
|
70 * @param commands - clhsdb commands to execute. |
|
71 * @param expectedStrMap - Map of expected strings per command which need to |
|
72 * be checked in the output of the command. |
|
73 * @param unExpectedStrMap - Map of unexpected strings per command which should |
|
74 * not be present in the output of the command. |
|
75 * @return Output of the commands as a String. |
|
76 */ |
|
77 private String runCmd(List<String> commands, |
|
78 Map<String, List<String>> expectedStrMap, |
|
79 Map<String, List<String>> unExpectedStrMap) |
|
80 throws IOException, InterruptedException { |
|
81 String output; |
|
82 |
|
83 if (commands == null) { |
|
84 throw new RuntimeException("CLHSDB command must be provided\n"); |
|
85 } |
|
86 |
|
87 try (OutputStream out = toolProcess.getOutputStream()) { |
|
88 for (String cmd : commands) { |
|
89 out.write((cmd + "\n").getBytes()); |
|
90 } |
|
91 out.write("quit\n".getBytes()); |
|
92 out.flush(); |
|
93 } |
|
94 |
|
95 OutputAnalyzer oa = new OutputAnalyzer(toolProcess); |
|
96 try { |
|
97 toolProcess.waitFor(); |
|
98 } catch (InterruptedException ie) { |
|
99 toolProcess.destroyForcibly(); |
|
100 throw new Error("Problem awaiting the child process: " + ie); |
|
101 } |
|
102 |
|
103 oa.shouldHaveExitValue(0); |
|
104 output = oa.getOutput(); |
|
105 System.out.println(output); |
|
106 |
|
107 String[] parts = output.split("hsdb>"); |
|
108 for (String cmd : commands) { |
|
109 int index = commands.indexOf(cmd) + 1; |
|
110 OutputAnalyzer out = new OutputAnalyzer(parts[index]); |
|
111 |
|
112 if (expectedStrMap != null) { |
|
113 List<String> expectedStr = expectedStrMap.get(cmd); |
|
114 if (expectedStr != null) { |
|
115 for (String exp : expectedStr) { |
|
116 out.shouldContain(exp); |
|
117 } |
|
118 } |
|
119 } |
|
120 |
|
121 if (unExpectedStrMap != null) { |
|
122 List<String> unExpectedStr = unExpectedStrMap.get(cmd); |
|
123 if (unExpectedStr != null) { |
|
124 for (String unExp : unExpectedStr) { |
|
125 out.shouldNotContain(unExp); |
|
126 } |
|
127 } |
|
128 } |
|
129 } |
|
130 return output; |
|
131 } |
|
132 |
|
133 /** |
|
134 * |
|
135 * Launches 'jhsdb clhsdb', attaches to the Lingered App, executes the commands, |
|
136 * checks for expected and unexpected strings. |
|
137 * @param lingeredAppPid - pid of the Lingered App or one its sub-classes. |
|
138 * @param commands - clhsdb commands to execute. |
|
139 * @param expectedStrMap - Map of expected strings per command which need to |
|
140 * be checked in the output of the command. |
|
141 * @param unExpectedStrMap - Map of unexpected strings per command which should |
|
142 * not be present in the output of the command. |
|
143 * @return Output of the commands as a String. |
|
144 */ |
|
145 public String run(long lingeredAppPid, |
|
146 List<String> commands, |
|
147 Map<String, List<String>> expectedStrMap, |
|
148 Map<String, List<String>> unExpectedStrMap) |
|
149 throws IOException, InterruptedException { |
|
150 |
|
151 if (!Platform.shouldSAAttach()) { |
|
152 // Silently skip the test if we don't have enough permissions to attach |
|
153 System.out.println("SA attach not expected to work - test skipped."); |
|
154 return null; |
|
155 } |
|
156 |
|
157 attach(lingeredAppPid); |
|
158 return runCmd(commands, expectedStrMap, unExpectedStrMap); |
|
159 } |
|
160 } |