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
];
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 Error
<< servers
.size() << " > " << host_to_shutdown
;
103 if (servers
.size() > host_to_shutdown
)
105 Server
* tmp
= servers
[host_to_shutdown
];
107 if (tmp
and tmp
->kill() == false)
118 void server_startup_st::clear()
120 for (std::vector
<Server
*>::iterator iter
= servers
.begin(); iter
!= servers
.end(); ++iter
)
127 bool server_startup_st::check() const
130 for (std::vector
<Server
*>::const_iterator iter
= servers
.begin(); iter
!= servers
.end(); ++iter
)
132 if ((*iter
)->check() == false)
141 bool server_startup_st::shutdown()
144 for (std::vector
<Server
*>::iterator iter
= servers
.begin(); iter
!= servers
.end(); ++iter
)
146 if ((*iter
) and (*iter
)->has_pid() and (*iter
)->kill() == false)
148 Error
<< "Unable to kill:" << *(*iter
);
156 void server_startup_st::restart()
158 for (std::vector
<Server
*>::iterator iter
= servers
.begin(); iter
!= servers
.end(); ++iter
)
164 #define MAGIC_MEMORY 123575
165 server_startup_st::server_startup_st() :
166 _magic(MAGIC_MEMORY
),
174 server_startup_st::~server_startup_st()
179 bool server_startup_st::validate()
181 return _magic
== MAGIC_MEMORY
;
184 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
)
186 construct
.start_server(server_type
, try_port
, argc
, argv
, opt_startup_message
);
189 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
)
193 throw libtest::fatal(LIBYATL_DEFAULT_PARAM
, "was passed the invalid port number %d", int(try_port
));
196 libtest::Server
*server
= NULL
;
200 else if (server_type
.compare("gearmand") == 0)
206 server
= build_gearmand("localhost", try_port
);
210 else if (server_type
.compare("drizzled") == 0)
216 server
= build_drizzled("localhost", try_port
);
220 else if (server_type
.compare("blobslap_worker") == 0)
224 if (GEARMAND_BLOBSLAP_WORKER
)
228 server
= build_blobslap_worker(try_port
);
233 else if (server_type
.compare("memcached-sasl") == 0)
235 if (MEMCACHED_SASL_BINARY
)
237 if (HAVE_LIBMEMCACHED
)
239 server
= build_memcached_sasl("localhost", try_port
, username(), password());
243 else if (server_type
.compare("memcached") == 0)
245 if (HAVE_MEMCACHED_BINARY
)
247 if (HAVE_LIBMEMCACHED
)
249 server
= build_memcached("localhost", try_port
);
253 else if (server_type
.compare("memcached-light") == 0)
255 if (MEMCACHED_LIGHT_BINARY
)
257 if (HAVE_LIBMEMCACHED
)
259 server
= build_memcached_light("localhost", try_port
);
266 throw libtest::fatal(LIBYATL_DEFAULT_PARAM
, "Launching of an unknown server was attempted: %s", server_type
.c_str());
270 We will now cycle the server we have created.
272 if (server
->cycle() == false)
274 Error
<< "Could not start up server " << *server
;
279 server
->build(argc
, argv
);
283 Out
<< "Pausing for startup, hit return when ready.";
284 std::string gdb_command
= server
->base_command();
287 Out
<< "run " << server
->args(options
);
291 else if (server
->start() == false)
298 if (opt_startup_message
)
301 Out
<< "STARTING SERVER(pid:" << server
->pid() << "): " << server
->running();
317 bool server_startup_st::start_socket_server(const std::string
& server_type
, const in_port_t try_port
, int argc
,
319 const bool opt_startup_message
)
324 Server
*server
= NULL
;
328 else if (server_type
.compare("gearmand") == 0)
330 Error
<< "Socket files are not supported for gearmand yet";
332 else if (server_type
.compare("memcached-sasl") == 0)
334 if (MEMCACHED_SASL_BINARY
)
336 if (HAVE_LIBMEMCACHED
)
338 server
= build_memcached_sasl_socket("localhost", try_port
, username(), password());
342 Error
<< "Libmemcached was not found";
347 Error
<< "No memcached binary is available";
350 else if (server_type
.compare("memcached") == 0)
352 if (MEMCACHED_BINARY
)
354 if (HAVE_LIBMEMCACHED
)
356 server
= build_memcached_socket("localhost", try_port
);
360 Error
<< "Libmemcached was not found";
365 Error
<< "No memcached binary is available";
370 Error
<< "Failed to start " << server_type
<< ", no support was found to be compiled in for it.";
375 Error
<< "Failure occured while creating server: " << server_type
;
380 We will now cycle the server we have created.
382 if (server
->cycle() == false)
384 Error
<< "Could not start up server " << *server
;
389 server
->build(argc
, argv
);
393 Out
<< "Pausing for startup, hit return when ready.";
394 std::string gdb_command
= server
->base_command();
397 Out
<< "run " << server
->args(options
);
401 else if (server
->start() == false)
403 Error
<< "Failed to start " << *server
;
409 if (opt_startup_message
)
412 Out
<< "STARTING SERVER(pid:" << server
->pid() << "): " << server
->running();
425 set_default_socket(server
->socket().c_str());
432 std::string
server_startup_st::option_string() const
434 std::string temp
= server_list
;
440 } // namespace libtest