]> git.proxmox.com Git - ovs.git/blob - tests/ovs-macros.at
System tests: Enable ALGs for userspace.
[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.m4])
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 the condition without waiting.
42 ovs_wait_cond && return 0
43
44 # Try a quick sleep, so that the test completes very quickly
45 # in the normal case. POSIX doesn't require fractional times to
46 # work, so this might not work.
47 sleep 0.1
48 ovs_wait_cond && return 0
49
50 # Then wait up to 10 seconds.
51 for d in 0 1 2 3 4 5 6 7 8 9; do
52 sleep 1
53 ovs_wait_cond && return 0
54 done
55 return 1
56 }
57
58 # Prints the integers from $1 to $2, increasing by $3 (default 1) on stdout.
59 seq () {
60 while test $1 -le $2; do
61 echo $1
62 set `expr $1 + ${3-1}` $2 $3
63 done
64 }
65
66 if test "$IS_WIN32" = "yes"; then
67 pwd () {
68 command pwd -W "$@"
69 }
70
71 diff () {
72 command diff --strip-trailing-cr "$@"
73 }
74
75 # tskill is more effective than taskkill but it isn't always installed.
76 if (tskill //?) >/dev/null 2>&1; then :; else
77 tskill () { taskkill //F //PID $1 >/dev/null; }
78 fi
79
80 kill () {
81 signal=
82 retval=0
83 for arg; do
84 case $arg in
85 -*) signal=$arg ;;
86 [1-9][0-9]*)
87 # tasklist always returns 0.
88 # If pid does exist, there will be a line with the pid.
89 if tasklist //fi "PID eq $arg" | grep $arg >/dev/null; then
90 if test "X$signal" != "X-0"; then
91 tskill $arg
92 fi
93 else
94 retval=1
95 fi
96 ;;
97 esac
98 done
99 return $retval
100 }
101 fi
102
103 # parent_pid PID
104 #
105 # Prints the PID of the parent of process PID.
106 parent_pid () {
107 # Using "ps" is portable to any POSIX system, but busybox "ps" (used in
108 # e.g. Alpine Linux) is noncompliant, so we use a Linux-specific approach
109 # when it's available. We check the format of the status file to avoid
110 # the NetBSD file with the same name but different contents.
111 if egrep '^PPid:[[:space:]]*[0-9]*$' /proc/$1/status > /dev/null 2>&1; then
112 sed -n 's/^PPid: \([0-9]*\)/\1/p' /proc/$1/status
113 else
114 ps -o ppid= -p $1
115 fi
116 }
117
118 # kill_ovs_vswitchd [PID]
119 #
120 # Signal the ovs-vswitchd daemon to exit gracefully and wait for it to
121 # terminate or kill it if that takes too long.
122 #
123 # It is used to cleanup all sorts of tests and results. It can't assume
124 # any state, including the availability of PID file which can be provided.
125 kill_ovs_vswitchd () {
126 # Use provided PID or save the current PID if available.
127 TMPPID=$1
128 if test -z "$TMPPID"; then
129 TMPPID=$(cat $OVS_RUNDIR/ovs-vswitchd.pid 2>/dev/null)
130 fi
131
132 # Tell the daemon to terminate gracefully
133 ovs-appctl --timeout=10 -t ovs-vswitchd exit --cleanup 2>/dev/null
134
135 # Nothing else to be done if there is no PID
136 test -z "$TMPPID" && return
137
138 for i in 1 2 3 4 5 6 7 8 9; do
139 # Check if the daemon is alive.
140 kill -0 $TMPPID 2>/dev/null || return
141
142 # Fallback to whole number since POSIX doesn't require
143 # fractional times to work.
144 sleep 0.1 || sleep 1
145 done
146
147 # Make sure it is terminated.
148 kill $TMPPID
149 }
150
151 # Normalize the output of 'wc' to match POSIX.
152 # POSIX says 'wc' should print "%d %d %d", but GNU prints "%7d %7d %7d".
153 # POSIX says 'wc -l' should print "%d %s", but BSD prints "%8d".
154 #
155 # This fixes all of those (it will screw up filenames that contain
156 # multiple sequential spaces, but that doesn't really matter).
157 wc () {
158 command wc "$@" | tr -s ' ' ' ' | sed 's/^ *//'
159 }
160 ]
161 m4_divert_pop([PREPARE_TESTS])
162
163 m4_define([OVS_WAIT], [dnl
164 ovs_wait_cond () {
165 $1
166 }
167 if ovs_wait; then :
168 else
169 $2
170 AT_FAIL_IF([:])
171 fi
172 ])
173
174 dnl OVS_WAIT_UNTIL(COMMAND)
175 dnl
176 dnl Executes shell COMMAND in a loop until it returns
177 dnl zero return code. If COMMAND did not return
178 dnl zero code within reasonable time limit, then
179 dnl the test fails.
180 m4_define([OVS_WAIT_UNTIL], [OVS_WAIT([$1], [$2])])
181
182 dnl OVS_WAIT_WHILE(COMMAND)
183 dnl
184 dnl Executes shell COMMAND in a loop until it returns
185 dnl non-zero return code. If COMMAND did not return
186 dnl non-zero code within reasonable time limit, then
187 dnl the test fails.
188 m4_define([OVS_WAIT_WHILE],
189 [OVS_WAIT([if $1; then return 1; else return 0; fi], [$2])])
190
191 dnl OVS_APP_EXIT_AND_WAIT(DAEMON)
192 dnl
193 dnl Ask the daemon named DAEMON to exit, via ovs-appctl, and then wait for it
194 dnl to exit.
195 m4_define([OVS_APP_EXIT_AND_WAIT],
196 [AT_CHECK([test -e $OVS_RUNDIR/$1.pid])
197 TMPPID=$(cat $OVS_RUNDIR/$1.pid)
198 AT_CHECK(m4_if([$1],[ovs-vswitchd],
199 [ovs-appctl -t $1 exit --cleanup],
200 [ovs-appctl -t $1 exit]))
201 OVS_WAIT_WHILE([kill -0 $TMPPID 2>/dev/null])])
202
203 dnl OVS_APP_EXIT_AND_WAIT_BY_TARGET(TARGET, PIDFILE)
204 dnl
205 dnl Ask the daemon identified by TARGET to exit, via ovs-appctl (using the target
206 dnl argument), and then wait for it to exit.
207 m4_define([OVS_APP_EXIT_AND_WAIT_BY_TARGET],
208 [AT_CHECK([test -e $2])
209 TMPPID=$(cat $2)
210 AT_CHECK([ovs-appctl --target=$1 exit])
211 OVS_WAIT_WHILE([kill -0 $TMPPID 2>/dev/null])])
212
213 dnl on_exit "COMMAND"
214 dnl
215 dnl Add the shell COMMAND to a collection executed when the current test
216 dnl completes, as a cleanup action. (The most common use is to kill a
217 dnl daemon started by the test. This is important to prevent tests that
218 dnl start daemons from hanging at exit.)
219 dnl
220 dnl Cleanup commands are executed in the reverse order of calls to this
221 dnl function.
222 m4_divert_text([PREPARE_TESTS], [dnl
223 on_exit () {
224 (echo "$1"; cat cleanup) > cleanup.tmp
225 mv cleanup.tmp cleanup
226 }
227 ])
228
229 dnl Autoconf 2.63 compatibility verison of macro introduced in Autoconf 2.64:
230 m4_ifndef([AS_VAR_APPEND],
231 [m4_divert_text([PREPARE_TESTS],
232 [as_var_append () {
233 eval $1=\$$1\$2
234 }
235 ])
236 m4_define([AS_VAR_APPEND], [as_var_append $1 $2])])
237
238 dnl Autoconf 2.63 compatibility verison of macro introduced in Autoconf 2.64:
239 m4_ifndef([AT_CHECK_UNQUOTED],
240 [m4_define([AT_CHECK_UNQUOTED],
241 [_AT_CHECK([$1], [$2], AS_ESCAPE(m4_dquote(m4_expand([$3])), [""]),
242 AS_ESCAPE(m4_dquote(m4_expand([$4])),[""]), [$5], [$6])])])
243
244 dnl Autoconf 2.63 compatibility verison of macro introduced in Autoconf 2.64:
245 m4_ifndef([AT_SKIP_IF],
246 [m4_define([AT_SKIP_IF],
247 [AT_CHECK([($1) \
248 && exit 77 || exit 0], [0], [ignore], [ignore])])])
249
250 dnl Autoconf 2.63 compatibility verison of macro introduced in Autoconf 2.64:
251 m4_ifndef([AT_FAIL_IF],
252 [m4_define([AT_FAIL_IF],
253 [AT_CHECK([($1) \
254 && exit 99 || exit 0], [0], [ignore], [ignore])])])