test: refactor test.rc + start.sh + stop.sh -> lib.sh

Refactor test.rc, start.sh, and stop.sh into lib.sh that each test
sources and uses independently of each other.

More simplfication and cleanup needed.

Signed-off-by: Joachim Wiberg <troglobit@gmail.com>
This commit is contained in:
Joachim Wiberg 2021-11-22 15:16:05 +01:00
parent 2d95e0ccf0
commit aad76ad03b
11 changed files with 189 additions and 168 deletions

View File

@ -1,4 +1,4 @@
EXTRA_DIST = test.rc opts.sh start.sh stop.sh
EXTRA_DIST = lib.sh opts.sh
EXTRA_DIST += api.sh local.sh remote.sh fwd.sh mark.sh
CLEANFILES = *~ *.trs *.log
TEST_EXTENSIONS = .sh

View File

@ -2,7 +2,8 @@
if [ x"${srcdir}" = x ]; then
srcdir=.
fi
. ${srcdir}/start.sh
. ${srcdir}/lib.sh
setup
export MSG="no-openlog-apitest"
@ -34,11 +35,9 @@ sleep 2
./api -i troglobit -p
sleep 2
ps fax |grep -A2 syslogd
grep "troglobit - MSGID - ${MSG}" ${LOGV1} || (echo "== ${LOGV1}"; tail -10 ${LOGV1}; echo "== ${LOG}"; tail -10 ${LOG}; cat ${CONFD}/v1.conf; false)
grep "troglobit - MSGID - ${MSG}" ${LOGV1} || (echo "== ${LOGV1}"; tail -10 ${LOGV1}; echo "== ${LOG}"; tail -10 ${LOG}; cat ${CONFD}/v1.conf; FAIL "Cannot find troglobit")
echo "= Phase 4 - Verify RFC5424 API with logger(1) ==========="
../src/logger -p ftp.notice -u ${SOCK} -m "MSDSD" -d '[exampleSDID@32473 iut="3" eventSource="Application" eventID="1011"]' "waldo"
sleep 2
grep "exampleSDID@32473" ${LOGV1} || (echo "== ${LOGV1}"; tail -10 ${LOGV1}; false)
. ./stop.sh
grep "exampleSDID@32473" ${LOGV1} || (echo "== ${LOGV1}"; tail -10 ${LOGV1}; FAIL "Cannot find exampleSDID@32473")

View File

@ -3,7 +3,9 @@
if [ x"${srcdir}" = x ]; then
srcdir=.
fi
. ${srcdir}/start.sh
. ${srcdir}/lib.sh
setup
MSG="fwd and allow"
@ -23,10 +25,9 @@ kill -HUP `cat ${PID}`
sleep 2
# Enable debug for second syslogd
cat ${PID2} >> "$DIR/PIDs"
kill -USR1 `cat ${PID2}`
../src/logger -t fwd -p ntp.notice -u ${SOCK} -m "NTP123" ${MSG}
sleep 3
grep "fwd - NTP123 - ${MSG}" ${LOG2}
. ./stop.sh
grep "fwd - NTP123 - ${MSG}" ${LOG2} || FAIL "Nothing forwarded."

161
test/lib.sh Executable file
View File

