summaryrefslogtreecommitdiffstats
path: root/tools/tests_in_valgrind.sh
blob: 0ed84feaf194755a1b95d0f845026125c8016b08 (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
#!/bin/sh

################################################################################
# This script runs all tests in valgrind. Configure and compile kea the way
# you want it to be tested (you should use --with-gtest or --with-gtest-source,
# however, or you get no tests). Then run this script from the top build
# directory.
#
# Note that the test isn't what you would call "production quality" (it is
# expected to be used by the kea developers, not end user) and might break,
# some ways of breaking it are known.
#
# There are two variables that modify it's behaviour.
# * VALGRIND_FLAGS are the flag passed to valgrind. There are some, hopefully
#      reasonable defaults which you can overwrite. Note that the variable is
#      used unmodified inside a sed pattern with # as a modifier, which can
#      easily break it. There was no motivation to fix this.
# * VALGRIND_FILE is the file to store the output into. Default is valgrind.log
################################################################################

# Change directory to Kea's top build directory.
script_path=$(cd "$(dirname "${0}")" && pwd)
cd "${script_path}/.."

# First, make sure the tests are up to date
make

if test $? = 2; then
    printf 'Did you run configure?\n'
    exit 1
fi

# Exit with error if commands exit with non-zero and if undefined variables are
# used.
set -eu

# Some configuration
# TODO Escape for sed, this might break
LOGFILE="${VALGRIND_FILE:-$(pwd)/valgrind.log}"
FLAGS="${VALGRIND_FLAGS:---leak-check=full --track-fds=yes}"
FLAGS="$FLAGS --log-file=$LOGFILE.%p"

FOUND_ANY=false
FAILED=

# Find all the tests (yes, doing it by a name is a nasty hack)
# Since the while runs in a subprocess, we need to get the assignments out, done by the eval
set -x
for testname in $(find . -type f -name '*_unittests' -print | grep -Fv '.libs/'); do
    sed -e 's#exec "#exec valgrind '"$FLAGS"' "#' "$testname" > "$testname.valgrind"
    chmod +x "$testname.valgrind"
    echo "$testname" >>"$LOGFILE"
    echo "===============" >>"$LOGFILE"
    OLDDIR=$(pwd)
    cd "$(dirname "$testname")"
    "./$(basename "$testname").valgrind" >&2 &
    PID="$!"
    cd "$OLDDIR"
    if ! wait "$PID"; then
        printf 'FAILED="%s
%s"' "${FAILED}" "${testname}"
    fi
    NAME="$LOGFILE.$PID"
    rm "$testname.valgrind"
    # Remove the ones from death tests
    if test -e "${NAME}"; then
        grep "==$PID==" "$NAME" >>"$LOGFILE"
        rm "$NAME"
    fi
    FOUND_ANY=true
done

if test -n "$FAILED"; then
    echo "These tests failed:" >&2
    echo "$FAILED" >&2
fi

if ! $FOUND_ANY; then
    echo "No test was found. It is possible you configured without --with-gtest or you run it from wrong directory" >&2
    exit 1
fi