daemon: Add support for process monitoring and restart.
[sliver-openvswitch.git] / tests / daemon.at
1 AT_BANNER([daemon unit tests])
2
3 AT_SETUP([daemon])
4 OVSDB_INIT([db])
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([ovsdb-server --pidfile=$PWD/pid --remote=punix:socket --unixctl=$PWD/unixctl db& 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])
22 OVSDB_INIT([db])
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([ovsdb-server --monitor --pidfile=$PWD/pid --remote=punix:socket --unixctl=$PWD/unixctl db& 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])
65 AT_CAPTURE_FILE([pid])
66 OVSDB_INIT([db])
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([ovsdb-server --detach --pidfile=$PWD/pid --remote=punix:socket --unixctl=$PWD/unixctl db], [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])
81 m4_define([CHECK], 
82   [AT_CHECK([$1], [$2], [$3], [$4], [kill `cat daemon monitor`])])
83 OVSDB_INIT([db])
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([ovsdb-server --detach --pidfile=$PWD/daemon --monitor --remote=punix:socket --unixctl=$PWD/unixctl db], [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])
130 AT_CAPTURE_FILE([pid])
131 OVSDB_INIT([db])
132 OVS_CHECK_LCOV([ovsdb-server --detach --pidfile=$PWD/pid --unixctl=$PWD/nonexistent/unixctl db], [1], [], [stderr])
133 AT_CHECK([grep 'ovsdb-server: could not initialize control socket' stderr],
134   [0], [ignore], [])
135 AT_CHECK([test ! -s pid])
136 AT_CLEANUP
137
138 AT_SETUP([daemon --detach --monitor startup errors])
139 AT_CAPTURE_FILE([pid])
140 OVSDB_INIT([db])
141 OVS_CHECK_LCOV([ovsdb-server --detach --pidfile=$PWD/pid --monitor --unixctl=$PWD/nonexistent/unixctl db], [1], [], [stderr])
142 AT_CHECK([grep 'ovsdb-server: could not initialize control socket' stderr],
143   [0], [ignore], [])
144 AT_CHECK([test ! -s pid])
145 AT_CLEANUP
146
147 AT_SETUP([daemon --detach closes standard fds])
148 AT_CAPTURE_FILE([pid])
149 OVSDB_INIT([db])
150 AT_CHECK([(yes; echo $? > status) | ovsdb-server --detach --pidfile=$PWD/pid --unixctl=$PWD/unixctl db], [0], [], [stderr])
151 AT_CHECK([kill `cat pid`])
152 AT_CHECK([test -s status])
153 AT_CHECK([kill -l `cat status`], [0], [PIPE
154 ])
155 AT_CLEANUP
156
157 AT_SETUP([daemon --detach --monitor closes standard fds])
158 AT_CAPTURE_FILE([pid])
159 OVSDB_INIT([db])
160 AT_CHECK([(yes; echo $? > status) | ovsdb-server --detach --monitor --pidfile=$PWD/pid --unixctl=$PWD/unixctl db], [0], [], [stderr])
161 AT_CHECK([kill `cat pid`])
162 AT_CHECK([test -s status])
163 AT_CHECK([kill -l `cat status`], [0], [PIPE
164 ])
165 AT_CLEANUP