mirror of
https://github.com/servalproject/serval-dna.git
synced 2025-01-18 02:39:44 +00:00
First server start/stop test
On the way to multi-instance test scripts
This commit is contained in:
parent
824763f5f0
commit
72640e830a
238
testdefs.sh
238
testdefs.sh
@ -4,58 +4,9 @@ testdefs_sh=$(abspath "${BASH_SOURCE[0]}")
|
||||
servald_source_root="${testdefs_sh%/*}"
|
||||
servald_build_root="$servald_source_root"
|
||||
|
||||
# Utility function for setting up a fixture with a DNA server process:
|
||||
# - Ensure that no servald processes are running
|
||||
# - Start a servald server process
|
||||
# - Ensure that it is still running after one second
|
||||
start_servald_server() {
|
||||
check_no_servald_processes
|
||||
# Start DNA server
|
||||
set -- $servald -v verbose -f $hlr_dat -S -n "$@"
|
||||
echo "+ run $*"
|
||||
"$@" >$DNATMP/servald.log 2>&1 &
|
||||
sleep 1
|
||||
local servald_basename="${servald##*/}"
|
||||
if [ -z "$servald_basename" ]; then
|
||||
error "cannot run test: \$servald not set"
|
||||
return 1
|
||||
fi
|
||||
pid=$(ps -u$UID | awk '$4 == "'"$servald_basename"'" {print $1}')
|
||||
if [ -z "$pid" ]; then
|
||||
echo "servald server did not start"
|
||||
tfw_cat --header=servald.log $SERVALINSTANCE_PATH/servald.log
|
||||
fail
|
||||
fi
|
||||
if ! [ -s $SERVALINSTANCE_PATH/serval.pid ] && kill -0 $(cat $SERVALINSTANCE_PATH/serval.pid); then
|
||||
echo "serval.pid was not created"
|
||||
tfw_cat --header=servald.log $SERVALINSTANCE_PATH/servald.log
|
||||
fail
|
||||
fi
|
||||
echo "# Started servald server process, pid=$pid"
|
||||
}
|
||||
|
||||
# Utility function for tearing down DNA fixtures:
|
||||
# - If a servald server process is running, then kill it
|
||||
# - Cat any servald log file into the test log
|
||||
# - Ensure that no servald processes are running
|
||||
stop_servald_server() {
|
||||
if [ -s $SERVALINSTANCE_PATH/servald.pid ]; then
|
||||
local pid=$(cat $SERVALINSTANCE_PATH/servald.pid)
|
||||
if kill $pid; then
|
||||
echo "# Killed servald process pid=$pid"
|
||||
else
|
||||
error "# Dna process pid=$pid was not running"
|
||||
fi
|
||||
fi
|
||||
if [ -s $DNATMP/servald.log ]; then
|
||||
tfw_cat --header=servald.log $DNATMP/servald.log
|
||||
fi
|
||||
check_no_servald_processes
|
||||
}
|
||||
|
||||
# Utility function for creating DNA fixtures:
|
||||
# - Create a temporary directory to contain all servald-related files
|
||||
# - set $servald and $hlr_dat variables
|
||||
# - set $servald variable (executable under test)
|
||||
# - set SERVALINSTANCE_PATH environment variable
|
||||
# - mkdir $SERVALINSTANCE_PATH unless --no-mkdir option given
|
||||
setup_servald() {
|
||||
@ -64,11 +15,29 @@ setup_servald() {
|
||||
error "servald executable not present: $servald"
|
||||
return 1
|
||||
fi
|
||||
export DNATMP=$TFWTMP/servaldtmp
|
||||
[ "$1" = --no-mkdir ] || mkdir $DNATMP
|
||||
export SERVALINSTANCE_PATH=$DNATMP
|
||||
hlr_dat=$SERVALINSTANCE_PATH/hlr.dat
|
||||
unset SERVALD_OUTPUT_DELIMITER
|
||||
}
|
||||
|
||||
# Utility function:
|
||||
# - set up environment and normal variables for the given instance name
|
||||
# - if the argument is not an instance name, then use the default instance name
|
||||
# and return 1, otherwise return 0
|
||||
set_instance() {
|
||||
case "$1" in
|
||||
[A-Z]|default) set_instance_vars "$1"; return 0;;
|
||||
*) set_instance_vars "default"; return 1;;
|
||||
esac
|
||||
}
|
||||
|
||||
# Utility function:
|
||||
# - set all the instance variables and environment variables and create the
|
||||
# instance directory for the given instance name
|
||||
set_instance_vars() {
|
||||
instance_name="${1:-default}"
|
||||
export instance_dir="$TFWTMP/instance/$instance_name"
|
||||
mkdir -p "$instance_dir"
|
||||
export instance_servald_log="$instance_dir/servald.log"
|
||||
export SERVALINSTANCE_PATH="$instance_dir/servald"
|
||||
export instance_servald_pidfile="$SERVALINSTANCE_PATH/servald.pid"
|
||||
}
|
||||
|
||||
# Utility function for setting up DNA JNI fixtures:
|
||||
@ -79,19 +48,158 @@ setup_servald_so() {
|
||||
export LD_LIBRARY_PATH="$servald_build_root"
|
||||
}
|
||||
|
||||
# Utility function for managing DNA fixtures:
|
||||
# - Ensure there are no existing DNA server processes
|
||||
check_no_servald_processes() {
|
||||
# Utility function for setting up a fixture with a DNA server process:
|
||||
# - Ensure that no servald processes are running
|
||||
# - Start a servald server process
|
||||
# - Ensure that it is still running after one second
|
||||
start_servald_server() {
|
||||
set_instance "$1" && shift
|
||||
executeOk $servald config set logfile "$instance_servald_log"
|
||||
# Start DNA server
|
||||
local -a before_pids
|
||||
local -a after_pids
|
||||
get_servald_pids before_pids
|
||||
echo "# before_pids=$before_pids"
|
||||
unset SERVALD_OUTPUT_DELIMITER
|
||||
executeOk $servald start "$@"
|
||||
tfw_cat --stdout
|
||||
get_servald_pids after_pids
|
||||
echo "# after_pids=$after_pids"
|
||||
# Assert that the servald pid file is present.
|
||||
assert --message="servald pidfile was created" [ -s "$instance_servald_pidfile" ]
|
||||
servald_pid=$(cat "$instance_servald_pidfile")
|
||||
assert --message="servald pidfile contains a valid pid" --dump-on-fail="$instance_servald_log" kill -0 "$servald_pid"
|
||||
# Assert that there is at least one new servald process running.
|
||||
local apid bpid
|
||||
local new_pids=
|
||||
local pidfile_running=false
|
||||
for apid in ${after_pids[*]}; do
|
||||
local isnew=true
|
||||
for bpid in ${before_pids[*]}; do
|
||||
if [ "$apid" -eq "$bpid" ]; then
|
||||
isnew=false
|
||||
break
|
||||
fi
|
||||
done
|
||||
if [ "$apid" -eq "$servald_pid" ]; then
|
||||
echo "# started servald process: pid=$servald_pid"
|
||||
new_pids="$new_pids $apid"
|
||||
pidfile_running=true
|
||||
elif $isnew; then
|
||||
echo "# unknown new servald process: pid=$apid"
|
||||
new_pids="$new_pids $apid"
|
||||
fi
|
||||
done
|
||||
assert --message="a new servald process is running" --dump-on-fail="$instance_servald_log" [ -n "$new_pids" ]
|
||||
assert --message="servald pidfile process is running" --dump-on-fail="$instance_servald_log" $pidfile_running
|
||||
echo "# Started servald server process $instance_name, pid=$servald_pid"
|
||||
}
|
||||
|
||||
stop_servald_server() {
|
||||
set_instance "$1" && shift
|
||||
# Stop DNA server
|
||||
servald_pid=$(cat "$instance_servald_pidfile")
|
||||
local -a before_pids
|
||||
local -a after_pids
|
||||
get_servald_pids before_pids
|
||||
echo "# before_pids=$before_pids"
|
||||
unset SERVALD_OUTPUT_DELIMITER
|
||||
executeOk $servald stop "$@"
|
||||
tfw_cat --stdout
|
||||
echo "# Stopped servald server process $instance_name, pid=${servald_pid:-unknown}"
|
||||
get_servald_pids after_pids
|
||||
echo "# after_pids=$after_pids"
|
||||
# Assert that the servald pid file is gone.
|
||||
assert --message="servald pidfile was removed" [ ! -e "$instance_servald_pidfile" ]
|
||||
# Assert that the servald process identified by the pidfile is no longer running.
|
||||
local apid bpid
|
||||
if [ -n "$servald_pid" ]; then
|
||||
for apid in ${after_pids[*]}; do
|
||||
assert --message="servald process still running" [ "$apid" -ne "$servald_pid" ]
|
||||
done
|
||||
fi
|
||||
# Append the server log file to the test log.
|
||||
[ -s "$instance_servald_log" ] && tfw_cat "$instance_servald_log"
|
||||
# Check there is at least one fewer servald processes running.
|
||||
for bpid in ${before_pids[*]}; do
|
||||
local isgone=true
|
||||
for apid in ${after_pids[*]}; do
|
||||
if [ "$apid" -eq "$bpid" ]; then
|
||||
isgone=false
|
||||
break
|
||||
fi
|
||||
done
|
||||
if $isgone; then
|
||||
echo "# ended servald process: pid=$bpid"
|
||||
fi
|
||||
done
|
||||
}
|
||||
|
||||
# Utility function for tearing down DNA fixtures:
|
||||
# - Kill all servald server process instances in an orderly fashion
|
||||
# - Cat any servald log file into the test log
|
||||
stop_all_servald_servers() {
|
||||
if pushd "$TFWTMP/instance" >/dev/null; then
|
||||
for name in *; do
|
||||
stop_servald_server "$name"
|
||||
done
|
||||
popd >/dev/null
|
||||
fi
|
||||
}
|
||||
|
||||
# Utility function for tearing down DNA fixtures:
|
||||
# - send a given signal to all running servald processes, identified by name
|
||||
# - return 1 if no processes were present, 0 if any signal was sent
|
||||
signal_all_servald_processes() {
|
||||
local sig="$1"
|
||||
local servald_pids
|
||||
get_servald_pids servald_pids
|
||||
local pid
|
||||
local ret=1
|
||||
for pid in $servald_pids; do
|
||||
if kill -$sig "$pid"; then
|
||||
echo "# Sent SIG$sig to servald process pid=$pid"
|
||||
ret=0
|
||||
else
|
||||
error "# servald process pid=$pid not running -- SIG$sig not sent"
|
||||
fi
|
||||
done
|
||||
return $ret
|
||||
}
|
||||
|
||||
# Utility function for tearing down DNA fixtures:
|
||||
# - terminate all running servald processes, identified by name, by sending
|
||||
# first SIGTERM then SIGHUP and finally SIGKILL
|
||||
# - assert that no more servald processes are running
|
||||
kill_all_servald_processes() {
|
||||
if signal_all_servald_processes TERM; then
|
||||
sleep 2
|
||||
if signal_all_servald_processes HUP; then
|
||||
sleep 2
|
||||
signal_all_servald_processes KILL
|
||||
fi
|
||||
fi
|
||||
}
|
||||
|
||||
# Utility function:
|
||||
# - return the PIDs of all servald processes the current user is running
|
||||
get_servald_pids() {
|
||||
local var="$1"
|
||||
local servald_basename="${servald##*/}"
|
||||
if [ -z "$servald_basename" ]; then
|
||||
error "cannot run test: \$servald not set"
|
||||
error "\$servald is not set"
|
||||
return 1
|
||||
fi
|
||||
local pids=$(ps -u$UID | awk '$4 == "'"$servald_basename"'" {print $1}')
|
||||
if [ -n "$pids" ]; then
|
||||
error "cannot run test: $servald_basename process already running with pid: $pids"
|
||||
return 1
|
||||
fi
|
||||
echo "# No other $servald_basename processes running for uid=$UID"
|
||||
local pids=$(ps -u$UID | awk -v servald="$servald_basename" '$4 == servald {print $1}')
|
||||
[ -n "$var" ] && eval "$var=($pids)"
|
||||
[ -n "$pids" ]
|
||||
}
|
||||
|
||||
# Assertion function:
|
||||
# - assert there are no existing DNA server processes
|
||||
assert_no_servald_processes() {
|
||||
local pids
|
||||
get_servald_pids pids
|
||||
assert --message="$servald_basename process(es) running: $pids" [ -z "$pids" ]
|
||||
return 0
|
||||
}
|
||||
|
13
tests/config
13
tests/config
@ -23,26 +23,27 @@ source "${0%/*}/../testdefs.sh"
|
||||
|
||||
setup() {
|
||||
setup_servald
|
||||
set_instance
|
||||
}
|
||||
|
||||
doc_GetCreateInstanceDir="Get creates instance directory"
|
||||
setup_GetCreateInstanceDir() {
|
||||
setup_servald --no-mkdir
|
||||
assert [ ! -d $SERVALINSTANCE_PATH ]
|
||||
setup
|
||||
assert [ ! -d "$SERVALINSTANCE_PATH" ]
|
||||
}
|
||||
test_GetCreateInstanceDir() {
|
||||
executeOk $servald config get
|
||||
assert [ -d $SERVALINSTANCE_PATH ]
|
||||
assert [ -d "$SERVALINSTANCE_PATH" ]
|
||||
}
|
||||
|
||||
doc_SetCreateInstanceDir="Set creates instance directory"
|
||||
setup_SetCreateInstanceDir() {
|
||||
setup_servald --no-mkdir
|
||||
assert [ ! -d $SERVALINSTANCE_PATH ]
|
||||
setup
|
||||
assert [ ! -d "$SERVALINSTANCE_PATH" ]
|
||||
}
|
||||
test_SetCreateInstanceDir() {
|
||||
executeOk $servald config set foo bar
|
||||
assert [ -d $SERVALINSTANCE_PATH ]
|
||||
assert [ -d "$SERVALINSTANCE_PATH" ]
|
||||
}
|
||||
|
||||
doc_GetNull="Get an unset config item"
|
||||
|
46
tests/server
Executable file
46
tests/server
Executable file
@ -0,0 +1,46 @@
|
||||
#!/bin/bash
|
||||
|
||||
# Tests for Serval DNA server operations.
|
||||
#
|
||||
# Copyright 2012 Paul Gardner-Stephen
|
||||
#
|
||||
# This program is free software; you can redistribute it and/or
|
||||
# modify it under the terms of the GNU General Public License
|
||||
# as published by the Free Software Foundation; either version 2
|
||||
# of the License, or (at your option) any later version.
|
||||
#
|
||||
# This program is distributed in the hope that it will be useful,
|
||||
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
# GNU General Public License for more details.
|
||||
#
|
||||
# You should have received a copy of the GNU General Public License
|
||||
# along with this program; if not, write to the Free Software
|
||||
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
||||
|
||||
source "${0%/*}/../testframework.sh"
|
||||
source "${0%/*}/../testdefs.sh"
|
||||
|
||||
setup() {
|
||||
setup_servald
|
||||
assert_no_servald_processes
|
||||
set_instance
|
||||
}
|
||||
|
||||
teardown() {
|
||||
stop_all_servald_servers
|
||||
kill_all_servald_processes
|
||||
assert_no_servald_processes
|
||||
}
|
||||
|
||||
doc_ServerStartCreateInstanceDir="Starting server creates instance directory"
|
||||
setup_ServerStartCreateInstanceDir() {
|
||||
setup
|
||||
assert [ ! -d "$SERVALINSTANCE_PATH" ]
|
||||
}
|
||||
test_ServerStartCreateInstanceDir() {
|
||||
start_servald_server
|
||||
assert [ -d "$SERVALINSTANCE_PATH" ]
|
||||
}
|
||||
|
||||
runTests "$@"
|
Loading…
Reference in New Issue
Block a user