Remove the need for the custom SPHINXBUILD plugin.
[m6w6/libmemcached] / libtest / server_container.cc
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 #include <config.h>
38 #include <libtest/common.h>
39
40 #include <cassert>
41 #include <cerrno>
42 #include <cstdlib>
43 #include <iostream>
44
45 #include <algorithm>
46 #include <functional>
47 #include <locale>
48
49 // trim from end
50 static inline std::string &rtrim(std::string &s)
51 {
52 s.erase(std::find_if(s.rbegin(), s.rend(), std::not1(std::ptr_fun<int, int>(std::isspace))).base(), s.end());
53 return s;
54 }
55
56 namespace libtest {
57
58 Server* server_startup_st::last()
59 {
60 return servers.back();
61 }
62
63 void server_startup_st::push_server(Server *arg)
64 {
65 servers.push_back(arg);
66
67 std::string server_config_string;
68 if (arg->has_socket())
69 {
70 server_config_string+= "--socket=";
71 server_config_string+= '"';
72 server_config_string+= arg->socket();
73 server_config_string+= '"';
74 server_config_string+= " ";
75 }
76 else
77 {
78 char port_str[NI_MAXSERV]= { 0 };
79 snprintf(port_str, sizeof(port_str), "%u", int(arg->port()));
80
81 server_config_string+= "--server=";
82 server_config_string+= arg->hostname();
83 server_config_string+= ":";
84 server_config_string+= port_str;
85 server_config_string+= " ";
86 }
87
88 server_list+= server_config_string;
89
90 }
91
92 Server* server_startup_st::pop_server()
93 {
94 Server *tmp= servers.back();
95 servers.pop_back();
96 return tmp;
97 }
98
99 // host_to_shutdown => host number to shutdown in array
100 bool server_startup_st::shutdown(uint32_t host_to_shutdown)
101 {
102 if (servers.size() > host_to_shutdown)
103 {
104 Server* tmp= servers[host_to_shutdown];
105
106 if (tmp and tmp->kill() == false)
107 { }
108 else
109 {
110 return true;
111 }
112 }
113
114 return false;
115 }
116
117 void server_startup_st::clear()
118 {
119 for (std::vector<Server *>::iterator iter= servers.begin(); iter != servers.end(); ++iter)
120 {
121 delete *iter;
122 }
123 servers.clear();
124 }
125
126 bool server_startup_st::check() const
127 {
128 bool success= true;
129 for (std::vector<Server *>::const_iterator iter= servers.begin(); iter != servers.end(); ++iter)
130 {
131 if ((*iter)->check() == false)
132 {
133 success= false;
134 }
135 }
136
137 return success;
138 }
139
140 bool server_startup_st::shutdown()
141 {
142 bool success= true;
143 for (std::vector<Server *>::iterator iter= servers.begin(); iter != servers.end(); ++iter)
144 {
145 if ((*iter)->has_pid() and (*iter)->kill() == false)
146 {
147 Error << "Unable to kill:" << *(*iter);
148 success= false;
149 }
150 }
151
152 return success;
153 }
154
155 void server_startup_st::restart()
156 {
157 for (std::vector<Server *>::iterator iter= servers.begin(); iter != servers.end(); ++iter)
158 {
159 (*iter)->start();
160 }
161 }
162
163 #define MAGIC_MEMORY 123575
164 server_startup_st::server_startup_st() :
165 _magic(MAGIC_MEMORY),
166 _socket(false),
167 _sasl(false),
168 _count(0),
169 udp(0),
170 _servers_to_run(5)
171 { }
172
173 server_startup_st::~server_startup_st()
174 {
175 clear();
176 }
177
178 bool server_startup_st::validate()
179 {
180 return _magic == MAGIC_MEMORY;
181 }
182
183 bool server_startup(server_startup_st& construct, const std::string& server_type, in_port_t try_port, int argc, const char *argv[], const bool opt_startup_message)
184 {
185 return construct.start_server(server_type, try_port, argc, argv, opt_startup_message);
186 }
187
188 bool server_startup_st::start_server(const std::string& server_type, in_port_t try_port, int argc, const char *argv[], const bool opt_startup_message)
189 {
190 if (try_port <= 0)
191 {
192 throw libtest::fatal(LIBYATL_DEFAULT_PARAM, "was passed the invalid port number %d", int(try_port));
193 }
194
195 libtest::Server *server= NULL;
196 try {
197 if (0)
198 { }
199 else if (server_type.compare("gearmand") == 0)
200 {
201 if (GEARMAND_BINARY)
202 {
203 if (HAVE_LIBGEARMAN)
204 {
205 server= build_gearmand("localhost", try_port);
206 }
207 }
208 }
209 else if (server_type.compare("hostile-gearmand") == 0)
210 {
211 if (GEARMAND_BINARY)
212 {
213 if (HAVE_LIBGEARMAN)
214 {
215 server= build_gearmand("localhost", try_port, "gearmand/hostile_gearmand");
216 }
217 }
218 }
219 else if (server_type.compare("drizzled") == 0)
220 {
221 if (DRIZZLED_BINARY)
222 {
223 if (HAVE_LIBDRIZZLE)
224 {
225 server= build_drizzled("localhost", try_port);
226 }
227 }
228 }
229 else if (server_type.compare("blobslap_worker") == 0)
230 {
231 if (GEARMAND_BINARY)
232 {
233 if (GEARMAND_BLOBSLAP_WORKER)
234 {
235 if (HAVE_LIBGEARMAN)
236 {
237 server= build_blobslap_worker(try_port);
238 }
239 }
240 }
241 }
242 else if (server_type.compare("memcached-sasl") == 0)
243 {
244 if (MEMCACHED_SASL_BINARY)
245 {
246 if (HAVE_LIBMEMCACHED)
247 {
248 server= build_memcached_sasl("localhost", try_port, username(), password());
249 }
250 }
251 }
252 else if (server_type.compare("memcached") == 0)
253 {
254 if (HAVE_MEMCACHED_BINARY)
255 {
256 if (HAVE_LIBMEMCACHED)
257 {
258 server= build_memcached("localhost", try_port);
259 }
260 }
261 }
262 else if (server_type.compare("memcached-light") == 0)
263 {
264 if (MEMCACHED_LIGHT_BINARY)
265 {
266 if (HAVE_LIBMEMCACHED)
267 {
268 server= build_memcached_light("localhost", try_port);
269 }
270 }
271 }
272
273 if (server == NULL)
274 {
275 throw libtest::fatal(LIBYATL_DEFAULT_PARAM, "Launching of an unknown server was attempted: %s", server_type.c_str());
276 }
277
278 /*
279 We will now cycle the server we have created.
280 */
281 if (server->cycle() == false)
282 {
283 Error << "Could not start up server " << *server;
284 delete server;
285 return false;
286 }
287
288 server->build(argc, argv);
289
290 #if 0
291 if (false)
292 {
293 Out << "Pausing for startup, hit return when ready.";
294 std::string gdb_command= server->base_command();
295 getchar();
296 }
297 else
298 #endif
299 if (server->start() == false)
300 {
301 delete server;
302 return false;
303 }
304 else
305 {
306 if (opt_startup_message)
307 {
308 Outn();
309 Out << "STARTING SERVER(pid:" << server->pid() << "): " << server->running();
310 Outn();
311 }
312 }
313 }
314 catch (libtest::disconnected err)
315 {
316 stream::cerr(err.file(), err.line(), err.func()) << err.what();
317 delete server;
318 return false;
319 }
320 catch (...)
321 {
322 delete server;
323 throw;
324 }
325
326 push_server(server);
327
328 return true;
329 }
330
331 bool server_startup_st::start_socket_server(const std::string& server_type, const in_port_t try_port, int argc,
332 const char *argv[],
333 const bool opt_startup_message)
334 {
335 (void)try_port;
336 Outn();
337
338 Server *server= NULL;
339 try {
340 if (0)
341 { }
342 else if (server_type.compare("gearmand") == 0)
343 {
344 Error << "Socket files are not supported for gearmand yet";
345 }
346 else if (server_type.compare("memcached-sasl") == 0)
347 {
348 if (MEMCACHED_SASL_BINARY)
349 {
350 if (HAVE_LIBMEMCACHED)
351 {
352 server= build_memcached_sasl_socket("localhost", try_port, username(), password());
353 }
354 else
355 {
356 Error << "Libmemcached was not found";
357 }
358 }
359 else
360 {
361 Error << "No memcached binary is available";
362 }
363 }
364 else if (server_type.compare("memcached") == 0)
365 {
366 if (MEMCACHED_BINARY)
367 {
368 if (HAVE_LIBMEMCACHED)
369 {
370 server= build_memcached_socket("localhost", try_port);
371 }
372 else
373 {
374 Error << "Libmemcached was not found";
375 }
376 }
377 else
378 {
379 Error << "No memcached binary is available";
380 }
381 }
382 else
383 {
384 Error << "Failed to start " << server_type << ", no support was found to be compiled in for it.";
385 }
386
387 if (server == NULL)
388 {
389 Error << "Failure occured while creating server: " << server_type;
390 return false;
391 }
392
393 /*
394 We will now cycle the server we have created.
395 */
396 if (server->cycle() == false)
397 {
398 Error << "Could not start up server " << *server;
399 delete server;
400 return false;
401 }
402
403 server->build(argc, argv);
404
405 #if 0
406 if (false)
407 {
408 Out << "Pausing for startup, hit return when ready.";
409 std::string gdb_command= server->base_command();
410 std::string options;
411 Out << "run " << server->args(options);
412 getchar();
413 }
414 else
415 #endif
416 if (server->start() == false)
417 {
418 Error << "Failed to start " << *server;
419 delete server;
420 return false;
421 }
422 else
423 {
424 if (opt_startup_message)
425 {
426 Outn();
427 Out << "STARTING SERVER(pid:" << server->pid() << "): " << server->running();
428 Outn();
429 }
430 }
431 }
432 catch (...)
433 {
434 delete server;
435 throw;
436 }
437
438 push_server(server);
439
440 set_default_socket(server->socket().c_str());
441
442 Outn();
443
444 return true;
445 }
446
447 std::string server_startup_st::option_string() const
448 {
449 std::string temp= server_list;
450 rtrim(temp);
451 return temp;
452 }
453
454
455 } // namespace libtest