On Sun, Jul 02, 2017 at 06:29:07PM +0200, Sebastian Benoit wrote: > Rob Pierce(r...@2keys.ca) on 2017.07.02 12:06:25 -0400: > > I am currently using this regression script for basic ifstated sanity > > testing. > > > > Still a work in progress. Requesting commit for safe keeping. > > > Hi, > > this should go into /usr/src/regress/usr.sbin/ifstated > (which does not esist yet). > > Also, it should hook into the regress framework (bsd.regress.mk(5)). > > As it needs some network configuration, maybe it should be similar to > relayd regress tests. > > Happy to work with you on that. > > /B.
I have updated the ifstated regression scripts based on your feedback. I also added a script to test drive the state machine. Both should be more systematic in coverage, but hopefully it is a good start. Regards, Rob Index: regress/usr.sbin/ifstated/Makefile =================================================================== RCS file: regress/usr.sbin/ifstated/Makefile diff -N regress/usr.sbin/ifstated/Makefile --- /dev/null 1 Jan 1970 00:00:00 -0000 +++ regress/usr.sbin/ifstated/Makefile 6 Jul 2017 16:55:57 -0000 @@ -0,0 +1,13 @@ +# $OpenBSD$ + +# Regress tests for ifstated + +REGRESS_TARGETS = run-regress-statemachine run-regress-ifstated + +run-regress-statemachine: + sh ${.CURDIR}/statemachine + +run-regress-ifstated: + sh ${.CURDIR}/ifstated + +.include <bsd.regress.mk> Index: regress/usr.sbin/ifstated/ifstated =================================================================== RCS file: regress/usr.sbin/ifstated/ifstated diff -N regress/usr.sbin/ifstated/ifstated --- /dev/null 1 Jan 1970 00:00:00 -0000 +++ regress/usr.sbin/ifstated/ifstated 6 Jul 2017 16:55:57 -0000 @@ -0,0 +1,148 @@ +# $OpenBSD$ + +# Basic ifstated regression script to test interface changes. + +# Golbal variables +VHIDA=252 +VHIDB=253 +PREFIX=172.16.0 +DEMOTE=ifconfig +PROMOTE=ifconfig +EVERY=5 +SLEEP=10 + +cleanup() { + ifconfig carp${VHIDA} destroy > /dev/null 2>&1 + ifconfig carp${VHIDB} destroy > /dev/null 2>&1 + rm working/ifstated.conf >/dev/null 2>&1 + rm working/ifstated.log >/dev/null 2>&1 + rm working/output.test >/dev/null 2>&1 + rm working/output.new >/dev/null 2>&1 + rm working/nohup.out >/dev/null 2>&1 + rmdir working >/dev/null 2>&1 +} + +fail() { + echo FAILED + cleanup + exit 1 +} + +skip() { + echo SKIPPED + cleanup + exit 0 +} + +trap 'skip' INT + +# look for a suitable physical interface for carp +NIC="$(netstat -rn -finet | grep ^default | awk '{ print $8 }')" +STATUS="$(ifconfig | grep -A5 ^${NIC} | grep status: | awk '{ print $2 }')" + +if [ "$STATUS" != "active" ] +then + echo "No suitable physical interface found." + echo SKIPPED + exit 0 +fi + +if [ "$(pgrep ifstated)" ] +then + echo "The ifstated daemon is already running." + echo SKIPPED + exit 0 +fi + +for interface in carp${VHIDA} carp${VHIDB} +do + ifconfig ${interface} > /dev/null 2>&1 + if [ $? -eq 0 ] + then + echo "Interface $interface already exists." + echo SKIPPED + exit 0 + fi +done + +mkdir -p working + +cat > working/ifstated.conf <<EOF +# This is a config template for ifstated regression testing +carp = "carp${VHIDA}.link.up" +init-state primary +net = '( "ping -q -c 1 -w 1 ${PREFIX}.${VHIDB} > /dev/null" every ${EVERY})' +state primary { + init { + run "ifconfig" + } + if ! \$net + set-state demoted + if ! \$carp + set-state demoted +} +state demoted { + init { + run "ifconfig" + } + if \$net && \$carp + set-state primary +} +EOF + +ifconfig carp${VHIDA} inet ${PREFIX}.${VHIDA} netmask 255.255.255.0 broadcast \ + ${PREFIX}.255 vhid ${VHIDA} carpdev ${NIC} +ifconfig carp${VHIDB} inet ${PREFIX}.${VHIDB} netmask 255.255.255.0 broadcast \ + ${PREFIX}.255 vhid ${VHIDB} carpdev ${NIC} + +# give the carp interface time to come up as MASTER +sleep 5 + +cat > working/output.test <<EOF +changing state to primary +changing state to demoted +changing state to primary +changing state to demoted +changing state to primary +changing state to demoted +changing state to primary +changing state to primary +EOF + +(cd working && nohup ifstated -dvf ./ifstated.conf > ifstated.log 2>&1) & + +sleep ${SLEEP} +ifconfig carp${VHIDA} down +sleep ${SLEEP} +ifconfig carp${VHIDA} up +sleep ${SLEEP} +ifconfig carp${VHIDB} destroy +sleep ${SLEEP} +ifconfig carp${VHIDB} inet ${PREFIX}.${VHIDB} netmask 255.255.255.0 broadcast \ + ${PREFIX}.255 vhid ${VHIDB} carpdev ${NIC} +sleep ${SLEEP} +ifconfig carp${VHIDA} down +sleep ${SLEEP} +ifconfig carp${VHIDB} destroy +sleep ${SLEEP} +ifconfig carp${VHIDA} up +sleep ${SLEEP} +ifconfig carp${VHIDB} inet ${PREFIX}.${VHIDB} netmask 255.255.255.0 broadcast \ + ${PREFIX}.255 vhid ${VHIDB} carpdev ${NIC} +sleep ${SLEEP} +kill -HUP $(pgrep ifstated) >/dev/null 2>&1 +sleep ${SLEEP} + +grep ^changing working/ifstated.log > working/output.new + +kill $(pgrep ifstated) >/dev/null 2>&1 + +diff working/output.test working/output.new +case $? in +0) echo PASSED + cleanup + exit 0 + ;; +1) fail + ;; +esac Index: regress/usr.sbin/ifstated/statemachine =================================================================== RCS file: regress/usr.sbin/ifstated/statemachine diff -N regress/usr.sbin/ifstated/statemachine --- /dev/null 1 Jan 1970 00:00:00 -0000 +++ regress/usr.sbin/ifstated/statemachine 6 Jul 2017 16:55:57 -0000 @@ -0,0 +1,183 @@ +# $OpenBSD$ + +# Basic ifstated regression script to test the finite state machine. + +# +# NOTE: Increase LSLEEP as required when adding additional test states. +# + +# Golbal variables +FILE1="truth1.test" +FILE2="truth2.test" +EVERY=2 +SLEEP=5 +LSLEEP=35 + +cleanup() { + rm working/$FILE1 >/dev/null 2>&1 + rm working/$FILE2 >/dev/null 2>&1 + rm working/ifstated.conf >/dev/null 2>&1 + rm working/ifstated.log >/dev/null 2>&1 + rm working/output.test >/dev/null 2>&1 + rm working/output.new >/dev/null 2>&1 + rm working/nohup.out >/dev/null 2>&1 + rmdir working >/dev/null 2>&1 +} + +fail() { + echo FAILED + cleanup + exit 1 +} + +skip() { + echo SKIPPED + cleanup + exit 0 +} + +trap 'skip' INT + +if [ "$(pgrep ifstated)" ] +then + echo "The ifstated daemon is already running." + echo SKIPPED + exit 0 +fi + +mkdir -p working + +rm -rf working/${FILE1} +rm -rf working/${FILE2} + +cat > working/ifstated.conf <<EOF +# This is a config template for ifstated regression testing +init-state one +true = '( "true" every $EVERY )' +false = '( "false" every $EVERY )' +test1 = '( "test -f ${FILE1}" every $EVERY )' +test2 = '( "test -f ${FILE2}" every $EVERY )' +state one { + init { + run "sleep $SLEEP && ( test -f ${FILE2} || touch ${FILE1} )" + } + if \$test1 && ! \$test2 + set-state two + if \$test2 + set-state ninetyeight +} +state two { + init { + run "sleep $SLEEP && rm ${FILE1}" + } + if ! \$test1 + set-state three +} +state three { + if ( \$false || \$false ) || ( ! \$test1 || \$false ) + set-state four +} +state four { + if ( \$true && \$true ) && ( ! \$test1 && \$true ) + set-state five +} +state five { + init { + run "sleep $SLEEP && touch ${FILE1}" + } + if ( \$false || \$true ) && ( ! \$true || \$test1 ) + set-state six +} +state six { + init { + run "sleep $SLEEP && touch ${FILE1}" + } + if ( \$false || \$true ) && ( ! \$true || \$test1 ) + if \$true + set-state seven +} +state seven { + if ( ! \$false ) + set-state eight +} +state eight { + if ! ( \$false ) + set-state nine +} +state nine { + if ! ( \$false ) { + set-state ten + } +} +state ten { + if \$true && ! ( \$true && ! \$test1 ) { + run "rm ${FILE1}" + run "touch ${FILE2}" + set-state one + } +} +state ninetyeight { + init { + if \$true + if ( \$true && ! \$false ) && ( \$false || \$true ) { + if \$true + run "touch ${FILE1}" + } + } + if \$test1 && \$test2 { + run "rm ${FILE2}" + set-state ninetynine + } +} +state ninetynine { + init { + run "touch ${FILE2}" + } + if \$test1 && \$test2 + set-state onehundred +} +state onehundred { + if ! ( ! ( ! \$false ) ) + set-state end +} +state end { + if \$false + set-state one +} +EOF + +cat > working/output.test <<EOF +changing state to one +changing state to two +changing state to three +changing state to four +changing state to five +changing state to six +changing state to seven +changing state to eight +changing state to nine +changing state to ten +changing state to one +changing state to ninetyeight +changing state to ninetynine +changing state to onehundred +changing state to end +EOF + +(cd working && nohup ifstated -dvf ./ifstated.conf > ifstated.log 2>&1) & + +sleep ${LSLEEP} + +grep ^changing working/ifstated.log > working/output.new + +kill $(pgrep ifstated) >/dev/null 2>&1 + +diff working/output.test working/output.new +case $? in +0) echo PASSED + cleanup + exit 0 + ;; +1) fail + ;; +esac