Test: Add the new log4j agent
[lttng-tools.git] / tests / utils / utils.sh
1 #!/src/bin/bash
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
18 SESSIOND_BIN="lttng-sessiond"
19 CONSUMERD_BIN="lttng-consumerd"
20 RELAYD_BIN="lttng-relayd"
21 LTTNG_BIN="lttng"
22 BABELTRACE_BIN="babeltrace"
23 OUTPUT_DEST=/dev/null 2>&1
24
25 # Minimal kernel version supported for session daemon tests
26 KERNEL_MAJOR_VERSION=2
27 KERNEL_MINOR_VERSION=6
28 KERNEL_PATCHLEVEL_VERSION=27
29
30 source $TESTDIR/utils/tap/tap.sh
31
32 function print_ok ()
33 {
34 # Check if we are a terminal
35 if [ -t 1 ]; then
36 echo -e "\e[1;32mOK\e[0m"
37 else
38 echo -e "OK"
39 fi
40 }
41
42 function print_fail ()
43 {
44 # Check if we are a terminal
45 if [ -t 1 ]; then
46 echo -e "\e[1;31mFAIL\e[0m"
47 else
48 echo -e "FAIL"
49 fi
50 }
51
52 function print_test_banner ()
53 {
54 local desc="$1"
55 diag "$desc"
56 }
57
58 function validate_kernel_version ()
59 {
60 local kern_version=($(uname -r | awk -F. '{ printf("%d.%d.%d\n",$1,$2,$3); }' | tr '.' '\n'))
61 if [ ${kern_version[0]} -gt $KERNEL_MAJOR_VERSION ]; then
62 return 0
63 fi
64 if [ ${kern_version[1]} -gt $KERNEL_MINOR_VERSION ]; then
65 return 0
66 fi
67 if [ ${kern_version[2]} -ge $KERNEL_PATCHLEVEL_VERSION ]; then
68 return 0
69 fi
70 return 1
71 }
72
73 # Generate a random string
74 # $1 = number of characters; defaults to 16
75 # $2 = include special characters; 1 = yes, 0 = no; defaults to yes
76 function randstring()
77 {
78 [ "$2" == "0" ] && CHAR="[:alnum:]" || CHAR="[:graph:]"
79 cat /dev/urandom | tr -cd "$CHAR" | head -c ${1:-16}
80 echo
81 }
82
83 function lttng_enable_kernel_event
84 {
85 local sess_name=$1
86 local event_name=$2
87 local channel_name=$3
88
89 if [ -z $event_name ]; then
90 # Enable all event if no event name specified
91 event_name="-a"
92 fi
93
94 if [ -z $channel_name ]; then
95 # default channel if none specified
96 chan=""
97 else
98 chan="-c $channel_name"
99 fi
100
101 $TESTDIR/../src/bin/lttng/$LTTNG_BIN enable-event "$event_name" $chan -s $sess_name -k >$OUTPUT_DEST
102 ok $? "Enable kernel event $event_name for session $sess_name"
103 }
104
105 function start_lttng_relayd
106 {
107 local opt=$1
108
109 DIR=$(readlink -f $TESTDIR)
110
111 if [ -z $(pidof lt-$RELAYD_BIN) ]; then
112 $DIR/../src/bin/lttng-relayd/$RELAYD_BIN -b $opt >$OUTPUT_DEST
113 #$DIR/../src/bin/lttng-relayd/$RELAYD_BIN $opt -vvv >>/tmp/relayd.log 2>&1 &
114 if [ $? -eq 1 ]; then
115 fail "Start lttng-relayd (opt: $opt)"
116 return 1
117 else
118 pass "Start lttng-relayd (opt: $opt)"
119 fi
120 else
121 pass "Start lttng-relayd (opt: $opt)"
122 fi
123 }
124
125 function stop_lttng_relayd_nocheck
126 {
127 PID_RELAYD=`pidof lt-$RELAYD_BIN`
128
129 diag "Killing lttng-relayd (pid: $PID_RELAYD)"
130 kill $PID_RELAYD >$OUTPUT_DEST
131 retval=$?
132
133 if [ $retval -eq 1 ]; then
134 out=1
135 while [ -n "$out" ]; do
136 out=$(pidof lt-$RELAYD_BIN)
137 sleep 0.5
138 done
139 fi
140 return $retval
141 }
142
143 function stop_lttng_relayd
144 {
145 stop_lttng_relayd_nocheck
146
147 if [ $? -eq 1 ]; then
148 fail "Killed lttng-relayd (pid: $PID_RELAYD)"
149 return 1
150 else
151 pass "Killed lttng-relayd (pid: $PID_RELAYD)"
152 return 0
153 fi
154 }
155
156 #First argument: load path for automatic loading
157 function start_lttng_sessiond()
158 {
159
160 local load_path="$1"
161 if [ -n $TEST_NO_SESSIOND ] && [ "$TEST_NO_SESSIOND" == "1" ]; then
162 # Env variable requested no session daemon
163 return
164 fi
165
166 validate_kernel_version
167 if [ $? -ne 0 ]; then
168 fail "Start session daemon"
169 BAIL_OUT "*** Kernel too old for session daemon tests ***"
170 fi
171
172 DIR=$(readlink -f $TESTDIR)
173 : ${LTTNG_SESSION_CONFIG_XSD_PATH=${DIR}/../src/common/config/}
174 export LTTNG_SESSION_CONFIG_XSD_PATH
175
176 if [ -z $(pidof lt-$SESSIOND_BIN) ]; then
177 # Have a load path ?
178 if [ -n "$1" ]; then
179 $DIR/../src/bin/lttng-sessiond/$SESSIOND_BIN --load "$1" --background --consumerd32-path="$DIR/../src/bin/lttng-consumerd/lttng-consumerd" --consumerd64-path="$DIR/../src/bin/lttng-consumerd/lttng-consumerd"
180 else
181 $DIR/../src/bin/lttng-sessiond/$SESSIOND_BIN --background --consumerd32-path="$DIR/../src/bin/lttng-consumerd/lttng-consumerd" --consumerd64-path="$DIR/../src/bin/lttng-consumerd/lttng-consumerd"
182 fi
183 #$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 &
184 status=$?
185 ok $status "Start session daemon"
186 fi
187 }
188
189 function stop_lttng_sessiond ()
190 {
191 if [ -n $TEST_NO_SESSIOND ] && [ "$TEST_NO_SESSIOND" == "1" ]; then
192 # Env variable requested no session daemon
193 return
194 fi
195
196 PID_SESSIOND=`pidof lt-$SESSIOND_BIN`
197
198 kill $PID_SESSIOND >$OUTPUT_DEST
199
200 if [ $? -eq 1 ]; then
201 fail "Kill sessions daemon"
202 return 1
203 else
204 out=1
205 while [ -n "$out" ]; do
206 out=$(pidof lt-$SESSIOND_BIN)
207 sleep 0.5
208 done
209 out=1
210 while [ -n "$out" ]; do
211 out=$(pidof $CONSUMERD_BIN)
212 sleep 0.5
213 done
214 pass "Kill session daemon"
215 fi
216 }
217
218 function list_lttng_with_opts ()
219 {
220 local opts=$1
221 $TESTDIR/../src/bin/lttng/$LTTNG_BIN list $opts >$OUTPUT_DEST
222 ok $? "Lttng-tool list command with option $opts"
223 }
224
225 function create_lttng_session_no_output ()
226 {
227 local sess_name=$1
228
229 $TESTDIR/../src/bin/lttng/$LTTNG_BIN create $sess_name --no-output >$OUTPUT_DEST
230 ok $? "Create session $sess_name in no-output mode"
231 }
232
233 function create_lttng_session ()
234 {
235 local sess_name=$1
236 local trace_path=$2
237 local expected_to_fail=$3
238
239 $TESTDIR/../src/bin/lttng/$LTTNG_BIN create $sess_name -o $trace_path > $OUTPUT_DEST
240 ret=$?
241 if [[ $expected_to_fail ]]; then
242 test "$ret" -ne "0"
243 ok $? "Expected fail on session creation $sess_name in $trace_path"
244 else
245 ok $ret "Create session $sess_name in $trace_path"
246 fi
247 }
248
249 function enable_ust_lttng_channel()
250 {
251 local sess_name=$1
252 local channel_name=$2
253 local expect_fail=$3
254
255 $TESTDIR/../src/bin/lttng/$LTTNG_BIN enable-channel -u $channel_name -s $sess_name >$OUTPUT_DEST
256 ret=$?
257 if [[ $expect_fail ]]; then
258 test "$ret" -ne "0"
259 ok $? "Expected fail on ust channel creation $channel_name in $sess_name"
260 else
261 ok $ret "Enable channel $channel_name for session $sess_name"
262 fi
263 }
264
265 function disable_ust_lttng_channel()
266 {
267 local sess_name=$1
268 local channel_name=$2
269
270 $TESTDIR/../src/bin/lttng/$LTTNG_BIN disable-channel -u $channel_name -s $sess_name >$OUTPUT_DEST
271 ok $? "Disable channel $channel_name for session $sess_name"
272 }
273
274 function enable_lttng_mmap_overwrite_kernel_channel()
275 {
276 local sess_name=$1
277 local channel_name=$2
278
279 $TESTDIR/../src/bin/lttng/$LTTNG_BIN enable-channel -s $sess_name $channel_name -k --output mmap --overwrite >$OUTPUT_DEST
280 ok $? "Enable channel $channel_name for session $sess_name"
281 }
282
283 function enable_lttng_mmap_overwrite_ust_channel()
284 {
285 local sess_name=$1
286 local channel_name=$2
287
288 $TESTDIR/../src/bin/lttng/$LTTNG_BIN enable-channel -s $sess_name $channel_name -u --output mmap --overwrite >$OUTPUT_DEST
289 ok $? "Enable channel $channel_name for session $sess_name"
290 }
291
292 function enable_ust_lttng_event ()
293 {
294 local sess_name=$1
295 local event_name="$2"
296 local channel_name=$3
297 local expected_to_fail=$4
298
299 if [ -z $channel_name ]; then
300 # default channel if none specified
301 chan=""
302 else
303 chan="-c $channel_name"
304 fi
305
306 $TESTDIR/../src/bin/lttng/$LTTNG_BIN enable-event "$event_name" $chan -s $sess_name -u >$OUTPUT_DEST
307 ret=$?
308 if [[ $expected_to_fail ]]; then
309 test $ret -ne "0"
310 ok $? "Enable ust event $event_name for session $session_name on channel $channel_name failed as expected"
311 else
312 ok $ret "Enable event $event_name for session $sess_name"
313 fi
314 }
315
316 function enable_jul_lttng_event()
317 {
318 sess_name=$1
319 event_name="$2"
320 channel_name=$3
321
322 if [ -z $channel_name ]; then
323 # default channel if none specified
324 chan=""
325 else
326 chan="-c $channel_name"
327 fi
328
329 $TESTDIR/../src/bin/lttng/$LTTNG_BIN enable-event "$event_name" $chan -s $sess_name -j >$OUTPUT_DEST
330 ok $? "Enable JUL event $event_name for session $sess_name"
331 }
332
333 function enable_jul_lttng_event_loglevel()
334 {
335 local sess_name=$1
336 local event_name="$2"
337 local loglevel=$3
338 local channel_name=$4
339
340 if [ -z $channel_name ]; then
341 # default channel if none specified
342 chan=""
343 else
344 chan="-c $channel_name"
345 fi
346
347 $TESTDIR/../src/bin/lttng/$LTTNG_BIN enable-event --loglevel $loglevel "$event_name" $chan -s $sess_name -j >$OUTPUT_DEST
348 ok $? "Enable JUL event $event_name for session $sess_name with loglevel $loglevel"
349 }
350
351 function enable_log4j_lttng_event()
352 {
353 sess_name=$1
354 event_name="$2"
355 channel_name=$3
356
357 if [ -z $channel_name ]; then
358 # default channel if none specified
359 chan=""
360 else
361 chan="-c $channel_name"
362 fi
363
364 $TESTDIR/../src/bin/lttng/$LTTNG_BIN enable-event "$event_name" $chan -s $sess_name -l >$OUTPUT_DEST
365 ok $? "Enable LOG4J event $event_name for session $sess_name"
366 }
367
368 function enable_log4j_lttng_event_loglevel()
369 {
370 local sess_name=$1
371 local event_name="$2"
372 local loglevel=$3
373 local channel_name=$4
374
375 if [ -z $channel_name ]; then
376 # default channel if none specified
377 chan=""
378 else
379 chan="-c $channel_name"
380 fi
381
382 $TESTDIR/../src/bin/lttng/$LTTNG_BIN enable-event --loglevel $loglevel "$event_name" $chan -s $sess_name -l >$OUTPUT_DEST
383 ok $? "Enable LOG4J event $event_name for session $sess_name with loglevel $loglevel"
384 }
385
386 function enable_ust_lttng_event_filter()
387 {
388 local sess_name="$1"
389 local event_name="$2"
390 local filter="$3"
391
392 $TESTDIR/../src/bin/lttng/$LTTNG_BIN enable-event "$event_name" -s $sess_name -u --filter "$filter" >$OUTPUT_DEST
393 ok $? "Enable event $event_name with filtering for session $sess_name"
394 }
395
396 function enable_ust_lttng_event_loglevel()
397 {
398 local sess_name="$1"
399 local event_name="$2"
400 local loglevel="$3"
401
402 $TESTDIR/../src/bin/lttng/$LTTNG_BIN enable-event "$event_name" -s $sess_name -u --loglevel $loglevel >$OUTPUT_DEST
403 ok $? "Enable event $event_name with loglevel $loglevel"
404 }
405
406 function enable_ust_lttng_event_loglevel_only()
407 {
408 local sess_name="$1"
409 local event_name="$2"
410 local loglevel="$3"
411
412 $TESTDIR/../src/bin/lttng/$LTTNG_BIN enable-event "$event_name" -s $sess_name -u --loglevel-only $loglevel >$OUTPUT_DEST
413 ok $? "Enable event $event_name with loglevel-only $loglevel"
414 }
415
416 function disable_ust_lttng_event ()
417 {
418 local sess_name="$1"
419 local event_name="$2"
420 local channel_name="$3"
421
422 if [ -z $channel_name ]; then
423 # default channel if none specified
424 chan=""
425 else
426 chan="-c $channel_name"
427 fi
428
429 $TESTDIR/../src/bin/lttng/$LTTNG_BIN disable-event "$event_name" -s $sess_name $chan -u >$OUTPUT_DEST
430 ok $? "Disable event $event_name for session $sess_name"
431 }
432
433 function disable_jul_lttng_event ()
434 {
435 local sess_name="$1"
436 local event_name="$2"
437
438 $TESTDIR/../src/bin/lttng/$LTTNG_BIN disable-event "$event_name" -s $sess_name -j >/dev/null 2>&1
439 ok $? "Disable JUL event $event_name for session $sess_name"
440 }
441
442 function disable_log4j_lttng_event ()
443 {
444 local sess_name="$1"
445 local event_name="$2"
446
447 $TESTDIR/../src/bin/lttng/$LTTNG_BIN disable-event "$event_name" -s $sess_name -l >/dev/null 2>&1
448 ok $? "Disable LOG4J event $event_name for session $sess_name"
449 }
450
451 function start_lttng_tracing ()
452 {
453 local sess_name=$1
454 local expected_to_fail=$2
455
456 $TESTDIR/../src/bin/lttng/$LTTNG_BIN start $sess_name >$OUTPUT_DEST
457 ret=$?
458 if [[ $expected_to_fail ]]; then
459 test "$ret" -ne "0"
460 ok $? "Expected fail on start tracing for session: $sess_name"
461 else
462 ok $ret "Start tracing for session $sess_name"
463 fi
464 }
465
466 function stop_lttng_tracing ()
467 {
468 local sess_name=$1
469 local expected_to_fail=$2
470
471 $TESTDIR/../src/bin/lttng/$LTTNG_BIN stop $sess_name >$OUTPUT_DEST
472 ret=$?
473 if [[ $expected_to_fail ]]; then
474 test "$ret" -ne "0"
475 ok $? "Expected fail on stop tracing for session: $sess_name"
476 else
477 ok $ret "Stop lttng tracing for session $sess_name"
478 fi
479 }
480
481 function destroy_lttng_session ()
482 {
483 local sess_name=$1
484 local expected_to_fail=$2
485
486 $TESTDIR/../src/bin/lttng/$LTTNG_BIN destroy $sess_name >$OUTPUT_DEST
487 ret=$?
488 if [[ $expected_to_fail ]]; then
489 test "$ret" -ne "0"
490 ok $? "Expected fail on session deletion $sess_name"
491 else
492 ok $ret "Destroy session $sess_name"
493 fi
494 }
495
496 function destroy_lttng_sessions ()
497 {
498 $TESTDIR/../src/bin/lttng/$LTTNG_BIN destroy --all >$OUTPUT_DEST
499 ok $? "Destroy all lttng sessions"
500 }
501
502 function lttng_snapshot_add_output ()
503 {
504 local sess_name=$1
505 local trace_path=$2
506 local expected_to_fail=$3
507
508 $TESTDIR/../src/bin/lttng/$LTTNG_BIN snapshot add-output -s $sess_name file://$trace_path >$OUTPUT_DEST
509 ret=$?
510 if [[ $expected_to_fail ]]; then
511 test "$ret" -ne "0"
512 ok $? "Failed to add a snapshot output file://$trace_path as expected"
513 else
514 ok $ret "Added snapshot output file://$trace_path"
515 fi
516 }
517
518 function lttng_snapshot_del_output ()
519 {
520 local sess_name=$1
521 local id=$2
522 local expected_to_fail=$3
523
524 $TESTDIR/../src/bin/lttng/$LTTNG_BIN snapshot del-output -s $sess_name $id >$OUTPUT_DEST
525 ret=$?
526 if [[ $expected_to_fail ]]; then
527 test "$ret" -ne "0"
528 ok $? "Expect fail on deletion of snapshot output id $id"
529 else
530 ok $ret "Deleted snapshot output id $id"
531 fi
532 }
533
534 function lttng_snapshot_record ()
535 {
536 local sess_name=$1
537 local trace_path=$2
538
539 $TESTDIR/../src/bin/lttng/$LTTNG_BIN snapshot record -s $sess_name >$OUTPUT_DEST
540 ok $? "Snapshot recorded"
541 }
542
543 function lttng_snapshot_list ()
544 {
545 local sess_name=$1
546 $TESTDIR/../src/bin/lttng/$LTTNG_BIN snapshot list-output -s $sess_name >$OUTPUT_DEST
547 ok $? "Snapshot list"
548 }
549
550 function lttng_save()
551 {
552 local sess_name=$1
553 local opts=$2
554
555 $TESTDIR/../src/bin/lttng/$LTTNG_BIN save $sess_name $opts >$OUTPUT_DEST
556 ok $? "Session successfully saved"
557 }
558
559 function lttng_load()
560 {
561 local opts=$1
562
563 $TESTDIR/../src/bin/lttng/$LTTNG_BIN load $opts >$OUTPUT_DEST
564 ok $? "Load command successful"
565 }
566
567 function trace_matches ()
568 {
569 local event_name=$1
570 local nr_iter=$2
571 local trace_path=$3
572
573 which $BABELTRACE_BIN >/dev/null
574 skip $? -ne 0 "Babeltrace binary not found. Skipping trace matches"
575
576 local count=$($BABELTRACE_BIN $trace_path | grep $event_name | wc -l)
577
578 if [ "$count" -ne "$nr_iter" ]; then
579 fail "Trace match"
580 diag "$count events found in trace"
581 else
582 pass "Trace match"
583 fi
584 }
585
586 function trace_match_only()
587 {
588 local event_name=$1
589 local nr_iter=$2
590 local trace_path=$3
591
592 which $BABELTRACE_BIN >/dev/null
593 skip $? -ne 0 "Babeltrace binary not found. Skipping trace matches"
594
595 local count=$($BABELTRACE_BIN $trace_path | grep $event_name | wc -l)
596 local total=$($BABELTRACE_BIN $trace_path | wc -l)
597
598 if [ "$nr_iter" -eq "$count" ] && [ "$total" -eq "$nr_iter" ]; then
599 pass "Trace match with $total event $event_name"
600 else
601 fail "Trace match"
602 diag "$total event(s) found, expecting $nr_iter of event $event_name and only found $count"
603 fi
604 }
605
606 function validate_trace
607 {
608 local event_name=$1
609 local trace_path=$2
610
611 which $BABELTRACE_BIN >/dev/null
612 if [ $? -ne 0 ]; then
613 skip 0 "Babeltrace binary not found. Skipping trace validation"
614 fi
615
616 OLDIFS=$IFS
617 IFS=","
618 for i in $event_name; do
619 traced=$($BABELTRACE_BIN $trace_path 2>/dev/null | grep $i | wc -l)
620 if [ "$traced" -ne 0 ]; then
621 pass "Validate trace for event $i, $traced events"
622 else
623 fail "Validate trace for event $i"
624 diag "Found $traced occurences of $i"
625 fi
626 done
627 ret=$?
628 IFS=$OLDIFS
629 return $ret
630 }
This page took 0.041071 seconds and 4 git commands to generate.