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 void server_startup_st::shutdown(bool remove
)
82 for (std::vector
<Server
*>::iterator iter
= servers
.begin(); iter
!= servers
.end(); iter
++)
90 for (std::vector
<Server
*>::iterator iter
= servers
.begin(); iter
!= servers
.end(); iter
++)
92 if ((*iter
)->has_pid() and not (*iter
)->kill((*iter
)->pid()))
94 Error
<< "Unable to kill:" << *(*iter
);
100 server_startup_st::~server_startup_st()
105 bool server_startup_st::is_debug() const
107 return bool(getenv("LIBTEST_MANUAL_GDB"));
110 bool server_startup_st::is_valgrind() const
112 return bool(getenv("LIBTEST_MANUAL_VALGRIND"));
115 bool server_startup_st::is_helgrind() const
117 return bool(getenv("LIBTEST_MANUAL_HELGRIND"));
121 bool server_startup(server_startup_st
& construct
, const std::string
& server_type
, in_port_t try_port
, int argc
, const char *argv
[])
126 // Look to see if we are being provided ports to use
128 char variable_buffer
[1024];
129 snprintf(variable_buffer
, sizeof(variable_buffer
), "LIBTEST_PORT_%lu", (unsigned long)construct
.count());
132 if ((var
= getenv(variable_buffer
)))
134 in_port_t tmp
= in_port_t(atoi(var
));
141 libtest::Server
*server
= NULL
;
144 else if (server_type
.compare("gearmand") == 0)
150 server
= build_gearmand("localhost", try_port
);
154 Error
<< "Libgearman was not found";
159 Error
<< "No gearmand binary is available";
162 else if (server_type
.compare("blobslap_worker") == 0)
164 if (GEARMAND_BINARY
and GEARMAND_BLOBSLAP_WORKER
)
168 server
= build_blobslap_worker(try_port
);
172 Error
<< "Libgearman was not found";
177 Error
<< "No gearmand binary is available";
180 else if (server_type
.compare("memcached-sasl") == 0)
182 if (MEMCACHED_SASL_BINARY
)
184 if (HAVE_LIBMEMCACHED
)
186 server
= build_memcached_sasl("localhost", try_port
, construct
.username(), construct
.password());
190 Error
<< "Libmemcached was not found";
195 Error
<< "No memcached binary that was compiled with sasl is available";
198 else if (server_type
.compare("memcached") == 0)
200 if (MEMCACHED_BINARY
)
202 if (HAVE_LIBMEMCACHED
)
204 server
= build_memcached("localhost", try_port
);
208 Error
<< "Libmemcached was not found";
213 Error
<< "No memcached binary is available";
218 Error
<< "Failed to start " << server_type
<< ", no support was found to be compiled in for it.";
223 Error
<< "Failure occured while creating server: " << server_type
;
228 We will now cycle the server we have created.
230 if (not server
->cycle())
232 Error
<< "Could not start up server " << *server
;
237 server
->build(argc
, argv
);
239 if (construct
.is_debug())
241 Out
<< "Pausing for startup, hit return when ready.";
242 std::string gdb_command
= server
->base_command();
244 Out
<< "run " << server
->args(options
);
247 else if (not server
->start())
249 Error
<< "Failed to start " << *server
;
255 Out
<< "STARTING SERVER(pid:" << server
->pid() << "): " << server
->running();
258 construct
.push_server(server
);
260 if (default_port() == 0)
262 assert(server
->has_port());
263 set_default_port(server
->port());
271 bool server_startup_st::start_socket_server(const std::string
& server_type
, const in_port_t try_port
, int argc
, const char *argv
[])
276 Server
*server
= NULL
;
279 else if (server_type
.compare("gearmand") == 0)
281 Error
<< "Socket files are not supported for gearmand yet";
283 else if (server_type
.compare("memcached-sasl") == 0)
285 if (MEMCACHED_SASL_BINARY
)
287 if (HAVE_LIBMEMCACHED
)
289 server
= build_memcached_sasl_socket("localhost", try_port
, username(), password());
293 Error
<< "Libmemcached was not found";
298 Error
<< "No memcached binary is available";
301 else if (server_type
.compare("memcached") == 0)
303 if (MEMCACHED_BINARY
)
305 if (HAVE_LIBMEMCACHED
)
307 server
= build_memcached_socket("localhost", try_port
);
311 Error
<< "Libmemcached was not found";
316 Error
<< "No memcached binary is available";
321 Error
<< "Failed to start " << server_type
<< ", no support was found to be compiled in for it.";
326 Error
<< "Failure occured while creating server: " << server_type
;
331 We will now cycle the server we have created.
333 if (not server
->cycle())
335 Error
<< "Could not start up server " << *server
;
340 server
->build(argc
, argv
);
344 Out
<< "Pausing for startup, hit return when ready.";
345 std::string gdb_command
= server
->base_command();
347 Out
<< "run " << server
->args(options
);
350 else if (not server
->start())
352 Error
<< "Failed to start " << *server
;
358 Out
<< "STARTING SERVER(pid:" << server
->pid() << "): " << server
->running();
363 set_default_socket(server
->socket().c_str());
370 std::string
server_startup_st::option_string() const
372 std::string temp
= server_list
;
378 } // namespace libtest