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