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
22
#include <libtest/common.h>
34
static inline std::string &rtrim(std::string &s)
36
s.erase(std::find_if(s.rbegin(), s.rend(), std::not1(std::ptr_fun<int, int>(std::isspace))).base(), s.end());
42
void server_startup_st::push_server(Server *arg)
44
servers.push_back(arg);
46
char port_str[NI_MAXSERV];
47
snprintf(port_str, sizeof(port_str), "%u", int(arg->port()));
49
std::string server_config_string;
50
if (arg->has_socket())
52
server_config_string+= "--socket=";
53
server_config_string+= '"';
54
server_config_string+= arg->socket();
55
server_config_string+= '"';
56
server_config_string+= " ";
60
server_config_string+= "--server=";
61
server_config_string+= arg->hostname();
62
server_config_string+= ":";
63
server_config_string+= port_str;
64
server_config_string+= " ";
67
server_list+= server_config_string;
71
Server* server_startup_st::pop_server()
73
Server *tmp= servers.back();
78
bool server_startup_st::shutdown(uint32_t number_of_host)
80
if (servers.size() > number_of_host)
82
Server* tmp= servers[number_of_host];
84
if (tmp and tmp->has_pid() and not tmp->kill(tmp->pid()))
95
void server_startup_st::shutdown_and_remove()
97
for (std::vector<Server *>::iterator iter= servers.begin(); iter != servers.end(); iter++)
104
void server_startup_st::shutdown()
106
for (std::vector<Server *>::iterator iter= servers.begin(); iter != servers.end(); iter++)
108
if ((*iter)->has_pid() and not (*iter)->kill((*iter)->pid()))
110
Error << "Unable to kill:" << *(*iter);
115
void server_startup_st::restart()
117
for (std::vector<Server *>::iterator iter= servers.begin(); iter != servers.end(); iter++)
123
server_startup_st::~server_startup_st()
125
shutdown_and_remove();
128
bool server_startup_st::is_debug() const
130
return bool(getenv("LIBTEST_MANUAL_GDB"));
133
bool server_startup_st::is_valgrind() const
135
return bool(getenv("LIBTEST_MANUAL_VALGRIND"));
138
bool server_startup_st::is_helgrind() const
140
return bool(getenv("LIBTEST_MANUAL_HELGRIND"));
144
bool server_startup(server_startup_st& construct, const std::string& server_type, in_port_t try_port, int argc, const char *argv[])
149
set_max_port(try_port);
151
// Look to see if we are being provided ports to use
153
char variable_buffer[1024];
154
snprintf(variable_buffer, sizeof(variable_buffer), "LIBTEST_PORT_%lu", (unsigned long)construct.count());
157
if ((var= getenv(variable_buffer)))
159
in_port_t tmp= in_port_t(atoi(var));
166
libtest::Server *server= NULL;
169
else if (server_type.compare("gearmand") == 0)
175
server= build_gearmand("127.0.0.1", try_port);
179
Error << "Libgearman was not found";
184
Error << "No gearmand binary is available";
187
else if (server_type.compare("blobslap_worker") == 0)
193
server= build_blobslap_worker(try_port);
197
Error << "Libgearman was not found";
202
Error << "No gearmand binary is available";
205
else if (server_type.compare("memcached-sasl") == 0)
207
if (MEMCACHED_SASL_BINARY)
209
if (HAVE_LIBMEMCACHED)
211
server= build_memcached_sasl("127.0.0.1", try_port, construct.username(), construct.password());
215
Error << "Libmemcached was not found";
220
Error << "No memcached binary that was compiled with sasl is available";
223
else if (server_type.compare("memcached") == 0)
225
if (MEMCACHED_BINARY)
227
if (HAVE_LIBMEMCACHED)
229
server= build_memcached("127.0.0.1", try_port);
233
Error << "Libmemcached was not found";
238
Error << "No memcached binary is available";
243
Error << "Failed to start " << server_type << ", no support was found to be compiled in for it.";
248
Error << "Failure occured while creating server: " << server_type;
253
We will now cycle the server we have created.
255
if (not server->cycle())
257
Error << "Could not start up server " << *server;
262
server->build(argc, argv);
264
if (construct.is_debug())
266
Out << "Pausing for startup, hit return when ready.";
267
std::string gdb_command= server->base_command();
269
Out << "run " << server->args(options);
272
else if (server->start() == false)
274
Error << "Failed to start " << *server;
280
Out << "STARTING SERVER(pid:" << server->pid() << "): " << server->running();
283
construct.push_server(server);
285
if (default_port() == 0)
287
assert(server->has_port());
288
set_default_port(server->port());
296
bool server_startup_st::start_socket_server(const std::string& server_type, const in_port_t try_port, int argc, const char *argv[])
301
Server *server= NULL;
304
else if (server_type.compare("gearmand") == 0)
306
Error << "Socket files are not supported for gearmand yet";
308
else if (server_type.compare("memcached-sasl") == 0)
310
if (MEMCACHED_SASL_BINARY)
312
if (HAVE_LIBMEMCACHED)
314
server= build_memcached_sasl_socket("127.0.0.1", try_port, username(), password());
318
Error << "Libmemcached was not found";
323
Error << "No memcached binary is available";
326
else if (server_type.compare("memcached") == 0)
328
if (MEMCACHED_BINARY)
330
if (HAVE_LIBMEMCACHED)
332
server= build_memcached_socket("127.0.0.1", try_port);
336
Error << "Libmemcached was not found";
341
Error << "No memcached binary is available";
346
Error << "Failed to start " << server_type << ", no support was found to be compiled in for it.";
351
Error << "Failure occured while creating server: " << server_type;
356
We will now cycle the server we have created.
358
if (not server->cycle())
360
Error << "Could not start up server " << *server;
365
server->build(argc, argv);
369
Out << "Pausing for startup, hit return when ready.";
370
std::string gdb_command= server->base_command();
372
Out << "run " << server->args(options);
375
else if (not server->start())
377
Error << "Failed to start " << *server;
383
Out << "STARTING SERVER(pid:" << server->pid() << "): " << server->running();
388
set_default_socket(server->socket().c_str());
395
std::string server_startup_st::option_string() const
397
std::string temp= server_list;
403
} // namespace libtest