tests: New m4 macro ON_EXIT to add a cleanup action.
authorBen Pfaff <blp@nicira.com>
Mon, 20 Aug 2012 17:29:39 +0000 (10:29 -0700)
committerBen Pfaff <blp@nicira.com>
Mon, 20 Aug 2012 19:28:05 +0000 (12:28 -0700)
Several of the tests start daemons and then need to make sure that the
daemons get killed when the test completes, even if it completes in the
middle due to an early failure.  Until now, they have been using manual
shell "trap" calls to do this.  This works well enough for simple cases,
but sometimes multiple macros start daemons in a single test, and then
each "trap" has to be carefully written to kill off the daemons for the
previously invoked macros.

This commit introduces a new macro ON_EXIT whose use is composable: each
call appends a new action to the ones already specified.

Signed-off-by: Ben Pfaff <blp@nicira.com>
tests/ofproto-macros.at
tests/ofproto.at
tests/ovs-monitor-ipsec.at
tests/ovs-vsctl.at
tests/ovs-xapi-sync.at
tests/testsuite.at
tests/unixctl-py.at
tests/vlog.at

index 944fbac..52f19fc 100644 (file)
@@ -33,7 +33,7 @@ m4_define([OVS_VSWITCHD_START],
    OVS_LOGDIR=`pwd`; export OVS_LOGDIR
    OVS_DBDIR=`pwd`; export OVS_DBDIR
    OVS_SYSCONFDIR=`pwd`; export OVS_SYSCONFDIR
-   trap 'kill `cat ovsdb-server.pid ovs-vswitchd.pid`' 0
+   ON_EXIT([kill `cat ovsdb-server.pid ovs-vswitchd.pid`])
 
    dnl Create database.
    touch .conf.db.~lock~
@@ -65,5 +65,4 @@ m4_define([OVS_VSWITCHD_START],
 
 m4_define([OVS_VSWITCHD_STOP],
   [AT_CHECK([ovs-appctl -t ovs-vswitchd exit])
-   AT_CHECK([ovs-appctl -t ovsdb-server exit])
-   trap '' 0])
+   AT_CHECK([ovs-appctl -t ovsdb-server exit])])
index e3239f9..47402f7 100644 (file)
@@ -910,7 +910,7 @@ echo n_msgs=$n_msgs
 OVS_VSWITCHD_START
 
 # Start a monitor watching the flow table, then make it block.
-trap 'kill `cat ovsdb-server.pid ovs-vswitchd.pid ovs-ofctl.pid`' 0
+ON_EXIT([kill `cat ovs-ofctl.pid`])
 ovs-ofctl monitor br0 watch: --detach --no-chdir --pidfile >monitor.log 2>&1
 AT_CAPTURE_FILE([monitor.log])
 ovs-appctl -t ovs-ofctl ofctl/block
index 1a83161..e66c943 100644 (file)
@@ -8,7 +8,7 @@ OVS_DBDIR=`pwd`; export OVS_DBDIR
 OVS_PKGDATADIR=`pwd`; export OVS_PKGDATADIR
 cp "$top_srcdir/vswitchd/vswitch.ovsschema" .
 
-trap 'kill `cat pid ovs-monitor-ipsec.pid`' 0
+ON_EXIT([kill `cat pid ovs-monitor-ipsec.pid`])
 
 mkdir etc etc/init.d etc/racoon etc/racoon/certs
 mkdir usr usr/sbin
index ef2c0c0..84cc272 100644 (file)
@@ -741,7 +741,7 @@ AT_CLEANUP
 
 AT_SETUP([database commands -- conditions])
 AT_KEYWORDS([ovs-vsctl])
