Tests: Add a check for color support when printing status
[lttng-tools.git] / tests / utils.sh
CommitLineData
10a8a223 1#!/src/bin/bash
d3e8f6bb
DG
2#
3# Copyright (C) - 2012 David Goulet <dgoulet@efficios.com>
4#
5# This library is free software; you can redistribute it and/or modify it under
6# the terms of the GNU Lesser General Public License as published by the Free
7# Software Foundation; version 2.1 of the License.
8#
9# This library is distributed in the hope that it will be useful, but WITHOUT
10# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11# FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
12# details.
13#
14# You should have received a copy of the GNU Lesser General Public License
15# along with this library; if not, write to the Free Software Foundation, Inc.,
16# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
17
18SESSIOND_BIN="lttng-sessiond"
f4e40ab6 19RELAYD_BIN="lttng-relayd"
d3e8f6bb
DG
20LTTNG_BIN="lttng"
21BABELTRACE_BIN="babeltrace"
22
fd4dfcec
DG
23# Minimal kernel version supported for session daemon tests
24KERNEL_MAJOR_VERSION=2
25KERNEL_MINOR_VERSION=6
26KERNEL_PATCHLEVEL_VERSION=27
27
28function validate_kernel_version ()
29{
30 kern_version=($(uname -r | awk -F. '{ printf("%d.%d.%d\n",$1,$2,$3); }' | tr '.' '\n'))
31 if [ ${kern_version[0]} -gt $KERNEL_MAJOR_VERSION ]; then
32 return 0
33 fi
34 if [ ${kern_version[1]} -gt $KERNEL_MINOR_VERSION ]; then
35 return 0
36 fi
37 if [ ${kern_version[2]} -ge $KERNEL_PATCHLEVEL_VERSION ]; then
38 return 0
39 fi
40 return 1
41}
42
f4e40ab6
DG
43# Generate a random string
44# $1 = number of characters; defaults to 16
45# $2 = include special characters; 1 = yes, 0 = no; defaults to yes
46function randstring()
47{
48 [ "$2" == "0" ] && CHAR="[:alnum:]" || CHAR="[:graph:]"
49 cat /dev/urandom | tr -cd "$CHAR" | head -c ${1:-16}
50 echo
51}
52
355f483d 53function spawn_sessiond ()
d3e8f6bb 54{
317eef93
DG
55 echo ""
56 echo -n "Starting session daemon... "
fd4dfcec
DG
57 validate_kernel_version
58 if [ $? -ne 0 ]; then
fbb28ea9 59 echo -e "\n*** Kernel too old for session daemon tests ***\n"
fd4dfcec
DG
60 return 2
61 fi
62
2e6a03f6 63 DIR=$(readlink -f $TESTDIR)
355f483d 64
317eef93 65 if [ -z $(pidof lt-$SESSIOND_BIN) ]; then
355f483d 66 $DIR/../src/bin/lttng-sessiond/$SESSIOND_BIN --daemonize --quiet --consumerd32-path="$DIR/../src/bin/lttng-consumerd/lttng-consumerd" --consumerd64-path="$DIR/../src/bin/lttng-consumerd/lttng-consumerd"
25a818af 67 #$DIR/../src/bin/lttng-sessiond/$SESSIOND_BIN --consumerd32-path="$DIR/../src/bin/lttng-consumerd/lttng-consumerd" --consumerd64-path="$DIR/../src/bin/lttng-consumerd/lttng-consumerd" --verbose-consumer >>/tmp/sessiond.log 2>&1 &
d3e8f6bb
DG
68 if [ $? -eq 1 ]; then
69 echo -e "\e[1;31mFAILED\e[0m"
70 return 1
71 else
72 echo -e "\e[1;32mOK\e[0m"
73 fi
74 fi
fd4dfcec
DG
75
76 return 0
d3e8f6bb
DG
77}
78
f4e40ab6
DG
79function lttng_enable_kernel_event
80{
81 sess_name=$1
82 event_name=$2
83
84 if [ -z $event_name ]; then
85 # Enable all event if no event name specified
86 $event_name="-a"
87 fi
88
89 echo -n "Enabling kernel event $event_name for session $sess_name"
90 $TESTDIR/../src/bin/lttng/$LTTNG_BIN enable-event $event_name -s $sess_name -k >/dev/null 2>&1
91 if [ $? -eq 1 ]; then
92 echo -e '\e[1;31mFAILED\e[0m'
93 return 1
94 else
95 echo -e "\e[1;32mOK\e[0m"
96 fi
97}
98
99function lttng_start_relayd
100{
173af62f
DG
101 local opt="$1"
102
103 echo -e -n "Starting lttng-relayd (opt: $opt)... "
104
f4e40ab6
DG
105 DIR=$(readlink -f $TESTDIR)
106
107 if [ -z $(pidof lt-$RELAYD_BIN) ]; then
173af62f 108 $DIR/../src/bin/lttng-relayd/$RELAYD_BIN $opt >/dev/null 2>&1 &
25a818af 109 #$DIR/../src/bin/lttng-relayd/$RELAYD_BIN $opt -vvv >>/tmp/relayd.log 2>&1 &
f4e40ab6
DG
110 if [ $? -eq 1 ]; then
111 echo -e "\e[1;31mFAILED\e[0m"
112 return 1
113 else
114 echo -e "\e[1;32mOK\e[0m"
115 fi
116 else
117 echo -e "\e[1;32mOK\e[0m"
118 fi
119}
120
121function lttng_stop_relayd
122{
123 PID_RELAYD=`pidof lt-$RELAYD_BIN`
124
125 echo -e -n "Killing lttng-relayd (pid: $PID_RELAYD)... "
126 kill $PID_RELAYD >/dev/null 2>&1
127 if [ $? -eq 1 ]; then
128 echo -e "\e[1;31mFAILED\e[0m"
129 return 1
130 else
131 out=1
132 while [ -n "$out" ]; do
133 out=$(pidof lt-$RELAYD_BIN)
134 sleep 0.5
135 done
136 echo -e "\e[1;32mOK\e[0m"
137 return 0
138 fi
139}
140
355f483d
DG
141function start_sessiond()
142{
143 if [ -n $TEST_NO_SESSIOND ] && [ "$TEST_NO_SESSIOND" == "1" ]; then
144 # Env variable requested no session daemon
145 return
146 fi
147
148 spawn_sessiond
149 out=$?
150 if [ $out -eq 2 ]; then
151 # Kernel version is not compatible.
152 exit 0
153 elif [ $out -ne 0 ]; then
154 echo "NOT bad $?"
155 exit 1
156 fi
157
158 # Simply wait for the session daemon bootstrap
159 echo "Waiting for the session daemon to bootstrap (2 secs)"
160 sleep 2
161}
162
d3e8f6bb
DG
163function stop_sessiond ()
164{
355f483d
DG
165 if [ -n $TEST_NO_SESSIOND ] && [ "$TEST_NO_SESSIOND" == "1" ]; then
166 # Env variable requested no session daemon
167 return
168 fi
169
d3e8f6bb
DG
170 PID_SESSIOND=`pidof lt-$SESSIOND_BIN`
171
172 echo -e -n "Killing session daemon... "
173 kill $PID_SESSIOND >/dev/null 2>&1
174 if [ $? -eq 1 ]; then
175 echo -e "\e[1;31mFAILED\e[0m"
176 return 1
177 else
5fa32580
DG
178 out=1
179 while [ -n "$out" ]; do
180 out=$(pidof lt-$SESSIOND_BIN)
181 sleep 0.5
182 done
d3e8f6bb
DG
183 echo -e "\e[1;32mOK\e[0m"
184 fi
185}
186
187function create_lttng_session ()
188{
189 sess_name=$1
190 trace_path=$2
191
908ee7bd 192 echo -n "Creating lttng session $sess_name in $trace_path "
10a8a223 193 $TESTDIR/../src/bin/lttng/$LTTNG_BIN create $sess_name -o $trace_path >/dev/null 2>&1
d3e8f6bb
DG
194 if [ $? -eq 1 ]; then
195 echo -e "\e[1;31mFAILED\e[0m"
196 return 1
197 else
198 echo -e "\e[1;32mOK\e[0m"
d4018451
DG
199 fi
200}
201
202function enable_lttng_channel()
203{
204 sess_name=$1
205 channel_name=$2
206
207 echo -n "Enabling lttng channel $channel_name for session $sess_name"
208 $TESTDIR/../src/bin/lttng/$LTTNG_BIN enable-channel $channel_name -s $sess_name >/dev/null 2>&1
209 if [ $? -eq 1 ]; then
210 echo -e "\e[1;31mFAILED\e[0m"
211 return 1
212 else
213 echo -e "\e[1;32mOK\e[0m"
214 fi
215}
216
217function disable_lttng_channel()
218{
219 sess_name=$1
220 channel_name=$2
221
222 echo -n "Disabling lttng channel $channel_name for session $sess_name"
223 $TESTDIR/../src/bin/lttng/$LTTNG_BIN disable-channel $channel_name -s $sess_name >/dev/null 2>&1
224 if [ $? -eq 1 ]; then
225 echo -e "\e[1;31mFAILED\e[0m"
226 return 1
227 else
228 echo -e "\e[1;32mOK\e[0m"
d3e8f6bb
DG
229 fi
230}
231
232function enable_ust_lttng_event ()
233{
234 sess_name=$1
235 event_name=$2
236
237 echo -n "Enabling lttng event $event_name for session $sess_name "
10a8a223 238 $TESTDIR/../src/bin/lttng/$LTTNG_BIN enable-event $event_name -s $sess_name -u >/dev/null 2>&1
d3e8f6bb
DG
239 if [ $? -eq 1 ]; then
240 echo -e '\e[1;31mFAILED\e[0m'
241 return 1
242 else
243 echo -e "\e[1;32mOK\e[0m"
244 fi
245}
246
247function start_tracing ()
248{
249 sess_name=$1
250
251 echo -n "Start lttng tracing for session $sess_name "
10a8a223 252 $TESTDIR/../src/bin/lttng/$LTTNG_BIN start $sess_name >/dev/null 2>&1
d3e8f6bb
DG
253 if [ $? -eq 1 ]; then
254 echo -e '\e[1;31mFAILED\e[0m'
255 return 1
256 else
257 echo -e "\e[1;32mOK\e[0m"
258 fi
259}
260
261function stop_tracing ()
262{
263 sess_name=$1
264
265 echo -n "Stop lttng tracing for session $sess_name "
10a8a223 266 $TESTDIR/../src/bin/lttng/$LTTNG_BIN stop $sess_name >/dev/null 2>&1
d3e8f6bb
DG
267 if [ $? -eq 1 ]; then
268 echo -e '\e[1;31mFAILED\e[0m'
269 return 1
270 else
271 echo -e "\e[1;32mOK\e[0m"
272 fi
273}
274
275function destroy_lttng_session ()
276{
277 sess_name=$1
278
279 echo -n "Destroy lttng session $sess_name "
10a8a223 280 $TESTDIR/../src/bin/lttng/$LTTNG_BIN destroy $sess_name >/dev/null 2>&1
d3e8f6bb
DG
281 if [ $? -eq 1 ]; then
282 echo -e '\e[1;31mFAILED\e[0m'
283 return 1
284 else
285 echo -e "\e[1;32mOK\e[0m"
286 fi
287}
288
289function trace_matches ()
290{
291 event_name=$1
292 nr_iter=$2
293 trace_path=$3
294
317eef93
DG
295 which $BABELTRACE_BIN >/dev/null
296 if [ $? -eq 1 ]; then
297 echo "Babeltrace binary not found. Skipping trace matches"
298 return 0
299 fi
300
d3e8f6bb
DG
301 echo -n "Looking for $nr_iter $event_name in $trace_path "
302
303 count=$($BABELTRACE_BIN $trace_path | grep $event_name | wc -l)
304 if [ "$count" -ne "$nr_iter" ]; then
305 echo -e "$count found in trace \e[1;31mFAILED\e[0m"
306 return 1
307 else
308 echo -e "Trace is coherent \e[1;32mOK\e[0m"
309 return 0
310 fi
311}
f4e40ab6
DG
312
313function validate_trace
314{
315 event_name=$1
316 trace_path=$2
317
318 which $BABELTRACE_BIN >/dev/null
319 if [ $? -eq 1 ]; then
320 echo "Babeltrace binary not found. Skipping trace matches"
321 return 0
322 fi
323
324 echo -n "Validating trace for event $event_name... "
325 traced=$($BABELTRACE_BIN $trace_path 2>/dev/null | grep $event_name | wc -l)
326 if [ $traced -eq 0 ]; then
327 echo -e "\e[1;31mFAILED\e[0m"
328 return 1
329 else
330 echo -e "\e[1;32mOK\e[0m"
331 return 0
332 fi
333}
This page took 0.039695 seconds and 5 git commands to generate.