diff options
author | Omar Polo <op@omarpolo.com> | 2021-10-04 09:04:33 +0000 |
---|---|---|
committer | Omar Polo <op@omarpolo.com> | 2021-10-04 09:04:33 +0000 |
commit | 176179b2a9e99af595892c396a219c2bc11fb6c9 (patch) | |
tree | 31a9683f9014418846896b50011a3216ecb3a8f8 /regress/lib.sh | |
parent | c28994868efd1da6259f805984cc93135bd74a3a (diff) |
rework the regression suite
The tests are still there, the suite is equivalent to the old one, but
this one is better structured.
The biggest annoyance I had with the old one was that it wasn't
straightforward to test only a specific set of tests. It's still
impossible, but it's way easier to do it now.
This extract all the tests to their own functions. It's overall
better in all possible regards.
Diffstat (limited to 'regress/lib.sh')
-rw-r--r-- | regress/lib.sh | 185 |
1 files changed, 185 insertions, 0 deletions
diff --git a/regress/lib.sh b/regress/lib.sh new file mode 100644 index 0000000..afee6f8 --- /dev/null +++ b/regress/lib.sh @@ -0,0 +1,185 @@ +failed= + +gg="./gg" +gmid="./../gmid" +current_test= + +run_test() { + ggflags= + port=10965 + config_common=" +ipv6 off +port $port +" + hdr= + body= + dont_check=no + + current_test=$1 + rm -f reg.conf + + if ! $1; then + echo "$1 failed" + failed="$failed $1" + else + echo "$1 passed" + fi + + if [ "$dont_check" != 'no' ]; then + return + fi + + if ! check; then + echo "gmid crashed?" + failed="$failed $1" + fi +} + +# usage: gen_config <global config> <server config> +# generates a configuration file reg.conf +gen_config() { + cat <<EOF > reg.conf +$config_common +$1 +server "localhost" { + cert "$PWD/cert.pem" + key "$PWD/key.pem" + root "$PWD/testdata" + $2 +} +EOF + if ! checkconf; then + echo "failed to parse the config" >&2 + return 1 + fi +} + +checkconf() { + $gmid -n -c reg.conf >/dev/null +} + +# usage: setup_simple_test <global config> <server config> +# generates a configuration file with `gen_config', validates it and +# launches the daemon +setup_simple_test() { + gen_config "$1" "$2" + run +} + +# usage: get <path> +# return the body of the request on stdout +get() { + $gg -T30 -b $ggflags "gemini://localhost:10965/$1" +} + +# usage: head <path> +# return the meta response line on stdout +head() { + $gg -T30 -h $ggflags "gemini://localhost:10965/$1" +} + +# usage: raw <path> +# return both header and body +raw() { + $gg -T30 $ggflags "gemini://localhost:10965/$1" +} + +# usage: fetch <path> +# fetches the header and the body. They're returned in $hdr and +# $body. +fetch() { + if ! hdr="$(head $1)" || ! body="$(get $1)"; then + return 1 + fi +} + +# usage: fetch_hdr <path> +# fetches the header into $hdr +fetch_hdr() { + hdr="$(head $1)" + body="" +} + +# usage: check_reply header body +# checks that $hdr and $body are equal to the given strings +check_reply() { + if [ "$hdr" != "$1" ]; then + echo "Header mismatch" >&2 + echo "wants : $1" >&2 + echo "got : $hdr" >&2 + return 1 + fi + + if [ "$body" != "$2" ]; then + echo "Body mismatch" >&2 + echo "wants : $1" >&2 + echo "got : $body" >&2 + return 1 + fi +} + +run() { + if check; then + kill -HUP "$(cat gmid.pid)" + sleep 1 + return + fi + + $gmid -P gmid.pid -c reg.conf + + # give gmid time to bind the port, otherwise we end up + # executing gg when gmid isn't ready yet. + sleep 1 +} + +check() { + if [ ! -f gmid.pid ]; then + return 1 + fi + + pid="$(cat gmid.pid || true)" + if [ "$pid" == "" ]; then + return 1 + fi + + # remember: we're running under ``set -e'' + if ps $pid >/dev/null; then + return 0 + fi + + return 1 +} + +# usage: sha in out +# writes the sha256 of `in' to `out' +sha() { + if which sha256 >/dev/null 2>&1; then + sha256 < "$1" > "$2" + return $? + fi + + if which sha256sum >/dev/null 2>&1; then + sha256sum "$1" | awk '{print $1}' > "$2" + return $? + fi + + echo "No sha binary found" >&2 + exit 1 +} + +count() { + wc -l | xargs +} + +quit() { + pid="$(cat gmid.pid || true)" + if [ "$pid" != "" ]; then + kill $pid || true + wait || true + fi +} + +onexit() { + rm -f bigfile bigfile.sha + quit +} |