xref: /freebsd/cddl/usr.sbin/dtrace/tests/tools/gentest.sh (revision 6be3386466ab79a84b48429ae66244f21526d3df)
1# $FreeBSD$
2
3usage()
4{
5    cat <<__EOF__ >&2
6Generate ATF test cases from a set of DTrace tests.
7
8usage: sh $(basename $0) [-e <excludes>] <category> [<testfiles>]
9
10  excludes:     A shell script which defines test cases that are to be skipped,
11                or aren't expected to pass.
12  category:     The test category, in the form of <arch>/<feature>. For example,
13                "common/aggs" is the test category for D aggregations.
14  testfiles:    The test files for the tests in the specified category.
15__EOF__
16    exit 1
17}
18
19gentestcase()
20{
21    local mod tcase tfile
22
23    tfile=$1
24    tcase=$2
25    mod=$3
26
27    cat <<__EOF__
28atf_test_case $tcase
29${tcase}_head()
30{
31    atf_set 'descr' 'DTrace test ${CATEGORY}/${tfile}'
32}
33${tcase}_body()
34{
35    $mod
36    atf_check -s exit:0 -o empty -e empty \\
37        "\$(atf_get_srcdir)/../../dtest" "\$(atf_get_srcdir)/${tfile}"
38}
39__EOF__
40}
41
42gentestcases()
43{
44    local mod tcase tfile tfiles
45
46    eval tfiles=\$$1
47    mod=$2
48
49    for tfile in ${tfiles}; do
50        case $tfile in
51        drp.*.d|err.*.d|tst.*.d|*.ksh)
52            # Test names need to be mangled for ATF.
53            tcase=$(echo "$tfile" | tr '.-' '_')
54            gentestcase "$tfile" "$tcase" "$mod"
55            TCASES="$TCASES $tcase"
56            ;;
57        esac
58    done
59}
60
61set -e
62
63#
64# Parse arguments.
65#
66case $1 in
67-e)
68    shift; EXCLUDES=$1; shift
69    ;;
70esac
71
72CATEGORY=$1
73shift
74if ! expr "$CATEGORY" : '[^/]*/[^/]*' >/dev/null 2>&1; then
75    usage
76fi
77FEATURE=$(basename ${CATEGORY})
78ARCH=$(dirname ${CATEGORY})
79
80#
81# Remove skipped tests and expected failures from the main test list.
82#
83. $EXCLUDES
84EXFAILS=$(echo -e "$EXFAIL" | grep "^${CATEGORY}/" | xargs basename -a)
85SKIPS=$(echo -e "$SKIP" | grep "^${CATEGORY}/" | xargs basename -a)
86
87FILELIST=$(mktemp)
88trap 'rm -f $FILELIST' EXIT
89
90echo "$@" | tr ' ' '\n' | xargs basename -a | sort > ${FILELIST}
91TFILES=$(printf '%s\n%s' "$EXFAILS" "$SKIPS" | sort | comm -13 /dev/stdin $FILELIST)
92
93#
94# Generate test cases.
95#
96gentestcases SKIPS "atf_skip \"test may hang or cause system instability\""
97gentestcases EXFAILS "atf_expect_fail \"test is known to fail\""
98gentestcases TFILES
99
100#
101# Generate the test init function.
102#
103cat <<__EOF__
104atf_init_test_cases()
105{
106$(for tcase in ${TCASES}; do echo "    atf_add_test_case $tcase"; done)
107}
108__EOF__
109
110rm -f $FILELIST
111