Sync libtest.
[awesomized/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 tmp->kill() == false)
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 bool server_startup_st::check() const
106 {
107 for (std::vector<Server *>::const_iterator iter= servers.begin(); iter != servers.end(); iter++)
108 {
109 if ((*iter)->check() == false)
110 {
111 return false;
112 }
113 }
114
115 return true;
116 }
117
118 void server_startup_st::shutdown()
119 {
120 for (std::vector<Server *>::iterator iter= servers.begin(); iter != servers.end(); iter++)
121 {
122 if ((*iter)->has_pid() and (*iter)->kill() == false)
123 {
124 Error << "Unable to kill:" << *(*iter);
125 }
126 }
127 }
128
129 void server_startup_st::restart()
130 {
131 for (std::vector<Server *>::iterator iter= servers.begin(); iter != servers.end(); iter++)
132 {
133 (*iter)->start();
134 }
135 }
136
137 #define MAGIC_MEMORY 123575
138 server_startup_st::server_startup_st() :
139 _magic(MAGIC_MEMORY),
140 _socket(false),
141 _sasl(false),
142 _count(5),
143 udp(0)
144 { }
145
146 server_startup_st::~server_startup_st()
147 {
148 shutdown_and_remove();
149 }
150
151 bool server_startup_st::validate()
152 {
153 return _magic == MAGIC_MEMORY;
154 }
155
156
157 bool server_startup_st::is_debug() const
158 {
159 return bool(getenv("LIBTEST_MANUAL_GDB"));
160 }
161
162 bool server_startup_st::is_valgrind() const
163 {
164 return bool(getenv("LIBTEST_MANUAL_VALGRIND"));
165 }
166
167 bool server_startup_st::is_helgrind() const
168 {
169 return bool(getenv("LIBTEST_MANUAL_HELGRIND"));
170 }
171
172
173 bool server_startup(server_startup_st& construct, const std::string& server_type, in_port_t try_port, int argc, const char *argv[])
174 {
175 Outn();
176 if (try_port <= 0)
177 {
178 libtest::fatal(LIBYATL_DEFAULT_PARAM, "was passed the invalid port number %d", int(try_port));
179 }
180
181 libtest::Server *server= NULL;
182 if (0)
183 { }
184 else if (server_type.compare("gearmand") == 0)
185 {
186 if (GEARMAND_BINARY)
187 {
188 if (HAVE_LIBGEARMAN)
189 {
190 server= build_gearmand("localhost", try_port);
191 }
192 }
193 }
194 else if (server_type.compare("blobslap_worker") == 0)
195 {
196 if (GEARMAND_BINARY)
197 {
198 if (GEARMAND_BLOBSLAP_WORKER)
199 {
200 if (HAVE_LIBGEARMAN)
201 {
202 server= build_blobslap_worker(try_port);
203 }
204 }
205 }
206 }
207 else if (server_type.compare("memcached-sasl") == 0)
208 {
209 if (MEMCACHED_SASL_BINARY)
210 {
211 if (HAVE_LIBMEMCACHED)
212 {
213 server= build_memcached_sasl("localhost", try_port, construct.username(), construct.password());
214 }
215 }
216 }
217 else if (server_type.compare("memcached") == 0)
218 {
219 if (MEMCACHED_BINARY)
220 {
221 if (HAVE_LIBMEMCACHED)
222 {
223 server= build_memcached("localhost", try_port);
224 }
225 }
226 }
227 else if (server_type.compare("memcached-light") == 0)
228 {
229 if (MEMCACHED_LIGHT_BINARY)
230 {
231 if (HAVE_LIBMEMCACHED)
232 {
233 server= build_memcached_light("localhost", try_port);
234 }
235 }
236 }
237
238 if (server == NULL)
239 {
240 fatal_message("Launching of an unknown server was attempted");
241 }
242
243 /*
244 We will now cycle the server we have created.
245 */
246 if (server->cycle() == false)
247 {
248 Error << "Could not start up server " << *server;
249 delete server;
250 return false;
251 }
252
253 server->build(argc, argv);
254
255 if (construct.is_debug())
256 {
257 Out << "Pausing for startup, hit return when ready.";
258 std::string gdb_command= server->base_command();
259 std::string options;
260 #if 0
261 Out << "run " << server->args(options);
262 #endif
263 getchar();
264 }
265 else if (server->start() == false)
266 {
267 delete server;
268 return false;
269 }
270 else
271 {
272 Out << "STARTING SERVER(pid:" << server->pid() << "): " << server->running();
273 }
274
275 construct.push_server(server);
276
277 Outn();
278
279 return true;
280 }
281
282 bool server_startup_st::start_socket_server(const std::string& server_type, const in_port_t try_port, int argc, const char *argv[])
283 {
284 (void)try_port;
285 Outn();
286
287 Server *server= NULL;
288 if (0)
289 { }
290 else if (server_type.compare("gearmand") == 0)
291 {
292 Error << "Socket files are not supported for gearmand yet";
293 }
294 else if (server_type.compare("memcached-sasl") == 0)
295 {
296 if (MEMCACHED_SASL_BINARY)
297 {
298 if (HAVE_LIBMEMCACHED)
299 {
300 server= build_memcached_sasl_socket("localhost", try_port, username(), password());
301 }
302 else
303 {
304 Error << "Libmemcached was not found";
305 }
306 }
307 else
308 {
309 Error << "No memcached binary is available";
310 }
311 }
312 else if (server_type.compare("memcached") == 0)
313 {
314 if (MEMCACHED_BINARY)
315 {
316 if (HAVE_LIBMEMCACHED)
317 {
318 server= build_memcached_socket("localhost", try_port);
319 }
320 else
321 {
322 Error << "Libmemcached was not found";
323 }
324 }
325 else
326 {
327 Error << "No memcached binary is available";
328 }
329 }
330 else
331 {
332 Error << "Failed to start " << server_type << ", no support was found to be compiled in for it.";
333 }
334
335 if (server == NULL)
336 {
337 Error << "Failure occured while creating server: " << server_type;
338 return false;
339 }
340
341 /*
342 We will now cycle the server we have created.
343 */
344 if (server->cycle() == false)
345 {
346 Error << "Could not start up server " << *server;
347 delete server;
348 return false;
349 }
350
351 server->build(argc, argv);
352
353 if (is_debug())
354 {
355 Out << "Pausing for startup, hit return when ready.";
356 std::string gdb_command= server->base_command();
357 std::string options;
358 #if 0
359 Out << "run " << server->args(options);
360 #endif
361 getchar();
362 }
363 else if (not server->start())
364 {
365 Error << "Failed to start " << *server;
366 delete server;
367 return false;
368 }
369 else
370 {
371 Out << "STARTING SERVER(pid:" << server->pid() << "): " << server->running();
372 }
373
374 push_server(server);
375
376 set_default_socket(server->socket().c_str());
377
378 Outn();
379
380 return true;
381 }
382
383 std::string server_startup_st::option_string() const
384 {
385 std::string temp= server_list;
386 rtrim(temp);
387 return temp;
388 }
389
390
391 } // namespace libtest