]> git.proxmox.com Git - mirror_ovs.git/blob - tests/ovs-macros.at
tests: Automatically initialize OVS_*DIR vars when tests begin.
[mirror_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 OVS_RUNDIR=$ovs_base; export OVS_RUNDIR
21 OVS_LOGDIR=$ovs_base; export OVS_LOGDIR
22 OVS_DBDIR=$ovs_base; export OVS_DBDIR
23 OVS_SYSCONFDIR=$ovs_base; export OVS_SYSCONFDIR
24 OVS_PKGDATADIR=$ovs_base; export OVS_PKGDATADIR
25 }
26
27 ovs_wait () {
28 # First try a quick sleep, so that the test completes very quickly
29 # in the normal case. POSIX doesn't require fractional times to
30 # work, so this might not work.
31 sleep 0.1
32 ovs_wait_cond && exit 0
33 # Then wait up to 10 seconds.
34 for d in 0 1 2 3 4 5 6 7 8 9; do
35 sleep 1
36 ovs_wait_cond && exit 0
37 done
38 exit 1
39 }
40
41 # Prints the integers from $1 to $2, increasing by $3 (default 1) on stdout.
42 seq () {
43 while test $1 -le $2; do
44 echo $1
45 set `expr $1 + ${3-1}` $2 $3
46 done
47 }
48
49 if test "$IS_WIN32" = "yes"; then
50 pwd () {
51 command pwd -W "$@"
52 }
53
54 diff () {
55 command diff --strip-trailing-cr "$@"
56 }
57
58 # tskill is more effective than taskkill but it isn't always installed.
59 if (tskill //?) >/dev/null 2>&1; then :; else
60 tskill () { taskkill //F //PID $1 >/dev/null; }
61 fi
62
63 kill () {
64 signal=
65 retval=0
66 for arg; do
67 case $arg in
68 -*) signal=$arg ;;
69 [1-9][0-9]*)
70 # tasklist always returns 0.
71 # If pid does exist, there will be a line with the pid.
72 if tasklist //fi "PID eq $arg" | grep $arg >/dev/null; then
73 if test "X$signal" != "X-0"; then
74 tskill $arg
75 fi
76 else
77 retval=1
78 fi
79 ;;
80 esac
81 done
82 return $retval
83 }
84 fi
85 ]
86 m4_divert_pop([PREPARE_TESTS])
87
88 m4_define([OVS_WAIT],
89 [AT_CHECK(
90 [ovs_wait_cond () { $1
91 }
92 ovs_wait], [0], [ignore], [ignore], [$2])])
93 m4_define([OVS_WAIT_UNTIL], [OVS_WAIT([$1], [$2])])
94 m4_define([OVS_WAIT_WHILE],
95 [OVS_WAIT([if $1; then return 1; else return 0; fi], [$2])])
96
97 dnl OVS_APP_EXIT_AND_WAIT(DAEMON)
98 dnl
99 dnl Ask the daemon named DAEMON to exit, via ovs-appctl, and then waits for it
100 dnl to exit.
101 m4_define([OVS_APP_EXIT_AND_WAIT],
102 [ovs-appctl -t $1 exit
103 OVS_WAIT_WHILE([test -e $1.pid])])
104
105 m4_define([ON_EXIT__], [trap '. ./cleanup' 0; cat - cleanup << $2 > __cleanup
106 $1
107 EOF
108 mv __cleanup cleanup
109 ])
110
111 dnl ON_EXIT([COMMANDS])
112 dnl ON_EXIT_UNQUOTED([COMMANDS])
113 dnl
114 dnl Add the shell COMMANDS to a collection executed when the current test
115 dnl completes, as a cleanup action. (The most common use is to kill a
116 dnl daemon started by the test. This is important to prevent tests that
117 dnl start daemons from hanging at exit.)
118 dnl
119 dnl The only difference between ON_EXIT and ON_EXIT_UNQUOTED is that only the
120 dnl latter performs shell variable (e.g. $var) substitution, command
121 dnl substitution (e.g. `command`), and backslash escaping (e.g. \\ becomes \)
122 dnl in COMMANDS at the time that ON_EXIT_UNQUOTED is encountered. ON_EXIT,
123 dnl in contrast, copies the literal COMMANDS and only executes shell expansion
124 dnl at cleanup time.
125 dnl
126 dnl Cleanup commands are executed in the reverse order of execution of
127 dnl these macros.
128 m4_define([ON_EXIT], [ON_EXIT__([$1], ['EOF'])])
129 m4_define([ON_EXIT_UNQUOTED], [ON_EXIT__([$1], [EOF])])