]> git.proxmox.com Git - mirror_ovs.git/blob - tests/ovs-macros.at
ovsdb: Use column diffs for ovsdb and raft log entries.
[mirror_ovs.git] / tests / ovs-macros.at
1 AT_TESTED([ovs-vswitchd])
2 AT_TESTED([ovs-vsctl])
3
4 m4_include([m4/compat.m4])
5
6 dnl Make AT_SETUP automatically run the ovs_init() shell function
7 dnl as the first step in every test.
8 m4_rename([AT_SETUP], [OVS_AT_SETUP])
9 m4_define([AT_SETUP], [OVS_AT_SETUP($@)
10 ovs_init
11 ])
12
13 dnl Make AT_CLEANUP check for Address Sanitizer errors as the last step
14 dnl in every test.
15 m4_rename([AT_CLEANUP], [OVS_AT_CLEANUP])
16 m4_define([AT_CLEANUP], [ovs_cleanup
17 OVS_AT_CLEANUP($@)
18 ])
19
20 dnl OVS_START_SHELL_HELPERS...OVS_END_SHELL_HELPERS may bracket shell
21 dnl function definitions that invoke AT_CHECK and other Autotest macros
22 dnl that can ordinarily be run only within AT_SETUP...AT_CLEANUP.
23 m4_define([OVS_START_SHELL_HELPERS],
24 [m4_ifdef([AT_ingroup], [m4_fatal([$0: AT_SETUP and OVS_DEFINE_SHELL_HELPERS may not nest])])
25 m4_define([AT_ingroup])
26 m4_divert_push([PREPARE_TESTS])])
27 m4_define([OVS_END_SHELL_HELPERS], [
28 m4_divert_pop([PREPARE_TESTS])
29 m4_undefine([AT_ingroup])])
30
31 m4_divert_push([PREPARE_TESTS])
32 [
33 # Set ovs_base to the base directory in which the test is running and
34 # initialize the OVS_*DIR environment variables to point to this
35 # directory.
36 ovs_init() {
37 ovs_base=`pwd`
38 trap ovs_on_exit 0
39 : > cleanup
40 ovs_setenv
41 }
42
43 # Catch testsuite error condition and cleanup test environment by tearing down
44 # all interfaces and processes spawned.
45 # User has an option to leave the test environment in error state so that system
46 # can be poked around to get more information. User can enable this option by setting
47 # environment variable OVS_PAUSE_TEST=1. User needs to press CTRL-D to resume the
48 # cleanup operation.
49 ovs_pause() {
50 echo "====================================================="
51 echo "Set following environment variable to use various ovs utilities"
52 echo "export OVS_RUNDIR=$ovs_base"
53 echo "Press ENTER to continue: "
54 read
55 }
56
57 ovs_on_exit () {
58 if [ ! -z "${OVS_PAUSE_TEST}" ] && [ -z $at_verbose ]; then
59 trap '' INT
60 ovs_pause
61 fi
62 . "$ovs_base/cleanup"
63 }
64
65 # With no parameter or an empty parameter, sets the OVS_*DIR
66 # environment variables to point to $ovs_base, the base directory in
67 # which the test is running.
68 #
69 # With a parameter, sets them to $ovs_base/$1.
70 ovs_setenv() {
71 sandbox=$1
72 ovs_dir=$ovs_base${1:+/$1}
73 OVS_RUNDIR=$ovs_dir; export OVS_RUNDIR
74 OVS_LOGDIR=$ovs_dir; export OVS_LOGDIR
75 OVS_DBDIR=$ovs_dir; export OVS_DBDIR
76 OVS_SYSCONFDIR=$ovs_dir; export OVS_SYSCONFDIR
77 OVS_PKGDATADIR=$ovs_dir; export OVS_PKGDATADIR
78 }
79
80 # Prints the integers from $1 to $2, increasing by $3 (default 1) on stdout.
81 seq () {
82 if test $# = 1; then
83 set 1 $1
84 fi
85 while test $1 -le $2; do
86 echo $1
87 set `expr $1 + ${3-1}` $2 $3
88 done
89 }
90
91 if test "$IS_WIN32" = "yes"; then
92 pwd () {
93 command pwd -W "$@"
94 }
95
96 diff () {
97 command diff --strip-trailing-cr "$@"
98 }
99
100 # tskill is more effective than taskkill but it isn't always installed.
101 if (tskill //?) >/dev/null 2>&1; then :; else
102 tskill () { taskkill //F //PID $1 >/dev/null; }
103 fi
104
105 kill () {
106 signal=
107 retval=0
108 for arg; do
109 arg=$(echo $arg | tr -d '\n\r')
110 case $arg in
111 -*) signal=$arg ;;
112 [1-9][0-9]*)
113 # tasklist always returns 0.
114 # If pid does exist, there will be a line with the pid.
115 if tasklist //fi "PID eq $arg" | grep $arg >/dev/null; then
116 if test "X$signal" != "X-0"; then
117 tskill $arg
118 fi
119 else
120 retval=1
121 fi
122 ;;
123 esac
124 done
125 return $retval
126 }
127 fi
128
129 # parent_pid PID
130 #
131 # Prints the PID of the parent of process PID.
132 parent_pid () {
133 # Using "ps" is portable to any POSIX system, but busybox "ps" (used in
134 # e.g. Alpine Linux) is noncompliant, so we use a Linux-specific approach
135 # when it's available. We check the format of the status file to avoid
136 # the NetBSD file with the same name but different contents.
137 if egrep '^PPid:[[:space:]]*[0-9]*$' /proc/$1/status > /dev/null 2>&1; then
138 sed -n 's/^PPid: \([0-9]*\)/\1/p' /proc/$1/status
139 else
140 ps -o ppid= -p $1
141 fi
142 }
143
144 # kill_ovs_vswitchd [PID]
145 #
146 # Signal the ovs-vswitchd daemon to exit gracefully and wait for it to
147 # terminate or kill it if that takes too long.
148 #
149 # It is used to cleanup all sorts of tests and results. It can't assume
150 # any state, including the availability of PID file which can be provided.
151 kill_ovs_vswitchd () {
152 # Use provided PID or save the current PID if available.
153 TMPPID=$1
154 if test -z "$TMPPID"; then
155 TMPPID=$(cat $OVS_RUNDIR/ovs-vswitchd.pid 2>/dev/null)
156 fi
157
158 # Tell the daemon to terminate gracefully
159 ovs-appctl -t ovs-vswitchd exit --cleanup 2>/dev/null
160
161 # Nothing else to be done if there is no PID
162 test -z "$TMPPID" && return
163
164 for i in 1 2 3 4 5 6 7 8 9; do
165 # Check if the daemon is alive.
166 kill -0 $TMPPID 2>/dev/null || return
167
168 # Fallback to whole number since POSIX doesn't require
169 # fractional times to work.
170 sleep 0.1 || sleep 1
171 done
172
173 # Make sure it is terminated.
174 kill $TMPPID
175 }
176
177 # Normalize the output of 'wc' to match POSIX.
178 # POSIX says 'wc' should print "%d %d %d", but GNU prints "%7d %7d %7d".
179 # POSIX says 'wc -l' should print "%d %s", but BSD prints "%8d".
180 #
181 # This fixes all of those (it will screw up filenames that contain
182 # multiple sequential spaces, but that doesn't really matter).
183 wc () {
184 command wc "$@" | tr -s ' ' ' ' | sed 's/^ *//'
185 }
186
187 uuidfilt () {
188 $PYTHON3 "$top_srcdir"/tests/uuidfilt.py "$@"
189 }
190
191 # run_as PROGRAM_NAME COMMAND [ARG...]
192 #
193 # Runs a command with argv[0] set to PROGRAM_NAME, if possible, in a
194 # subshell. Most utilities print argc[0] as part of their messages,
195 # so this makes it easier to figure out which particular utility
196 # prints a message if a bunch of identical processes are running.
197 #
198 # Not all shells support "exec -a NAME", so test for it.
199 if (exec -a myname true 2>/dev/null); then
200 run_as () {
201 (exec -a "$@")
202 }
203 else
204 run_as () {
205 shift
206 (exec "$@")
207 }
208 fi
209 ]
210 m4_divert_pop([PREPARE_TESTS])
211
212 OVS_START_SHELL_HELPERS
213 ovs_cleanup() {
214 if test "$(echo asan.*)" != 'asan.*'; then
215 echo "Address Sanitizer reported errors in:" asan.*
216 cat asan.*
217 AT_FAIL_IF([:])
218 fi
219 }
220
221 ovs_wait () {
222 echo "$1: waiting $2..." >&AS_MESSAGE_LOG_FD
223
224 # First try the condition without waiting.
225 if ovs_wait_cond; then echo "$1: wait succeeded immediately" >&AS_MESSAGE_LOG_FD; return 0; fi
226
227 # Try a quick sleep, so that the test completes very quickly
228 # in the normal case. POSIX doesn't require fractional times to
229 # work, so this might not work.
230 sleep 0.1
231 if ovs_wait_cond; then echo "$1: wait succeeded quickly" >&AS_MESSAGE_LOG_FD; return 0; fi
232
233 # Then wait up to OVS_CTL_TIMEOUT seconds.
234 local d
235 for d in `seq 1 "$OVS_CTL_TIMEOUT"`; do
236 sleep 1
237 if ovs_wait_cond; then echo "$1: wait succeeded after $d seconds" >&AS_MESSAGE_LOG_FD; return 0; fi
238 done
239
240 echo "$1: wait failed after $d seconds" >&AS_MESSAGE_LOG_FD
241 ovs_wait_failed
242 AT_FAIL_IF([:])
243 }
244 OVS_END_SHELL_HELPERS
245 m4_define([OVS_WAIT], [dnl
246 ovs_wait_cond () {
247 $1
248 }
249 ovs_wait_failed () {
250 :
251 $2
252 }
253 ovs_wait "AS_ESCAPE([$3])" "AS_ESCAPE([$4])"
254 ])
255
256 dnl OVS_WAIT_UNTIL(COMMAND, [IF-FAILED])
257 dnl
258 dnl Executes shell COMMAND in a loop until it returns zero. If COMMAND does
259 dnl not return zero within a reasonable time limit, executes the commands
260 dnl in IF-FAILED (if provided) and fails the test.
261 m4_define([OVS_WAIT_UNTIL],
262 [OVS_WAIT([$1], [$2], [AT_LINE], [until $1])])
263
264 dnl OVS_WAIT_WHILE(COMMAND, [IF-FAILED])
265 dnl
266 dnl Executes shell COMMAND in a loop until it returns nonzero. If COMMAND does
267 dnl not return nonzero within a reasonable time limit, executes the commands
268 dnl in IF-FAILED (if provided) and fails the test.
269 m4_define([OVS_WAIT_WHILE],
270 [OVS_WAIT([if $1; then return 1; else return 0; fi], [$2],
271 [AT_LINE], [while $1])])
272
273 dnl OVS_APP_EXIT_AND_WAIT(DAEMON)
274 dnl
275 dnl Ask the daemon named DAEMON to exit, via ovs-appctl, and then wait for it
276 dnl to exit.
277 m4_define([OVS_APP_EXIT_AND_WAIT],
278 [AT_CHECK([test -e $OVS_RUNDIR/$1.pid])
279 TMPPID=$(cat $OVS_RUNDIR/$1.pid)
280 AT_CHECK(m4_if([$1],[ovs-vswitchd],
281 [ovs-appctl -t $1 exit --cleanup],
282 [ovs-appctl -t $1 exit]))
283 OVS_WAIT_WHILE([kill -0 $TMPPID 2>/dev/null])])
284
285 dnl OVS_APP_EXIT_AND_WAIT_BY_TARGET(TARGET, PIDFILE)
286 dnl
287 dnl Ask the daemon identified by TARGET to exit, via ovs-appctl (using the target
288 dnl argument), and then wait for it to exit.
289 m4_define([OVS_APP_EXIT_AND_WAIT_BY_TARGET],
290 [AT_CHECK([test -e $2])
291 TMPPID=$(cat $2)
292 AT_CHECK([ovs-appctl --target=$1 exit])
293 OVS_WAIT_WHILE([kill -0 $TMPPID 2>/dev/null])])
294
295 dnl on_exit "COMMAND"
296 dnl
297 dnl Add the shell COMMAND to a collection executed when the current test
298 dnl completes, as a cleanup action. (The most common use is to kill a
299 dnl daemon started by the test. This is important to prevent tests that
300 dnl start daemons from hanging at exit.)
301 dnl
302 dnl Cleanup commands are executed in the reverse order of calls to this
303 dnl function.
304 m4_divert_text([PREPARE_TESTS], [dnl
305 on_exit () {
306 (echo "$1"; cat cleanup) > cleanup.tmp
307 mv cleanup.tmp cleanup
308 }
309 ])
310
311 dnl Autoconf 2.63 compatibility verison of macro introduced in Autoconf 2.64:
312 m4_ifndef([AS_VAR_APPEND],
313 [m4_divert_text([PREPARE_TESTS],
314 [as_var_append () {
315 eval $1=\$$1\$2
316 }
317 ])
318 m4_define([AS_VAR_APPEND], [as_var_append $1 $2])])
319
320 dnl Autoconf 2.63 compatibility verison of macro introduced in Autoconf 2.64:
321 m4_ifndef([AT_CHECK_UNQUOTED],
322 [m4_define([AT_CHECK_UNQUOTED],
323 [_AT_CHECK([$1], [$2], AS_ESCAPE(m4_dquote(m4_expand([$3])), [""]),
324 AS_ESCAPE(m4_dquote(m4_expand([$4])),[""]), [$5], [$6])])])
325
326 dnl Autoconf 2.63 compatibility verison of macro introduced in Autoconf 2.64:
327 m4_ifndef([AT_SKIP_IF],
328 [m4_define([AT_SKIP_IF],
329 [AT_CHECK([($1) \
330 && exit 77 || exit 0], [0], [ignore], [ignore])])])
331
332 dnl Autoconf 2.63 compatibility verison of macro introduced in Autoconf 2.64:
333 m4_ifndef([AT_FAIL_IF],
334 [m4_define([AT_FAIL_IF],
335 [AT_CHECK([($1) \
336 && exit 99 || exit 0], [0], [ignore], [ignore])])])
337
338 dnl Certain Linux distributions, like CentOS, have default iptable rules
339 dnl to reject input traffic from bridges such as br-underlay.
340 dnl Add a rule to always accept the traffic.
341 m4_define([IPTABLES_ACCEPT],
342 [AT_CHECK([iptables -I INPUT 1 -i $1 -j ACCEPT])
343 on_exit 'iptables -D INPUT 1 -i $1'])