empserver/tests/test-common.sh
Markus Armbruster d0d692e415 tests: New helper function feed_input, for comment support
feed_input drops lines starting with a bar character '|', so they can
serve as comments.  Syntax chosen because such lines shouldn't be
needed in tests.  In particular, the server already ignores such lines
when it reads commands, because they get parsed as empty command with
a pipeline, and empty commands get ignored, regardless of
redirections.

Signed-off-by: Markus Armbruster <armbru@pond.sub.org>
2014-01-06 20:49:56 +01:00

132 lines
2.8 KiB
Bash

export EMPIREHOST=127.0.0.1 LOGNAME=tester
if [ -x ./config.status ] && ./config.status --version | grep -q '^Wolfpack Empire'
then :
else echo "$0: Must be run in root of build tree" >&2; exit 1
fi
test=${0##*/}
test=${test%-test}
create_sandbox()
{
rm -rf sandbox
mkdir -p sandbox/etc/empire sandbox/share/empire/builtin sandbox/var/empire
touch sandbox/etc/empire/schedule
cat >sandbox/etc/empire/econfig <<EOF
data "../../var/empire"
info "../../../../info.nr"
builtin "../../share/empire/builtin"
listen_addr "$EMPIREHOST"
keep_journal 2
EOF
cp `git ls-files "$srcdir"/src/lib/global | grep '\.config$'` sandbox/share/empire/builtin
}
now()
{
# date +%s isn't portable...
perl -e 'printf "%s\n", time'
}
start_server()
{
local pidfile=sandbox/var/empire/server.pid
local timeout
pid=
trap 'if [ "$pid" ]; then kill -9 "$pid" 2>/dev/null || true; fi' EXIT
src/server/emp_server -e sandbox/etc/empire/econfig -R 1 -s
timeout=$((`now`+5))
until pid=`cat $pidfile 2>/dev/null` && [ -n "$pid" ]
do
if [ `now` -gt $timeout ]
then
echo "Timed out waiting for server to create $pidfile" >&2
exit 1
fi
done
while src/client/empire red herring 2>&1 | grep -q "Connection refused"
do
if [ `now` -gt $timeout ]
then
echo "Timed out waiting for server to accept connections" >&2
exit 1
fi
done
}
stop_server()
{
local timeout
kill "$pid"
timeout=$((`now`+5))
while kill -0 "$pid" 2>/dev/null
do
if [ `now` -gt $timeout ]
then
echo "Timed out waiting for server to terminate" >&2
exit 1
fi
done
}
feed_input()
{
c=$1
r=$2
shift 2
sed '/^|/d' "$@" | src/client/empire "$c" "$r" >/dev/null
}
begin_test()
{
src/util/files -e sandbox/etc/empire/econfig -f >/dev/null
case "$1" in
*.xdump)
src/util/empdump -e sandbox/etc/empire/econfig -i "$1"
;;
*)
cp -r sandbox/var/empire/tel sandbox/var/empire/empty.tel
start_server
src/client/empire POGO peter <"$1" >/dev/null
stop_server
mv sandbox/var/empire/tel sandbox/var/empire/init.tel
mv sandbox/var/empire/empty.tel sandbox/var/empire/tel
mv sandbox/var/empire/journal.log sandbox/var/empire/init.journal.log
mv sandbox/var/empire/server.log sandbox/var/empire/init.server.log
;;
esac
start_server
}
end_test ()
{
stop_server
src/util/empdump -e sandbox/etc/empire/econfig -x >sandbox/$test.xdump
}
cmp_out()
{
local opt exp act nrm msg ret=0
for i
do
case "$i" in
*/journal.log) opt=-j ;;
*/server.log) opt=-s ;;
*) opt= ;;
esac
exp="$srcdir/tests/$test/${i##*/}"
act="sandbox/$i"
nrm="sandbox/normalized-${i##*/}"
perl "$srcdir"/tests/normalize.pl $opt "$act" >"$nrm"
if msg=`diff -q "$exp" "$nrm"`
then
echo "$exp OK"
else
ret=$?
echo "$exp FAIL"
echo $msg
fi
done
return $ret
}