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
23 #include <libtest/common.h>
35 static inline std::string
&rtrim(std::string
&s
)
37 s
.erase(std::find_if(s
.rbegin(), s
.rend(), std::not1(std::ptr_fun
<int, int>(std::isspace
))).base(), s
.end());
43 void server_startup_st::push_server(Server
*arg
)
45 servers
.push_back(arg
);
47 char port_str
[NI_MAXSERV
];
48 snprintf(port_str
, sizeof(port_str
), "%u", int(arg
->port()));
50 std::string server_config_string
;
51 if (arg
->has_socket())
53 server_config_string
+= "--socket=";
54 server_config_string
+= '"';
55 server_config_string
+= arg
->socket();
56 server_config_string
+= '"';
57 server_config_string
+= " ";
61 server_config_string
+= "--server=";
62 server_config_string
+= arg
->hostname();
63 server_config_string
+= ":";
64 server_config_string
+= port_str
;
65 server_config_string
+= " ";
68 server_list
+= server_config_string
;
72 Server
* server_startup_st::pop_server()
74 Server
*tmp
= servers
.back();
79 bool server_startup_st::shutdown(uint32_t number_of_host
)
81 if (servers
.size() > number_of_host
)
83 Server
* tmp
= servers
[number_of_host
];
85 if (tmp
and tmp
->has_pid() and not tmp
->kill(tmp
->pid()))
96 void server_startup_st::shutdown_and_remove()
98 for (std::vector
<Server
*>::iterator iter
= servers
.begin(); iter
!= servers
.end(); iter
++)
105 bool server_startup_st::check() const
107 for (std::vector
<Server
*>::const_iterator iter
= servers
.begin(); iter
!= servers
.end(); iter
++)
109 if ((*iter
)->check() == false)
118 void server_startup_st::shutdown()
120 for (std::vector
<Server
*>::iterator iter
= servers
.begin(); iter
!= servers
.end(); iter
++)
122 if ((*iter
)->has_pid() and not (*iter
)->kill((*iter
)->pid()))
124 Error
<< "Unable to kill:" << *(*iter
);
129 void server_startup_st::restart()
131 for (std::vector
<Server
*>::iterator iter
= servers
.begin(); iter
!= servers
.end(); iter
++)
137 #define MAGIC_MEMORY 123575
138 server_startup_st::server_startup_st() :
139 _magic(MAGIC_MEMORY
),
146 server_startup_st::~server_startup_st()
148 shutdown_and_remove();
151 bool server_startup_st::validate()
153 return _magic
== MAGIC_MEMORY
;
157 bool server_startup_st::is_debug() const
159 return bool(getenv("LIBTEST_MANUAL_GDB"));
162 bool server_startup_st::is_valgrind() const
164 return bool(getenv("LIBTEST_MANUAL_VALGRIND"));
167 bool server_startup_st::is_helgrind() const
169 return bool(getenv("LIBTEST_MANUAL_HELGRIND"));
173 bool server_startup(server_startup_st
& construct
, const std::string
& server_type
, in_port_t try_port
, int argc
, const char *argv
[])
178 libtest::fatal(LIBYATL_DEFAULT_PARAM
, "was passed the invalid port number %d", int(try_port
));
181 libtest::Server
*server
= NULL
;
184 else if (server_type
.compare("gearmand") == 0)
190 server
= build_gearmand("localhost", try_port
);
194 else if (server_type
.compare("blobslap_worker") == 0)
198 if (GEARMAND_BLOBSLAP_WORKER
)
202 server
= build_blobslap_worker(try_port
);
207 else if (server_type
.compare("memcached-sasl") == 0)
209 if (MEMCACHED_SASL_BINARY
)
211 if (HAVE_LIBMEMCACHED
)
213 server
= build_memcached_sasl("localhost", try_port
, construct
.username(), construct
.password());
217 else if (server_type
.compare("memcached") == 0)
219 if (MEMCACHED_BINARY
)
221 if (HAVE_LIBMEMCACHED
)
223 server
= build_memcached("localhost", try_port
);
227 else if (server_type
.compare("memcached-light") == 0)
229 if (MEMCACHED_LIGHT_BINARY
)
231 if (HAVE_LIBMEMCACHED
)
233 server
= build_memcached_light("localhost", try_port
);
240 fatal_message("Launching of an unknown server was attempted");
244 We will now cycle the server we have created.
246 if (server
->cycle() == false)
248 Error
<< "Could not start up server " << *server
;
253 server
->build(argc
, argv
);
255 if (construct
.is_debug())
257 Out
<< "Pausing for startup, hit return when ready.";
258 std::string gdb_command
= server
->base_command();
261 Out
<< "run " << server
->args(options
);
265 else if (server
->start() == false)
272 Out
<< "STARTING SERVER(pid:" << server
->pid() << "): " << server
->running();
275 construct
.push_server(server
);
282 bool server_startup_st::start_socket_server(const std::string
& server_type
, const in_port_t try_port
, int argc
, const char *argv
[])
287 Server
*server
= NULL
;
290 else if (server_type
.compare("gearmand") == 0)
292 Error
<< "Socket files are not supported for gearmand yet";
294 else if (server_type
.compare("memcached-sasl") == 0)
296 if (MEMCACHED_SASL_BINARY
)
298 if (HAVE_LIBMEMCACHED
)
300 server
= build_memcached_sasl_socket("localhost", try_port
, username(), password());
304 Error
<< "Libmemcached was not found";
309 Error
<< "No memcached binary is available";
312 else if (server_type
.compare("memcached") == 0)
314 if (MEMCACHED_BINARY
)
316 if (HAVE_LIBMEMCACHED
)
318 server
= build_memcached_socket("localhost", try_port
);
322 Error
<< "Libmemcached was not found";
327 Error
<< "No memcached binary is available";
332 Error
<< "Failed to start " << server_type
<< ", no support was found to be compiled in for it.";
337 Error
<< "Failure occured while creating server: " << server_type
;
342 We will now cycle the server we have created.
344 if (server
->cycle() == false)
346 Error
<< "Could not start up server " << *server
;
351 server
->build(argc
, argv
);
355 Out
<< "Pausing for startup, hit return when ready.";
356 std::string gdb_command
= server
->base_command();
359 Out
<< "run " << server
->args(options
);
363 else if (not server
->start())
365 Error
<< "Failed to start " << *server
;
371 Out
<< "STARTING SERVER(pid:" << server
->pid() << "): " << server
->running();
376 set_default_socket(server
->socket().c_str());
383 std::string
server_startup_st::option_string() const
385 std::string temp
= server_list
;
391 } // namespace libtest