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.
37 #include "libtest/yatlcon.h"
39 #include "libtest/common.h"
51 static inline std::string
&rtrim(std::string
&s
)
53 s
.erase(std::find_if(s
.rbegin(), s
.rend(), std::not1(std::ptr_fun
<int, int>(std::isspace
))).base(), s
.end());
59 Server
* server_startup_st::last()
61 return servers
.back();
64 void server_startup_st::push_server(Server
*arg
)
66 servers
.push_back(arg
);
68 std::string server_config_string
;
69 if (arg
->has_socket())
71 server_config_string
+= "--socket=";
72 server_config_string
+= '"';
73 server_config_string
+= arg
->socket();
74 server_config_string
+= '"';
75 server_config_string
+= " ";
79 libtest::vchar_t port_str
;
80 port_str
.resize(NI_MAXSERV
);
81 snprintf(&port_str
[0], port_str
.size(), "%u", int(arg
->port()));
83 server_config_string
+= "--server=";
84 server_config_string
+= arg
->hostname();
85 server_config_string
+= ":";
86 server_config_string
+= &port_str
[0];
87 server_config_string
+= " ";
90 server_list
+= server_config_string
;
94 Server
* server_startup_st::pop_server()
96 Server
*tmp
= servers
.back();
101 // host_to_shutdown => host number to shutdown in array
102 bool server_startup_st::shutdown(uint32_t host_to_shutdown
)
104 if (servers
.size() > host_to_shutdown
)
106 Server
* tmp
= servers
[host_to_shutdown
];
108 if (tmp
and tmp
->kill() == false)
119 void server_startup_st::clear()
121 for (std::vector
<Server
*>::iterator iter
= servers
.begin(); iter
!= servers
.end(); ++iter
)
128 bool server_startup_st::check() const
131 for (std::vector
<Server
*>::const_iterator iter
= servers
.begin(); iter
!= servers
.end(); ++iter
)
133 if ((*iter
)->check() == false)
142 bool server_startup_st::shutdown()
145 for (std::vector
<Server
*>::iterator iter
= servers
.begin(); iter
!= servers
.end(); ++iter
)
147 if ((*iter
)->has_pid() and (*iter
)->kill() == false)
149 Error
<< "Unable to kill:" << *(*iter
);
157 void server_startup_st::restart()
159 for (std::vector
<Server
*>::iterator iter
= servers
.begin(); iter
!= servers
.end(); ++iter
)
165 #define MAGIC_MEMORY 123575
166 server_startup_st::server_startup_st() :
167 _magic(MAGIC_MEMORY
),
175 server_startup_st::~server_startup_st()
180 bool server_startup_st::validate()
182 return _magic
== MAGIC_MEMORY
;
185 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
)
187 return construct
.start_server(server_type
, try_port
, argc
, argv
, opt_startup_message
);
190 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
)
194 throw libtest::fatal(LIBYATL_DEFAULT_PARAM
, "was passed the invalid port number %d", int(try_port
));
197 libtest::Server
*server
= NULL
;
201 else if (server_type
.compare("gearmand") == 0)
205 server
= build_gearmand("localhost", try_port
);
208 else if (server_type
.compare("hostile-gearmand") == 0)
212 server
= build_gearmand("localhost", try_port
, "gearmand/hostile_gearmand");
215 else if (server_type
.compare("drizzled") == 0)
221 server
= build_drizzled("localhost", try_port
);
225 else if (server_type
.compare("blobslap_worker") == 0)
229 if (GEARMAND_BLOBSLAP_WORKER
)
233 server
= build_blobslap_worker(try_port
);
238 else if (server_type
.compare("memcached") == 0)
240 if (HAVE_MEMCACHED_BINARY
)
242 server
= build_memcached("localhost", try_port
);
248 throw libtest::fatal(LIBYATL_DEFAULT_PARAM
, "Launching of an unknown server was attempted: %s", server_type
.c_str());
258 We will now cycle the server we have created.
260 if (server
->cycle() == false)
262 Error
<< "Could not start up server " << *server
;
267 server
->build(argc
, argv
);
272 Out
<< "Pausing for startup, hit return when ready.";
273 std::string gdb_command
= server
->base_command();
279 if (server
->start() == false)
286 if (opt_startup_message
)
292 Out
<< "STARTING SERVER(pid:" << server
->pid() << "): " << server
->running();
299 catch (libtest::disconnected
& err
)
301 if (fatal::is_disabled() == false and try_port
!= LIBTEST_FAIL_PORT
)
303 stream::cerr(err
.file(), err
.line(), err
.func()) << err
.what();
319 bool server_startup_st::start_socket_server(const std::string
& server_type
, const in_port_t try_port
, int argc
,
321 const bool opt_startup_message
)
326 Server
*server
= NULL
;
330 else if (server_type
.compare("gearmand") == 0)
332 Error
<< "Socket files are not supported for gearmand yet";
334 else if (server_type
.compare("memcached") == 0)
336 if (HAVE_MEMCACHED_BINARY
)
338 server
= build_memcached_socket("localhost", try_port
);
342 Error
<< "No memcached binary is available";
347 Error
<< "Failed to start " << server_type
<< ", no support was found to be compiled in for it.";
352 Error
<< "Failure occured while creating server: " << server_type
;
357 We will now cycle the server we have created.
359 if (server
->cycle() == false)
361 Error
<< "Could not start up server " << *server
;
366 server
->build(argc
, argv
);
371 Out
<< "Pausing for startup, hit return when ready.";
372 std::string gdb_command
= server
->base_command();
374 Out
<< "run " << server
->args(options
);
379 if (server
->start() == false)
381 Error
<< "Failed to start " << *server
;
387 if (opt_startup_message
)
393 Out
<< "STARTING SERVER(pid:" << server
->pid() << "): " << server
->running();
408 set_default_socket(server
->socket().c_str());
415 std::string
server_startup_st::option_string() const
417 std::string temp
= server_list
;
423 } // namespace libtest