source: mod_gnutls/test/runtests @ e5566dc

debian/masterdebian/stretch-backportsjessie-backports
Last change on this file since e5566dc was 50eab8e, checked in by Thomas Klute <thomas2.klute@…>, 5 years ago

Test suite: Configure max wait time for instance locks from Makefile.am

The increased maximum (30 seconds) should help to avoid false failures
with parallel builds on slow systems.

  • Property mode set to 100755
File size: 6.3 KB
RevLine 
[4b53371]1#!/bin/bash
2
[ae38a49]3# Authors:
4# Daniel Kahn Gillmor <dkg@fifthhorseman.net>
5# Thomas Klute <thomas2.klute@uni-dortmund.de>
[4b53371]6
7set -e
8
9tests="${1##t-}"
10
[9a06bbd]11if [ -n "${TEST_LOCK}" ]; then
12    TEST_LOCK="$(realpath ${TEST_LOCK})"
[50eab8e]13    flock_cmd="flock -w ${TEST_LOCK_WAIT} ${TEST_LOCK}"
[9a06bbd]14fi
15
[4b53371]16BADVARS=0
[af7da2d]17for v in APACHE2 TEST_HOST TEST_IP TEST_PORT TEST_QUERY_DELAY TEST_GAP MSVA_PORT; do
[4b53371]18    if [ ! -v "$v" ]; then
19        printf "You need to set the %s environment variable\n" "$v" >&2
20        BADVARS=1
21    fi
22done
23
24if [ 0 != "$BADVARS" ]; then
25    exit 1
26fi
27
[45ae2ef]28# write script file and line to stderr on error
29function pinpoint_error()
30{
31    echo "${1} failed at line ${2}!" >&2
32}
33trap 'pinpoint_error ${BASH_SOURCE} ${LINENO}' ERR
34
[ae38a49]35function stop_msva()
36{
37    kill_by_pidfile "${msva_pidfile}"
38    unset msva_pidfile
[e3cbda4]39}
40
[a213967]41# Compare expected/actual outputs, filtering out headers from actual
42# output that are expected to change between runs or builds (currently
43# "Date" and "Server"). The headers must be excluded in the expected
44# output.
[54aa269]45#
46# Parameters:
47# $1: path to expected output
48# $2: path to actual output
49# $3: additional options for diff (optional)
[a213967]50function diff_output_filter_headers()
[54aa269]51{
[232fb60]52    local expected="$1"
53    local actual="$2"
[54aa269]54    diff $3 -u "${expected}" <( cat "${actual}" | \
[a213967]55        grep -v -P '^Date:\s.*GMT\s?$' | \
56        grep -v -P '^Server:\sApache'  | \
57        tail -n "$(wc -l < ${expected})" )
[54aa269]58}
59
[232fb60]60# Run a command, storing its PID in the given file
61# Usage: run_with_pidfile PIDFILE COMMAND [ARGS]
62function run_with_pidfile()
63{
64    local pidfile=$1
65    local cmd=$2
66    shift 2
67    echo $BASHPID >${pidfile}
68    exec ${cmd} $*
69}
70
71# Kills the process with the PID contained in a given file, then
72# deletes the file.
73# Usage: kill_by_pidfile PIDFILE
74function kill_by_pidfile()
75{
76    local pidfile="${1}"
77    # In some testcases with expected failure, gnutls-cli sometimes
78    # failed before the subshell in front of the pipe (see gnutls-cli
79    # call below) got so far as to write the PID, much less exec
80    # sleep. So we need to check if there actually is anything to
81    # kill.
82    if [ -n "${pidfile}" ]; then
83        local pid=$(cat "${pidfile}")
84        if [ -n "${pid}" ] && ps -p "${pid}"; then
85            kill "${pid}"
86        fi
87        rm "${pidfile}"
88    fi
89}
90
[4b53371]91function apache_down_err() {
92    printf "FAILURE: %s\n" "$TEST_NAME"
[af7da2d]93    ${APACHE2} -f "${t}/apache.conf" -k stop || true
[4b53371]94    if [ -e output ]; then
[54aa269]95        printf "\ngnutls-cli outputs:\n"
[a213967]96        diff_output_filter_headers "output" "$output" || true
[4b53371]97    fi
[232fb60]98
99    if [ -n "${sleep_pidfile}" ]; then
100        kill_by_pidfile "${sleep_pidfile}"
101    fi
102
[03295a9]103    local errlog="logs/${TEST_NAME}.error.log"
104    if [ -r "${errlog}" ]; then
105        printf "\nApache error logs:\n"
106        tail "${errlog}"
107    fi
[232fb60]108
[302965e]109    if [ -n "${USE_MSVA}" ]; then
110        stop_msva
111    fi
[4b53371]112}
113
114if [ -z "$tests" ] ; then
[33af2b7]115    tests=${srcdir}/tests/*
[4b53371]116else
[33af2b7]117    tests=${srcdir}/tests/"$(printf "%02d" "$tests")"_*
[4b53371]118fi
119
[302965e]120if [ -n "${USE_MSVA}" ]; then
[ae38a49]121    msva_pidfile="$(mktemp mod_gnutls_test-XXXXXX.pid)"
122    GNUPGHOME=msva.gnupghome MSVA_KEYSERVER_POLICY=never run_with_pidfile "${msva_pidfile}" monkeysphere-validation-agent &
[302965e]123    trap stop_msva EXIT
[e3cbda4]124
[302965e]125    printf "TESTING: initial MSVA verification\n"
[7adbcd7]126    # set to 0 if MSVA is up
127    ret=1
128    export MONKEYSPHERE_VALIDATION_AGENT_SOCKET="http://127.0.0.1:$MSVA_PORT"
129    # wait at most TEST_MSVA_MAX_WAIT seconds for MSVA to get ready
130    waited=0.0
131    until [ ${ret} -eq 0 ] \
132              || [ $(echo "${waited} > ${TEST_MSVA_MAX_WAIT}" | bc) -eq 1 ]; do
133        if msva-query-agent https "$(cat client.uid)" x509pem client < client/x509.pem
134        then
135            ret=0
136        else
137            echo "MSVA not ready yet"
138        fi
139        sleep "${TEST_GAP}"
140        waited=$(echo "${waited} + ${TEST_GAP}" | bc)
141    done
142
143    # check if MSVA is up, fail if not
144    if [ ${ret} -eq 0 ]; then
145        printf "\nSUCCESS: initial MSVA verification\n"
146    else
147        printf "\nFAIL: initial MSVA verification\n"
148        exit 1
149    fi
[302965e]150fi
[e3cbda4]151
[9a06bbd]152for t in $tests; do
153    if [ -z "${flock_cmd}" ]; then
154        echo "Warning: no lock file set"
155        sleep "$TEST_GAP"
156    fi
[33af2b7]157    t="$(realpath ${t})"
158    export srcdir="$(realpath ${srcdir})"
[4b53371]159    export TEST_NAME="$(basename "$t")"
[33af2b7]160    output="outputs/${TEST_NAME}.output"
[4b53371]161    rm -f "$output"
[33af2b7]162
163    if [ -e ${t}/fail.* ]; then
[4b53371]164        EXPECTED_FAILURE="$(printf " (expected: %s)" fail.*)"
165    else
166        unset EXPECTED_FAILURE
167    fi
168    printf "TESTING: %s%s\n" "$TEST_NAME" "$EXPECTED_FAILURE"
169    trap apache_down_err EXIT
[302965e]170    if [ -n "${USE_MSVA}" ]; then
[9e35c48]171        MONKEYSPHERE_VALIDATION_AGENT_SOCKET="http://127.0.0.1:$MSVA_PORT" \
172            ${flock_cmd} \
[af7da2d]173            ${APACHE2} -f "${t}/apache.conf" -k start \
[33af2b7]174            || [ -e "${t}/fail.server" ]
[302965e]175    else
[9a06bbd]176        ${flock_cmd} \
[af7da2d]177            ${APACHE2} -f "${t}/apache.conf" -k start \
[33af2b7]178            || [ -e "${t}/fail.server" ]
[302965e]179    fi
[4b53371]180
[232fb60]181    # PID file for sleep command (explanation below)
182    sleep_pidfile="$(mktemp mod_gnutls_test-XXXXXX.pid)"
183
[dda3acf]184    # The sleep call keeps the pipe from the subshell to gnutls-cli
185    # open. Without it gnutls-cli would terminate as soon as sed is
186    # done, and not wait for a response from the server, leading to
187    # failing tests. Sending sleep to the background allows the test
[232fb60]188    # case to proceed instead of waiting for it to return. The sleep
189    # process is stopped after gnutls-cli terminates.
[33af2b7]190    if (sed "s/__HOSTNAME__/${TEST_HOST}/" <${t}/input && \
[232fb60]191        run_with_pidfile "${sleep_pidfile}" sleep "${TEST_QUERY_DELAY}" &) | \
[33af2b7]192        gnutls-cli -p "${TEST_PORT}" $(cat ${t}/gnutls-cli.args) "${TEST_HOST}" \
[dda3acf]193        >"$output";
194    then
[33af2b7]195        if [ -e ${t}/fail* ]; then
[4b53371]196            printf "%s should have failed but succeeded\n" "$(basename "$t")" >&2
197            exit 1
198        fi
199    else
[33af2b7]200        if [ ! -e ${t}/fail* ]; then
[4b53371]201            printf "%s should have succeeded but failed\n" "$(basename "$t")" >&2
202            exit 1
203        fi
204    fi
[dda3acf]205
[232fb60]206    kill_by_pidfile "${sleep_pidfile}"
[49d25a9]207    unset sleep_pidfile
[232fb60]208
[33af2b7]209    if [ -e ${t}/output ] ; then
210        diff_output_filter_headers "${t}/output" "$output" "-q"
[4b53371]211    fi
[302965e]212    if [ -n "${USE_MSVA}" ]; then
213        trap stop_msva EXIT
214    else
215        trap - EXIT
216    fi
[af7da2d]217    ${APACHE2} -f "${t}/apache.conf" -k stop || [ -e ${t}/fail.server ]
[4b53371]218    printf "SUCCESS: %s\n" "$TEST_NAME"
219done
[e3cbda4]220
[302965e]221if [ -n "${USE_MSVA}" ]; then
222    stop_msva
[ae38a49]223    # Without explicitly resetting the trap function, it would be
224    # called again on exit. Of course, we could just not stop MSVA and
225    # let the trap do the work, but I think the code is easier to
226    # understand like this.
227    trap - EXIT
[302965e]228fi
Note: See TracBrowser for help on using the repository browser.