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