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(_pid
))
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)
141 if (kill(current_pid
))
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(_pid
))
179 Error
<< "Could not kill() existing server during start() pid:" << _pid
;
182 assert(not has_pid());
185 if (not command(_running
))
187 Error
<< "Could not build command()";
191 if (is_valgrind() or is_helgrind())
196 if (system(_running
.c_str()) == -1)
198 Error
<< "system() failed:" << strerror(errno
);
208 if (pid_file_option() and not pid_file().empty())
210 Wait
wait(pid_file());
212 if (not wait
.successful())
214 Error
<< "Unable to open pidfile: " << pid_file();
218 int count
= is_helgrind() ? 20 : 5;
219 while (not ping() and --count
)
226 Error
<< "Failed to ping() server once started:" << *this;
231 // A failing get_pid() at this point is considered an error
237 void Server::reset_pid()
249 bool Server::set_socket_file()
251 char file_buffer
[FILENAME_MAX
];
254 if (broken_pid_file())
256 snprintf(file_buffer
, sizeof(file_buffer
), "/tmp/%s.socketXXXXXX", name());
260 snprintf(file_buffer
, sizeof(file_buffer
), "tests/var/run/%s.socketXXXXXX", name());
264 if ((fd
= mkstemp(file_buffer
)) == -1)
272 _socket
= file_buffer
;
277 bool Server::set_pid_file()
279 char file_buffer
[FILENAME_MAX
];
282 if (broken_pid_file())
284 snprintf(file_buffer
, sizeof(file_buffer
), "/tmp/%s.pidXXXXXX", name());
288 snprintf(file_buffer
, sizeof(file_buffer
), "tests/var/run/%s.pidXXXXXX", name());
292 if ((fd
= mkstemp(file_buffer
)) == -1)
300 _pid_file
= file_buffer
;
305 bool Server::set_log_file()
307 char file_buffer
[FILENAME_MAX
];
310 snprintf(file_buffer
, sizeof(file_buffer
), "tests/var/log/%s.logXXXXXX", name());
312 if ((fd
= mkstemp(file_buffer
)) == -1)
319 _log_file
= file_buffer
;
324 void Server::rebuild_base_command()
326 _base_command
.clear();
329 _base_command
+= "./libtool --mode=execute ";
334 _base_command
+= "gdb ";
336 else if (is_valgrind())
338 _base_command
+= "valgrind --log-file=tests/var/tmp/valgrind.out --error-exitcode=1 --leak-check=yes --show-reachable=yes --track-fds=yes --malloc-fill=A5 --free-fill=DE ";
341 else if (is_helgrind())
343 _base_command
+= "valgrind --log-file=tests/var/tmp/helgrind.out --tool=helgrind --read-var-info=yes --error-exitcode=1 -v ";
346 _base_command
+= executable();
349 void Server::set_extra_args(const std::string
&arg
)
354 bool Server::args(std::string
& options
)
356 std::stringstream arg_buffer
;
358 // Set a log file if it was requested (and we can)
359 if (getenv("LIBTEST_LOG") and log_file_option())
361 if (not set_log_file())
364 arg_buffer
<< " " << log_file_option() << _log_file
;
368 if (pid_file_option())
370 if (not set_pid_file())
373 arg_buffer
<< " " << pid_file_option() << pid_file();
376 assert(daemon_file_option());
377 if (daemon_file_option() and not is_valgrind() and not is_helgrind())
379 arg_buffer
<< " " << daemon_file_option();
382 if (_is_socket
and socket_file_option())
384 if (not set_socket_file())
387 arg_buffer
<< " " << socket_file_option() << "\"" << _socket
<< "\"";
390 assert(port_option());
391 if (port_option() and _port
> 0)
393 arg_buffer
<< " " << port_option() << _port
;
396 options
+= arg_buffer
.str();
398 if (not _extra_args
.empty())
399 options
+= _extra_args
;
404 bool Server::is_debug() const
406 return bool(getenv("LIBTEST_MANUAL_GDB"));
409 bool Server::is_valgrind() const
411 return bool(getenv("LIBTEST_MANUAL_VALGRIND"));
414 bool Server::is_helgrind() const
416 return bool(getenv("LIBTEST_MANUAL_HELGRIND"));
419 bool Server::kill(pid_t pid_arg
)
421 if (check_pid(pid_arg
) and kill_pid(pid_arg
)) // If we kill it, reset
423 if (broken_pid_file() and not pid_file().empty())
425 unlink(pid_file().c_str());
436 void server_startup_st::push_server(Server
*arg
)
438 servers
.push_back(arg
);
440 char port_str
[NI_MAXSERV
];
441 snprintf(port_str
, sizeof(port_str
), "%u", int(arg
->port()));
443 std::string server_config_string
;
444 if (arg
->has_socket())
446 server_config_string
+= "--socket=";
447 server_config_string
+= '"';
448 server_config_string
+= arg
->socket();
449 server_config_string
+= '"';
450 server_config_string
+= " ";
454 server_config_string
+= "--server=";
455 server_config_string
+= arg
->hostname();
456 server_config_string
+= ":";
457 server_config_string
+= port_str
;
458 server_config_string
+= " ";
461 server_list
+= server_config_string
;
465 Server
* server_startup_st::pop_server()
467 Server
*tmp
= servers
.back();
472 void server_startup_st::shutdown(bool remove
)
476 for (std::vector
<Server
*>::iterator iter
= servers
.begin(); iter
!= servers
.end(); iter
++)
484 for (std::vector
<Server
*>::iterator iter
= servers
.begin(); iter
!= servers
.end(); iter
++)
486 if ((*iter
)->has_pid() and not (*iter
)->kill((*iter
)->pid()))
488 Error
<< "Unable to kill:" << *(*iter
);
494 server_startup_st::~server_startup_st()
499 bool server_startup_st::is_debug() const
501 return bool(getenv("LIBTEST_MANUAL_GDB"));
504 bool server_startup_st::is_valgrind() const
506 return bool(getenv("LIBTEST_MANUAL_VALGRIND"));
509 bool server_startup_st::is_helgrind() const
511 return bool(getenv("LIBTEST_MANUAL_HELGRIND"));
515 bool server_startup(server_startup_st
& construct
, const std::string
& server_type
, in_port_t try_port
, int argc
, const char *argv
[])
519 // Look to see if we are being provided ports to use
521 char variable_buffer
[1024];
522 snprintf(variable_buffer
, sizeof(variable_buffer
), "LIBTEST_PORT_%lu", (unsigned long)construct
.count());
525 if ((var
= getenv(variable_buffer
)))
527 in_port_t tmp
= in_port_t(atoi(var
));
534 Server
*server
= NULL
;
537 else if (server_type
.compare("gearmand") == 0)
539 #ifdef GEARMAND_BINARY
540 #ifdef HAVE_LIBGEARMAN
541 server
= build_gearmand("localhost", try_port
);
543 Error
<< "Libgearman was not found";
546 Error
<< "No gearmand binary is available";
549 else if (server_type
.compare("memcached") == 0)
551 #ifdef MEMCACHED_BINARY
552 #ifdef HAVE_LIBMEMCACHED
553 server
= build_memcached("localhost", try_port
);
555 Error
<< "Libmemcached was not found";
558 Error
<< "No memcached binary is available";
563 Error
<< "Failed to start " << server_type
<< ", no support was found to be compiled in for it.";
568 Error
<< "Failure occured while creating server: " << server_type
;
573 We will now cycle the server we have created.
575 if (not server
->cycle())
577 Error
<< "Could not start up server " << *server
;
582 server
->build(argc
, argv
);
584 if (construct
.is_debug())
586 Out
<< "Pausing for startup, hit return when ready.";
587 std::string gdb_command
= server
->base_command();
589 Out
<< "run " << server
->args(options
);
592 else if (not server
->start())
594 Error
<< "Failed to start " << *server
;
600 Out
<< "STARTING SERVER(pid:" << server
->pid() << "): " << server
->running();
603 construct
.push_server(server
);
605 if (default_port() == 0)
607 assert(server
->has_port());
608 set_default_port(server
->port());
616 bool server_startup_st::start_socket_server(const std::string
& server_type
, const in_port_t try_port
, int argc
, const char *argv
[])
621 Server
*server
= NULL
;
624 else if (server_type
.compare("gearmand") == 0)
626 Error
<< "Socket files are not supported for gearmand yet";
628 else if (server_type
.compare("memcached") == 0)
630 #ifdef MEMCACHED_BINARY
631 #ifdef HAVE_LIBMEMCACHED
632 server
= build_memcached_socket("localhost", try_port
);
634 Error
<< "Libmemcached was not found";
637 Error
<< "No memcached binary is available";
642 Error
<< "Failed to start " << server_type
<< ", no support was found to be compiled in for it.";
647 Error
<< "Failure occured while creating server: " << server_type
;
652 We will now cycle the server we have created.
654 if (not server
->cycle())
656 Error
<< "Could not start up server " << *server
;
661 server
->build(argc
, argv
);
665 Out
<< "Pausing for startup, hit return when ready.";
666 std::string gdb_command
= server
->base_command();
668 Out
<< "run " << server
->args(options
);
671 else if (not server
->start())
673 Error
<< "Failed to start " << *server
;
679 Out
<< "STARTING SERVER(pid:" << server
->pid() << "): " << server
->running();
684 set_default_socket(server
->socket().c_str());
691 } // namespace libtest