xref: /freebsd/cddl/usr.sbin/dtrace/tests/tools/dtest.sh (revision 13464e4a44fc58490a03bb8bfc7e3c972e9c30b2)
1# $FreeBSD$
2
3usage()
4{
5    cat >&2 <<__EOF__
6A harness for test cases in the DTrace test suite.
7
8usage: $(basename $0) <testfile>
9__EOF__
10    exit 1
11}
12
13gettag()
14{
15    local tag
16
17    tag=$(basename $1)
18    tag=${tag#*.}
19    tag=${tag%%[a-z.]*}
20    echo $tag
21}
22
23runtest()
24{
25    local dflags exe exstatus pid retval status
26
27    exstatus=0
28    retval=0
29
30    case $TFILE in
31    drp.DTRACEDROP_*.d|err.*.d|tst.*.d)
32        case $TFILE in
33        drp.DTRACEDROP_*.d)
34            dflags="-x droptags"
35            tag=$(gettag "$TFILE")
36            ;;
37        err.D_*.d)
38            exstatus=1
39            dflags="-x errtags"
40            tag=$(gettag "$TFILE")
41            ;;
42        err.*.d)
43            exstatus=1
44            ;;
45        esac
46
47        exe=${TFILE%.*}.exe
48        if [ -f "$exe" -a -x "$exe" ]; then
49            ./$exe &
50            pid=$!
51            dflags="$dflags ${pid}"
52        fi
53
54        dtrace -C -s "${TFILE}" $dflags >$STDOUT 2>$STDERR
55        status=$?
56
57        if [ $status -ne $exstatus ]; then
58            ERRMSG="dtrace exited with status ${status}, expected ${exstatus}"
59            retval=1
60        elif [ -n "${tag}" ] && ! grep -Fq " [${tag}] " ${STDERR}; then
61            ERRMSG="dtrace's error output did not contain expected tag ${tag}"
62            retval=1
63        fi
64
65        if [ -n "$pid" ]; then
66            kill -0 $pid >/dev/null 2>&1 && kill -9 $pid >/dev/null 2>&1
67            wait
68        fi
69        ;;
70    err.*.ksh|tst.*.ksh)
71        expr "$TFILE" : 'err.*' >/dev/null && exstatus=1
72
73        tst=$TFILE ksh "$TFILE" /usr/sbin/dtrace >$STDOUT 2>$STDERR
74        status=$?
75
76        if [ $status -ne $exstatus ]; then
77            ERRMSG="script exited with status ${status}, expected ${exstatus}"
78            retval=1
79        fi
80        ;;
81    *)
82        ERRMSG="unexpected test file name $TFILE"
83        retval=1
84        ;;
85    esac
86
87    return $retval
88}
89
90[ $# -eq 1 ] || usage
91
92readonly STDERR=$(mktemp)
93readonly STDOUT=$(mktemp)
94readonly TFILE=$(basename $1)
95readonly EXOUT=${TFILE}.out
96
97kldstat -q -m dtrace_test || kldload dtrace_test
98cd $(dirname $1)
99runtest
100RESULT=$?
101
102if [ $RESULT -eq 0 -a -f $EXOUT -a -r $EXOUT ] && \
103   ! cmp $STDOUT $EXOUT >/dev/null 2>&1; then
104    ERRMSG="test output mismatch"
105    RESULT=1
106fi
107
108if [ $RESULT -ne 0 ]; then
109    echo "test $TFILE failed: $ERRMSG" >&2
110    if [ $(stat -f '%z' $STDOUT) -gt 0 ]; then
111        cat >&2 <<__EOF__
112test stdout:
113--
114$(cat $STDOUT)
115--
116__EOF__
117    fi
118    if [ $(stat -f '%z' $STDERR) -gt 0 ]; then
119        cat >&2 <<__EOF__
120test stderr:
121--
122$(cat $STDERR)
123--
124__EOF__
125    fi
126fi
127
128rm -f $STDERR $STDOUT
129exit $RESULT
130