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