Sync libtest.
[awesomized/libmemcached] / libtest / server.h
1 /* vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
2 *
3 * libtest
4 *
5 * Copyright (C) 2011 Data Differential, http://datadifferential.com/
6 *
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 3 of the License, or (at your option) any later version.
11 *
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
16 *
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this library; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20 */
21
22 #pragma once
23
24 #include <libtest/cmdline.h>
25
26 #include <cassert>
27 #include <cstdio>
28 #include <cstring>
29 #include <netdb.h>
30 #include <netinet/in.h>
31 #include <string>
32 #include <unistd.h>
33 #include <vector>
34
35 namespace libtest {
36
37 struct Server {
38 private:
39 typedef std::vector< std::pair<std::string, std::string> > Options;
40
41 private:
42 uint64_t _magic;
43 bool _is_socket;
44 std::string _socket;
45 std::string _sasl;
46 std::string _pid_file;
47 std::string _log_file;
48 std::string _base_command; // executable command which include libtool, valgrind, gdb, etc
49 std::string _running; // Current string being used for system()
50 pid_t _pid;
51
52 protected:
53 in_port_t _port;
54 std::string _hostname;
55 std::string _extra_args;
56
57 public:
58 Server(const std::string& hostname, const in_port_t port_arg,
59 const std::string& executable, const bool _is_libtool,
60 const bool is_socket_arg= false);
61
62 virtual ~Server();
63
64 virtual const char *name()= 0;
65 virtual bool is_libtool()= 0;
66
67 virtual bool has_socket_file_option() const
68 {
69 return false;
70 }
71
72 virtual void socket_file_option(Application& app, const std::string& socket_arg)
73 {
74 if (socket_arg.empty() == false)
75 {
76 std::string buffer("--socket=");
77 buffer+= socket_arg;
78 app.add_option(buffer);
79 }
80 }
81
82 virtual bool has_log_file_option() const
83 {
84 return false;
85 }
86
87 virtual void log_file_option(Application& app, const std::string& arg)
88 {
89 if (arg.empty() == false)
90 {
91 std::string buffer("--log-file=");
92 buffer+= arg;
93 app.add_option(buffer);
94 }
95 }
96
97 virtual void pid_file_option(Application& app, const std::string& arg)
98 {
99 if (arg.empty() == false)
100 {
101 std::string buffer("--pid-file=");
102 buffer+= arg;
103 app.add_option(buffer);
104 }
105 }
106
107 virtual bool has_port_option() const
108 {
109 return false;
110 }
111
112 virtual void port_option(Application& app, in_port_t arg)
113 {
114 if (arg > 0)
115 {
116 char buffer[1024];
117 snprintf(buffer, sizeof(buffer), "--port=%d", int(arg));
118 app.add_option(buffer);
119 }
120 }
121
122 virtual bool broken_socket_cleanup()
123 {
124 return false;
125 }
126
127 virtual bool broken_pid_file()
128 {
129 return false;
130 }
131
132 const std::string& pid_file() const
133 {
134 return _pid_file;
135 }
136
137 const std::string& base_command() const
138 {
139 return _base_command;
140 }
141
142 const std::string& log_file() const
143 {
144 return _log_file;
145 }
146
147 const std::string& hostname() const
148 {
149 return _hostname;
150 }
151
152 const std::string& socket() const
153 {
154 return _socket;
155 }
156
157 bool has_socket() const
158 {
159 return _is_socket;
160 }
161
162 bool cycle();
163
164 virtual bool ping()= 0;
165
166 virtual pid_t get_pid(bool error_is_ok= false)= 0;
167
168 virtual bool build(size_t argc, const char *argv[])= 0;
169
170 void add_option(const std::string&);
171 void add_option(const std::string&, const std::string&);
172
173 in_port_t port() const
174 {
175 return _port;
176 }
177
178 bool has_port() const
179 {
180 return (_port != 0);
181 }
182
183 virtual bool has_syslog() const
184 {
185 return false;
186 }
187
188 // Reset a server if another process has killed the server
189 void reset()
190 {
191 _pid= -1;
192 _pid_file.clear();
193 _log_file.clear();
194 }
195
196 bool args(Application&);
197
198 pid_t pid();
199
200 pid_t pid() const
201 {
202 return _pid;
203 }
204
205 bool has_pid() const;
206
207 virtual bool wait_for_pidfile() const;
208
209 bool check_pid(pid_t pid_arg) const
210 {
211 return (pid_arg > 1);
212 }
213
214 bool is_socket() const
215 {
216 return _hostname[0] == '/';
217 }
218
219 const std::string running() const
220 {
221 return _running;
222 }
223
224 bool check();
225
226 std::string log_and_pid();
227
228 bool kill();
229 bool start();
230 bool command(libtest::Application& app);
231
232 bool validate();
233
234 protected:
235 bool set_pid_file();
236 Options _options;
237 Application _app;
238
239 private:
240 bool is_helgrind() const;
241 bool is_valgrind() const;
242 bool is_debug() const;
243 bool set_log_file();
244 bool set_socket_file();
245 void reset_pid();
246 };
247
248 std::ostream& operator<<(std::ostream& output, const libtest::Server &arg);
249
250 } // namespace libtest
251
252