Implement initial Python bindings for Open vSwitch database.
[sliver-openvswitch.git] / tests / daemon-py.at
1 AT_BANNER([daemon unit tests - Python])
2
3 AT_SETUP([daemon - Python])
4 AT_SKIP_IF([test $HAVE_PYTHON = no])
5 AT_CAPTURE_FILE([pid])
6 AT_CAPTURE_FILE([expected])
7 # Start the daemon and wait for the pidfile to get created
8 # and that its contents are the correct pid.
9 AT_CHECK([$PYTHON $srcdir/test-daemon.py --pidfile-name=$PWD/pid& echo $! > expected], [0], [ignore], [ignore])
10 OVS_WAIT_UNTIL([test -s pid], [kill `cat expected`])
11 AT_CHECK(
12   [pid=`cat pid` && expected=`cat expected` && test "$pid" = "$expected"],
13   [0], [], [], [kill `cat expected`])
14 AT_CHECK([kill -0 `cat pid`], [0], [], [], [kill `cat expected`])
15 # Kill the daemon and make sure that the pidfile gets deleted.
16 kill `cat expected`
17 OVS_WAIT_WHILE([kill -0 `cat expected`])
18 AT_CHECK([test ! -e pid])
19 AT_CLEANUP
20
21 AT_SETUP([daemon --monitor - Python])
22 AT_SKIP_IF([test $HAVE_PYTHON = no])
23 AT_CAPTURE_FILE([pid])
24 AT_CAPTURE_FILE([parent])
25 AT_CAPTURE_FILE([parentpid])
26 AT_CAPTURE_FILE([newpid])
27 # Start the daemon and wait for the pidfile to get created.
28 AT_CHECK([$PYTHON $srcdir/test-daemon.py --pidfile-name=$PWD/pid --monitor& echo $! > parent], [0], [ignore], [ignore])
29 OVS_WAIT_UNTIL([test -s pid], [kill `cat parent`])
30 # Check that the pidfile names a running process,
31 # and that the parent process of that process is our child process.
32 AT_CHECK([kill -0 `cat pid`], [0], [], [], [kill `cat parent`])
33 AT_CHECK([ps -o ppid= -p `cat pid` > parentpid],
34   [0], [], [], [kill `cat parent`])
35 AT_CHECK(
36   [parentpid=`cat parentpid` && 
37    parent=`cat parent` && 
38    test $parentpid = $parent],
39   [0], [], [], [kill `cat parent`])
40 # Kill the daemon process, making it look like a segfault,
41 # and wait for a new child process to get spawned.
42 AT_CHECK([cp pid oldpid], [0], [], [], [kill `cat parent`])
43 AT_CHECK([kill -SEGV `cat pid`], [0], [], [ignore], [kill `cat parent`])
44 OVS_WAIT_WHILE([kill -0 `cat oldpid`], [kill `cat parent`])
45 OVS_WAIT_UNTIL([test -s pid && test `cat pid` != `cat oldpid`],
46   [kill `cat parent`])
47 AT_CHECK([cp pid newpid], [0], [], [], [kill `cat parent`])
48 # Check that the pidfile names a running process,
49 # and that the parent process of that process is our child process.
50 AT_CHECK([ps -o ppid= -p `cat pid` > parentpid],
51   [0], [], [], [kill `cat parent`])
52 AT_CHECK(
53   [parentpid=`cat parentpid` && 
54    parent=`cat parent` && 
55    test $parentpid = $parent],
56   [0], [], [], [kill `cat parent`])
57 # Kill the daemon process with SIGTERM, and wait for the daemon
58 # and the monitor processes to go away and the pidfile to get deleted.
59 AT_CHECK([kill `cat pid`], [0], [], [ignore], [kill `cat parent`])
60 OVS_WAIT_WHILE([kill -0 `cat parent` || kill -0 `cat newpid` || test -e pid],
61   [kill `cat parent`])
62 AT_CLEANUP
63
64 AT_SETUP([daemon --detach - Python])
65 AT_SKIP_IF([test $HAVE_PYTHON = no])
66 AT_CAPTURE_FILE([pid])
67 # Start the daemon and make sure that the pidfile exists immediately.
68 # We don't wait for the pidfile to get created because the daemon is
69 # supposed to do so before the parent exits.
70 AT_CHECK([$PYTHON $srcdir/test-daemon.py --pidfile-name=$PWD/pid --detach], [0], [ignore], [ignore])
71 AT_CHECK([test -s pid])
72 AT_CHECK([kill -0 `cat pid`])
73 # Kill the daemon and make sure that the pidfile gets deleted.
74 cp pid saved-pid
75 kill `cat pid`
76 OVS_WAIT_WHILE([kill -0 `cat saved-pid`])
77 AT_CHECK([test ! -e pid])
78 AT_CLEANUP
79
80 AT_SETUP([daemon --detach --monitor - Python])
81 AT_SKIP_IF([test $HAVE_PYTHON = no])
82 m4_define([CHECK], 
83   [AT_CHECK([$1], [$2], [$3], [$4], [kill `cat daemon monitor`])])
84 AT_CAPTURE_FILE([daemon])
85 AT_CAPTURE_FILE([olddaemon])
86 AT_CAPTURE_FILE([newdaemon])
87 AT_CAPTURE_FILE([monitor])
88 AT_CAPTURE_FILE([newmonitor])
89 AT_CAPTURE_FILE([init])
90 # Start the daemon and make sure that the pidfile exists immediately.
91 # We don't wait for the pidfile to get created because the daemon is
92 # supposed to do so before the parent exits.
93 AT_CHECK([$PYTHON $srcdir/test-daemon.py --pidfile-name=$PWD/daemon --detach --monitor], [0], [ignore], [ignore])
94 AT_CHECK([test -s daemon])
95 # Check that the pidfile names a running process,
96 # and that the parent process of that process is a running process,
97 # and that the parent process of that process is init.
98 CHECK([kill -0 `cat daemon`])
99 CHECK([ps -o ppid= -p `cat daemon` > monitor])
100 CHECK([kill -0 `cat monitor`])
101 CHECK([ps -o ppid= -p `cat monitor` > init])
102 CHECK([test `cat init` = 1])
103 # Kill the daemon process, making it look like a segfault,
104 # and wait for a new daemon process to get spawned.
105 CHECK([cp daemon olddaemon])
106 CHECK([kill -SEGV `cat daemon`], [0], [ignore], [ignore])
107 OVS_WAIT_WHILE([kill -0 `cat olddaemon`], [kill `cat olddaemon daemon`])
108 OVS_WAIT_UNTIL([test -s daemon && test `cat daemon` != `cat olddaemon`],
109   [kill `cat olddaemon daemon`])
110 CHECK([cp daemon newdaemon])
111 # Check that the pidfile names a running process,
112 # and that the parent process of that process is our child process.
113 CHECK([kill -0 `cat daemon`])
114 CHECK([diff olddaemon newdaemon], [1], [ignore])
115 CHECK([ps -o ppid= -p `cat daemon` > newmonitor])
116 CHECK([diff monitor newmonitor])
117 CHECK([kill -0 `cat newmonitor`])
118 CHECK([ps -o ppid= -p `cat newmonitor` > init])
119 CHECK([test `cat init` = 1])
120 # Kill the daemon process with SIGTERM, and wait for the daemon
121 # and the monitor processes to go away and the pidfile to get deleted.
122 CHECK([kill `cat daemon`], [0], [], [ignore])
123 OVS_WAIT_WHILE(
124   [kill -0 `cat monitor` || kill -0 `cat newdaemon` || test -e daemon],
125   [kill `cat monitor newdaemon`])
126 m4_undefine([CHECK])
127 AT_CLEANUP
128
129 AT_SETUP([daemon --detach startup errors - Python])
130 AT_SKIP_IF([test $HAVE_PYTHON = no])
131 AT_CAPTURE_FILE([pid])
132 AT_CHECK([$PYTHON $srcdir/test-daemon.py --pidfile-name=$PWD/pid --detach --bail], [1], [], [stderr])
133 AT_CHECK([grep 'test-daemon.py: exiting after daemonize_start() as requested' stderr],
134   [0], [ignore], [])
135 AT_CHECK([test ! -s pid])
136 AT_CLEANUP
137
138 AT_SETUP([daemon --detach --monitor startup errors - Python])
139 AT_SKIP_IF([test $HAVE_PYTHON = no])
140 AT_CAPTURE_FILE([pid])
141 AT_CHECK([$PYTHON $srcdir/test-daemon.py --pidfile-name=$PWD/pid --detach --monitor --bail], [1], [], [stderr])
142 AT_CHECK([grep 'test-daemon.py: exiting after daemonize_start() as requested' stderr],
143   [0], [ignore], [])
144 AT_CHECK([test ! -s pid])
145 AT_CLEANUP
146
147 AT_SETUP([daemon --detach closes standard fds - Python])
148 AT_SKIP_IF([test $HAVE_PYTHON = no])
149 AT_CAPTURE_FILE([pid])
150 AT_CAPTURE_FILE([status])
151 AT_CAPTURE_FILE([stderr])
152 AT_CHECK([(yes 2>stderr; echo $? > status) | $PYTHON $srcdir/test-daemon.py --pidfile-name=$PWD/pid --detach], [0], [], [])
153 AT_CHECK([kill `cat pid`])
154 AT_CHECK([test -s status])
155 if grep '[[bB]]roken pipe' stderr >/dev/null 2>&1; then
156   # Something in the environment caused SIGPIPE to be ignored, but
157   # 'yes' at least told us that it got EPIPE.  Good enough; we know
158   # that stdout was closed.
159   :
160 else
161   # Otherwise make sure that 'yes' died from SIGPIPE.
162   AT_CHECK([kill -l `cat status`], [0], [PIPE
163 ])
164 fi
165 AT_CLEANUP
166
167 AT_SETUP([daemon --detach --monitor closes standard fds])
168 AT_CAPTURE_FILE([pid])
169 AT_CAPTURE_FILE([status])
170 AT_CAPTURE_FILE([stderr])
171 OVSDB_INIT([db])
172 AT_CHECK([(yes 2>stderr; echo $? > status) | $PYTHON $srcdir/test-daemon.py --pidfile-name=$PWD/pid --detach], [0], [], [])
173 AT_CHECK([kill `cat pid`])
174 AT_CHECK([test -s status])
175 if grep '[[bB]]roken pipe' stderr >/dev/null 2>&1; then
176   # Something in the environment caused SIGPIPE to be ignored, but
177   # 'yes' at least told us that it got EPIPE.  Good enough; we know
178   # that stdout was closed.
179   :
180 else
181   # Otherwise make sure that 'yes' died from SIGPIPE.
182   AT_CHECK([kill -l `cat status`], [0], [PIPE
183 ])
184 fi
185 AT_CLEANUP