| 1 | package org.lttng.ust.agent.utils; |
| 2 | |
| 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; |
| 8 | import java.util.List; |
| 9 | |
| 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; |
| 13 | |
| 14 | /** |
| 15 | * Utility methods to help with UST-Java tests |
| 16 | */ |
| 17 | public final class TestUtils { |
| 18 | |
| 19 | private TestUtils() {} |
| 20 | |
| 21 | /** |
| 22 | * Check the the JUL native library is available, effectively allowing LTTng |
| 23 | * JUL handlers to be used. |
| 24 | * |
| 25 | * @return True if JUL works fine, false if it does not. |
| 26 | */ |
| 27 | public static boolean checkForJulLibrary() { |
| 28 | try { |
| 29 | LttngLogHandler testHandler = new LttngLogHandler(); |
| 30 | testHandler.close(); |
| 31 | } catch (SecurityException | IOException e) { |
| 32 | return false; |
| 33 | } |
| 34 | return true; |
| 35 | } |
| 36 | |
| 37 | /** |
| 38 | * Check the the Log4j native library is available, effectively allowing |
| 39 | * LTTng Log4j appenders to be used. |
| 40 | * |
| 41 | * @return True if Log4j works fine, false if it does not. |
| 42 | */ |
| 43 | public static boolean checkForLog4jLibrary() { |
| 44 | try { |
| 45 | LttngLogAppender testAppender = new LttngLogAppender(); |
| 46 | testAppender.close(); |
| 47 | } catch (SecurityException | IOException e) { |
| 48 | return false; |
| 49 | } |
| 50 | return true; |
| 51 | } |
| 52 | |
| 53 | /** |
| 54 | * Check that lttng-tools and babeltrace are installed on the system and |
| 55 | * working. |
| 56 | * |
| 57 | * @param domain |
| 58 | * The tracing domain to test for (we will try to setup a session |
| 59 | * with this domain) |
| 60 | * @return True if the environment should allow tracing fine, false if there |
| 61 | * was an error |
| 62 | */ |
| 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(); |
| 68 | /* |
| 69 | * "lttng view" also tests that Babeltrace is installed and working |
| 70 | */ |
| 71 | List<String> contents = session.view(); |
| 72 | return (ret1 && ret2 && ret3 && contents.isEmpty()); |
| 73 | } |
| 74 | } |
| 75 | |
| 76 | public static boolean checkForUserSessiond() { |
| 77 | String userName = System.getProperty("user.name"); |
| 78 | |
| 79 | /* The user name is truncated to 7 characters in "ps" */ |
| 80 | String shortUserName = userName.substring(0, Math.min(userName.length(), 7)); |
| 81 | |
| 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)); |
| 87 | } |
| 88 | |
| 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")); |
| 95 | } |
| 96 | |
| 97 | |
| 98 | static List<String> getOutputFromCommand(List<String> command) { |
| 99 | return TestUtils.getOutputFromCommand(true, command); |
| 100 | } |
| 101 | |
| 102 | static List<String> getOutputFromCommand(boolean print, List<String> command) { |
| 103 | try { |
| 104 | Path tempFile = Files.createTempFile("test-output", null); |
| 105 | |
| 106 | ProcessBuilder builder = new ProcessBuilder(command); |
| 107 | builder.redirectErrorStream(true); |
| 108 | builder.redirectOutput(Redirect.to(tempFile.toFile())); |
| 109 | |
| 110 | Process p = builder.start(); |
| 111 | p.waitFor(); |
| 112 | |
| 113 | List<String> lines = Files.readAllLines(tempFile); |
| 114 | Files.delete(tempFile); |
| 115 | |
| 116 | if (print) { |
| 117 | /* Also print the output to the console */ |
| 118 | lines.stream().forEach(s -> System.out.println(s)); |
| 119 | } |
| 120 | |
| 121 | return lines; |
| 122 | |
| 123 | } catch (IOException | InterruptedException e) { |
| 124 | return null; |
| 125 | } |
| 126 | } |
| 127 | } |