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>
63 #ifdef HAVE_LIBMEMCACHED
64 #include <libtest/memcached.h>
69 std::ostream
& operator<<(std::ostream
& output
, const Server
&arg
)
73 output
<< arg
.hostname();
77 output
<< arg
.hostname() << ":" << arg
.port();
82 output
<< " Pid:" << arg
.pid();
87 output
<< " Socket:" << arg
.socket();
90 if (not arg
.running().empty())
92 output
<< " Exec:" << arg
.running();
96 return output
; // for multiple << operators
99 void Server::nap(void)
104 struct timespec global_sleep_value
= { 0, 50000 };
105 nanosleep(&global_sleep_value
, NULL
);
109 Server::Server(const std::string
& host_arg
, const in_port_t port_arg
, bool is_socket_arg
) :
110 _is_socket(is_socket_arg
),
119 if (has_pid() and not kill())
121 Error
<< "Unable to kill:" << *this;
125 std::string
server_startup_st::option_string() const
127 std::string temp
= server_list
;
132 // If the server exists, kill it
137 // Try to ping, and kill the server #limit number of times
139 while (--limit
and (current_pid
= get_pid()) != -1)
143 Log
<< "Killed existing server," << *this << " with pid:" << current_pid
;
149 // For whatever reason we could not kill it, and we reached limit
152 Error
<< "Reached limit, could not kill server pid:" << current_pid
;
159 // Grab a one off command
160 bool Server::command(std::string
& command_arg
)
162 rebuild_base_command();
164 command_arg
+= _base_command
;
166 if (args(command_arg
))
176 // If we find that we already have a pid then kill it.
177 if (has_pid() and not kill())
179 Error
<< "Could not kill() existing server during start()";
182 assert(not has_pid());
185 if (not command(_running
))
187 Error
<< "Could not build command()";
191 if (system(_running
.c_str()) == -1)
193 Error
<< "system() failed:" << strerror(errno
);
198 if (pid_file_option() and not pid_file().empty())
200 Wait
wait(pid_file());
202 if (not wait
.successful())
204 Error
<< "Unable to open pidfile: " << pid_file();
209 while (not ping() and --count
)
216 Error
<< "Failed to ping() server once started:" << *this;
221 // A failing get_pid() at this point is considered an error
222 _pid
= get_pid(false);
227 void Server::reset_pid()
239 bool Server::set_socket_file()
241 char file_buffer
[FILENAME_MAX
];
244 if (broken_pid_file())
246 snprintf(file_buffer
, sizeof(file_buffer
), "/tmp/%s.socketXXXXXX", name());
250 snprintf(file_buffer
, sizeof(file_buffer
), "tests/var/run/%s.socketXXXXXX", name());
254 if ((fd
= mkstemp(file_buffer
)) == -1)
262 _socket
= file_buffer
;
267 bool Server::set_pid_file()
269 char file_buffer
[FILENAME_MAX
];
272 if (broken_pid_file())
274 snprintf(file_buffer
, sizeof(file_buffer
), "/tmp/%s.pidXXXXXX", name());
278 snprintf(file_buffer
, sizeof(file_buffer
), "tests/var/run/%s.pidXXXXXX", name());
282 if ((fd
= mkstemp(file_buffer
)) == -1)
290 _pid_file
= file_buffer
;
295 bool Server::set_log_file()
297 char file_buffer
[FILENAME_MAX
];
300 snprintf(file_buffer
, sizeof(file_buffer
), "tests/var/log/%s.logXXXXXX", name());
302 if ((fd
= mkstemp(file_buffer
)) == -1)
309 _log_file
= file_buffer
;
314 void Server::rebuild_base_command()
316 _base_command
.clear();
319 _base_command
+= "./libtool --mode=execute ";
324 _base_command
+= "gdb ";
326 else if (is_valgrind())
328 _base_command
+= "valgrind --log-file=tests/var/tmp/valgrind.out --leak-check=full --show-reachable=yes ";
331 _base_command
+= executable();
334 void Server::set_extra_args(const std::string
&arg
)
339 bool Server::args(std::string
& options
)
341 std::stringstream arg_buffer
;
343 // Set a log file if it was requested (and we can)
344 if (getenv("LIBTEST_LOG") and log_file_option())
346 if (not set_log_file())
349 arg_buffer
<< " " << log_file_option() << _log_file
;
353 if (pid_file_option())
355 if (not set_pid_file())
358 arg_buffer
<< " " << pid_file_option() << pid_file();
361 assert(daemon_file_option());
362 if (daemon_file_option())
364 arg_buffer
<< " " << daemon_file_option();
367 if (_is_socket
and socket_file_option())
369 if (not set_socket_file())
372 arg_buffer
<< " " << socket_file_option() << "\"" << _socket
<< "\"";
375 assert(port_option());
376 if (port_option() and _port
> 0)
378 arg_buffer
<< " " << port_option() << _port
;
381 options
+= arg_buffer
.str();
383 if (not _extra_args
.empty())
384 options
+= _extra_args
;
389 bool Server::is_debug() const
391 return bool(getenv("LIBTEST_MANUAL_GDB"));
394 bool Server::is_valgrind() const
396 return bool(getenv("LIBTEST_MANUAL_VALGRIND"));
401 if (has_pid() and kill_pid(_pid
)) // If we kill it, reset
403 if (broken_pid_file() and not pid_file().empty())
405 unlink(pid_file().c_str());
416 void server_startup_st::push_server(Server
*arg
)
418 servers
.push_back(arg
);
420 char port_str
[NI_MAXSERV
];
421 snprintf(port_str
, sizeof(port_str
), "%u", int(arg
->port()));
423 std::string server_config_string
;
424 if (arg
->has_socket())
426 server_config_string
+= "--socket=";
427 server_config_string
+= '"';
428 server_config_string
+= arg
->socket();
429 server_config_string
+= '"';
430 server_config_string
+= " ";
434 server_config_string
+= "--server=";
435 server_config_string
+= arg
->hostname();
436 server_config_string
+= ":";
437 server_config_string
+= port_str
;
438 server_config_string
+= " ";
441 server_list
+= server_config_string
;
445 Server
* server_startup_st::pop_server()
447 Server
*tmp
= servers
.back();
452 void server_startup_st::shutdown(bool remove
)
456 for (std::vector
<Server
*>::iterator iter
= servers
.begin(); iter
!= servers
.end(); iter
++)
464 for (std::vector
<Server
*>::iterator iter
= servers
.begin(); iter
!= servers
.end(); iter
++)
466 if ((*iter
)->has_pid() and not (*iter
)->kill())
468 Error
<< "Unable to kill:" << *(*iter
);
474 server_startup_st::~server_startup_st()
479 bool server_startup_st::is_debug() const
481 return bool(getenv("LIBTEST_MANUAL_GDB"));
484 bool server_startup_st::is_valgrind() const
486 return bool(getenv("LIBTEST_MANUAL_VALGRIND"));
489 bool server_startup(server_startup_st
& construct
, const std::string
& server_type
, in_port_t try_port
, int argc
, const char *argv
[])
493 // Look to see if we are being provided ports to use
495 char variable_buffer
[1024];
496 snprintf(variable_buffer
, sizeof(variable_buffer
), "LIBTEST_PORT_%lu", (unsigned long)construct
.count());
499 if ((var
= getenv(variable_buffer
)))
501 in_port_t tmp
= in_port_t(atoi(var
));
508 Server
*server
= NULL
;
511 else if (server_type
.compare("gearmand") == 0)
513 #ifdef GEARMAND_BINARY
514 #ifdef HAVE_LIBGEARMAN
515 server
= build_gearmand("localhost", try_port
);
517 Error
<< "Libgearman was not found";
520 Error
<< "No gearmand binary is available";
523 else if (server_type
.compare("memcached") == 0)
525 #ifdef MEMCACHED_BINARY
526 #ifdef HAVE_LIBMEMCACHED
527 server
= build_memcached("localhost", try_port
);
529 Error
<< "Libmemcached was not found";
532 Error
<< "No memcached binary is available";
537 Error
<< "Failed to start " << server_type
<< ", no support was found to be compiled in for it.";
542 Error
<< "Failure occured while creating server: " << server_type
;
547 We will now cycle the server we have created.
549 if (not server
->cycle())
551 Error
<< "Could not start up server " << *server
;
556 server
->build(argc
, argv
);
558 if (construct
.is_debug())
560 Log
<< "Pausing for startup, hit return when ready.";
561 std::string gdb_command
= server
->base_command();
563 Log
<< "run " << server
->args(options
);
566 else if (not server
->start())
568 Error
<< "Failed to start " << *server
;
574 Log
<< "STARTING SERVER(pid:" << server
->pid() << "): " << server
->running();
577 construct
.push_server(server
);
579 if (default_port() == 0)
581 assert(server
->has_port());
582 set_default_port(server
->port());
590 bool server_startup_st::start_socket_server(const std::string
& server_type
, const in_port_t try_port
, int argc
, const char *argv
[])
594 Server
*server
= NULL
;
597 else if (server_type
.compare("gearmand") == 0)
599 Error
<< "Socket files are not supported for gearmand yet";
601 else if (server_type
.compare("memcached") == 0)
603 #ifdef MEMCACHED_BINARY
604 #ifdef HAVE_LIBMEMCACHED
605 server
= build_memcached_socket("localhost", try_port
);
607 Error
<< "Libmemcached was not found";
610 Error
<< "No memcached binary is available";
615 Error
<< "Failed to start " << server_type
<< ", no support was found to be compiled in for it.";
620 Error
<< "Failure occured while creating server: " << server_type
;
625 We will now cycle the server we have created.
627 if (not server
->cycle())
629 Error
<< "Could not start up server " << *server
;
634 server
->build(argc
, argv
);
638 Log
<< "Pausing for startup, hit return when ready.";
639 std::string gdb_command
= server
->base_command();
641 Log
<< "run " << server
->args(options
);
644 else if (not server
->start())
646 Error
<< "Failed to start " << *server
;
652 Log
<< "STARTING SERVER(pid:" << server
->pid() << "): " << server
->running();
657 set_default_socket(server
->socket().c_str());
664 } // namespace libtest