2 * Copyright (C) 2015, EfficiOS Inc., Alexandre Montplaisir <alexmonthy@efficios.com>
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.
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.
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.
19 package org
.lttng
.ust
.agent
.utils
;
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
;
28 import org
.lttng
.ust
.agent
.jul
.LttngLogHandler
;
29 import org
.lttng
.ust
.agent
.log4j
.LttngLogAppender
;
30 import org
.lttng
.ust
.agent
.utils
.LttngSession
.Domain
;
33 * Utility methods to help with UST-Java tests
35 public final class MiscTestUtils
{
37 private MiscTestUtils() {}
40 * Check the the JUL native library is available, effectively allowing LTTng
41 * JUL handlers to be used.
43 * @return True if JUL works fine, false if it does not.
45 public static boolean checkForJulLibrary() {
47 LttngLogHandler testHandler
= new LttngLogHandler();
49 } catch (SecurityException
| IOException e
) {
56 * Check the the Log4j native library is available, effectively allowing
57 * LTTng Log4j appenders to be used.
59 * @return True if Log4j works fine, false if it does not.
61 public static boolean checkForLog4jLibrary() {
63 LttngLogAppender testAppender
= new LttngLogAppender();
65 } catch (SecurityException
| IOException e
) {
72 * Check that lttng-tools and babeltrace are installed on the system and
76 * The tracing domain to test for (we will try to setup a session
78 * @return True if the environment should allow tracing fine, false if there
81 public static boolean checkForLttngTools(Domain domain
) {
82 try (LttngSession session
= new LttngSession(null, domain
)) {
83 boolean ret1
= session
.enableAllEvents();
84 boolean ret2
= session
.start();
85 boolean ret3
= session
.stop();
87 * "lttng view" also tests that Babeltrace is installed and working
89 List
<String
> contents
= session
.view();
90 return (ret1
&& ret2
&& ret3
&& contents
.isEmpty());
95 * Check if there is a user session daemon currently running on the system.
96 * It needs to be of the same user as the application running this program.
98 * @return If there is a user session daemon currently running
100 public static boolean checkForUserSessiond() {
101 String userName
= System
.getProperty("user.name");
103 /* The user name is truncated to 7 characters in "ps" */
104 String shortUserName
= userName
.substring(0, Math
.min(userName
.length(), 7));
106 List
<String
> command
= Arrays
.asList("ps", "-e", "u");
107 List
<String
> output
= getOutputFromCommand(false, command
);
108 return output
.stream()
109 .filter(s
-> s
.contains("lttng-sessiond"))
110 .anyMatch(s
-> s
.startsWith(shortUserName
));
114 * Check if there is a root user session daemon daemon currently running on
117 * @return If there is a root session daemon currently running
119 public static boolean checkForRootSessiond() {
120 List
<String
> command
= Arrays
.asList("ps", "-e", "u");
121 List
<String
> output
= getOutputFromCommand(false, command
);
122 return output
.stream()
123 .filter(s
-> s
.contains("lttng-sessiond"))
124 .anyMatch(s
-> s
.startsWith("root"));
128 static List
<String
> getOutputFromCommand(List
<String
> command
) {
129 return MiscTestUtils
.getOutputFromCommand(true, command
);
132 static List
<String
> getOutputFromCommand(boolean print
, List
<String
> command
) {
134 Path tempFile
= Files
.createTempFile("test-output", null);
136 ProcessBuilder builder
= new ProcessBuilder(command
);
137 builder
.redirectErrorStream(true);
138 builder
.redirectOutput(Redirect
.to(tempFile
.toFile()));
140 Process p
= builder
.start();
143 List
<String
> lines
= Files
.readAllLines(tempFile
);
144 Files
.delete(tempFile
);
147 /* Also print the output to the console */
148 lines
.stream().forEach(s
-> System
.out
.println(s
));
153 } catch (IOException
| InterruptedException e
) {