1 /* vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
5 * Copyright (C) 2011 Data Differential, http://datadifferential.com/
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions are
11 * * Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
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
19 * * The names of its contributors may not be used to endorse or
20 * promote products derived from this software without specific prior
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.
37 #include <libtest/common.h>
49 static inline std::string
&rtrim(std::string
&s
)
51 s
.erase(std::find_if(s
.rbegin(), s
.rend(), std::not1(std::ptr_fun
<int, int>(std::isspace
))).base(), s
.end());
55 #include <libtest/server.h>
56 #include <libtest/stream.h>
57 #include <libtest/killpid.h>
59 #ifdef HAVE_LIBGEARMAN
60 #include <libtest/gearmand.h>
61 #include <libtest/blobslap_worker.h>
64 #ifdef HAVE_LIBMEMCACHED
65 #include <libtest/memcached.h>
70 std::ostream
& operator<<(std::ostream
& output
, const Server
&arg
)
74 output
<< arg
.hostname();
78 output
<< arg
.hostname() << ":" << arg
.port();
83 output
<< " Pid:" << arg
.pid();
88 output
<< " Socket:" << arg
.socket();
91 if (not arg
.running().empty())
93 output
<< " Exec:" << arg
.running();
97 return output
; // for multiple << operators
100 void Server::nap(void)
105 struct timespec global_sleep_value
= { 0, 50000 };
106 nanosleep(&global_sleep_value
, NULL
);
110 Server::Server(const std::string
& host_arg
, const in_port_t port_arg
, bool is_socket_arg
) :
111 _is_socket(is_socket_arg
),
120 if (has_pid() and not kill(_pid
))
122 Error
<< "Unable to kill:" << *this;
126 std::string
server_startup_st::option_string() const
128 std::string temp
= server_list
;
133 // If the server exists, kill it
138 // Try to ping, and kill the server #limit number of times
140 while (--limit
and (current_pid
= get_pid()) != -1)
142 if (kill(current_pid
))
144 Log
<< "Killed existing server," << *this << " with pid:" << current_pid
;
150 // For whatever reason we could not kill it, and we reached limit
153 Error
<< "Reached limit, could not kill server pid:" << current_pid
;
160 // Grab a one off command
161 bool Server::command(std::string
& command_arg
)
163 rebuild_base_command();
165 command_arg
+= _base_command
;
167 if (args(command_arg
))
177 // If we find that we already have a pid then kill it.
178 if (has_pid() and not kill(_pid
))
180 Error
<< "Could not kill() existing server during start() pid:" << _pid
;
183 assert(not has_pid());
186 if (not command(_running
))
188 Error
<< "Could not build command()";
192 if (is_valgrind() or is_helgrind())
197 if (system(_running
.c_str()) == -1)
199 Error
<< "system() failed:" << strerror(errno
);
209 if (pid_file_option() and not pid_file().empty())
211 Wait
wait(pid_file());
213 if (not wait
.successful())
215 Error
<< "Unable to open pidfile: " << pid_file();
219 int count
= is_helgrind() ? 20 : 5;
220 while (not ping() and --count
)
227 Error
<< "Failed to ping() server once started:" << *this;
232 // A failing get_pid() at this point is considered an error
238 void Server::reset_pid()
250 bool Server::set_socket_file()
252 char file_buffer
[FILENAME_MAX
];
255 if (broken_pid_file())
257 snprintf(file_buffer
, sizeof(file_buffer
), "/tmp/%s.socketXXXXXX", name());
261 snprintf(file_buffer
, sizeof(file_buffer
), "tests/var/run/%s.socketXXXXXX", name());
265 if ((fd
= mkstemp(file_buffer
)) == -1)
273 _socket
= file_buffer
;
278 bool Server::set_pid_file()
280 char file_buffer
[FILENAME_MAX
];
283 if (broken_pid_file())
285 snprintf(file_buffer
, sizeof(file_buffer
), "/tmp/%s.pidXXXXXX", name());
289 snprintf(file_buffer
, sizeof(file_buffer
), "tests/var/run/%s.pidXXXXXX", name());
293 if ((fd
= mkstemp(file_buffer
)) == -1)
301 _pid_file
= file_buffer
;
306 bool Server::set_log_file()
308 char file_buffer
[FILENAME_MAX
];
311 snprintf(file_buffer
, sizeof(file_buffer
), "tests/var/log/%s.logXXXXXX", name());
313 if ((fd
= mkstemp(file_buffer
)) == -1)
320 _log_file
= file_buffer
;
325 void Server::rebuild_base_command()
327 _base_command
.clear();
328 if (is_libtool() and getenv("LIBTOOL_COMMAND"))
330 _base_command
+= getenv("LIBTOOL_COMMAND");
334 if (is_debug() and getenv("GDB_COMMAND"))
336 _base_command
+= getenv("GDB_COMMAND");
339 else if (is_valgrind() and getenv("VALGRIND_COMMAND"))
341 _base_command
+= getenv("VALGRIND_COMMAND");
344 else if (is_helgrind() and getenv("HELGRIND_COMMAND"))
346 _base_command
+= getenv("HELGRIND_COMMAND");
350 _base_command
+= executable();
353 void Server::set_extra_args(const std::string
&arg
)
358 bool Server::args(std::string
& options
)
360 std::stringstream arg_buffer
;
362 // Set a log file if it was requested (and we can)
363 if (getenv("LIBTEST_LOG") and log_file_option())
365 if (not set_log_file())
370 arg_buffer
<< " " << log_file_option() << _log_file
;
374 if (pid_file_option())
376 if (not set_pid_file())
381 arg_buffer
<< " " << pid_file_option() << pid_file();
384 assert(daemon_file_option());
385 if (daemon_file_option() and not is_valgrind() and not is_helgrind())
387 arg_buffer
<< " " << daemon_file_option();
390 if (_is_socket
and socket_file_option())
392 if (not set_socket_file())
397 arg_buffer
<< " " << socket_file_option() << "\"" << _socket
<< "\"";
400 assert(port_option());
401 if (port_option() and _port
> 0)
403 arg_buffer
<< " " << port_option() << _port
;
406 options
+= arg_buffer
.str();
408 if (not _extra_args
.empty())
410 options
+= _extra_args
;
416 bool Server::is_debug() const
418 return bool(getenv("LIBTEST_MANUAL_GDB"));
421 bool Server::is_valgrind() const
423 return bool(getenv("LIBTEST_MANUAL_VALGRIND"));
426 bool Server::is_helgrind() const
428 return bool(getenv("LIBTEST_MANUAL_HELGRIND"));
431 bool Server::kill(pid_t pid_arg
)
433 if (check_pid(pid_arg
) and kill_pid(pid_arg
)) // If we kill it, reset
435 if (broken_pid_file() and not pid_file().empty())
437 unlink(pid_file().c_str());
448 void server_startup_st::push_server(Server
*arg
)
450 servers
.push_back(arg
);
452 char port_str
[NI_MAXSERV
];
453 snprintf(port_str
, sizeof(port_str
), "%u", int(arg
->port()));
455 std::string server_config_string
;
456 if (arg
->has_socket())
458 server_config_string
+= "--socket=";
459 server_config_string
+= '"';
460 server_config_string
+= arg
->socket();
461 server_config_string
+= '"';
462 server_config_string
+= " ";
466 server_config_string
+= "--server=";
467 server_config_string
+= arg
->hostname();
468 server_config_string
+= ":";
469 server_config_string
+= port_str
;
470 server_config_string
+= " ";
473 server_list
+= server_config_string
;
477 Server
* server_startup_st::pop_server()
479 Server
*tmp
= servers
.back();
484 void server_startup_st::shutdown(bool remove
)
488 for (std::vector
<Server
*>::iterator iter
= servers
.begin(); iter
!= servers
.end(); iter
++)
496 for (std::vector
<Server
*>::iterator iter
= servers
.begin(); iter
!= servers
.end(); iter
++)
498 if ((*iter
)->has_pid() and not (*iter
)->kill((*iter
)->pid()))
500 Error
<< "Unable to kill:" << *(*iter
);
506 server_startup_st::~server_startup_st()
511 bool server_startup_st::is_debug() const
513 return bool(getenv("LIBTEST_MANUAL_GDB"));
516 bool server_startup_st::is_valgrind() const
518 return bool(getenv("LIBTEST_MANUAL_VALGRIND"));
521 bool server_startup_st::is_helgrind() const
523 return bool(getenv("LIBTEST_MANUAL_HELGRIND"));
527 bool server_startup(server_startup_st
& construct
, const std::string
& server_type
, in_port_t try_port
, int argc
, const char *argv
[])
531 // Look to see if we are being provided ports to use
533 char variable_buffer
[1024];
534 snprintf(variable_buffer
, sizeof(variable_buffer
), "LIBTEST_PORT_%lu", (unsigned long)construct
.count());
537 if ((var
= getenv(variable_buffer
)))
539 in_port_t tmp
= in_port_t(atoi(var
));
546 Server
*server
= NULL
;
549 else if (server_type
.compare("gearmand") == 0)
551 #ifdef GEARMAND_BINARY
552 #ifdef HAVE_LIBGEARMAN
553 server
= build_gearmand("localhost", try_port
);
555 Error
<< "Libgearman was not found";
558 Error
<< "No gearmand binary is available";
561 else if (server_type
.compare("blobslap_worker") == 0)
563 #ifdef GEARMAND_BINARY
564 #ifdef HAVE_LIBGEARMAN
565 server
= build_blobslap_worker(try_port
);
567 Error
<< "Libgearman was not found";
570 Error
<< "No gearmand binary is available";
573 else if (server_type
.compare("memcached") == 0)
575 #ifdef MEMCACHED_BINARY
576 #ifdef HAVE_LIBMEMCACHED
577 server
= build_memcached("localhost", try_port
);
579 Error
<< "Libmemcached was not found";
582 Error
<< "No memcached binary is available";
587 Error
<< "Failed to start " << server_type
<< ", no support was found to be compiled in for it.";
592 Error
<< "Failure occured while creating server: " << server_type
;
597 We will now cycle the server we have created.
599 if (not server
->cycle())
601 Error
<< "Could not start up server " << *server
;
606 server
->build(argc
, argv
);
608 if (construct
.is_debug())
610 Out
<< "Pausing for startup, hit return when ready.";
611 std::string gdb_command
= server
->base_command();
613 Out
<< "run " << server
->args(options
);
616 else if (not server
->start())
618 Error
<< "Failed to start " << *server
;
624 Out
<< "STARTING SERVER(pid:" << server
->pid() << "): " << server
->running();
627 construct
.push_server(server
);
629 if (default_port() == 0)
631 assert(server
->has_port());
632 set_default_port(server
->port());
640 bool server_startup_st::start_socket_server(const std::string
& server_type
, const in_port_t try_port
, int argc
, const char *argv
[])
645 Server
*server
= NULL
;
648 else if (server_type
.compare("gearmand") == 0)
650 Error
<< "Socket files are not supported for gearmand yet";
652 else if (server_type
.compare("memcached") == 0)
654 #ifdef MEMCACHED_BINARY
655 #ifdef HAVE_LIBMEMCACHED
656 server
= build_memcached_socket("localhost", try_port
);
658 Error
<< "Libmemcached was not found";
661 Error
<< "No memcached binary is available";
666 Error
<< "Failed to start " << server_type
<< ", no support was found to be compiled in for it.";
671 Error
<< "Failure occured while creating server: " << server_type
;
676 We will now cycle the server we have created.
678 if (not server
->cycle())
680 Error
<< "Could not start up server " << *server
;
685 server
->build(argc
, argv
);
689 Out
<< "Pausing for startup, hit return when ready.";
690 std::string gdb_command
= server
->base_command();
692 Out
<< "run " << server
->args(options
);
695 else if (not server
->start())
697 Error
<< "Failed to start " << *server
;
703 Out
<< "STARTING SERVER(pid:" << server
->pid() << "): " << server
->running();
708 set_default_socket(server
->socket().c_str());
715 } // namespace libtest