5b26fcddde2f1e10b9a005ee0917f63647650179
[awesomized/libmemcached] / libtest / server.h
1 /* vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
2 *
3 * Data Differential YATL (i.e. libtest) library
4 *
5 * Copyright (C) 2012 Data Differential, http://datadifferential.com/
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions are
9 * met:
10 *
11 * * Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 *
14 * * Redistributions in binary form must reproduce the above
15 * copyright notice, this list of conditions and the following disclaimer
16 * in the documentation and/or other materials provided with the
17 * distribution.
18 *
19 * * The names of its contributors may not be used to endorse or
20 * promote products derived from this software without specific prior
21 * written permission.
22 *
23 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
24 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
25 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
26 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
27 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
28 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
29 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
30 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
31 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
32 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
33 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
34 *
35 */
36
37 #pragma once
38
39 #include <libtest/cmdline.h>
40
41 #include <cassert>
42 #include <cstdio>
43 #include <cstring>
44
45 #include <netdb.h>
46 #include <netinet/in.h>
47
48 #include <string>
49 #include <unistd.h>
50 #include <vector>
51
52 namespace libtest {
53
54 struct Server {
55 private:
56 typedef std::vector< std::pair<std::string, std::string> > Options;
57
58 private:
59 uint64_t _magic;
60 bool _is_socket;
61 std::string _socket;
62 std::string _sasl;
63 std::string _pid_file;
64 std::string _log_file;
65 std::string _base_command; // executable command which include libtool, valgrind, gdb, etc
66 std::string _running; // Current string being used for system()
67
68 protected:
69 in_port_t _port;
70 std::string _hostname;
71 std::string _extra_args;
72
73 public:
74 Server(const std::string& hostname, const in_port_t port_arg,
75 const std::string& executable, const bool _is_libtool,
76 const bool is_socket_arg= false);
77
78 virtual ~Server();
79
80 virtual const char *name()= 0;
81 virtual bool is_libtool()= 0;
82
83 virtual bool has_socket_file_option() const
84 {
85 return false;
86 }
87
88 virtual void socket_file_option(Application& app, const std::string& socket_arg)
89 {
90 if (socket_arg.empty() == false)
91 {
92 std::string buffer("--socket=");
93 buffer+= socket_arg;
94 app.add_option(buffer);
95 }
96 }
97
98 virtual bool has_log_file_option() const
99 {
100 return false;
101 }
102
103 virtual void log_file_option(Application& app, const std::string& arg)
104 {
105 if (arg.empty() == false)
106 {
107 std::string buffer("--log-file=");
108 buffer+= arg;
109 app.add_option(buffer);
110 }
111 }
112
113 virtual void pid_file_option(Application& app, const std::string& arg)
114 {
115 if (arg.empty() == false)
116 {
117 std::string buffer("--pid-file=");
118 buffer+= arg;
119 app.add_option(buffer);
120 }
121 }
122
123 virtual bool has_port_option() const
124 {
125 return false;
126 }
127
128 virtual void port_option(Application& app, in_port_t arg)
129 {
130 if (arg > 0)
131 {
132 char buffer[1024];
133 snprintf(buffer, sizeof(buffer), "--port=%d", int(arg));
134 app.add_option(buffer);
135 }
136 }
137
138 virtual bool broken_socket_cleanup()
139 {
140 return false;
141 }
142
143 virtual bool broken_pid_file()
144 {
145 return false;
146 }
147
148 const std::string& pid_file() const
149 {
150 return _pid_file;
151 }
152
153 const std::string& base_command() const
154 {
155 return _base_command;
156 }
157
158 const std::string& log_file() const
159 {
160 return _log_file;
161 }
162
163 const std::string& hostname() const
164 {
165 return _hostname;
166 }
167
168 const std::string& socket() const
169 {
170 return _socket;
171 }
172
173 bool has_socket() const
174 {
175 return _is_socket;
176 }
177
178 bool cycle();
179
180 virtual bool ping()= 0;
181
182 virtual bool build(size_t argc, const char *argv[])= 0;
183
184 void add_option(const std::string&);
185 void add_option(const std::string&, const std::string&);
186
187 in_port_t port() const
188 {
189 return _port;
190 }
191
192 bool has_port() const
193 {
194 return (_port != 0);
195 }
196
197 virtual bool has_syslog() const
198 {
199 return false;
200 }
201
202 // Reset a server if another process has killed the server
203 void reset()
204 {
205 _pid_file.clear();
206 _log_file.clear();
207 }
208
209 pid_t pid() const;
210
211 bool has_pid() const;
212
213 virtual bool has_pid_file() const
214 {
215 return true;
216 }
217
218 const std::string& error()
219 {
220 return _error;
221 }
222
223 void error(std::string arg)
224 {
225 _error= arg;
226 }
227
228 void reset_error()
229 {
230 _error.clear();
231 }
232
233 virtual bool wait_for_pidfile() const;
234
235 bool check_pid(pid_t pid_arg) const
236 {
237 return (pid_arg > 1);
238 }
239
240 bool is_socket() const
241 {
242 return _is_socket;
243 }
244
245 const std::string running() const
246 {
247 return _running;
248 }
249
250 bool check();
251
252 std::string log_and_pid();
253
254 bool murder();
255 bool start();
256 bool command(libtest::Application& app);
257
258 bool validate();
259
260 void out_of_ban_killed(bool arg)
261 {
262 out_of_ban_killed_= arg;
263 }
264
265 bool out_of_ban_killed()
266 {
267 return out_of_ban_killed_;
268 }
269
270 void timeout(uint32_t timeout_)
271 {
272 _timeout= timeout_;
273 }
274
275 protected:
276 bool set_pid_file();
277 Options _options;
278 Application _app;
279
280 private:
281 bool is_helgrind() const;
282 bool is_valgrind() const;
283 bool is_debug() const;
284 bool set_log_file();
285 bool set_socket_file();
286 void reset_pid();
287 bool out_of_ban_killed_;
288 bool args(Application&);
289
290 std::string _error;
291 uint32_t _timeout; // This number should be high enough for valgrind startup (which is slow)
292 };
293
294 std::ostream& operator<<(std::ostream& output, const libtest::Server &arg);
295
296 } // namespace libtest
297
298