1 package org
.lttng
.ust
.agent
.utils
;
3 import java
.io
.IOException
;
4 import java
.lang
.ProcessBuilder
.Redirect
;
5 import java
.nio
.file
.Files
;
6 import java
.nio
.file
.Path
;
7 import java
.util
.Arrays
;
10 import org
.lttng
.ust
.agent
.jul
.LttngLogHandler
;
11 import org
.lttng
.ust
.agent
.log4j
.LttngLogAppender
;
12 import org
.lttng
.ust
.agent
.utils
.LttngSession
.Domain
;
15 * Utility methods to help with UST-Java tests
17 public final class TestUtils
{
19 private TestUtils() {}
22 * Check the the JUL native library is available, effectively allowing LTTng
23 * JUL handlers to be used.
25 * @return True if JUL works fine, false if it does not.
27 public static boolean checkForJulLibrary() {
29 LttngLogHandler testHandler
= new LttngLogHandler();
31 } catch (SecurityException
| IOException e
) {
38 * Check the the Log4j native library is available, effectively allowing
39 * LTTng Log4j appenders to be used.
41 * @return True if Log4j works fine, false if it does not.
43 public static boolean checkForLog4jLibrary() {
45 LttngLogAppender testAppender
= new LttngLogAppender();
47 } catch (SecurityException
| IOException e
) {
54 * Check that lttng-tools and babeltrace are installed on the system and
58 * The tracing domain to test for (we will try to setup a session
60 * @return True if the environment should allow tracing fine, false if there
63 public static boolean checkForLttngTools(Domain domain
) {
64 try (LttngSession session
= new LttngSession(null, domain
)) {
65 boolean ret1
= session
.enableAllEvents();
66 boolean ret2
= session
.start();
67 boolean ret3
= session
.stop();
69 * "lttng view" also tests that Babeltrace is installed and working
71 List
<String
> contents
= session
.view();
72 return (ret1
&& ret2
&& ret3
&& contents
.isEmpty());
76 public static boolean checkForUserSessiond() {
77 String userName
= System
.getProperty("user.name");
79 /* The user name is truncated to 7 characters in "ps" */
80 String shortUserName
= userName
.substring(0, Math
.min(userName
.length(), 7));
82 List
<String
> command
= Arrays
.asList("ps", "-e", "u");
83 List
<String
> output
= getOutputFromCommand(false, command
);
84 return output
.stream()
85 .filter(s
-> s
.contains("lttng-sessiond"))
86 .anyMatch(s
-> s
.startsWith(shortUserName
));
89 public static boolean checkForRootSessiond() {
90 List
<String
> command
= Arrays
.asList("ps", "-e", "u");
91 List
<String
> output
= getOutputFromCommand(false, command
);
92 return output
.stream()
93 .filter(s
-> s
.contains("lttng-sessiond"))
94 .anyMatch(s
-> s
.startsWith("root"));
98 static List
<String
> getOutputFromCommand(List
<String
> command
) {
99 return TestUtils
.getOutputFromCommand(true, command
);
102 static List
<String
> getOutputFromCommand(boolean print
, List
<String
> command
) {
104 Path tempFile
= Files
.createTempFile("test-output", null);
106 ProcessBuilder builder
= new ProcessBuilder(command
);
107 builder
.redirectErrorStream(true);
108 builder
.redirectOutput(Redirect
.to(tempFile
.toFile()));
110 Process p
= builder
.start();
113 List
<String
> lines
= Files
.readAllLines(tempFile
);
114 Files
.delete(tempFile
);
117 /* Also print the output to the console */
118 lines
.stream().forEach(s
-> System
.out
.println(s
));
123 } catch (IOException
| InterruptedException e
) {