From 0b7140bbc55768f9f1d12f2cc42fa29c5fa98483 Mon Sep 17 00:00:00 2001 From: Ben Pfaff Date: Mon, 20 Aug 2012 10:29:39 -0700 Subject: [PATCH] tests: New m4 macro ON_EXIT to add a cleanup action. 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 --- tests/ofproto-macros.at | 5 ++--- tests/ofproto.at | 2 +- tests/ovs-monitor-ipsec.at | 2 +- tests/ovs-vsctl.at | 2 +- tests/ovs-xapi-sync.at | 2 +- tests/testsuite.at | 11 +++++++++++ tests/unixctl-py.at | 3 +-- tests/vlog.at | 9 ++++----- 8 files changed, 22 insertions(+), 14 deletions(-) diff --git a/tests/ofproto-macros.at b/tests/ofproto-macros.at index 944fbac0..52f19fc8 100644 --- a/tests/ofproto-macros.at +++ b/tests/ofproto-macros.at @@ -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])]) diff --git a/tests/ofproto.at b/tests/ofproto.at index e3239f98..47402f79 100644 --- a/tests/ofproto.at +++ b/tests/ofproto.at @@ -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 diff --git a/tests/ovs-monitor-ipsec.at b/tests/ovs-monitor-ipsec.at index 1a831612..e66c943d 100644 --- a/tests/ovs-monitor-ipsec.at +++ b/tests/ovs-monitor-ipsec.at @@ -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 diff --git a/tests/ovs-vsctl.at b/tests/ovs-vsctl.at index ef2c0c09..84cc272d 100644 --- a/tests/ovs-vsctl.at +++ b/tests/ovs-vsctl.at @@ -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( diff --git a/tests/ovs-xapi-sync.at b/tests/ovs-xapi-sync.at index 25acc743..29d47379 100644 --- a/tests/ovs-xapi-sync.at +++ b/tests/ovs-xapi-sync.at @@ -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 diff --git a/tests/testsuite.at b/tests/testsuite.at index e8f74987..2b4ccdb7 100644 --- a/tests/testsuite.at +++ b/tests/testsuite.at @@ -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]) diff --git a/tests/unixctl-py.at b/tests/unixctl-py.at index 15fd86be..b54d4091 100644 --- a/tests/unixctl-py.at +++ b/tests/unixctl-py.at @@ -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 diff --git a/tests/vlog.at b/tests/vlog.at index a8a947c1..957d8722 100644 --- a/tests/vlog.at +++ b/tests/vlog.at @@ -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]) -- 2.30.2