summaryrefslogtreecommitdiffstats
path: root/tester/run
blob: b116c128137e19b99b572fed824cb204e0debad6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
#!/bin/bash

# This script exits with exit status 0 (success), 1 (failure), 2 (cant finish
# testing).

###  test case return codes  ###

# basic error codes
export DTF_RESULT_SUCCESS=0
export DTF_RESULT_FAILURE=1
export DTF_RESULT_EXP_FAILURE=2

# Inherited from (reserved for) autoconf
export DTF_RESULT_SKIP=77
export DTF_RESULT_HARDFAIL=99

# Should not be used from test-cases
export DTF_RESULT_TS_SKIP=66
export DTF_RESULT_TS_CONFIG=67
export DTF_RESULT_USER_ERROR=68

srcdir=$(cd -P -- "$(dirname -- "$0")" && pwd -P) || exit $DTF_RESULT_TS_CONFIG

###  paths  ###

export DTF_LIBDIR=${DTF_LIBDIR-$srcdir/libdtf}
export DTF_TASKS=${DTF_TASKS-$srcdir/tasks}
export DTF_RESULTDIR=${DTF_RESULTDIR-/var/tmp/dtf}
export DTF_RESULTS_YML=${DTF_RESULTDIR}/result.yml

__result_yml_print()
{
    test "$dtf_option_listonly" -eq 0 && \
        echo 2>/dev/null "$1" >> "$DTF_RESULTS_YML"
}

# when exists per-testsuite config.sh, source it here
test -r "$srcdir/config.sh" && . "$srcdir/config.sh"

###  options  ###

export dtf_option_verbose=0
export dtf_option_force=0
export dtf_option_testids=""
export dtf_option_listonly=0

. "$DTF_LIBDIR/libdtf.sh" || exit $DTF_RESULT_TS_CONFIG

###  function definitions  ###

info()
{
    echo " * $*"
}

die()
{
    echo "$@" >&2
    __result_yml_print "exit_status: 2"
    exit 2
}

dist()
(
    cd "$srcdir/../"
    dir="$(basename "$srcdir")"
    test -t 1 && die "can't put tarball to stdout"

    if test -f "$dir/dist.include"; then
        cmd="tar -ch"
        while read line; do
            cmd+=" $dir/$line"
        done <"$srcdir/dist.include"

        $cmd
    elif declare -f "dtf_cb_dist_tasks" >/dev/null; then
        dtf_cb_dist_tasks
    else
        tar -ch "$dir"
    fi
)

# run TESTDIR
# -----------
#
# Sources the $TESTDIR/runtest.sh and executes the 'run' method from the sourced
# file.  The RUN's exit value is propagated to the caller of RUN_TEST.

run_test()
(
    cd "$1"

    test ! -e ./runtest.sh \
        && echo "runtest.sh not found" \
        && exit $DTF_RESULT_TS_CONFIG

    . ./runtest.sh

    test -z "$DTF_TEST_ID" && exit $DTF_RESULT_TS_CONFIG

    result_dir="$DTF_RESULTDIR/tasks/$DTF_TEST_ID"

    outlog="$result_dir/run.log"

    output_wrapper="cat > $outlog"
    test $dtf_option_verbose = 1 \
        && output_wrapper="tee $outlog"

    if test "$dtf_option_listonly" -eq 1; then
        echo "$DTF_TEST_ID"
        exit $DTF_RESULT_TS_SKIP
    fi

    if test -n "$dtf_option_testids"; then
        [[ "$dtf_option_testids" = *\ $DTF_TEST_ID\ * ]] \
            || exit $DTF_RESULT_TS_SKIP
    fi

    mkdir -p "$result_dir" || exit $DTF_RESULT_TS_CONFIG

    printf "%-40s" "RUN $DTF_TEST_ID ..."

    set -o pipefail

    (
        for i in "${dtf_libfiles[@]}"; do
            . "$i"
        done

        run
        rv=$?

        test -n "$DTF_RESULT_TARBALL" \
            && cp "$DTF_RESULT_TARBALL" \
                  "$DTF_RESULTDIR/tasks/$DTF_TEST_ID/output.tar.gz"
        {   echo "---"
            echo "exit_status: $rv"
            echo "finished: \"$(date)\""
        } >> "$DTF_RESULTDIR/tasks/$DTF_TEST_ID.result"

        exit $rv
    ) 2>&1 | eval "$output_wrapper"
)

prepare_resultdir()
{
    if test -e "$DTF_RESULTDIR"; then
        test $dtf_option_force -eq 0 && die "result dir $DTF_RESULTDIR exits"

        rm -rf "$DTF_RESULTDIR" || die "can not remove $DTF_RESULTDIR"
    fi

    mkdir -p "$DTF_RESULTDIR/tasks" || die "can not create $DTF_RESULTDIR"
}

show_help()
{
cat <<EOHELP >&2
Usage: $0 [OPTIONS]

Script is aimed to perform set of tests in subdirectories.

Options:
  --force                    Overwrite result dir if exists
  --dist                     Prepare self-standing distribution tarball
  --listonly                 List the available test ids
  --testid=TESTID            Perform this test only
  --verbose                  Show complete output from each test
  --help                     Show this help
EOHELP
test -n "$1" && exit "$1"
}

longopts="verbose,help,force,testid:,listonly,dist"
ARGS=$(getopt -o "v" -l "$longopts" -n "getopt" -- "$@")
if [ $? -ne 0 ]; then
   echo "getopt error"
   exit $DTF_RESULT_TS_CONFIG
fi
eval set -- "$ARGS"

while true; do
    case "$1" in
    --verbose)
        dtf_option_verbose=1
        shift
        ;;
    --force)
        dtf_option_force=1
        shift
        ;;
    --listonly)
        dtf_option_listonly=1
        shift
        ;;
    --testid)
        dtf_option_testids="$dtf_option_testids $2 "
        shift 2
        ;;
    --help)
        show_help 0
        ;;
    --dist)
        dist
        exit $?
        ;;
    --)
        shift
        break
        ;;
    esac
done

test $dtf_option_listonly -ne 1 && prepare_resultdir

__result_yml_print "$(date -u "+date: \"%Y%m%d_%H%M%S_UTC\"")"

result=0
while read i; do
    testdir=$(dirname "$i")
    run_test "$testdir"
    case "$?" in
        $DTF_RESULT_SUCCESS)
            echo "[  OK  ]"
            ;;
        $DTF_RESULT_EXP_FAILURE)
            echo "[  EXP_FAIL ]"
            ;;
        $DTF_RESULT_TS_SKIP)
            ;;
        $DTF_RESULT_SKIP)
            echo "[  SKIP  ]"
            ;;
        $DTF_RESULT_HARDFAIL)
            die "hard failure"
            ;;
        *)
            echo "[  FAIL  ]"
            result=1
            ;;
    esac
done <<<"$(find "$DTF_TASKS" -name runtest.sh)"

__result_yml_print "exit_status: $result"

exit $result