pec: Fix multiple event test
PATCH v2 1/1 pec: Fix multiple event test
From: Joerg Vehlow <[email protected]>
The test case for NUM_EVENTS != 1 was not correct at all. Now the test looks for each event recorded by the generator in the correct order in the listener's log.
Additionally the test is parameterized, to the the number of generated events and the default is now set to 10.
--- testcases/kernel/connectors/pec/cn_pec.sh | 100 +++++++++++++++--- .../kernel/connectors/pec/event_generator.c | 54 +++++----- 2 files changed, 116 insertions(+), 38 deletions(-)
testcases/kernel/connectors/pec/cn_pec.sh +88 -12
@@ -9,16 +9,52 @@ # Process event connector is a netlink connector that reports process events # to userspace. It sends events such as fork, exec, id change and exit. +TST_OPTS="n:" TST_SETUP=setup TST_TESTFUNC=test +TST_PARSE_ARGS=parse_args +TST_USAGE=usage TST_NEEDS_ROOT=1 TST_NEEDS_TMPDIR=1 TST_TEST_DATA="fork exec exit uid gid" -NUM_EVENTS=1 - . tst_test.sh +num_events=10 + +usage() +{ + cat << EOF +usage: $0 [-n <nevents>] + +OPTIONS +-n The number of evetns to generate per test (default 10) +EOF +} + +parse_args() +{ + case $1 in + n) num_events=$2;; + esac +} + +# Find a free file handle +free_fd() +{ + local fd + + for fd in $(seq 200); do + # Sapwn a new sh, because redirecting to a non existing file handle + # will trigger a syntax error. + /bin/sh -c ": 2>/dev/null >&$fd || : 2>/dev/null <&$fd" 2>/dev/null + if [ $? -eq 2 ]; then + echo $fd + return + fi + done +} + setup() { if ! grep -q cn_proc /proc/net/connector; then
@@ -31,7 +67,9 @@ setup() test() { local event=$2 - local expected_events lis_rc pid + local expected_events lis_rc pid fd_act failed act_nevents exp act + + tst_res TINFO "Testing $2 event (nevents=$num_events)" pec_listener >lis_$event.log 2>lis_$event.err & pid=$!
@@ -39,28 +77,66 @@ test() tst_sleep 100ms # generator must be in PATH - ROD event_generator -n $NUM_EVENTS -e $event \>gen_$event.log 2\>gen_$event.err + ROD event_generator -n $num_events -e $event \>gen_$event.log 2\>gen_$event.err - # sleep until pec_listener has seen and handled all of the generated events - tst_sleep 100ms kill -s INT $pid 2> /dev/null wait $pid lis_rc=$? if [ ! -s gen_$event.log ]; then - tst_brk TBROK "failed to generate process events" + tst_brk TBROK "failed to generate process events: $(cat gen_$event.err)" fi if [ $lis_rc -ne 0 ]; then tst_brk TBROK "failed to execute the listener: $(cat lis_$event.err)" fi - expected_events="$(cat gen_$event.log)" - if grep -q "$expected_events" lis_$event.log; then - tst_res TPASS "$event detected by listener" + # The listener writes the same messages as the generator, but it can + # also see more events (e.g. for testing exit, a fork is generated). + # So: The events generated by the generator have to be in the same order + # as the events printed by the listener, but my interleaved with other + # messages. To correctly compare them, we have to open both logs + # and iterate over both of them at the same time, skipping messages + # in the listener log, that are not of interest. + # Because some messages may be multiple times in the listener log, + # we have to open it only once! + # This however does not check, if the listener sees more messages, + # than expected. + + fd_act=$(free_fd) + [ -z "$fd_act" ] && tst_brk TBROK "No free filehandle found" + eval "exec ${fd_act}<lis_$event.log" + + failed=0 + act_nevents=0 + while read -r exp; do + local found=0 + act_nevents=$((act_nevents + 1)) + while read -r act; do + if [ "$exp" = "$act" ]; then + found=1 + break + fi + done <&${fd_act} + if [ $found -ne 1 ]; then + failed=1 + tst_res TFAIL "Event was not detected by the event listener: $exp" + break + fi + done <gen_$event.log + + eval "exec ${fd_act}<&-" + + if [ $failed -eq 0 ]; then + if [ $act_nevents -ne $num_events ]; then + tst_res TBROK "Expected $num_events, but $act_nevents generated" + else + tst_res TPASS "All events detected" + fi else - tst_res TFAIL "$event not detected by listener" - fi + # TFAIL message is already printed in the loop above + cat lis_$event.log + fi } tst_run
testcases/kernel/connectors/pec/event_generator.c +28 -26
90 lines not on this page · raw message
Signature · 1 line
2.25.1
PATCH v2 1/1 pec: Fix multiple event test
Hi Joerg,
...
testcases/kernel/connectors/pec/cn_pec.sh line 50
Quoted 9 lines
testcases/kernel/connectors/pec/cn_pec.sh +9
-0+# Find a free file handle +free_fd() +{ + local fd + + for fd in $(seq 200); do + # Sapwn a new sh, because redirecting to a non existing file handle + # will trigger a syntax error. + /bin/sh -c ": 2>/dev/null >&$fd || : 2>/dev/null <&$fd" 2>/dev/null
Probably better to use sh -c "..."
testcases/kernel/connectors/pec/cn_pec.sh line 55
Quoted 5 lines
testcases/kernel/connectors/pec/cn_pec.sh +5
-0+ if [ $? -eq 2 ]; then + echo $fd + return + fi + done
maybe I do something wrong, but this version fails for me (and I'm still testing it only on bash):
cn_pec 1 TINFO: timeout per run is 0h 5m 0s cn_pec 1 TINFO: Test process events connector cn_pec 1 TINFO: Testing fork event (nevents=10) cn_pec 1 TBROK: No free filehandle found
I guess there is something wrong free_fd().
Kind regards,
Petr
PATCH v2 1/1 pec: Fix multiple event test
Hi,
Quoted 13 lines · Petr Vorel
Hi Joerg,
...
testcases/kernel/connectors/pec/cn_pec.sh line 50
Quoted 9 lines
testcases/kernel/connectors/pec/cn_pec.sh +9
-0+# Find a free file handle +free_fd() +{ + local fd + + for fd in $(seq 200); do + # Sapwn a new sh, because redirecting to a non existing file handle + # will trigger a syntax error. + /bin/sh -c ": 2>/dev/null >&$fd || : 2>/dev/null <&$fd" 2>/dev/nullProbably better to use sh -c "..."
I used? /bin/sh, because that is the same interpreter specified in the shebang. Just using sh could theoretically fail or spawn a different shell. Maybe $SHELL would be better, but I don't know how widely this is supported...
Quoted 9 lines
testcases/kernel/connectors/pec/cn_pec.sh line 55
Quoted 6 lines
testcases/kernel/connectors/pec/cn_pec.sh +5
-0+ if [ $? -eq 2 ]; then + echo $fd + return + fi + donemaybe I do something wrong, but this version fails for me (and I'm still testing it only on bash):
Quoted 6 lines
cn_pec1 TINFO: timeout per run is 0h 5m 0scn_pec1 TINFO: Test process events connectorcn_pec1 TINFO: Testing fork event (nevents=10)cn_pec1 TBROK: No free filehandle foundI guess there is something wrong
free_fd().
Damn shells... I just realized /bin/sh on my system was dash's sh emulation and it does not emulate sh the same way bash does... The return value in bash is 1, not two... Can you please check the patch with [ $? -ne 0 ]? That should work for all shells.
J?rg
PATCH v2 1/1 pec: Fix multiple event test
Hi Joerg,
Quoted 16 lines
Hi,
Quoted 13 lines · Petr Vorel
Hi Joerg,
...
testcases/kernel/connectors/pec/cn_pec.sh line 50
Quoted 9 lines
testcases/kernel/connectors/pec/cn_pec.sh +9
-0+# Find a free file handle +free_fd() +{ + local fd + + for fd in $(seq 200); do + # Sapwn a new sh, because redirecting to a non existing file handle + # will trigger a syntax error. + /bin/sh -c ": 2>/dev/null >&$fd || : 2>/dev/null <&$fd" 2>/dev/nullProbably better to use sh -c "..."
Quoted 4 lines
I used?
/bin/sh, because that is the same interpreter specified in the shebang. Just using sh could theoretically fail or spawn a different shell. Maybe $SHELL would be better, but I don't know how widely this is supported...
I meant that 'sh' is on any linux system /bin/sh (or a symlink to /usr/bin/sh, i.e. a default shell. No need to specify it with full path. We happily use elsewhere just sh, not /bin/sh.
Quoted 13 lines
Quoted 12 lines
testcases/kernel/connectors/pec/cn_pec.sh line 55
Quoted 5 lines
testcases/kernel/connectors/pec/cn_pec.sh +5
-0+ if [ $? -eq 2 ]; then + echo $fd + return + fi + donemaybe I do something wrong, but this version fails for me (and I'm still testing it only on bash):
cn_pec1 TINFO: timeout per run is 0h 5m 0scn_pec1 TINFO: Test process events connectorcn_pec1 TINFO: Testing fork event (nevents=10)cn_pec1 TBROK: No free filehandle found
Quoted 4 lines
Quoted 1 line
I guess there is something wrong
free_fd().Damn shells... I just realized
/bin/shon my system was dash's sh emulation and it does not emulate sh the same way bash does...
Yes, unfortunately it's required to test shell tests at least on bash and dash.
I also try to test on busybox sh (ash implementation), which commonly used on embedded (it mostly works if dash works). Some time ago I tested even on android shell, but I give up (expecting anybody playing with LTP on android should install busybox).
Quoted 2 lines
The return value in bash is 1, not two... Can you please check the patch with [ $? -ne 0 ]? That should work for all shells.
Yes it works. I'll have a look at the rest of the patch today.
Kind regards,
Petr
Quoted 1 line
J?rg
PATCH v2 1/1 pec: Fix multiple event test
In reply to Joerg VehlowHi Joerg,
thanks a lot, merged!
Kind regards,
Petr