1 /* vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
3 * Data Differential YATL (i.e. libtest) library
5 * Copyright (C) 2012 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.
38 #include <libtest/common.h>
50 static inline std::string
&rtrim(std::string
&s
)
52 s
.erase(std::find_if(s
.rbegin(), s
.rend(), std::not1(std::ptr_fun
<int, int>(std::isspace
))).base(), s
.end());
58 void server_startup_st::push_server(Server
*arg
)
60 servers
.push_back(arg
);
62 char port_str
[NI_MAXSERV
];
63 snprintf(port_str
, sizeof(port_str
), "%u", int(arg
->port()));
65 std::string server_config_string
;
66 if (arg
->has_socket())
68 server_config_string
+= "--socket=";
69 server_config_string
+= '"';
70 server_config_string
+= arg
->socket();
71 server_config_string
+= '"';
72 server_config_string
+= " ";
76 server_config_string
+= "--server=";
77 server_config_string
+= arg
->hostname();
78 server_config_string
+= ":";
79 server_config_string
+= port_str
;
80 server_config_string
+= " ";
83 server_list
+= server_config_string
;
87 Server
* server_startup_st::pop_server()
89 Server
*tmp
= servers
.back();
94 bool server_startup_st::shutdown(uint32_t number_of_host
)
96 if (servers
.size() > number_of_host
)
98 Server
* tmp
= servers
[number_of_host
];
100 if (tmp
and tmp
->has_pid() and tmp
->kill() == false)
111 void server_startup_st::shutdown_and_remove()
113 for (std::vector
<Server
*>::iterator iter
= servers
.begin(); iter
!= servers
.end(); iter
++)
120 bool server_startup_st::check() const
123 for (std::vector
<Server
*>::const_iterator iter
= servers
.begin(); iter
!= servers
.end(); iter
++)
125 if ((*iter
)->check() == false)
134 bool server_startup_st::shutdown()
137 for (std::vector
<Server
*>::iterator iter
= servers
.begin(); iter
!= servers
.end(); iter
++)
139 if ((*iter
)->has_pid() and (*iter
)->kill() == false)
141 Error
<< "Unable to kill:" << *(*iter
);
149 void server_startup_st::restart()
151 for (std::vector
<Server
*>::iterator iter
= servers
.begin(); iter
!= servers
.end(); iter
++)
157 #define MAGIC_MEMORY 123575
158 server_startup_st::server_startup_st() :
159 _magic(MAGIC_MEMORY
),
167 server_startup_st::~server_startup_st()
169 shutdown_and_remove();
172 bool server_startup_st::validate()
174 return _magic
== MAGIC_MEMORY
;
177 bool server_startup(server_startup_st
& construct
, const std::string
& server_type
, in_port_t try_port
, int argc
, const char *argv
[], const bool opt_startup_message
)
181 throw libtest::fatal(LIBYATL_DEFAULT_PARAM
, "was passed the invalid port number %d", int(try_port
));
184 libtest::Server
*server
= NULL
;
187 else if (server_type
.compare("gearmand") == 0)
193 server
= build_gearmand("localhost", try_port
);
197 else if (server_type
.compare("drizzled") == 0)
203 server
= build_drizzled("localhost", try_port
);
207 else if (server_type
.compare("blobslap_worker") == 0)
211 if (GEARMAND_BLOBSLAP_WORKER
)
215 server
= build_blobslap_worker(try_port
);
220 else if (server_type
.compare("memcached-sasl") == 0)
222 if (MEMCACHED_SASL_BINARY
)
224 if (HAVE_LIBMEMCACHED
)
226 server
= build_memcached_sasl("localhost", try_port
, construct
.username(), construct
.password());
230 else if (server_type
.compare("memcached") == 0)
232 if (MEMCACHED_BINARY
)
234 if (HAVE_LIBMEMCACHED
)
236 server
= build_memcached("localhost", try_port
);
240 else if (server_type
.compare("memcached-light") == 0)
242 if (MEMCACHED_LIGHT_BINARY
)
244 if (HAVE_LIBMEMCACHED
)
246 server
= build_memcached_light("localhost", try_port
);
253 fatal_message("Launching of an unknown server was attempted");
257 We will now cycle the server we have created.
259 if (server
->cycle() == false)
261 Error
<< "Could not start up server " << *server
;
266 server
->build(argc
, argv
);
270 Out
<< "Pausing for startup, hit return when ready.";
271 std::string gdb_command
= server
->base_command();
274 Out
<< "run " << server
->args(options
);
278 else if (server
->start() == false)
285 if (opt_startup_message
)
288 Out
<< "STARTING SERVER(pid:" << server
->pid() << "): " << server
->running();
293 construct
.push_server(server
);
298 bool server_startup_st::start_socket_server(const std::string
& server_type
, const in_port_t try_port
, int argc
, const char *argv
[])
303 Server
*server
= NULL
;
306 else if (server_type
.compare("gearmand") == 0)
308 Error
<< "Socket files are not supported for gearmand yet";
310 else if (server_type
.compare("memcached-sasl") == 0)
312 if (MEMCACHED_SASL_BINARY
)
314 if (HAVE_LIBMEMCACHED
)
316 server
= build_memcached_sasl_socket("localhost", try_port
, username(), password());
320 Error
<< "Libmemcached was not found";
325 Error
<< "No memcached binary is available";
328 else if (server_type
.compare("memcached") == 0)
330 if (MEMCACHED_BINARY
)
332 if (HAVE_LIBMEMCACHED
)
334 server
= build_memcached_socket("localhost", try_port
);
338 Error
<< "Libmemcached was not found";
343 Error
<< "No memcached binary is available";
348 Error
<< "Failed to start " << server_type
<< ", no support was found to be compiled in for it.";
353 Error
<< "Failure occured while creating server: " << server_type
;
358 We will now cycle the server we have created.
360 if (server
->cycle() == false)
362 Error
<< "Could not start up server " << *server
;
367 server
->build(argc
, argv
);
371 Out
<< "Pausing for startup, hit return when ready.";
372 std::string gdb_command
= server
->base_command();
375 Out
<< "run " << server
->args(options
);
379 else if (server
->start() == false)
381 Error
<< "Failed to start " << *server
;
387 Out
<< "STARTING SERVER(pid:" << server
->pid() << "): " << server
->running();
392 set_default_socket(server
->socket().c_str());
399 std::string
server_startup_st::option_string() const
401 std::string temp
= server_list
;
407 } // namespace libtest