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 Server
* server_startup_st::last()
60 return servers
.back();
63 void server_startup_st::push_server(Server
*arg
)
65 servers
.push_back(arg
);
67 std::string server_config_string
;
68 if (arg
->has_socket())
70 server_config_string
+= "--socket=";
71 server_config_string
+= '"';
72 server_config_string
+= arg
->socket();
73 server_config_string
+= '"';
74 server_config_string
+= " ";
78 char port_str
[NI_MAXSERV
]= { 0 };
79 snprintf(port_str
, sizeof(port_str
), "%u", int(arg
->port()));
81 server_config_string
+= "--server=";
82 server_config_string
+= arg
->hostname();
83 server_config_string
+= ":";
84 server_config_string
+= port_str
;
85 server_config_string
+= " ";
88 server_list
+= server_config_string
;
92 Server
* server_startup_st::pop_server()
94 Server
*tmp
= servers
.back();
99 // host_to_shutdown => host number to shutdown in array
100 bool server_startup_st::shutdown(uint32_t host_to_shutdown
)
102 if (servers
.size() > host_to_shutdown
)
104 Server
* tmp
= servers
[host_to_shutdown
];
106 if (tmp
and tmp
->kill() == false)
117 void server_startup_st::clear()
119 for (std::vector
<Server
*>::iterator iter
= servers
.begin(); iter
!= servers
.end(); ++iter
)
126 bool server_startup_st::check() const
129 for (std::vector
<Server
*>::const_iterator iter
= servers
.begin(); iter
!= servers
.end(); ++iter
)
131 if ((*iter
)->check() == false)
140 bool server_startup_st::shutdown()
143 for (std::vector
<Server
*>::iterator iter
= servers
.begin(); iter
!= servers
.end(); ++iter
)
145 if ((*iter
)->has_pid() and (*iter
)->kill() == false)
147 Error
<< "Unable to kill:" << *(*iter
);
155 void server_startup_st::restart()
157 for (std::vector
<Server
*>::iterator iter
= servers
.begin(); iter
!= servers
.end(); ++iter
)
163 #define MAGIC_MEMORY 123575
164 server_startup_st::server_startup_st() :
165 _magic(MAGIC_MEMORY
),
173 server_startup_st::~server_startup_st()
178 bool server_startup_st::validate()
180 return _magic
== MAGIC_MEMORY
;
183 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
)
185 return construct
.start_server(server_type
, try_port
, argc
, argv
, opt_startup_message
);
188 bool server_startup_st::start_server(const std::string
& server_type
, in_port_t try_port
, int argc
, const char *argv
[], const bool opt_startup_message
)
192 throw libtest::fatal(LIBYATL_DEFAULT_PARAM
, "was passed the invalid port number %d", int(try_port
));
195 libtest::Server
*server
= NULL
;
199 else if (server_type
.compare("gearmand") == 0)
205 server
= build_gearmand("localhost", try_port
);
209 else if (server_type
.compare("hostile-gearmand") == 0)
215 server
= build_gearmand("localhost", try_port
, "gearmand/hostile_gearmand");
219 else if (server_type
.compare("drizzled") == 0)
225 server
= build_drizzled("localhost", try_port
);
229 else if (server_type
.compare("blobslap_worker") == 0)
233 if (GEARMAND_BLOBSLAP_WORKER
)
237 server
= build_blobslap_worker(try_port
);
242 else if (server_type
.compare("memcached-sasl") == 0)
244 if (MEMCACHED_SASL_BINARY
)
246 if (HAVE_LIBMEMCACHED
)
248 server
= build_memcached_sasl("localhost", try_port
, username(), password());
252 else if (server_type
.compare("memcached") == 0)
254 if (HAVE_MEMCACHED_BINARY
)
256 if (HAVE_LIBMEMCACHED
)
258 server
= build_memcached("localhost", try_port
);
262 else if (server_type
.compare("memcached-light") == 0)
264 if (MEMCACHED_LIGHT_BINARY
)
266 if (HAVE_LIBMEMCACHED
)
268 server
= build_memcached_light("localhost", try_port
);
275 throw libtest::fatal(LIBYATL_DEFAULT_PARAM
, "Launching of an unknown server was attempted: %s", server_type
.c_str());
279 We will now cycle the server we have created.
281 if (server
->cycle() == false)
283 Error
<< "Could not start up server " << *server
;
288 server
->build(argc
, argv
);
293 Out
<< "Pausing for startup, hit return when ready.";
294 std::string gdb_command
= server
->base_command();
299 if (server
->start() == false)
306 if (opt_startup_message
)
309 Out
<< "STARTING SERVER(pid:" << server
->pid() << "): " << server
->running();
314 catch (libtest::disconnected err
)
316 stream::cerr(err
.file(), err
.line(), err
.func()) << err
.what();
331 bool server_startup_st::start_socket_server(const std::string
& server_type
, const in_port_t try_port
, int argc
,
333 const bool opt_startup_message
)
338 Server
*server
= NULL
;
342 else if (server_type
.compare("gearmand") == 0)
344 Error
<< "Socket files are not supported for gearmand yet";
346 else if (server_type
.compare("memcached-sasl") == 0)
348 if (MEMCACHED_SASL_BINARY
)
350 if (HAVE_LIBMEMCACHED
)
352 server
= build_memcached_sasl_socket("localhost", try_port
, username(), password());
356 Error
<< "Libmemcached was not found";
361 Error
<< "No memcached binary is available";
364 else if (server_type
.compare("memcached") == 0)
366 if (MEMCACHED_BINARY
)
368 if (HAVE_LIBMEMCACHED
)
370 server
= build_memcached_socket("localhost", try_port
);
374 Error
<< "Libmemcached was not found";
379 Error
<< "No memcached binary is available";
384 Error
<< "Failed to start " << server_type
<< ", no support was found to be compiled in for it.";
389 Error
<< "Failure occured while creating server: " << server_type
;
394 We will now cycle the server we have created.
396 if (server
->cycle() == false)
398 Error
<< "Could not start up server " << *server
;
403 server
->build(argc
, argv
);
408 Out
<< "Pausing for startup, hit return when ready.";
409 std::string gdb_command
= server
->base_command();
411 Out
<< "run " << server
->args(options
);
416 if (server
->start() == false)
418 Error
<< "Failed to start " << *server
;
424 if (opt_startup_message
)
427 Out
<< "STARTING SERVER(pid:" << server
->pid() << "): " << server
->running();
440 set_default_socket(server
->socket().c_str());
447 std::string
server_startup_st::option_string() const
449 std::string temp
= server_list
;
455 } // namespace libtest