Commit | Line | Data |
---|---|---|
2b408e85 AM |
1 | /* |
2 | * Copyright (C) 2015, EfficiOS Inc., Alexandre Montplaisir <alexmonthy@efficios.com> | |
3 | * | |
4 | * This program is free software; you can redistribute it and/or modify | |
5 | * it under the terms of the GNU General Public License as published by | |
6 | * the Free Software Foundation; either version 2 of the License, or | |
7 | * (at your option) any later version. | |
8 | * | |
9 | * This program is distributed in the hope that it will be useful, | |
10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
12 | * GNU General Public License for more details. | |
13 | * | |
14 | * You should have received a copy of the GNU General Public License along | |
15 | * with this program; if not, write to the Free Software Foundation, Inc., | |
16 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. | |
17 | */ | |
18 | ||
8576633f AM |
19 | package org.lttng.ust.agent.utils; |
20 | ||
21 | import java.io.IOException; | |
22 | import java.lang.ProcessBuilder.Redirect; | |
23 | import java.nio.file.Files; | |
24 | import java.nio.file.Path; | |
25 | import java.util.Arrays; | |
26 | import java.util.List; | |
dd1cba3f | 27 | import java.util.StringJoiner; |
8576633f AM |
28 | |
29 | import org.lttng.ust.agent.jul.LttngLogHandler; | |
30 | import org.lttng.ust.agent.log4j.LttngLogAppender; | |
31 | import org.lttng.ust.agent.utils.LttngSession.Domain; | |
32 | ||
33 | /** | |
34 | * Utility methods to help with UST-Java tests | |
35 | */ | |
24b260d9 | 36 | public final class MiscTestUtils { |
8576633f | 37 | |
24b260d9 | 38 | private MiscTestUtils() {} |
8576633f AM |
39 | |
40 | /** | |
41 | * Check the the JUL native library is available, effectively allowing LTTng | |
42 | * JUL handlers to be used. | |
43 | * | |
44 | * @return True if JUL works fine, false if it does not. | |
45 | */ | |
46 | public static boolean checkForJulLibrary() { | |
47 | try { | |
48 | LttngLogHandler testHandler = new LttngLogHandler(); | |
49 | testHandler.close(); | |
50 | } catch (SecurityException | IOException e) { | |
51 | return false; | |
52 | } | |
53 | return true; | |
54 | } | |
55 | ||
56 | /** | |
57 | * Check the the Log4j native library is available, effectively allowing | |
58 | * LTTng Log4j appenders to be used. | |
59 | * | |
60 | * @return True if Log4j works fine, false if it does not. | |
61 | */ | |
62 | public static boolean checkForLog4jLibrary() { | |
63 | try { | |
64 | LttngLogAppender testAppender = new LttngLogAppender(); | |
65 | testAppender.close(); | |
66 | } catch (SecurityException | IOException e) { | |
67 | return false; | |
68 | } | |
69 | return true; | |
70 | } | |
71 | ||
72 | /** | |
73 | * Check that lttng-tools and babeltrace are installed on the system and | |
74 | * working. | |
75 | * | |
76 | * @param domain | |
77 | * The tracing domain to test for (we will try to setup a session | |
78 | * with this domain) | |
79 | * @return True if the environment should allow tracing fine, false if there | |
80 | * was an error | |
81 | */ | |
82 | public static boolean checkForLttngTools(Domain domain) { | |
83 | try (LttngSession session = new LttngSession(null, domain)) { | |
84 | boolean ret1 = session.enableAllEvents(); | |
85 | boolean ret2 = session.start(); | |
86 | boolean ret3 = session.stop(); | |
87 | /* | |
88 | * "lttng view" also tests that Babeltrace is installed and working | |
89 | */ | |
90 | List<String> contents = session.view(); | |
91 | return (ret1 && ret2 && ret3 && contents.isEmpty()); | |
92 | } | |
93 | } | |
94 | ||
8a0613fa AM |
95 | /** |
96 | * Check if there is a user session daemon currently running on the system. | |
97 | * It needs to be of the same user as the application running this program. | |
98 | * | |
99 | * @return If there is a user session daemon currently running | |
100 | */ | |
8576633f AM |
101 | public static boolean checkForUserSessiond() { |
102 | String userName = System.getProperty("user.name"); | |
103 | ||
104 | /* The user name is truncated to 7 characters in "ps" */ | |
105 | String shortUserName = userName.substring(0, Math.min(userName.length(), 7)); | |
106 | ||
107 | List<String> command = Arrays.asList("ps", "-e", "u"); | |
108 | List<String> output = getOutputFromCommand(false, command); | |
109 | return output.stream() | |
110 | .filter(s -> s.contains("lttng-sessiond")) | |
111 | .anyMatch(s -> s.startsWith(shortUserName)); | |
112 | } | |
113 | ||
8a0613fa AM |
114 | /** |
115 | * Check if there is a root user session daemon daemon currently running on | |
116 | * the system. | |
117 | * | |
118 | * @return If there is a root session daemon currently running | |
119 | */ | |
8576633f AM |
120 | public static boolean checkForRootSessiond() { |
121 | List<String> command = Arrays.asList("ps", "-e", "u"); | |
122 | List<String> output = getOutputFromCommand(false, command); | |
123 | return output.stream() | |
124 | .filter(s -> s.contains("lttng-sessiond")) | |
125 | .anyMatch(s -> s.startsWith("root")); | |
126 | } | |
127 | ||
128 | ||
129 | static List<String> getOutputFromCommand(List<String> command) { | |
24b260d9 | 130 | return MiscTestUtils.getOutputFromCommand(true, command); |
8576633f AM |
131 | } |
132 | ||
133 | static List<String> getOutputFromCommand(boolean print, List<String> command) { | |
134 | try { | |
dd1cba3f AM |
135 | /* "echo" the command to stdout */ |
136 | StringJoiner sj = new StringJoiner(" ", "$ ", ""); | |
137 | command.stream().forEach(sj::add); | |
138 | System.out.println(sj.toString()); | |
139 | ||
8576633f AM |
140 | Path tempFile = Files.createTempFile("test-output", null); |
141 | ||
142 | ProcessBuilder builder = new ProcessBuilder(command); | |
143 | builder.redirectErrorStream(true); | |
144 | builder.redirectOutput(Redirect.to(tempFile.toFile())); | |
145 | ||
146 | Process p = builder.start(); | |
147 | p.waitFor(); | |
148 | ||
149 | List<String> lines = Files.readAllLines(tempFile); | |
150 | Files.delete(tempFile); | |
151 | ||
152 | if (print) { | |
153 | /* Also print the output to the console */ | |
dd1cba3f AM |
154 | lines.stream().forEach(System.out::println); |
155 | } else { | |
156 | System.out.println("(output silenced)"); | |
8576633f AM |
157 | } |
158 | ||
dd1cba3f | 159 | System.out.println("(returned from command)"); |
8576633f AM |
160 | return lines; |
161 | ||
162 | } catch (IOException | InterruptedException e) { | |
163 | return null; | |
164 | } | |
165 | } | |
166 | } |