test: Reverse the order of commands added by ON_EXIT macro
[cascardo/ovs.git] / tests / ovs-macros.at
1 AT_TESTED([ovs-vswitchd])
2 AT_TESTED([ovs-vsctl])
3 AT_TESTED([perl])
4
5 m4_include([m4/compat.at])
6
7 m4_divert_push([PREPARE_TESTS])
8 [
9 ovs_wait () {
10     # First try a quick sleep, so that the test completes very quickly
11     # in the normal case.  POSIX doesn't require fractional times to
12     # work, so this might not work.
13     sleep 0.1
14     ovs_wait_cond && exit 0
15     # Then wait up to 10 seconds.
16     for d in 0 1 2 3 4 5 6 7 8 9; do
17         sleep 1
18         ovs_wait_cond && exit 0
19     done
20     exit 1
21 }
22
23 # Prints the integers from $1 to $2, increasing by $3 (default 1) on stdout.
24 seq () {
25     while test $1 -le $2; do
26         echo $1
27         set `expr $1 + ${3-1}` $2 $3
28     done
29 }
30
31 if test "$IS_WIN32" = "yes"; then
32     pwd () {
33         command pwd -W "$@"
34     }
35
36     diff () {
37         command diff --strip-trailing-cr "$@"
38     }
39
40     kill () {
41         case "$1" in
42             -0)
43                 shift
44                 for i in $*; do
45                     # tasklist will always have return code 0.
46                     # If pid does exist, there will be a line with the pid.
47                     if tasklist //fi "PID eq $i" | grep $i >/dev/null; then
48                         :
49                     else
50                         return 1
51                     fi
52                 done
53                 return 0
54                 ;;
55             -[1-9]*)
56                 shift
57                 for i in $*; do
58                     taskkill //F //PID $i >/dev/null
59                 done
60                 ;;
61             [1-9][0-9]*)
62                 for i in $*; do
63                     taskkill //F //PID $i >/dev/null
64                 done
65                 ;;
66         esac
67     }
68 fi
69 ]
70 m4_divert_pop([PREPARE_TESTS])
71
72 m4_define([OVS_WAIT],
73   [AT_CHECK(
74      [ovs_wait_cond () { $1
75 }
76 ovs_wait], [0], [ignore], [ignore], [$2])])
77 m4_define([OVS_WAIT_UNTIL], [OVS_WAIT([$1], [$2])])
78 m4_define([OVS_WAIT_WHILE],
79   [OVS_WAIT([if $1; then return 1; else return 0; fi], [$2])])
80
81 dnl OVS_APP_EXIT_AND_WAIT(DAEMON)
82 dnl
83 dnl Ask the daemon named DAEMON to exit, via ovs-appctl, and then waits for it
84 dnl to exit.
85 m4_define([OVS_APP_EXIT_AND_WAIT],
86   [ovs-appctl -t $1 exit
87    OVS_WAIT_WHILE([test -e $1.pid])])
88
89 dnl ON_EXIT([COMMANDS])
90 dnl
91 dnl Adds the shell COMMANDS to a collection executed when the current test
92 dnl completes, as a cleanup action.  (The most common use is to kill a
93 dnl daemon started by the test.  This is important to prevent tests that
94 dnl start daemons from hanging at exit.)
95 dnl The commands will be added will be tht first one to excute.
96 m4_define([ON_EXIT], [trap '. ./cleanup' 0; cat - cleanup << 'EOF' > __cleanup
97 $1
98 EOF
99 mv __cleanup cleanup
100 ])