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