Add copyright header to all source files
[lttng-ust-java-tests.git] / src / test / java / org / lttng / ust / agent / utils / MiscTestUtils.java
CommitLineData
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
19package org.lttng.ust.agent.utils;
20
21import java.io.IOException;
22import java.lang.ProcessBuilder.Redirect;
23import java.nio.file.Files;
24import java.nio.file.Path;
25import java.util.Arrays;
26import java.util.List;
27
28import org.lttng.ust.agent.jul.LttngLogHandler;
29import org.lttng.ust.agent.log4j.LttngLogAppender;
30import org.lttng.ust.agent.utils.LttngSession.Domain;
31
32/**
33 * Utility methods to help with UST-Java tests
34 */
24b260d9 35public final class MiscTestUtils {
8576633f 36
24b260d9 37 private MiscTestUtils() {}
8576633f
AM
38
39 /**
40 * Check the the JUL native library is available, effectively allowing LTTng
41 * JUL handlers to be used.
42 *
43 * @return True if JUL works fine, false if it does not.
44 */
45 public static boolean checkForJulLibrary() {
46 try {
47 LttngLogHandler testHandler = new LttngLogHandler();
48 testHandler.close();
49 } catch (SecurityException | IOException e) {
50 return false;
51 }
52 return true;
53 }
54
55 /**
56 * Check the the Log4j native library is available, effectively allowing
57 * LTTng Log4j appenders to be used.
58 *
59 * @return True if Log4j works fine, false if it does not.
60 */
61 public static boolean checkForLog4jLibrary() {
62 try {
63 LttngLogAppender testAppender = new LttngLogAppender();
64 testAppender.close();
65 } catch (SecurityException | IOException e) {
66 return false;
67 }
68 return true;
69 }
70
71 /**
72 * Check that lttng-tools and babeltrace are installed on the system and
73 * working.
74 *
75 * @param domain
76 * The tracing domain to test for (we will try to setup a session
77 * with this domain)
78 * @return True if the environment should allow tracing fine, false if there
79 * was an error
80 */
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();
86 /*
87 * "lttng view" also tests that Babeltrace is installed and working
88 */
89 List<String> contents = session.view();
90 return (ret1 && ret2 && ret3 && contents.isEmpty());
91 }
92 }
93
94 public static boolean checkForUserSessiond() {
95 String userName = System.getProperty("user.name");
96
97 /* The user name is truncated to 7 characters in "ps" */
98 String shortUserName = userName.substring(0, Math.min(userName.length(), 7));
99
100 List<String> command = Arrays.asList("ps", "-e", "u");
101 List<String> output = getOutputFromCommand(false, command);
102 return output.stream()
103 .filter(s -> s.contains("lttng-sessiond"))
104 .anyMatch(s -> s.startsWith(shortUserName));
105 }
106
107 public static boolean checkForRootSessiond() {
108 List<String> command = Arrays.asList("ps", "-e", "u");
109 List<String> output = getOutputFromCommand(false, command);
110 return output.stream()
111 .filter(s -> s.contains("lttng-sessiond"))
112 .anyMatch(s -> s.startsWith("root"));
113 }
114
115
116 static List<String> getOutputFromCommand(List<String> command) {
24b260d9 117 return MiscTestUtils.getOutputFromCommand(true, command);
8576633f
AM
118 }
119
120 static List<String> getOutputFromCommand(boolean print, List<String> command) {
121 try {
122 Path tempFile = Files.createTempFile("test-output", null);
123
124 ProcessBuilder builder = new ProcessBuilder(command);
125 builder.redirectErrorStream(true);
126 builder.redirectOutput(Redirect.to(tempFile.toFile()));
127
128 Process p = builder.start();
129 p.waitFor();
130
131 List<String> lines = Files.readAllLines(tempFile);
132 Files.delete(tempFile);
133
134 if (print) {
135 /* Also print the output to the console */
136 lines.stream().forEach(s -> System.out.println(s));
137 }
138
139 return lines;
140
141 } catch (IOException | InterruptedException e) {
142 return null;
143 }
144 }
145}
This page took 0.027727 seconds and 4 git commands to generate.