daemon-py.at: Run tests with Python 2 and 3.
[cascardo/ovs.git] / tests / daemon-py.at
1 AT_BANNER([daemon unit tests - Python])
2
3 m4_define([DAEMON_PYN],
4   [AT_SETUP([daemon - $1])
5    AT_SKIP_IF([test $2 = no])
6    AT_KEYWORDS([python daemon])
7    AT_CAPTURE_FILE([pid])
8    AT_CAPTURE_FILE([expected])
9    # Start the daemon and wait for the pidfile to get created
10    # and that its contents are the correct pid.
11    AT_CHECK([$3 $srcdir/test-daemon.py --pidfile=`pwd`/pid& echo $! > expected], [0])
12    OVS_WAIT_UNTIL([test -s pid], [kill `cat expected`])
13    AT_CHECK(
14      [pid=`cat pid` && expected=`cat expected` && test "$pid" = "$expected"],
15      [0], [], [], [kill `cat expected`])
16    AT_CHECK([kill -0 `cat pid`], [0], [], [], [kill `cat expected`])
17    # Kill the daemon and make sure that the pidfile gets deleted.
18    kill `cat expected`
19    OVS_WAIT_WHILE([kill -0 `cat expected`])
20    AT_CHECK([test ! -e pid])
21    AT_CLEANUP])
22
23 DAEMON_PYN([Python2], [$HAVE_PYTHON], [$PYTHON])
24 DAEMON_PYN([Python3], [$HAVE_PYTHON3], [$PYTHON3])
25
26 m4_define([DAEMON_MONITOR_PYN],
27   [AT_SETUP([daemon --monitor - $1])
28    AT_SKIP_IF([test $2 = no])
29    AT_CAPTURE_FILE([pid])
30    AT_CAPTURE_FILE([parent])
31    AT_CAPTURE_FILE([parentpid])
32    AT_CAPTURE_FILE([newpid])
33    # Start the daemon and wait for the pidfile to get created.
34    AT_CHECK([$3 $srcdir/test-daemon.py --pidfile=`pwd`/pid --monitor& echo $! > parent], [0])
35    OVS_WAIT_UNTIL([test -s pid], [kill `cat parent`])
36    # Check that the pidfile names a running process,
37    # and that the parent process of that process is our child process.
38    AT_CHECK([kill -0 `cat pid`], [0], [], [], [kill `cat parent`])
39    AT_CHECK([ps -o ppid= -p `cat pid` > parentpid],
40      [0], [], [], [kill `cat parent`])
41    AT_CHECK(
42      [parentpid=`cat parentpid` &&
43       parent=`cat parent` &&
44       test $parentpid = $parent],
45      [0], [], [], [kill `cat parent`])
46    # Kill the daemon process, making it look like a segfault,
47    # and wait for a new child process to get spawned.
48    AT_CHECK([cp pid oldpid], [0], [], [], [kill `cat parent`])
49    AT_CHECK([kill -SEGV `cat pid`], [0], [], [ignore], [kill `cat parent`])
50    OVS_WAIT_WHILE([kill -0 `cat oldpid`], [kill `cat parent`])
51    OVS_WAIT_UNTIL([test -s pid && test `cat pid` != `cat oldpid`],
52      [kill `cat parent`])
53    AT_CHECK([cp pid newpid], [0], [], [], [kill `cat parent`])
54    # Check that the pidfile names a running process,
55    # and that the parent process of that process is our child process.
56    AT_CHECK([ps -o ppid= -p `cat pid` > parentpid],
57      [0], [], [], [kill `cat parent`])
58    AT_CHECK(
59      [parentpid=`cat parentpid` &&
60       parent=`cat parent` &&
61       test $parentpid = $parent],
62      [0], [], [], [kill `cat parent`])
63    # Kill the daemon process with SIGTERM, and wait for the daemon
64    # and the monitor processes to go away and the pidfile to get deleted.
65    AT_CHECK([kill `cat pid`], [0], [], [ignore], [kill `cat parent`])
66    OVS_WAIT_WHILE([kill -0 `cat parent` || kill -0 `cat newpid` || test -e pid],
67      [kill `cat parent`])
68    AT_CLEANUP])
69
70 DAEMON_MONITOR_PYN([Python2], [$HAVE_PYTHON], [$PYTHON])
71 DAEMON_MONITOR_PYN([Python3], [$HAVE_PYTHON3], [$PYTHON3])
72
73 m4_define([DAEMON_MONITOR_RESTART_PYN],
74   [AT_SETUP([daemon --monitor restart exit code - $1])
75    AT_SKIP_IF([test $2 = no])
76    AT_CAPTURE_FILE([pid])
77    AT_CAPTURE_FILE([parent])
78    AT_CAPTURE_FILE([parentpid])
79    AT_CAPTURE_FILE([newpid])
80    # Start the daemon and wait for the pidfile to get created.
81    AT_CHECK([$3 $srcdir/test-daemon.py --pidfile=`pwd`/pid --monitor& echo $! > parent], [0])
82    OVS_WAIT_UNTIL([test -s pid], [kill `cat parent`])
83    # Check that the pidfile names a running process,
84    # and that the parent process of that process is our child process.
85    AT_CHECK([kill -0 `cat pid`], [0], [], [], [kill `cat parent`])
86    AT_CHECK([ps -o ppid= -p `cat pid` > parentpid],
87      [0], [], [], [kill `cat parent`])
88    AT_CHECK(
89      [parentpid=`cat parentpid` &&
90       parent=`cat parent` &&
91       test $parentpid = $parent],
92      [0], [], [], [kill `cat parent`])
93    # HUP the daemon process causing it to throw an exception,
94    # and wait for a new child process to get spawned.
95    AT_CHECK([cp pid oldpid], [0], [], [], [kill `cat parent`])
96    AT_CHECK([kill -HUP `cat pid`], [0], [], [ignore], [kill `cat parent`])
97    OVS_WAIT_WHILE([kill -0 `cat oldpid`], [kill `cat parent`])
98    OVS_WAIT_UNTIL([test -s pid && test `cat pid` != `cat oldpid`],
99      [kill `cat parent`])
100    AT_CHECK([cp pid newpid], [0], [], [], [kill `cat parent`])
101    # Check that the pidfile names a running process,
102    # and that the parent process of that process is our child process.
103    AT_CHECK([ps -o ppid= -p `cat pid` > parentpid],
104      [0], [], [], [kill `cat parent`])
105    AT_CHECK(
106      [parentpid=`cat parentpid` &&
107       parent=`cat parent` &&
108       test $parentpid = $parent],
109      [0], [], [], [kill `cat parent`])
110    # Kill the daemon process with SIGTERM, and wait for the daemon
111    # and the monitor processes to go away and the pidfile to get deleted.
112    AT_CHECK([kill `cat pid`], [0], [], [ignore], [kill `cat parent`])
113    OVS_WAIT_WHILE([kill -0 `cat parent` || kill -0 `cat newpid` || test -e pid],
114      [kill `cat parent`])
115    AT_CLEANUP])
116
117 DAEMON_MONITOR_RESTART_PYN([Python2], [$HAVE_PYTHON], [$PYTHON])
118 DAEMON_MONITOR_RESTART_PYN([Python3], [$HAVE_PYTHON3], [$PYTHON3])
119
120 m4_define([DAEMON_DETACH_PYN],
121   [AT_SETUP([daemon --detach - $1])
122    AT_SKIP_IF([test $2 = no])
123    AT_CAPTURE_FILE([pid])
124    # Start the daemon and make sure that the pidfile exists immediately.
125    # We don't wait for the pidfile to get created because the daemon is
126    # supposed to do so before the parent exits.
127    AT_CHECK([$3 $srcdir/test-daemon.py --pidfile=`pwd`/pid --detach], [0])
128    AT_CHECK([test -s pid])
129    AT_CHECK([kill -0 `cat pid`])
130    # Kill the daemon and make sure that the pidfile gets deleted.
131    cp pid saved-pid
132    kill `cat pid`
133    OVS_WAIT_WHILE([kill -0 `cat saved-pid`])
134    AT_CHECK([test ! -e pid])
135    AT_CLEANUP])
136
137 DAEMON_DETACH_PYN([Python2], [$HAVE_PYTHON], [$PYTHON])
138 DAEMON_DETACH_PYN([Python3], [$HAVE_PYTHON3], [$PYTHON3])
139
140 m4_define([CHECK],
141  [AT_CHECK([$1], [$2], [$3], [$4], [kill `cat daemon monitor`])])
142 m4_define([DAEMON_DETACH_MONITOR_PYN],
143   [AT_SETUP([daemon --detach --monitor - $1])
144    AT_SKIP_IF([test $2 = no])
145    AT_CAPTURE_FILE([daemon])
146    AT_CAPTURE_FILE([olddaemon])
147    AT_CAPTURE_FILE([newdaemon])
148    AT_CAPTURE_FILE([monitor])
149    AT_CAPTURE_FILE([newmonitor])
150    AT_CAPTURE_FILE([init])
151    # Start the daemon and make sure that the pidfile exists immediately.
152    # We don't wait for the pidfile to get created because the daemon is
153    # supposed to do so before the parent exits.
154    AT_CHECK([$3 $srcdir/test-daemon.py --pidfile=`pwd`/daemon --detach --monitor], [0])
155    AT_CHECK([test -s daemon])
156    # Check that the pidfile names a running process,
157    # and that the parent process of that process is a running process,
158    # and that the parent process of that process is init.
159    CHECK([kill -0 `cat daemon`])
160    CHECK([ps -o ppid= -p `cat daemon` > monitor])
161    CHECK([kill -0 `cat monitor`])
162    CHECK([ps -o ppid= -p `cat monitor` > init])
163    CHECK([test `cat init` != $$])
164    # Kill the daemon process, making it look like a segfault,
165    # and wait for a new daemon process to get spawned.
166    CHECK([cp daemon olddaemon])
167    CHECK([kill -SEGV `cat daemon`], [0], [ignore], [ignore])
168    OVS_WAIT_WHILE([kill -0 `cat olddaemon`], [kill `cat olddaemon daemon`])
169    OVS_WAIT_UNTIL([test -s daemon && test `cat daemon` != `cat olddaemon`],
170      [kill `cat olddaemon daemon`])
171    CHECK([cp daemon newdaemon])
172    # Check that the pidfile names a running process,
173    # and that the parent process of that process is our child process.
174    CHECK([kill -0 `cat daemon`])
175    CHECK([diff olddaemon newdaemon], [1], [ignore])
176    CHECK([ps -o ppid= -p `cat daemon` > newmonitor])
177    CHECK([diff monitor newmonitor])
178    CHECK([kill -0 `cat newmonitor`])
179    CHECK([ps -o ppid= -p `cat newmonitor` > init])
180    CHECK([test `cat init` != $$])
181    # Kill the daemon process with SIGTERM, and wait for the daemon
182    # and the monitor processes to go away and the pidfile to get deleted.
183    CHECK([kill `cat daemon`], [0], [], [ignore])
184    OVS_WAIT_WHILE(
185      [kill -0 `cat monitor` || kill -0 `cat newdaemon` || test -e daemon],
186      [kill `cat monitor newdaemon`])
187    AT_CLEANUP])
188
189 DAEMON_DETACH_MONITOR_PYN([Python2], [$HAVE_PYTHON], [$PYTHON])
190 DAEMON_DETACH_MONITOR_PYN([Python3], [$HAVE_PYTHON3], [$PYTHON3])
191 m4_undefine([CHECK])
192
193 m4_define([DAEMON_DETACH_ERRORS_PYN],
194   [AT_SETUP([daemon --detach startup errors - $1])
195    AT_SKIP_IF([test $2 = no])
196    AT_CAPTURE_FILE([pid])
197    AT_CHECK([$3 $srcdir/test-daemon.py --pidfile=`pwd`/pid --detach --bail], [1], [], [stderr])
198    AT_CHECK([grep 'test-daemon.py: exiting after daemonize_start() as requested' stderr],
199      [0], [ignore], [])
200    AT_CHECK([test ! -s pid])
201    AT_CLEANUP])
202
203 DAEMON_DETACH_ERRORS_PYN([Python2], [$HAVE_PYTHON], [$PYTHON])
204 DAEMON_DETACH_ERRORS_PYN([Python3], [$HAVE_PYTHON3], [$PYTHON3])
205
206 m4_define([DAEMON_DETACH_MONITOR_ERRORS_PYN],
207   [AT_SETUP([daemon --detach --monitor startup errors - $1])
208    AT_SKIP_IF([test $2 = no])
209    AT_CAPTURE_FILE([pid])
210    AT_CHECK([$3 $srcdir/test-daemon.py --pidfile=`pwd`/pid --detach --monitor --bail], [1], [], [stderr])
211    AT_CHECK([grep 'test-daemon.py: exiting after daemonize_start() as requested' stderr],
212      [0], [ignore], [])
213    AT_CHECK([test ! -s pid])
214    AT_CLEANUP])
215
216 DAEMON_DETACH_MONITOR_ERRORS_PYN([Python2], [$HAVE_PYTHON], [$PYTHON])
217 DAEMON_DETACH_MONITOR_ERRORS_PYN([Python3], [$HAVE_PYTHON3], [$PYTHON3])
218
219 m4_define([DAEMON_DETACH_CLOSES_FDS_PYN],
220   [AT_SETUP([daemon --detach closes standard fds - $1])
221    AT_SKIP_IF([test $2 = no])
222    AT_CAPTURE_FILE([pid])
223    AT_CAPTURE_FILE([status])
224    AT_CAPTURE_FILE([stderr])
225    AT_CHECK([(yes 2>stderr; echo $? > status) | $3 $srcdir/test-daemon.py --pidfile=`pwd`/pid --detach], [0], [], [])
226    AT_CHECK([kill `cat pid`])
227    AT_CHECK([test -s status])
228    if grep '[[bB]]roken pipe' stderr >/dev/null 2>&1; then
229      # Something in the environment caused SIGPIPE to be ignored, but
230      # 'yes' at least told us that it got EPIPE.  Good enough; we know
231      # that stdout was closed.
232      :
233    else
234      # Otherwise make sure that 'yes' died from SIGPIPE.
235      AT_CHECK([kill -l `cat status`], [0], [PIPE
236 ])
237    fi
238    AT_CLEANUP])
239
240 DAEMON_DETACH_CLOSES_FDS_PYN([Python2], [$HAVE_PYTHON], [$PYTHON])
241 DAEMON_DETACH_CLOSES_FDS_PYN([Python3], [$HAVE_PYTHON3], [$PYTHON3])
242
243 m4_define([DAEMON_DETACH_MONITOR_CLOSES_FDS_PYN],
244   [AT_SETUP([daemon --detach --monitor closes standard fds - $1])
245    AT_SKIP_IF([test $2 = no])
246    AT_CAPTURE_FILE([pid])
247    AT_CAPTURE_FILE([status])
248    AT_CAPTURE_FILE([stderr])
249    OVSDB_INIT([db])
250    AT_CHECK([(yes 2>stderr; echo $? > status) | $3 $srcdir/test-daemon.py --pidfile=`pwd`/pid --detach], [0], [], [])
251    AT_CHECK([kill `cat pid`])
252    AT_CHECK([test -s status])
253    if grep '[[bB]]roken pipe' stderr >/dev/null 2>&1; then
254      # Something in the environment caused SIGPIPE to be ignored, but
255      # 'yes' at least told us that it got EPIPE.  Good enough; we know
256      # that stdout was closed.
257      :
258    else
259      # Otherwise make sure that 'yes' died from SIGPIPE.
260      AT_CHECK([kill -l `cat status`], [0], [PIPE
261 ])
262    fi
263    AT_CLEANUP])
264
265 DAEMON_DETACH_MONITOR_CLOSES_FDS_PYN([Python2], [$HAVE_PYTHON], [$PYTHON])
266 DAEMON_DETACH_MONITOR_CLOSES_FDS_PYN([Python3], [$HAVE_PYTHON3], [$PYTHON3])