@ -0,0 +1,161 @@
#!/bin/sh
# Test name, used everywhere as /tmp/sysklogd/$NM/foo
NM=$(basename "$0" .sh)
DIR=/tmp/sysklogd/$NM
FN=syslog-test
FN2=syslog-test2
LOG=$DIR/${FN}.log
LOG2=$DIR/${FN2}.log
LOGV1=$DIR/${FN}-v1.log
LOGCONS=$DIR/${FN}-cons.log
PID=$DIR/${FN}.pid
PID2=$DIR/${FN2}.pid
CAP=$DIR/${FN}.pcapng
CACHE=$DIR/${FN}.cache
CACHE2=$DIR/${FN2}.cache
CONF=$DIR/${FN}.conf
CONF2=$DIR/${FN2}.conf
CONFD=$DIR/${FN}.d
CONFD2=$DIR/${FN2}.d
SOCK=$DIR/${FN}.sock
SOCK2=$DIR/${FN2}.sock
ALTSOCK=$DIR/${FN}-alt.sock
PORT=5514
PORT2=5555
export SYSLOG_UNIX_PATH=${SOCK}
# Print heading for test phases
print()
{
printf "\e[7m>> %-76s\e[0m\n" "$1"
}
dprint()
{
printf "\e[2m%-76s\e[0m\n" "$1"
}
SKIP()
{
print "TEST: SKIP"
[ $# -gt 0 ] && echo "$*"
exit 77
}
FAIL()
{
print "TEST: FAIL"
[ $# -gt 0 ] && echo "$*"
exit 99
}
OK()
{
print "TEST: OK"
[ $# -gt 0 ] && echo "$*"
exit 0
}
# shellcheck disable=SC2068
tenacious()
{
timeout=$1
shift
while [ $timeout -gt 0 ]; do
$@ && return
timeout=$((timeout - 1))
done
FAIL "Timeed out $*"
}
setup()
{
ip link set lo up
cat <<-EOF > ${CONF}
# Nothing here yo
include ${CONFD}/*.conf
EOF
mkdir -p ${CONFD2}
cat <<-EOF > ${CONF2}
# Nothing here yo
include ${CONFD2}/*.conf
EOF
cat <<-EOF > ${CONFD}/foo.conf
# Local log file, avoid sync to disk
*.* -${LOG}
EOF
cat <<-EOF > ${CONFD}/bar.conf
# For remote logging
*.* @127.0.0.2
EOF
../src/syslogd -m1 -b :${PORT} -d -sF -f ${CONF} -p ${SOCK} -p ${ALTSOCK} -C ${CACHE} -P ${PID} &
sleep 2
cat ${PID} >> "$DIR/PIDs"
kill -USR1 `cat ${PID}`
sleep 1
}
# Stop all lingering collectors and other tools
kill_pids()
{
# shellcheck disable=SC2162
if [ -f "$DIR/PIDs" ]; then
while read ln; do kill "$ln" 2>/dev/null; done < "$DIR/PIDs"
rm "$DIR/PIDs"
fi
}
teardown()
{
kill_pids
sleep 1
rm -f ${LOG}
rm -f ${LOGV1}
rm -f ${LOG2}
rm -f ${LOGCONS}
rm -f ${PID}
rm -f ${PID2}
rm -f ${CAP}
rm -f ${SOCK}
rm -f ${CACHE}
rm -f ${CACHE2}
rm -f ${CONF}
rm -f ${CONF2}
rm -rf ${CONFD}
rm -rf ${CONFD2}
}
signal()
{
echo
if [ "$1" != "EXIT" ]; then
print "Got signal, cleaning up"
fi
teardown
}
# props to https://stackoverflow.com/a/2183063/1708249
trapit()
{
func="$1" ; shift
for sig ; do
trap "$func $sig" "$sig"
done
}
# Runs once when including lib.sh
mkdir -p ${CONFD}
touch "$DIR/PIDs"
trapit signal INT TERM QUIT EXIT

View File

@ -2,15 +2,14 @@
if [ x"${srcdir}" = x ]; then
srcdir=.
fi
. ${srcdir}/start.sh
. ${srcdir}/lib.sh
setup
MSG="foobar"
MSG2="xyzzy"
../src/logger -u ${SOCK} ${MSG}
grep ${MSG} ${LOG}
grep ${MSG} ${LOG} || FAIL "Cannot find: ${MSG}"
../src/logger -u ${ALTSOCK} ${MSG2}
grep ${MSG2} ${LOG}
. ./stop.sh
grep ${MSG2} ${LOG} || FAIL "Cannot find: ${MSG2}"

View File

@ -3,7 +3,8 @@
if [ x"${srcdir}" = x ]; then
srcdir=.
fi
. ${srcdir}/start.sh
. ${srcdir}/lib.sh
setup
check_mark()
{
@ -12,6 +13,5 @@ check_mark()
return 1
}
tenacious 120 check_mark
. ./stop.sh
tenacious 120 check_mark && OK
FAIL "Missing MARK in log"

View File

@ -1,10 +1,8 @@
#!/bin/sh
set -x
if [ x"${srcdir}" = x ]; then
srcdir=.
fi
. ${srcdir}/test.rc
. ${srcdir}/lib.sh
mkdir -p ${CONFD}
cat <<EOF > ${CONF}
@ -14,8 +12,7 @@ EOF
../src/syslogd -m1 -b :${PORT} -d -sF -f ${CONF} -p ${SOCK} -p ${ALTSOCK} >${LOG2} &
echo "$!" > ${PID}
cat ${PID} >> "$DIR/PIDs"
sleep 1
kill -9 ${PID}
grep ';RFC5424,rotate=10000000:5' ${LOG2}
sleep 2
grep ';RFC5424,rotate=10000000:5' ${LOG2} || FAIL "Failed parsing RFC542 .conf"

View File

@ -1,8 +1,11 @@
#!/bin/sh
set -x
if [ x"${srcdir}" = x ]; then
srcdir=.
fi
. ${srcdir}/start.sh
. ${srcdir}/lib.sh
setup
MSG="kilroy"
@ -10,6 +13,7 @@ MSG="kilroy"
#tshark -Qni lo -w ${CAP} port ${PORT} &
tshark -Qni lo -w ${CAP} port 514 &
TPID="$!"
echo "$TPID" >> "$DIR/PIDs"
# Wait for tshark to start up properly
sleep 3
@ -25,7 +29,5 @@ wait ${TPID}
# Analyze content, should have $MSG now ...
#tshark -d udp.port==${PORT},syslog -r ${CAP} | grep ${MSG}
tshark -r ${CAP} | grep ${MSG}
tshark -r ${CAP} | grep ${MSG} || FAIL "Cannot find: ${MSG}"
rm ${CAP}
. ./stop.sh

View File

@ -1,81 +0,0 @@
#!/bin/sh
if [ x"${srcdir}" = x ]; then
srcdir=.
fi
. ${srcdir}/test.rc
# Print heading for test phases
print()
{
printf "\e[7m>> %-76s\e[0m\n" "$1"
}
dprint()
{
printf "\e[2m%-76s\e[0m\n" "$1"
}
SKIP()
{
print "TEST: SKIP"
[ $# -gt 0 ] && echo "$*"
exit 77
}
FAIL()
{
print "TEST: FAIL"
[ $# -gt 0 ] && echo "$*"
exit 99
}
OK()
{
print "TEST: OK"
[ $# -gt 0 ] && echo "$*"
exit 0
}
# shellcheck disable=SC2068
tenacious()
{
timeout=$1
shift
while [ $timeout -gt 0 ]; do
$@ && return
timeout=$((timeout - 1))
done
FAIL "Timeed out $*"
}
ip link set lo up
mkdir -p ${CONFD}
cat <<EOF > ${CONF}
# Nothing here yo
include ${CONFD}/*.conf
EOF
mkdir -p ${CONFD2}
cat <<EOF > ${CONF2}
# Nothing here yo
include ${CONFD2}/*.conf
EOF
cat <<EOF > ${CONFD}/foo.conf
# Local log file, avoid sync to disk
*.* -${LOG}
EOF
cat <<EOF > ${CONFD}/bar.conf
# For remote logging
*.* @127.0.0.2
EOF
../src/syslogd -m1 -b :${PORT} -d -sF -f ${CONF} -p ${SOCK} -p ${ALTSOCK} -C ${CACHE} -P ${PID} &
sleep 2
kill -USR1 `cat ${PID}`
sleep 1

View File

@ -1,31 +0,0 @@
#!/bin/sh -e
if [ x"${srcdir}" = x ]; then
srcdir=.
fi
. ${srcdir}/test.rc
if [ -e ${PID} ]; then
echo "Killing 1st syslogd, PID:`cat ${PID}` ..."
kill `cat ${PID}`
fi
if [ -e ${PID2} ]; then
echo "Killing 2nd syslogd, PID:`cat ${PID2}` ..."
kill `cat ${PID2}`
fi
rm -f ${LOG}
rm -f ${LOGV1}
rm -f ${LOG2}
rm -f ${LOGCONS}
rm -f ${PID}
rm -f ${PID2}
rm -f ${CAP}
rm -f ${SOCK}
rm -f ${CACHE}
rm -f ${CACHE2}
rm -f ${CONF}
rm -f ${CONF2}
rm -rf ${CONFD}
rm -rf ${CONFD2}

View File

@ -1,26 +0,0 @@
# Test name, used everywhere as /tmp/sysklogd/$NM/foo
NM=$(basename "$0" .sh)
DIR=/tmp/sysklogd/$NM
FN=syslog-test
FN2=syslog-test2
LOG=$DIR/${FN}.log
LOG2=$DIR/${FN2}.log
LOGV1=$DIR/${FN}-v1.log
LOGCONS=$DIR/${FN}-cons.log
PID=$DIR/${FN}.pid
PID2=$DIR/${FN2}.pid
CAP=$DIR/${FN}.pcapng
CACHE=$DIR/${FN}.cache
CACHE2=$DIR/${FN2}.cache
CONF=$DIR/${FN}.conf
CONF2=$DIR/${FN2}.conf
CONFD=$DIR/${FN}.d
CONFD2=$DIR/${FN2}.d
SOCK=$DIR/${FN}.sock
SOCK2=$DIR/${FN2}.sock
ALTSOCK=$DIR/${FN}-alt.sock
PORT=5514
PORT2=5555
export SYSLOG_UNIX_PATH=${SOCK}