1 /* vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
5 * Copyright (C) 2011 Data Differential, http://datadifferential.com/
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.
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.
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
23 #include <libtest/common.h>
35 static inline std::string
&rtrim(std::string
&s
)
37 s
.erase(std::find_if(s
.rbegin(), s
.rend(), std::not1(std::ptr_fun
<int, int>(std::isspace
))).base(), s
.end());
41 #include <libtest/server.h>
42 #include <libtest/stream.h>
43 #include <libtest/killpid.h>
45 #ifdef HAVE_LIBGEARMAN
46 #include <libtest/gearmand.h>
47 #include <libtest/blobslap_worker.h>
50 #ifdef HAVE_LIBMEMCACHED
51 #include <libtest/memcached.h>
56 std::ostream
& operator<<(std::ostream
& output
, const Server
&arg
)
60 output
<< arg
.hostname();
64 output
<< arg
.hostname() << ":" << arg
.port();
69 output
<< " Pid:" << arg
.pid();
74 output
<< " Socket:" << arg
.socket();
77 if (not arg
.running().empty())
79 output
<< " Exec:" << arg
.running();
83 return output
; // for multiple << operators
86 void Server::nap(void)
91 struct timespec global_sleep_value
= { 0, 50000 };
92 nanosleep(&global_sleep_value
, NULL
);
96 Server::Server(const std::string
& host_arg
, const in_port_t port_arg
, bool is_socket_arg
) :
97 _is_socket(is_socket_arg
),
106 if (has_pid() and not kill(_pid
))
108 Error
<< "Unable to kill:" << *this;
112 std::string
server_startup_st::option_string() const
114 std::string temp
= server_list
;
119 // If the server exists, kill it
124 // Try to ping, and kill the server #limit number of times
126 while (--limit
and (current_pid
= get_pid()) != -1)
128 if (kill(current_pid
))
130 Log
<< "Killed existing server," << *this << " with pid:" << current_pid
;
136 // For whatever reason we could not kill it, and we reached limit
139 Error
<< "Reached limit, could not kill server pid:" << current_pid
;
146 // Grab a one off command
147 bool Server::command(std::string
& command_arg
)
149 rebuild_base_command();
151 command_arg
+= _base_command
;
153 if (args(command_arg
))
163 // If we find that we already have a pid then kill it.
164 if (has_pid() and not kill(_pid
))
166 Error
<< "Could not kill() existing server during start() pid:" << _pid
;
169 assert(not has_pid());
172 if (not command(_running
))
174 Error
<< "Could not build command()";
178 if (is_valgrind() or is_helgrind())
183 if (system(_running
.c_str()) == -1)
185 Error
<< "system() failed:" << strerror(errno
);
195 if (pid_file_option() and not pid_file().empty())
197 Wait
wait(pid_file());
199 if (not wait
.successful())
201 Error
<< "Unable to open pidfile: " << pid_file();
205 int count
= is_helgrind() ? 20 : 5;
206 while (not ping() and --count
)
213 Error
<< "Failed to ping() server once started:" << *this;
218 // A failing get_pid() at this point is considered an error
224 void Server::reset_pid()
236 bool Server::set_socket_file()
238 char file_buffer
[FILENAME_MAX
];
241 if (broken_pid_file())
243 snprintf(file_buffer
, sizeof(file_buffer
), "/tmp/%s.socketXXXXXX", name());
247 snprintf(file_buffer
, sizeof(file_buffer
), "tests/var/run/%s.socketXXXXXX", name());
251 if ((fd
= mkstemp(file_buffer
)) == -1)
259 _socket
= file_buffer
;
264 bool Server::set_pid_file()
266 char file_buffer
[FILENAME_MAX
];
269 if (broken_pid_file())
271 snprintf(file_buffer
, sizeof(file_buffer
), "/tmp/%s.pidXXXXXX", name());
275 snprintf(file_buffer
, sizeof(file_buffer
), "tests/var/run/%s.pidXXXXXX", name());
279 if ((fd
= mkstemp(file_buffer
)) == -1)
287 _pid_file
= file_buffer
;
292 bool Server::set_log_file()
294 char file_buffer
[FILENAME_MAX
];
297 snprintf(file_buffer
, sizeof(file_buffer
), "tests/var/log/%s.logXXXXXX", name());
299 if ((fd
= mkstemp(file_buffer
)) == -1)
306 _log_file
= file_buffer
;
311 void Server::rebuild_base_command()
313 _base_command
.clear();
314 if (is_libtool() and getenv("LIBTOOL_COMMAND"))
316 _base_command
+= getenv("LIBTOOL_COMMAND");
320 if (is_debug() and getenv("GDB_COMMAND"))
322 _base_command
+= getenv("GDB_COMMAND");
325 else if (is_valgrind() and getenv("VALGRIND_COMMAND"))
327 _base_command
+= getenv("VALGRIND_COMMAND");
330 else if (is_helgrind() and getenv("HELGRIND_COMMAND"))
332 _base_command
+= getenv("HELGRIND_COMMAND");
336 _base_command
+= executable();
339 void Server::set_extra_args(const std::string
&arg
)
344 bool Server::args(std::string
& options
)
346 std::stringstream arg_buffer
;
348 // Set a log file if it was requested (and we can)
349 if (getenv("LIBTEST_LOG") and log_file_option())
351 if (not set_log_file())
356 arg_buffer
<< " " << log_file_option() << _log_file
;
360 if (pid_file_option())
362 if (not set_pid_file())
367 arg_buffer
<< " " << pid_file_option() << pid_file();
370 assert(daemon_file_option());
371 if (daemon_file_option() and not is_valgrind() and not is_helgrind())
373 arg_buffer
<< " " << daemon_file_option();
376 if (_is_socket
and socket_file_option())
378 if (not set_socket_file())
383 arg_buffer
<< " " << socket_file_option() << "\"" << _socket
<< "\"";
386 assert(port_option());
387 if (port_option() and _port
> 0)
389 arg_buffer
<< " " << port_option() << _port
;
392 options
+= arg_buffer
.str();
394 if (not _extra_args
.empty())
396 options
+= _extra_args
;
402 bool Server::is_debug() const
404 return bool(getenv("LIBTEST_MANUAL_GDB"));
407 bool Server::is_valgrind() const
409 return bool(getenv("LIBTEST_MANUAL_VALGRIND"));
412 bool Server::is_helgrind() const
414 return bool(getenv("LIBTEST_MANUAL_HELGRIND"));
417 bool Server::kill(pid_t pid_arg
)
419 if (check_pid(pid_arg
) and kill_pid(pid_arg
)) // If we kill it, reset
421 if (broken_pid_file() and not pid_file().empty())
423 unlink(pid_file().c_str());
434 void server_startup_st::push_server(Server
*arg
)
436 servers
.push_back(arg
);
438 char port_str
[NI_MAXSERV
];
439 snprintf(port_str
, sizeof(port_str
), "%u", int(arg
->port()));
441 std::string server_config_string
;
442 if (arg
->has_socket())
444 server_config_string
+= "--socket=";
445 server_config_string
+= '"';
446 server_config_string
+= arg
->socket();
447 server_config_string
+= '"';
448 server_config_string
+= " ";
452 server_config_string
+= "--server=";
453 server_config_string
+= arg
->hostname();
454 server_config_string
+= ":";
455 server_config_string
+= port_str
;
456 server_config_string
+= " ";
459 server_list
+= server_config_string
;
463 Server
* server_startup_st::pop_server()
465 Server
*tmp
= servers
.back();
470 void server_startup_st::shutdown(bool remove
)
474 for (std::vector
<Server
*>::iterator iter
= servers
.begin(); iter
!= servers
.end(); iter
++)
482 for (std::vector
<Server
*>::iterator iter
= servers
.begin(); iter
!= servers
.end(); iter
++)
484 if ((*iter
)->has_pid() and not (*iter
)->kill((*iter
)->pid()))
486 Error
<< "Unable to kill:" << *(*iter
);
492 server_startup_st::~server_startup_st()
497 bool server_startup_st::is_debug() const
499 return bool(getenv("LIBTEST_MANUAL_GDB"));
502 bool server_startup_st::is_valgrind() const
504 return bool(getenv("LIBTEST_MANUAL_VALGRIND"));
507 bool server_startup_st::is_helgrind() const
509 return bool(getenv("LIBTEST_MANUAL_HELGRIND"));
513 bool server_startup(server_startup_st
& construct
, const std::string
& server_type
, in_port_t try_port
, int argc
, const char *argv
[])
517 // Look to see if we are being provided ports to use
519 char variable_buffer
[1024];
520 snprintf(variable_buffer
, sizeof(variable_buffer
), "LIBTEST_PORT_%lu", (unsigned long)construct
.count());
523 if ((var
= getenv(variable_buffer
)))
525 in_port_t tmp
= in_port_t(atoi(var
));
532 Server
*server
= NULL
;
535 else if (server_type
.compare("gearmand") == 0)
537 #ifdef GEARMAND_BINARY
538 #ifdef HAVE_LIBGEARMAN
539 server
= build_gearmand("localhost", try_port
);
541 Error
<< "Libgearman was not found";
544 Error
<< "No gearmand binary is available";
547 else if (server_type
.compare("blobslap_worker") == 0)
549 #ifdef GEARMAND_BINARY
550 #ifdef HAVE_LIBGEARMAN
551 server
= build_blobslap_worker(try_port
);
553 Error
<< "Libgearman was not found";
556 Error
<< "No gearmand binary is available";
559 else if (server_type
.compare("memcached") == 0)
561 #ifdef MEMCACHED_BINARY
562 #ifdef HAVE_LIBMEMCACHED
563 server
= build_memcached("localhost", try_port
);
565 Error
<< "Libmemcached was not found";
568 Error
<< "No memcached binary is available";
573 Error
<< "Failed to start " << server_type
<< ", no support was found to be compiled in for it.";
578 Error
<< "Failure occured while creating server: " << server_type
;
583 We will now cycle the server we have created.
585 if (not server
->cycle())
587 Error
<< "Could not start up server " << *server
;
592 server
->build(argc
, argv
);
594 if (construct
.is_debug())
596 Out
<< "Pausing for startup, hit return when ready.";
597 std::string gdb_command
= server
->base_command();
599 Out
<< "run " << server
->args(options
);
602 else if (not server
->start())
604 Error
<< "Failed to start " << *server
;
610 Out
<< "STARTING SERVER(pid:" << server
->pid() << "): " << server
->running();
613 construct
.push_server(server
);
615 if (default_port() == 0)
617 assert(server
->has_port());
618 set_default_port(server
->port());
626 bool server_startup_st::start_socket_server(const std::string
& server_type
, const in_port_t try_port
, int argc
, const char *argv
[])
631 Server
*server
= NULL
;
634 else if (server_type
.compare("gearmand") == 0)
636 Error
<< "Socket files are not supported for gearmand yet";
638 else if (server_type
.compare("memcached") == 0)
640 #ifdef MEMCACHED_BINARY
641 #ifdef HAVE_LIBMEMCACHED
642 server
= build_memcached_socket("localhost", try_port
);
644 Error
<< "Libmemcached was not found";
647 Error
<< "No memcached binary is available";
652 Error
<< "Failed to start " << server_type
<< ", no support was found to be compiled in for it.";
657 Error
<< "Failure occured while creating server: " << server_type
;
662 We will now cycle the server we have created.
664 if (not server
->cycle())
666 Error
<< "Could not start up server " << *server
;
671 server
->build(argc
, argv
);
675 Out
<< "Pausing for startup, hit return when ready.";
676 std::string gdb_command
= server
->base_command();
678 Out
<< "run " << server
->args(options
);
681 else if (not server
->start())
683 Error
<< "Failed to start " << *server
;
689 Out
<< "STARTING SERVER(pid:" << server
->pid() << "): " << server
->running();
694 set_default_socket(server
->socket().c_str());
701 } // namespace libtest