source: mod_gnutls/test/runtests @ 6ce02e2

asynciodebian/masterdebian/stretch-backportsjessie-backportsproxy-ticketupstream
Last change on this file since 6ce02e2 was 7adbcd7, checked in by Thomas Klute <thomas2.klute@…>, 6 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
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})"
13    flock_cmd="flock -w 10 ${TEST_LOCK}"
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
[ae38a49]28function stop_msva()
29{
30    kill_by_pidfile "${msva_pidfile}"
31    unset msva_pidfile
[e3cbda4]32}
33
[a213967]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.
[54aa269]38#
39# Parameters:
40# $1: path to expected output
41# $2: path to actual output
42# $3: additional options for diff (optional)
[a213967]43function diff_output_filter_headers()
[54aa269]44{
[232fb60]45    local expected="$1"
46    local actual="$2"
[54aa269]47    diff $3 -u "${expected}" <( cat "${actual}" | \
[a213967]48        grep -v -P '^Date:\s.*GMT\s?$' | \
49        grep -v -P '^Server:\sApache'  | \
50        tail -n "$(wc -l < ${expected})" )
[54aa269]51}
52
[232fb60]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
[4b53371]84function apache_down_err() {
85    printf "FAILURE: %s\n" "$TEST_NAME"
[af7da2d]86    ${APACHE2} -f "${t}/apache.conf" -k stop || true
[4b53371]87    if [ -e output ]; then
[54aa269]88        printf "\ngnutls-cli outputs:\n"
[a213967]89        diff_output_filter_headers "output" "$output" || true
[4b53371]90    fi
[232fb60]91
92    if [ -n "${sleep_pidfile}" ]; then
93        kill_by_pidfile "${sleep_pidfile}"
94    fi
95
[03295a9]96    local errlog="logs/${TEST_NAME}.error.log"
97    if [ -r "${errlog}" ]; then
98        printf "\nApache error logs:\n"
99        tail "${errlog}"
100    fi
[232fb60]101
[302965e]102    if [ -n "${USE_MSVA}" ]; then
103        stop_msva
104    fi
[4b53371]105}
106
107if [ -z "$tests" ] ; then
[33af2b7]108    tests=${srcdir}/tests/*
[4b53371]109else
[33af2b7]110    tests=${srcdir}/tests/"$(printf "%02d" "$tests")"_*
[4b53371]111fi
112
[302965e]113if [ -n "${USE_MSVA}" ]; then
[ae38a49]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 &
[302965e]116    trap stop_msva EXIT
[e3cbda4]117
[302965e]118    printf "TESTING: initial MSVA verification\n"
[7adbcd7]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
[302965e]143fi
[e3cbda4]144
[9a06bbd]145for t in $tests; do
146    if [ -z "${flock_cmd}" ]; then
147        echo "Warning: no lock file set"
148        sleep "$TEST_GAP"
149    fi
[33af2b7]150    t="$(realpath ${t})"
151    export srcdir="$(realpath ${srcdir})"
[4b53371]152    export TEST_NAME="$(basename "$t")"
[33af2b7]153    output="outputs/${TEST_NAME}.output"
[4b53371]154    rm -f "$output"
[33af2b7]155
156    if [ -e ${t}/fail.* ]; then
[4b53371]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
[302965e]163    if [ -n "${USE_MSVA}" ]; then
[9e35c48]164        MONKEYSPHERE_VALIDATION_AGENT_SOCKET="http://127.0.0.1:$MSVA_PORT" \
165            ${flock_cmd} \
[af7da2d]166            ${APACHE2} -f "${t}/apache.conf" -k start \
[33af2b7]167            || [ -e "${t}/fail.server" ]
[302965e]168    else
[9a06bbd]169        ${flock_cmd} \
[af7da2d]170            ${APACHE2} -f "${t}/apache.conf" -k start \
[33af2b7]171            || [ -e "${t}/fail.server" ]
[302965e]172    fi
[4b53371]173
[232fb60]174    # PID file for sleep command (explanation below)
175    sleep_pidfile="$(mktemp mod_gnutls_test-XXXXXX.pid)"
176
[dda3acf]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
[232fb60]181    # case to proceed instead of waiting for it to return. The sleep
182    # process is stopped after gnutls-cli terminates.
[33af2b7]183    if (sed "s/__HOSTNAME__/${TEST_HOST}/" <${t}/input && \
[232fb60]184        run_with_pidfile "${sleep_pidfile}" sleep "${TEST_QUERY_DELAY}" &) | \
[33af2b7]185        gnutls-cli -p "${TEST_PORT}" $(cat ${t}/gnutls-cli.args) "${TEST_HOST}" \
[dda3acf]186        >"$output";
187    then
[33af2b7]188        if [ -e ${t}/fail* ]; then
[4b53371]189            printf "%s should have failed but succeeded\n" "$(basename "$t")" >&2
190            exit 1
191        fi
192    else
[33af2b7]193        if [ ! -e ${t}/fail* ]; then
[4b53371]194            printf "%s should have succeeded but failed\n" "$(basename "$t")" >&2
195            exit 1
196        fi
197    fi
[dda3acf]198
[232fb60]199    kill_by_pidfile "${sleep_pidfile}"
[49d25a9]200    unset sleep_pidfile
[232fb60]201
[33af2b7]202    if [ -e ${t}/output ] ; then
203        diff_output_filter_headers "${t}/output" "$output" "-q"
[4b53371]204    fi
[302965e]205    if [ -n "${USE_MSVA}" ]; then
206        trap stop_msva EXIT
207    else
208        trap - EXIT
209    fi
[af7da2d]210    ${APACHE2} -f "${t}/apache.conf" -k stop || [ -e ${t}/fail.server ]
[4b53371]211    printf "SUCCESS: %s\n" "$TEST_NAME"
212done
[e3cbda4]213
[302965e]214if [ -n "${USE_MSVA}" ]; then
215    stop_msva
[ae38a49]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
[302965e]221fi
Note: See TracBrowser for help on using the repository browser.