datapath: Add support for 4.2 kernel.
[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 dnl Make AT_SETUP automatically run the ovs_init() shell function
8 dnl as the first step in every test.
9 m4_rename([AT_SETUP], [OVS_AT_SETUP])
10 m4_define([AT_SETUP], [OVS_AT_SETUP($@)
11 ovs_init
12 ])
13 m4_divert_push([PREPARE_TESTS])
14 [
15 # Set ovs_base to the base directory in which the test is running and
16 # initialize the OVS_*DIR environment variables to point to this
17 # directory.
18 ovs_init() {
19     ovs_base=`pwd`
20     trap '. "$ovs_base/cleanup"' 0
21     : > cleanup
22     OVS_RUNDIR=$ovs_base; export OVS_RUNDIR
23     OVS_LOGDIR=$ovs_base; export OVS_LOGDIR
24     OVS_DBDIR=$ovs_base; export OVS_DBDIR
25     OVS_SYSCONFDIR=$ovs_base; export OVS_SYSCONFDIR
26     OVS_PKGDATADIR=$ovs_base; export OVS_PKGDATADIR
27 }
28
29 ovs_wait () {
30     # First try a quick sleep, so that the test completes very quickly
31     # in the normal case.  POSIX doesn't require fractional times to
32     # work, so this might not work.
33     sleep 0.1
34     ovs_wait_cond && exit 0
35     # Then wait up to 10 seconds.
36     for d in 0 1 2 3 4 5 6 7 8 9; do
37         sleep 1
38         ovs_wait_cond && exit 0
39     done
40     exit 1
41 }
42
43 # Prints the integers from $1 to $2, increasing by $3 (default 1) on stdout.
44 seq () {
45     while test $1 -le $2; do
46         echo $1
47         set `expr $1 + ${3-1}` $2 $3
48     done
49 }
50
51 if test "$IS_WIN32" = "yes"; then
52     pwd () {
53         command pwd -W "$@"
54     }
55
56     diff () {
57         command diff --strip-trailing-cr "$@"
58     }
59
60     # tskill is more effective than taskkill but it isn't always installed.
61     if (tskill //?) >/dev/null 2>&1; then :; else
62         tskill () { taskkill //F //PID $1 >/dev/null; }
63     fi
64
65     kill () {
66         signal=
67         retval=0
68         for arg; do
69             case $arg in
70             -*) signal=$arg ;;
71             [1-9][0-9]*)
72                 # tasklist always returns 0.
73                 # If pid does exist, there will be a line with the pid.
74                 if tasklist //fi "PID eq $arg" | grep $arg >/dev/null; then
75                     if test "X$signal" != "X-0"; then
76                         tskill $arg
77                     fi
78                 else
79                     retval=1
80                 fi
81                 ;;
82             esac
83         done
84         return $retval
85     }
86 fi
87 ]
88 m4_divert_pop([PREPARE_TESTS])
89
90 m4_define([OVS_WAIT],
91   [AT_CHECK(
92      [ovs_wait_cond () { $1
93 }
94 ovs_wait], [0], [ignore], [ignore], [$2])])
95 m4_define([OVS_WAIT_UNTIL], [OVS_WAIT([$1], [$2])])
96 m4_define([OVS_WAIT_WHILE],
97   [OVS_WAIT([if $1; then return 1; else return 0; fi], [$2])])
98
99 dnl OVS_APP_EXIT_AND_WAIT(DAEMON)
100 dnl
101 dnl Ask the daemon named DAEMON to exit, via ovs-appctl, and then waits for it
102 dnl to exit.
103 m4_define([OVS_APP_EXIT_AND_WAIT],
104   [ovs-appctl -t $1 exit
105    OVS_WAIT_WHILE([test -e $1.pid])])
106
107 dnl on_exit "COMMAND"
108 dnl
109 dnl Add the shell COMMAND to a collection executed when the current test
110 dnl completes, as a cleanup action.  (The most common use is to kill a
111 dnl daemon started by the test.  This is important to prevent tests that
112 dnl start daemons from hanging at exit.)
113 dnl
114 dnl Cleanup commands are executed in the reverse order of calls to this
115 dnl function.
116 m4_divert_text([PREPARE_TESTS], [dnl
117 on_exit () {
118     (echo "$1"; cat cleanup) > cleanup.tmp
119     mv cleanup.tmp cleanup
120 }
121 ])