cdc0c4c34470c6751da5b9ebdfbc9af62c975825
[lttng-tools.git] / tests / regression / ust / java-log4j / test_java_log4j
1 #!/bin/bash
2 #
3 # Copyright (C) - 2014 David Goulet <dgoulet@efficios.com>
4 #
5 # This program is free software; you can redistribute it and/or modify it under
6 # the terms of the GNU General Public License, version 2 only, as published by
7 # the Free Software Foundation.
8 #
9 # This program 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 General Public License for more
12 # details.
13 #
14 # You should have received a copy of the GNU General Public License along with
15 # this program; if not, write to the Free Software Foundation, Inc., 51
16 # Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
17
18 TEST_DESC="Java LOG4J support"
19
20 CURDIR=$(dirname $0)/
21 TESTDIR=$CURDIR/../../..
22 NR_ITER=6
23 NR_MSEC_WAIT=1000
24 TESTAPP_NAME="JTestLTTng"
25 TESTAPP_BIN="$TESTAPP_NAME.java"
26 TESTAPP_PATH="$CURDIR/$TESTAPP_NAME"
27 SESSION_NAME="log4j"
28 EVENT_NAME="log4j-event"
29 EVENT_NAME2="log4j-event-2"
30 JAVA_CP="$CURDIR:/usr/local/share/java/liblttng-ust-agent.jar:/usr/share/java/*"
31 OUTPUT_DEST="/dev/null"
32
33 NUM_TESTS=155
34
35 source $TESTDIR/utils/utils.sh
36
37 function run_app
38 {
39 local debug_tp=$1
40 local fire_second_tp=$2
41
42 # FIXME: test app should have synchro.
43 java -cp $JAVA_CP -Djava.library.path="/usr/local/lib:/usr/lib" $TESTAPP_NAME $NR_ITER $NR_MSEC_WAIT $debug_tp $fire_second_tp >/dev/null 2>&1
44 }
45
46 function run_app_background
47 {
48 run_app $@ &
49 }
50
51 function enable_log4j_loglevel_only()
52 {
53 sess_name=$1
54 event_name="$2"
55 loglevel=$3
56 channel_name=$4
57
58 if [ -z $channel_name ]; then
59 # default channel if none specified
60 chan=""
61 else
62 chan="-c $channel_name"
63 fi
64
65 $TESTDIR/../src/bin/lttng/$LTTNG_BIN enable-event --loglevel-only $loglevel "$event_name" $chan -s $sess_name -l >/dev/null 2>&1
66 ok $? "Enable LOG4J event $event_name for session $sess_name with loglevel-only $loglevel"
67 }
68
69 function enable_log4j_filter()
70 {
71 local sess_name="$1"
72 local event_name="$2"
73 local filter="$3"
74
75 $TESTDIR/../src/bin/lttng/$LTTNG_BIN enable-event "$event_name" -s $sess_name -l --filter "$filter" >/dev/null 2>&1
76 ok $? "Enable event $event_name with filter $filter for session $sess_name"
77 }
78
79 function enable_log4j_filter_loglevel_only()
80 {
81 local sess_name="$1"
82 local event_name="$2"
83 local filter="$3"
84 local loglevel="$4"
85
86 $TESTDIR/../src/bin/lttng/$LTTNG_BIN enable-event --loglevel-only $loglevel "$event_name" -s $sess_name -l --filter "$filter" >/dev/null 2>&1
87 ok $? "Enable event $event_name with filter \"$filter\" and loglevel-only $loglevel for session $sess_name"
88 }
89
90 # MUST set TESTDIR before calling those functions
91
92 function test_log4j_before_start ()
93 {
94 local file_sync_after_first=$(mktemp -u)
95 local file_sync_before_last=$(mktemp -u)
96
97 diag "Test LOG4J application BEFORE tracing starts"
98 create_lttng_session_ok $SESSION_NAME $TRACE_PATH
99 enable_log4j_lttng_event $SESSION_NAME $EVENT_NAME
100
101 # Run 5 times with a 1 second delay
102 run_app_background
103
104 start_lttng_tracing_ok $SESSION_NAME
105
106 touch ${file_sync_before_last}
107
108 # Wait for the applications started in background
109 wait
110
111 stop_lttng_tracing_ok $SESSION_NAME
112 destroy_lttng_session_ok $SESSION_NAME
113
114 rm -f ${file_sync_after_first}
115 rm -f ${file_sync_before_last}
116
117 # Validate test. Expecting all events.
118 trace_match_only $EVENT_NAME $NR_ITER $TRACE_PATH
119 if [ $? -ne 0 ]; then
120 return $?
121 fi
122 }
123
124 function test_log4j_after_start ()
125 {
126 diag "Test LOG4J application AFTER tracing starts"
127
128 create_lttng_session_ok $SESSION_NAME $TRACE_PATH
129 enable_log4j_lttng_event $SESSION_NAME $EVENT_NAME
130 start_lttng_tracing_ok $SESSION_NAME
131
132 # Run 5 times with a 1 second delay
133 run_app
134
135 stop_lttng_tracing_ok $SESSION_NAME
136 destroy_lttng_session_ok $SESSION_NAME
137
138 # Validate test. Expecting all events.
139 trace_match_only $EVENT_NAME $NR_ITER $TRACE_PATH
140 if [ $? -ne 0 ]; then
141 return $?
142 fi
143 }
144
145 function test_log4j_loglevel ()
146 {
147 diag "Test LOG4J application with loglevel"
148
149 create_lttng_session_ok $SESSION_NAME $TRACE_PATH
150 enable_log4j_lttng_event_loglevel $SESSION_NAME $EVENT_NAME "LOG4J_INFO"
151 start_lttng_tracing_ok $SESSION_NAME
152
153 # Run 5 times with a 1 second delay
154 run_app
155
156 stop_lttng_tracing_ok $SESSION_NAME
157 destroy_lttng_session_ok $SESSION_NAME
158
159 # Validate test. Expecting all events.
160 trace_match_only $EVENT_NAME $NR_ITER $TRACE_PATH
161 if [ $? -ne 0 ]; then
162 return $?
163 fi
164
165 diag "Test LOG4J applications with lower loglevel"
166
167 create_lttng_session_ok $SESSION_NAME $TRACE_PATH
168 enable_log4j_lttng_event_loglevel $SESSION_NAME $EVENT_NAME "LOG4J_FATAL"
169 start_lttng_tracing_ok $SESSION_NAME
170
171 # Run 5 times with a 1 second delay
172 run_app
173
174 stop_lttng_tracing_ok $SESSION_NAME
175 destroy_lttng_session_ok $SESSION_NAME
176
177 # Validate test. Expecting 0 events.
178 trace_match_only $EVENT_NAME 0 $TRACE_PATH
179 if [ $? -ne 0 ]; then
180 return $?
181 fi
182
183 diag "Test LOG4J applications with higher loglevel"
184
185 create_lttng_session_ok $SESSION_NAME $TRACE_PATH
186 enable_log4j_lttng_event_loglevel $SESSION_NAME $EVENT_NAME "LOG4J_TRACE"
187 start_lttng_tracing_ok $SESSION_NAME
188
189 # Run 5 times with a 1 second delay
190 run_app
191
192 stop_lttng_tracing_ok $SESSION_NAME
193 destroy_lttng_session_ok $SESSION_NAME
194
195 # Validate test. Expecting all events.
196 trace_match_only $EVENT_NAME $NR_ITER $TRACE_PATH
197 return $?
198 }
199
200 function test_log4j_loglevel_multiple ()
201 {
202 diag "Test LOG4J application with multiple loglevel"
203
204 create_lttng_session_ok $SESSION_NAME $TRACE_PATH
205 enable_log4j_lttng_event_loglevel $SESSION_NAME $EVENT_NAME "LOG4J_INFO"
206 enable_log4j_lttng_event_loglevel $SESSION_NAME $EVENT_NAME "LOG4J_DEBUG"
207 start_lttng_tracing_ok $SESSION_NAME
208
209 # Run 5 times with a 1 second delay and fire two TP.
210 run_app 1
211
212 stop_lttng_tracing_ok $SESSION_NAME
213 destroy_lttng_session_ok $SESSION_NAME
214
215 # Validate test. Expecting all events times two.
216 trace_match_only $EVENT_NAME $(($NR_ITER * 2)) $TRACE_PATH
217 if [ $? -ne 0 ]; then
218 return $?
219 fi
220
221 create_lttng_session_ok $SESSION_NAME $TRACE_PATH
222 enable_log4j_lttng_event_loglevel $SESSION_NAME '*' "LOG4J_INFO"
223 enable_log4j_lttng_event_loglevel $SESSION_NAME '*' "LOG4J_DEBUG"
224 start_lttng_tracing_ok $SESSION_NAME
225
226 # Run 5 times with a 1 second delay and fire two TP.
227 run_app 1
228
229 stop_lttng_tracing_ok $SESSION_NAME
230 destroy_lttng_session_ok $SESSION_NAME
231
232 # Validate test. Expecting all events times two.
233 trace_match_only $EVENT_NAME $(($NR_ITER * 2)) $TRACE_PATH
234 if [ $? -ne 0 ]; then
235 return $?
236 fi
237 }
238
239 function test_log4j_multi_session_loglevel()
240 {
241 diag "Test LOG4J with multiple session"
242
243 create_lttng_session_ok $SESSION_NAME-1 $TRACE_PATH/$SESSION_NAME-1
244 enable_log4j_loglevel_only $SESSION_NAME-1 '*' "LOG4J_INFO"
245 start_lttng_tracing_ok $SESSION_NAME-1
246
247 create_lttng_session_ok $SESSION_NAME-2 $TRACE_PATH/$SESSION_NAME-2
248 enable_log4j_loglevel_only $SESSION_NAME-2 '*' "LOG4J_DEBUG"
249 start_lttng_tracing_ok $SESSION_NAME-2
250
251 # Run 5 times with a 1 second delay and fire second TP.
252 run_app 1 1
253
254 stop_lttng_tracing_ok $SESSION_NAME-1
255 stop_lttng_tracing_ok $SESSION_NAME-2
256 destroy_lttng_session_ok $SESSION_NAME-1
257 destroy_lttng_session_ok $SESSION_NAME-2
258
259 # Expecting 6 events being the main event plus the second tp.
260 trace_match_only $EVENT_NAME $(($NR_ITER + 1)) $TRACE_PATH/$SESSION_NAME-1
261 if [ $? -ne 0 ]; then
262 return $?
263 fi
264
265 # Expectin 5 events being the debug TP.
266 trace_match_only $EVENT_NAME $NR_ITER $TRACE_PATH/$SESSION_NAME-2
267 if [ $? -ne 0 ]; then
268 return $?
269 fi
270 }
271
272 function test_log4j_multi_session_disable()
273 {
274 diag "Test LOG4J with multiple session with disabled event"
275
276 create_lttng_session_ok $SESSION_NAME-1 $TRACE_PATH/$SESSION_NAME-1
277 enable_log4j_lttng_event $SESSION_NAME-1 $EVENT_NAME
278 enable_log4j_lttng_event $SESSION_NAME-1 $EVENT_NAME2
279 disable_log4j_lttng_event $SESSION_NAME-1 $EVENT_NAME
280 start_lttng_tracing_ok $SESSION_NAME-1
281
282 create_lttng_session_ok $SESSION_NAME-2 $TRACE_PATH/$SESSION_NAME-2
283 enable_log4j_lttng_event $SESSION_NAME-2 $EVENT_NAME2
284 start_lttng_tracing_ok $SESSION_NAME-2
285
286 # Run 5 times with a 1 second delay and fire second TP.
287 run_app 0 1
288
289 stop_lttng_tracing_ok $SESSION_NAME-1
290 stop_lttng_tracing_ok $SESSION_NAME-2
291 destroy_lttng_session_ok $SESSION_NAME-1
292 destroy_lttng_session_ok $SESSION_NAME-2
293
294 # Validate test. Expecting one event of the second TP.
295 trace_match_only $EVENT_NAME2 1 $TRACE_PATH/$SESSION_NAME-1
296 if [ $? -ne 0 ]; then
297 return $?
298 fi
299
300 # Validate test. Expecting one event of the second TP.
301 trace_match_only $EVENT_NAME2 1 $TRACE_PATH/$SESSION_NAME-2
302 if [ $? -ne 0 ]; then
303 return $?
304 fi
305 }
306
307 function test_log4j_multi_session_disable_wildcard()
308 {
309 diag "Test LOG4J with multiple session with disabled wildcard event"
310
311 create_lttng_session_ok $SESSION_NAME-1 $TRACE_PATH/$SESSION_NAME-1
312 enable_log4j_lttng_event $SESSION_NAME-1 '*'
313
314 create_lttng_session_ok $SESSION_NAME-2 $TRACE_PATH/$SESSION_NAME-2
315 enable_log4j_lttng_event $SESSION_NAME-2 '*'
316
317 disable_log4j_lttng_event $SESSION_NAME-1 '*'
318
319 start_lttng_tracing_ok $SESSION_NAME-1
320 start_lttng_tracing_ok $SESSION_NAME-2
321
322 run_app
323
324 stop_lttng_tracing_ok $SESSION_NAME-1
325 stop_lttng_tracing_ok $SESSION_NAME-2
326 destroy_lttng_session_ok $SESSION_NAME-1
327 destroy_lttng_session_ok $SESSION_NAME-2
328
329 # Validate test. Expecting NO event of the first TP.
330 trace_match_only $EVENT_NAME 0 $TRACE_PATH/$SESSION_NAME-1
331 if [ $? -ne 0 ]; then
332 return $?
333 fi
334
335 # Validate test. Expecting all events of the first TP.
336 trace_match_only $EVENT_NAME $NR_ITER $TRACE_PATH/$SESSION_NAME-2
337 if [ $? -ne 0 ]; then
338 return $?
339 fi
340 }
341
342 function test_log4j_disable_all()
343 {
344 diag "Test LOG4J with multiple session with disabled all event"
345
346 create_lttng_session_ok $SESSION_NAME $TRACE_PATH/$SESSION_NAME
347 enable_log4j_lttng_event $SESSION_NAME '*'
348 enable_log4j_lttng_event $SESSION_NAME $EVENT_NAME
349 enable_log4j_lttng_event $SESSION_NAME $EVENT_NAME2
350
351 disable_log4j_lttng_event $SESSION_NAME '*'
352
353 start_lttng_tracing_ok $SESSION_NAME
354
355 run_app 0 1
356
357 stop_lttng_tracing_ok $SESSION_NAME
358 destroy_lttng_session_ok $SESSION_NAME
359
360 # Validate test. Expecting NO event of the first TP and second TP.
361 trace_match_only $EVENT_NAME 0 $TRACE_PATH/$SESSION_NAME
362 trace_match_only $EVENT_NAME2 0 $TRACE_PATH/$SESSION_NAME
363 if [ $? -ne 0 ]; then
364 return $?
365 fi
366 }
367
368 function test_log4j_multi_session()
369 {
370 diag "Test LOG4J with multiple session"
371
372 create_lttng_session_ok $SESSION_NAME-1 $TRACE_PATH/$SESSION_NAME-1
373 enable_log4j_lttng_event $SESSION_NAME-1 $EVENT_NAME
374 start_lttng_tracing_ok $SESSION_NAME-1
375
376 create_lttng_session_ok $SESSION_NAME-2 $TRACE_PATH/$SESSION_NAME-2
377 enable_log4j_lttng_event $SESSION_NAME-2 $EVENT_NAME2
378 start_lttng_tracing_ok $SESSION_NAME-2
379
380 # Run 5 times with a 1 second delay and fire second TP.
381 run_app 0 1
382
383 stop_lttng_tracing_ok $SESSION_NAME-1
384 stop_lttng_tracing_ok $SESSION_NAME-2
385 destroy_lttng_session_ok $SESSION_NAME-1
386 destroy_lttng_session_ok $SESSION_NAME-2
387
388 # Validate test. Expecting all events of first TP
389 trace_match_only $EVENT_NAME $NR_ITER $TRACE_PATH/$SESSION_NAME-1
390 if [ $? -ne 0 ]; then
391 return $?
392 fi
393
394 # Validate test. Expecting one event of the second TP.
395 trace_match_only $EVENT_NAME2 1 $TRACE_PATH/$SESSION_NAME-2
396 if [ $? -ne 0 ]; then
397 return $?
398 fi
399 }
400
401 function test_log4j_destroy_session()
402 {
403 diag "Test LOG4J two session with destroy"
404
405 create_lttng_session_ok $SESSION_NAME $TRACE_PATH/first-sess
406 enable_log4j_lttng_event $SESSION_NAME $EVENT_NAME
407 start_lttng_tracing_ok $SESSION_NAME
408
409 # Run 5 times with a 1 second delay
410 run_app_background 0 1
411 # Wait for the applications started in background
412 wait
413
414 stop_lttng_tracing_ok $SESSION_NAME
415 destroy_lttng_session_ok $SESSION_NAME
416
417 # Validate test. Expecting at least one event num 1
418 validate_trace $EVENT_NAME $TRACE_PATH/first-sess
419 if [ $? -ne 0 ]; then
420 return $?
421 fi
422
423 create_lttng_session_ok $SESSION_NAME $TRACE_PATH/second-sess
424 enable_log4j_lttng_event $SESSION_NAME $EVENT_NAME2
425 start_lttng_tracing_ok $SESSION_NAME
426
427 # Run 5 times with a 1 second delay
428 run_app_background 0 1
429 # Wait for the applications started in background
430 wait
431
432 stop_lttng_tracing_ok $SESSION_NAME
433 destroy_lttng_session_ok $SESSION_NAME
434
435 # Validate test. Expecting only one event num 2
436 trace_match_only $EVENT_NAME2 1 $TRACE_PATH/second-sess
437 if [ $? -ne 0 ]; then
438 return $?
439 fi
440 }
441
442 function test_log4j_filtering()
443 {
444 diag "Test LOG4J filtering"
445
446 create_lttng_session_ok $SESSION_NAME $TRACE_PATH/$SESSION_NAME
447 # Enable all event with a filter.
448 enable_log4j_filter $SESSION_NAME '*' 'msg == "LOG4J second logger fired"'
449 start_lttng_tracing_ok $SESSION_NAME
450
451 # Run 5 times with a 1 second delay and fire second TP.
452 run_app 0 1
453
454 stop_lttng_tracing_ok $SESSION_NAME
455 destroy_lttng_session_ok $SESSION_NAME
456
457 # Validate test. Expecting one event of the second TP only.
458 trace_match_only $EVENT_NAME2 1 $TRACE_PATH/$SESSION_NAME
459 if [ $? -ne 0 ]; then
460 return $?
461 fi
462
463 create_lttng_session_ok $SESSION_NAME $TRACE_PATH/$SESSION_NAME
464 # Enable first Logger but filter msg payload for the INFO one while
465 # triggering the debug and second TP.
466 enable_log4j_filter $SESSION_NAME $EVENT_NAME 'msg == "LOG4J tp fired!"'
467 start_lttng_tracing_ok $SESSION_NAME
468
469 # Run 5 times with a 1 second delay, fire debug and second TP.
470 run_app 1 1
471
472 stop_lttng_tracing_ok $SESSION_NAME
473 destroy_lttng_session_ok $SESSION_NAME
474
475 # Validate test. Expecting NR_ITER event of the main INFO tp.
476 trace_match_only $EVENT_NAME $NR_ITER $TRACE_PATH/$SESSION_NAME
477 if [ $? -ne 0 ]; then
478 return $?
479 fi
480 }
481
482 function test_log4j_disable()
483 {
484 diag "Test LOG4J disable event"
485
486 create_lttng_session_ok $SESSION_NAME $TRACE_PATH/$SESSION_NAME
487 # Enable all event with a filter.
488 enable_log4j_lttng_event $SESSION_NAME $EVENT_NAME
489 enable_log4j_lttng_event $SESSION_NAME $EVENT_NAME2
490 disable_log4j_lttng_event $SESSION_NAME $EVENT_NAME
491 start_lttng_tracing_ok $SESSION_NAME
492
493 # Run 5 times with a 1 second delay and fire second TP.
494 run_app 0 1
495
496 stop_lttng_tracing_ok $SESSION_NAME
497 destroy_lttng_session_ok $SESSION_NAME
498
499 # Validate test. Expecting one event of the second TP only.
500 trace_match_only $EVENT_NAME2 1 $TRACE_PATH/$SESSION_NAME
501 if [ $? -ne 0 ]; then
502 return $?
503 fi
504 }
505
506 function test_log4j_disable_enable()
507 {
508 diag "Test LOG4J disable event followed by an enable"
509
510 create_lttng_session_ok $SESSION_NAME $TRACE_PATH/$SESSION_NAME
511 # Enable all event with a filter.
512 enable_log4j_lttng_event $SESSION_NAME $EVENT_NAME
513 disable_log4j_lttng_event $SESSION_NAME $EVENT_NAME
514 enable_log4j_lttng_event $SESSION_NAME $EVENT_NAME
515 start_lttng_tracing_ok $SESSION_NAME
516
517 # Run 5 times with a 1 second delay and fire second TP.
518 run_app 0 1
519
520 stop_lttng_tracing_ok $SESSION_NAME
521 destroy_lttng_session_ok $SESSION_NAME
522
523 # Validate test. Expecting NR_ITER event of the main INFO tp.
524 trace_match_only $EVENT_NAME $NR_ITER $TRACE_PATH/$SESSION_NAME
525 if [ $? -ne 0 ]; then
526 return $?
527 fi
528 }
529
530 function test_log4j_filter_loglevel()
531 {
532 local BOGUS_EVENT_NAME="not_a_real_event"
533 local FILTER="int_loglevel > 700 || int_loglevel < 700"
534 local ALL_EVENTS="."
535
536 diag "Test LOG4J a filter with a loglevel"
537
538 create_lttng_session_ok $SESSION_NAME $TRACE_PATH/$SESSION_NAME
539 # Enable an event with a filter and the loglevel-only option.
540 enable_log4j_filter_loglevel_only $SESSION_NAME $BOGUS_EVENT_NAME "$FILTER" LOG4J_INFO
541 disable_log4j_lttng_event $SESSION_NAME $BOGUS_EVENT_NAME
542 enable_log4j_filter_loglevel_only $SESSION_NAME $BOGUS_EVENT_NAME "$FILTER" LOG4J_INFO
543 start_lttng_tracing_ok $SESSION_NAME
544
545 # Run 5 times with a 1 second delay and fire second TP.
546 run_app 0 1
547
548 stop_lttng_tracing_ok $SESSION_NAME
549 destroy_lttng_session_ok $SESSION_NAME
550
551 # Validate test. Expecting no events.
552 trace_match_only $ALL_EVENTS 0 $TRACE_PATH/$SESSION_NAME
553 if [ $? -ne 0 ]; then
554 return $?
555 fi
556 }
557
558 plan_tests $NUM_TESTS
559
560 print_test_banner "$TEST_DESC"
561
562 if [ ! -f "$TESTAPP_PATH.class" ]; then
563 withapp=0
564 else
565 withapp=1
566 fi
567
568 skip $withapp "LOG4J support is needed. Skipping all tests." $NUM_TESTS ||
569 {
570 start_lttng_sessiond
571
572 tests=(
573 test_log4j_multi_session_disable_wildcard
574 test_log4j_multi_session_disable
575 test_log4j_disable
576 test_log4j_disable_enable
577 test_log4j_disable_all
578 test_log4j_filtering
579 test_log4j_multi_session_loglevel
580 test_log4j_destroy_session
581 test_log4j_loglevel
582 test_log4j_loglevel_multiple
583 test_log4j_before_start
584 test_log4j_after_start
585 test_log4j_multi_session
586 test_log4j_filter_loglevel
587 )
588
589 for fct_test in ${tests[@]};
590 do
591 TRACE_PATH=$(mktemp -d)
592
593 ${fct_test}
594 if [ $? -ne 0 ]; then
595 break;
596 fi
597 rm -rf $TRACE_PATH
598 done
599
600 stop_lttng_sessiond
601 }
This page took 0.068559 seconds and 3 git commands to generate.