source: mod_gnutls/test/runtests @ 7adbcd7

debian/masterdebian/stretch-backportsjessie-backportsupstream
Last change on this file since 7adbcd7 was 7adbcd7, checked in by Thomas Klute <thomas2.klute@…>, 4 years ago

Test suite: Wait loop for MSVA startup

Until now, there was a fixed wait time for MSVA startup. This caused one
of two problems: Either the wait time was short, risiking a test failure
due to MSVA not being ready in time, or it was long, making test runs
slow. Waiting just a short time but retrying for a longer time if
necessary helps.

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