de7b2330c679a892d817f8403ee23c27d3b4fa54
[lttng-ust-java-tests.git] / src / test / java / org / lttng / tools / utils / LttngUtils.java
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
19 package org.lttng.tools.utils;
20
21 import java.io.IOException;
22 import java.util.Arrays;
23 import java.util.List;
24
25 import org.lttng.tools.ILttngSession;
26 import org.lttng.tools.ILttngSession.Domain;
27 import org.lttng.ust.agent.jul.LttngLogHandler;
28 import org.lttng.ust.agent.log4j.LttngLogAppender;
29
30 /**
31 * Utility methods to test the presence of certain LTTng tools or libraries in
32 * the runtime environment.
33 */
34 public final class LttngUtils {
35
36 private LttngUtils() {}
37
38 /**
39 * Check the the JUL native library is available, effectively allowing LTTng
40 * JUL handlers to be used.
41 *
42 * @return True if JUL works fine, false if it does not.
43 */
44 public static boolean checkForJulLibrary() {
45 try {
46 LttngLogHandler testHandler = new LttngLogHandler();
47 testHandler.close();
48 } catch (SecurityException | IOException e) {
49 return false;
50 }
51 return true;
52 }
53
54 /**
55 * Check the the Log4j native library is available, effectively allowing
56 * LTTng Log4j appenders to be used.
57 *
58 * @return True if Log4j works fine, false if it does not.
59 */
60 public static boolean checkForLog4jLibrary() {
61 try {
62 LttngLogAppender testAppender = new LttngLogAppender();
63 testAppender.close();
64 } catch (SecurityException | IOException e) {
65 return false;
66 }
67 return true;
68 }
69
70 /**
71 * Check that lttng-tools and babeltrace are installed on the system and
72 * working.
73 *
74 * @param domain
75 * The tracing domain to test for (we will try to setup a session
76 * with this domain)
77 * @return True if the environment should allow tracing fine, false if there
78 * was an error
79 */
80 public static boolean checkForLttngTools(Domain domain) {
81 try (ILttngSession session = ILttngSession.createSession(null, domain)) {
82 boolean ret1 = session.enableAllEvents();
83 boolean ret2 = session.start();
84 boolean ret3 = session.stop();
85 /*
86 * "lttng view" also tests that Babeltrace is installed and working
87 */
88 List<String> contents = session.view();
89 return (ret1 && ret2 && ret3 && contents.isEmpty());
90 }
91 }
92
93 /**
94 * Check if there is a user session daemon currently running on the system.
95 * It needs to be of the same user as the application running this program.
96 *
97 * @return If there is a user session daemon currently running
98 */
99 public static boolean checkForUserSessiond() {
100 String userName = System.getProperty("user.name");
101
102 /* The user name is truncated to 7 characters in "ps" */
103 String shortUserName = userName.substring(0, Math.min(userName.length(), 7));
104
105 List<String> command = Arrays.asList("ps", "-e", "u");
106 List<String> output = ShellUtils.getOutputFromCommand(false, command);
107 return output.stream()
108 .filter(s -> s.contains("lttng-sessiond"))
109 .anyMatch(s -> s.startsWith(shortUserName));
110 }
111
112 /**
113 * Check if there is a root user session daemon daemon currently running on
114 * the system.
115 *
116 * @return If there is a root session daemon currently running
117 */
118 public static boolean checkForRootSessiond() {
119 List<String> command = Arrays.asList("ps", "-e", "u");
120 List<String> output = ShellUtils.getOutputFromCommand(false, command);
121 return output.stream()
122 .filter(s -> s.contains("lttng-sessiond"))
123 .anyMatch(s -> s.startsWith("root"));
124 }
125
126 }
This page took 0.031685 seconds and 3 git commands to generate.