48eacc52852e0ce797ac8c906732626d9333addb
[lttng-ust-java-tests.git] / src / org / lttng / ust / agent / utils / LttngSessionControl.java
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 import java.util.stream.Collectors;
10
11 public final class LttngSessionControl {
12
13 private LttngSessionControl() {}
14
15 public enum Domain {
16 JUL("-j"),
17 LOG4J("-l");
18
19 private final String flag;
20
21 private Domain(String flag) {
22 this.flag = flag;
23 }
24
25 public String flag() {
26 return flag;
27 }
28 }
29
30 // ------------------------------------------------------------------------
31 // Public utility methods
32 // ------------------------------------------------------------------------
33
34 /**
35 * Setup a LTTng session by enabling certain events (or none).
36 *
37 * @param sessionName
38 * The name of the session to create. May be null to use the
39 * default one from lttng-tools.
40 * @param domain
41 * The tracing domain
42 * @param enabledEvents
43 * The list of events to enable. May be null or empty, to not
44 * enable any events.
45 * @return If the command executed successfully (return code = 0).
46 */
47 public static boolean setupSession(String sessionName, Domain domain, String... enabledEvents) {
48 String[] createCommand = (sessionName == null ?
49 new String[] { "lttng", "create" } :
50 new String[] { "lttng", "create", sessionName}
51 );
52
53 String eventsToEnable = (enabledEvents == null || enabledEvents.length == 0 ?
54 /*
55 * We have to enable a channel for 'lttng start' to work.
56 * However, we cannot enable a channel directly, see
57 * https://bugs.lttng.org/issues/894 . Instead we will enable an
58 * event we know does not exist
59 */
60 "non-event" :
61 Arrays.stream(enabledEvents).collect(Collectors.joining(","))
62 );
63
64 return executeCommands(new String[][] {
65 createCommand,
66 { "lttng", "enable-event", domain.flag(), eventsToEnable},
67 { "lttng", "start" }
68 });
69 }
70
71 /**
72 * Setup a LTTng session with all events enabled (lttng enable-event -a).
73 *
74 * @param sessionName
75 * The name of the session to create. May be null to use the
76 * default one from lttng-tools.
77 * @param domain
78 * The tracing domain
79 * @return If the command executed successfully (return code = 0).
80 */
81 public static boolean setupSessionAllEvents(String sessionName, Domain domain) {
82 String[] createCommand = (sessionName == null ?
83 new String[] { "lttng", "create" } :
84 new String[] { "lttng", "create", sessionName}
85 );
86
87 return executeCommands(new String[][] {
88 createCommand,
89 { "lttng", "enable-event", domain.flag(), "-a" },
90 { "lttng", "start" }
91 });
92 }
93
94 /**
95 * Stop the current tracing session
96 *
97 * @return If the command executed successfully (return code = 0).
98 */
99 public static boolean stopSession() {
100 return executeCommand(new String[] { "lttng", "stop" });
101 }
102
103 public static List<String> viewSession() {
104 return getOutputFromCommand(new String[] { "lttng", "view" });
105 }
106
107 /**
108 * Destroy the current tracing session
109 *
110 * @return If the command executed successfully (return code = 0).
111 */
112 public static boolean destroySession() {
113 return executeCommand(new String[] { "lttng", "destroy" });
114 }
115
116 // ------------------------------------------------------------------------
117 // Private helper methods
118 // ------------------------------------------------------------------------
119
120 private static boolean executeCommands(String [][] commands) {
121 for (String[] command : commands) {
122 if (executeCommand(command) == false) {
123 return false;
124 }
125 }
126 return true;
127 }
128
129 /**
130 * Just to test the environment / stdout are working correctly
131 */
132 public static void main(String[] args) {
133 executeCommand(new String[] {"ls", "-l"});
134 }
135
136 private static boolean executeCommand(String[] command) {
137 try {
138 ProcessBuilder builder = new ProcessBuilder(command);
139 builder.redirectErrorStream(true);
140 builder.redirectOutput(Redirect.INHERIT);
141
142 Process p = builder.start();
143 int ret = p.waitFor();
144 return (ret == 0);
145
146 } catch (IOException | InterruptedException e) {
147 return false;
148 }
149 }
150
151 private static List<String> getOutputFromCommand(String[] command) {
152 try {
153 Path tempFile = Files.createTempFile("test-output", null);
154
155 ProcessBuilder builder = new ProcessBuilder(command);
156 builder.redirectErrorStream(true);
157 builder.redirectOutput(Redirect.to(tempFile.toFile()));
158
159 Process p = builder.start();
160 p.waitFor();
161
162 List<String> lines = Files.readAllLines(tempFile);
163 Files.delete(tempFile);
164
165 /* Also print the output to the console */
166 lines.stream().forEach(s -> System.out.println(s));
167
168 return lines;
169
170 } catch (IOException | InterruptedException e) {
171 return null;
172 }
173 }
174 }
This page took 0.034362 seconds and 3 git commands to generate.