ff44228ab4c8c85c6d05211640437626c5907bec
[m6w6/libmemcached] / libtest / server_container.cc
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 #include <config.h>
23 #include <libtest/common.h>
24
25 #include <cassert>
26 #include <cerrno>
27 #include <cstdlib>
28 #include <iostream>
29
30 #include <algorithm>
31 #include <functional>
32 #include <locale>
33
34 // trim from end
35 static inline std::string &rtrim(std::string &s)
36 {
37 s.erase(std::find_if(s.rbegin(), s.rend(), std::not1(std::ptr_fun<int, int>(std::isspace))).base(), s.end());
38 return s;
39 }
40
41 namespace libtest {
42
43 void server_startup_st::push_server(Server *arg)
44 {
45 servers.push_back(arg);
46
47 char port_str[NI_MAXSERV];
48 snprintf(port_str, sizeof(port_str), "%u", int(arg->port()));
49
50 std::string server_config_string;
51 if (arg->has_socket())
52 {
53 server_config_string+= "--socket=";
54 server_config_string+= '"';
55 server_config_string+= arg->socket();
56 server_config_string+= '"';
57 server_config_string+= " ";
58 }
59 else
60 {
61 server_config_string+= "--server=";
62 server_config_string+= arg->hostname();
63 server_config_string+= ":";
64 server_config_string+= port_str;
65 server_config_string+= " ";
66 }
67
68 server_list+= server_config_string;
69
70 }
71
72 Server* server_startup_st::pop_server()
73 {
74 Server *tmp= servers.back();
75 servers.pop_back();
76 return tmp;
77 }
78
79 bool server_startup_st::shutdown(uint32_t number_of_host)
80 {
81 if (servers.size() > number_of_host)
82 {
83 Server* tmp= servers[number_of_host];
84
85 if (tmp and tmp->has_pid() and not tmp->kill(tmp->pid()))
86 { }
87 else
88 {
89 return true;
90 }
91 }
92
93 return false;
94 }
95
96 void server_startup_st::shutdown_and_remove()
97 {
98 for (std::vector<Server *>::iterator iter= servers.begin(); iter != servers.end(); iter++)
99 {
100 delete *iter;
101 }
102 servers.clear();
103 }
104
105 void server_startup_st::shutdown()
106 {
107 for (std::vector<Server *>::iterator iter= servers.begin(); iter != servers.end(); iter++)
108 {
109 if ((*iter)->has_pid() and not (*iter)->kill((*iter)->pid()))
110 {
111 Error << "Unable to kill:" << *(*iter);
112 }
113 }
114 }
115
116 void server_startup_st::restart()
117 {
118 for (std::vector<Server *>::iterator iter= servers.begin(); iter != servers.end(); iter++)
119 {
120 (*iter)->start();
121 }
122 }
123
124 server_startup_st::~server_startup_st()
125 {
126 shutdown_and_remove();
127 }
128
129 bool server_startup_st::is_debug() const
130 {
131 return bool(getenv("LIBTEST_MANUAL_GDB"));
132 }
133
134 bool server_startup_st::is_valgrind() const
135 {
136 return bool(getenv("LIBTEST_MANUAL_VALGRIND"));
137 }
138
139 bool server_startup_st::is_helgrind() const
140 {
141 return bool(getenv("LIBTEST_MANUAL_HELGRIND"));
142 }
143
144
145 bool server_startup(server_startup_st& construct, const std::string& server_type, in_port_t try_port, int argc, const char *argv[])
146 {
147 Outn();
148 if (try_port <= 0)
149 {
150 libtest::fatal(LIBYATL_DEFAULT_PARAM, "was passed the invalid port number %d", int(try_port));
151 }
152
153 libtest::Server *server= NULL;
154 if (0)
155 { }
156 else if (server_type.compare("gearmand") == 0)
157 {
158 if (GEARMAND_BINARY)
159 {
160 if (HAVE_LIBGEARMAN)
161 {
162 server= build_gearmand("localhost", try_port);
163 }
164 }
165 }
166 else if (server_type.compare("blobslap_worker") == 0)
167 {
168 if (GEARMAND_BINARY)
169 {
170 if (GEARMAND_BLOBSLAP_WORKER)
171 {
172 if (HAVE_LIBGEARMAN)
173 {
174 server= build_blobslap_worker(try_port);
175 }
176 }
177 }
178 }
179 else if (server_type.compare("memcached-sasl") == 0)
180 {
181 if (MEMCACHED_SASL_BINARY)
182 {
183 if (HAVE_LIBMEMCACHED)
184 {
185 server= build_memcached_sasl("localhost", try_port, construct.username(), construct.password());
186 }
187 }
188 }
189 else if (server_type.compare("memcached") == 0)
190 {
191 if (MEMCACHED_BINARY)
192 {
193 if (HAVE_LIBMEMCACHED)
194 {
195 server= build_memcached("localhost", try_port);
196 }
197 }
198 }
199 else if (server_type.compare("memcached-light") == 0)
200 {
201 if (MEMCACHED_LIGHT_BINARY)
202 {
203 if (HAVE_LIBMEMCACHED)
204 {
205 server= build_memcached_light("localhost", try_port);
206 }
207 }
208 }
209
210 if (server == NULL)
211 {
212 fatal_message("Launching of an unknown server was attempted");
213 }
214
215 /*
216 We will now cycle the server we have created.
217 */
218 if (server->cycle() == false)
219 {
220 Error << "Could not start up server " << *server;
221 delete server;
222 return false;
223 }
224
225 server->build(argc, argv);
226
227 if (construct.is_debug())
228 {
229 Out << "Pausing for startup, hit return when ready.";
230 std::string gdb_command= server->base_command();
231 std::string options;
232 #if 0
233 Out << "run " << server->args(options);
234 #endif
235 getchar();
236 }
237 else if (server->start() == false)
238 {
239 delete server;
240 return false;
241 }
242 else
243 {
244 Out << "STARTING SERVER(pid:" << server->pid() << "): " << server->running();
245 }
246
247 construct.push_server(server);
248
249 Outn();
250
251 return true;
252 }
253
254 bool server_startup_st::start_socket_server(const std::string& server_type, const in_port_t try_port, int argc, const char *argv[])
255 {
256 (void)try_port;
257 Outn();
258
259 Server *server= NULL;
260 if (0)
261 { }
262 else if (server_type.compare("gearmand") == 0)
263 {
264 Error << "Socket files are not supported for gearmand yet";
265 }
266 else if (server_type.compare("memcached-sasl") == 0)
267 {
268 if (MEMCACHED_SASL_BINARY)
269 {
270 if (HAVE_LIBMEMCACHED)
271 {
272 server= build_memcached_sasl_socket("localhost", try_port, username(), password());
273 }
274 else
275 {
276 Error << "Libmemcached was not found";
277 }
278 }
279 else
280 {
281 Error << "No memcached binary is available";
282 }
283 }
284 else if (server_type.compare("memcached") == 0)
285 {
286 if (MEMCACHED_BINARY)
287 {
288 if (HAVE_LIBMEMCACHED)
289 {
290 server= build_memcached_socket("localhost", try_port);
291 }
292 else
293 {
294 Error << "Libmemcached was not found";
295 }
296 }
297 else
298 {
299 Error << "No memcached binary is available";
300 }
301 }
302 else
303 {
304 Error << "Failed to start " << server_type << ", no support was found to be compiled in for it.";
305 }
306
307 if (server == NULL)
308 {
309 Error << "Failure occured while creating server: " << server_type;
310 return false;
311 }
312
313 /*
314 We will now cycle the server we have created.
315 */
316 if (not server->cycle())
317 {
318 Error << "Could not start up server " << *server;
319 delete server;
320 return false;
321 }
322
323 server->build(argc, argv);
324
325 if (is_debug())
326 {
327 Out << "Pausing for startup, hit return when ready.";
328 std::string gdb_command= server->base_command();
329 std::string options;
330 #if 0
331 Out << "run " << server->args(options);
332 #endif
333 getchar();
334 }
335 else if (not server->start())
336 {
337 Error << "Failed to start " << *server;
338 delete server;
339 return false;
340 }
341 else
342 {
343 Out << "STARTING SERVER(pid:" << server->pid() << "): " << server->running();
344 }
345
346 push_server(server);
347
348 set_default_socket(server->socket().c_str());
349
350 Outn();
351
352 return true;
353 }
354
355 std::string server_startup_st::option_string() const
356 {
357 std::string temp= server_list;
358 rtrim(temp);
359 return temp;
360 }
361
362
363 } // namespace libtest