-trap 'kill `cat pid`' 0
+ON_EXIT([kill `cat pid`])
 OVS_VSCTL_SETUP
 AT_CHECK(
   [RUN_OVS_VSCTL_TOGETHER(
index 25acc74..29d4737 100644 (file)
@@ -16,7 +16,7 @@ cp "$top_srcdir/vswitchd/vswitch.ovsschema" .
 cp "$top_srcdir/xenserver/usr_share_openvswitch_scripts_ovs-xapi-sync" \
    ovs-xapi-sync
 
-trap 'kill `cat pid ovs-xapi-sync.pid`' 0
+ON_EXIT([kill `cat pid ovs-xapi-sync.pid`])
 
 mkdir var var/run
 touch var/run/xapi_init_complete.cookie
index e8f7498..2b4ccdb 100644 (file)
@@ -53,6 +53,17 @@ m4_define([OVS_WAIT_UNTIL], [OVS_WAIT([$1], [$2])])
 m4_define([OVS_WAIT_WHILE],
   [OVS_WAIT([if $1; then return 1; else return 0; fi], [$2])])
 
+dnl ON_EXIT([COMMANDS])
+dnl
+dnl Adds the shell COMMANDS to a collection executed when the current test
+dnl completes, as a cleanup action.  (The most common use is to kill a
+dnl daemon started by the test.  This is important to prevent tests that
+dnl start daemons from hanging at exit.)
+m4_define([ON_EXIT], [trap '. ./cleanup' 0; cat >>cleanup <<'EOF'
+$1
+EOF
+])
+
 m4_include([tests/ovsdb-macros.at])
 m4_include([tests/ofproto-macros.at])
 
index 15fd86b..b54d409 100644 (file)
@@ -94,7 +94,7 @@ OVS_RUNDIR=`pwd`; export OVS_RUNDIR
 OVS_LOGDIR=`pwd`; export OVS_LOGDIR
 OVS_DBDIR=`pwd`; export OVS_DBDIR
 OVS_SYSCONFDIR=`pwd`; export OVS_SYSCONFDIR
-trap 'kill `cat test-unixctl.py.pid`' 0
+ON_EXIT([kill `cat test-unixctl.py.pid`])
 AT_CAPTURE_FILE([`pwd`/test-unixctl.py.log])
 AT_CHECK([$PYTHON $srcdir/test-unixctl.py --log-file --pidfile --detach])
 
@@ -159,7 +159,6 @@ sed 's/ovs-appctl/appctl.py/' stderr > experr
 AT_CHECK([PYAPPCTL -t test-unixctl.py bogus], [2], [], [experr])
 
 AT_CHECK([APPCTL -t test-unixctl.py exit])
-trap '' 0]
 AT_CLEANUP
 
 
index a8a947c..957d872 100644 (file)
@@ -109,7 +109,7 @@ OVS_RUNDIR=`pwd`; export OVS_RUNDIR
 OVS_LOGDIR=`pwd`; export OVS_LOGDIR
 OVS_DBDIR=`pwd`; export OVS_DBDIR
 OVS_SYSCONFDIR=`pwd`; export OVS_SYSCONFDIR
-trap 'kill `cat test-unixctl.py.pid`' 0
+ON_EXIT([kill `cat test-unixctl.py.pid`])
 
 AT_CAPTURE_FILE([log])
 AT_CAPTURE_FILE([log.old])
@@ -121,7 +121,6 @@ AT_CHECK([APPCTL -t test-unixctl.py log message2])
 AT_CHECK([APPCTL -t test-unixctl.py vlog/reopen])
 AT_CHECK([APPCTL -t test-unixctl.py log message3])
 AT_CHECK([APPCTL -t test-unixctl.py exit])
-trap '' 0
 
 AT_CHECK([sed 's/.*|//' log.old], [0], [dnl
 Entering run loop.
@@ -139,7 +138,7 @@ OVS_RUNDIR=`pwd`; export OVS_RUNDIR
 OVS_LOGDIR=`pwd`; export OVS_LOGDIR
 OVS_DBDIR=`pwd`; export OVS_DBDIR
 OVS_SYSCONFDIR=`pwd`; export OVS_SYSCONFDIR
-trap 'kill `cat test-unixctl.py.pid`' 0
+ON_EXIT([kill `cat test-unixctl.py.pid`])
 
 AT_CHECK([$PYTHON $srcdir/test-unixctl.py --pidfile --detach])
 
@@ -162,7 +161,7 @@ OVS_RUNDIR=`pwd`; export OVS_RUNDIR
 OVS_LOGDIR=`pwd`; export OVS_LOGDIR
 OVS_DBDIR=`pwd`; export OVS_DBDIR
 OVS_SYSCONFDIR=`pwd`; export OVS_SYSCONFDIR
-trap 'kill `cat test-unixctl.py.pid`' 0
+ON_EXIT([kill `cat test-unixctl.py.pid`])
 
 AT_CHECK([$PYTHON $srcdir/test-unixctl.py --log-file=`pwd`/log --pidfile --detach])
 AT_CHECK([APPCTL -t test-unixctl.py log message])
@@ -189,7 +188,7 @@ OVS_RUNDIR=`pwd`; export OVS_RUNDIR
 OVS_LOGDIR=`pwd`; export OVS_LOGDIR
 OVS_DBDIR=`pwd`; export OVS_DBDIR
 OVS_SYSCONFDIR=`pwd`; export OVS_SYSCONFDIR
-trap 'kill `cat test-unixctl.py.pid`' 0
+ON_EXIT([kill `cat test-unixctl.py.pid`])
 
 AT_CAPTURE_FILE([log])
 AT_CHECK([$PYTHON $srcdir/test-unixctl.py --log-file=`pwd`/log --pidfile --detach])