source: mod_gnutls/test/runtests @ a733a90

debian/masterdebian/stretch-backportsjessie-backportsupstream
Last change on this file since a733a90 was 50eab8e, checked in by Thomas Klute <thomas2.klute@…>, 4 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
Line 
1#!/bin/bash
2
3# Authors:
4# Daniel Kahn Gillmor <dkg@fifthhorseman.net>
5# Thomas Klute <thomas2.klute@uni-dortmund.de>
6
7set -e
8
9tests="${1##t-}"
10
11if [ -n "${TEST_LOCK}" ]; then
12    TEST_LOCK="$(realpath ${TEST_LOCK})"
13    flock_cmd="flock -w ${TEST_LOCK_WAIT} ${TEST_LOCK}"
14fi
15
16BADVARS=0
17for v in APACHE2 TEST_HOST TEST_IP TEST_PORT TEST_QUERY_DELAY TEST_GAP MSVA_PORT; do
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
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
35function stop_msva()
36{
37    kill_by_pidfile "${msva_pidfile}"
38    unset msva_pidfile
39}
40
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.
45#
46# Parameters:
47# $1: path to expected output
48# $2: path to actual output
49# $3: additional options for diff (optional)
50function diff_output_filter_headers()
51{
52    local expected="$1"
53    local actual="$2"
54    diff $3 -u "${expected}" <( cat "${actual}" | \
55        grep -v -P '^Date:\s.*GMT\s?$' | \
56        grep -v -P '^Server:\sApache'  | \
57        tail -n "$(wc -l < ${expected})" )
58}
59
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
91function apache_down_err() {
92    printf "FAILURE: %s\n" "$TEST_NAME"
93    ${APACHE2} -f "${t}/apache.conf" -k stop || true
94    if [ -e output ]; then
95        printf "\ngnutls-cli outputs:\n"
96        diff_output_filter_headers "output" "$output" || true
97    fi
98
99    if [ -n "${sleep_pidfile}" ]; then
100        kill_by_pidfile "${sleep_pidfile}"
101    fi
102
103    local errlog="logs/${TEST_NAME}.error.log"
104    if [ -r "${errlog}" ]; then
105        printf "\nApache error logs:\n"
106        tail "${errlog}"
107    fi
108
109    if [ -n "${USE_MSVA}" ]; then
110        stop_msva
111    fi
112}
113
114if [ -z "$tests" ] ; then
115    tests=${srcdir}/tests/*
116else
117    tests=${srcdir}/tests/"$(printf "%02d" "$tests")"_*
118fi
119
120if [ -n "${USE_MSVA}" ]; then
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 &
123    trap stop_msva EXIT
124
125    printf "TESTING: initial MSVA verification\n"
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
150fi
151
152for t in $tests; do
153    if [ -z "${flock_cmd}" ]; then
154        echo "Warning: no lock file set"
155        sleep "$TEST_GAP"
156    fi
157    t="$(realpath ${t})"
158    export srcdir="$(realpath ${srcdir})"
159    export TEST_NAME="$(basename "$t")"
160    output="outputs/${TEST_NAME}.output"
161    rm -f "$output"
162
163    if [ -e ${t}/fail.* ]; then
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
170    if [ -n "${USE_MSVA}" ]; then
171        MONKEYSPHERE_VALIDATION_AGENT_SOCKET="http://127.0.0.1:$MSVA_PORT" \
172            ${flock_cmd} \
173            ${APACHE2} -f "${t}/apache.conf" -k start \
174            || [ -e "${t}/fail.server" ]
175    else
176        ${flock_cmd} \
177            ${APACHE2} -f "${t}/apache.conf" -k start \
178            || [ -e "${t}/fail.server" ]
179    fi
180
181    # PID file for sleep command (explanation below)
182    sleep_pidfile="$(mktemp mod_gnutls_test-XXXXXX.pid)"
183
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
188    # case to proceed instead of waiting for it to return. The sleep
189    # process is stopped after gnutls-cli terminates.
190    if (sed "s/__HOSTNAME__/${TEST_HOST}/" <${t}/input && \
191        run_with_pidfile "${sleep_pidfile}" sleep "${TEST_QUERY_DELAY}" &) | \
192        gnutls-cli -p "${TEST_PORT}" $(cat ${t}/gnutls-cli.args) "${TEST_HOST}" \
193        >"$output";
194    then
195        if [ -e ${t}/fail* ]; then
196            printf "%s should have failed but succeeded\n" "$(basename "$t")" >&2
197            exit 1
198        fi
199    else
200        if [ ! -e ${t}/fail* ]; then
201            printf "%s should have succeeded but failed\n" "$(basename "$t")" >&2
202            exit 1
203        fi
204    fi
205
206    kill_by_pidfile "${sleep_pidfile}"
207    unset sleep_pidfile
208
209    if [ -e ${t}/output ] ; then
210        diff_output_filter_headers "${t}/output" "$output" "-q"
211    fi
212    if [ -n "${USE_MSVA}" ]; then
213        trap stop_msva EXIT
214    else
215        trap - EXIT
216    fi
217    ${APACHE2} -f "${t}/apache.conf" -k stop || [ -e ${t}/fail.server ]
218    printf "SUCCESS: %s\n" "$TEST_NAME"
219done
220
221if [ -n "${USE_MSVA}" ]; then
222    stop_msva
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
228fi
Note: See TracBrowser for help on using the repository browser.