Update framework.
[m6w6/libmemcached] / libtest / server_container.cc
1 /* vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
2 *
3 * libtest
4 *
5 * Copyright (C) 2011 Data Differential, http://datadifferential.com/
6 *
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.
11 *
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.
16 *
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
20 */
21
22 #include <config.h>
23 #include <libtest/common.h>
24
25 #include <cassert>
26 #include <cerrno>
27 #include <cstdlib>
28 #include <iostream>
29
30 #include <algorithm>
31 #include <functional>
32 #include <locale>
33
34 // trim from end
35 static inline std::string &rtrim(std::string &s)
36 {
37 s.erase(std::find_if(s.rbegin(), s.rend(), std::not1(std::ptr_fun<int, int>(std::isspace))).base(), s.end());
38 return s;
39 }
40
41 namespace libtest {
42
43 void server_startup_st::push_server(Server *arg)
44 {
45 servers.push_back(arg);
46
47 char port_str[NI_MAXSERV];
48 snprintf(port_str, sizeof(port_str), "%u", int(arg->port()));
49
50 std::string server_config_string;
51 if (arg->has_socket())
52 {
53 server_config_string+= "--socket=";
54 server_config_string+= '"';
55 server_config_string+= arg->socket();
56 server_config_string+= '"';
57 server_config_string+= " ";
58 }
59 else
60 {
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+= " ";
66 }
67
68 server_list+= server_config_string;
69
70 }
71
72 Server* server_startup_st::pop_server()
73 {
74 Server *tmp= servers.back();
75 servers.pop_back();
76 return tmp;
77 }
78
79 bool server_startup_st::shutdown(uint32_t number_of_host)
80 {
81 if (servers.size() > number_of_host)
82 {
83 Server* tmp= servers[number_of_host];
84
85 if (tmp and tmp->has_pid() and tmp->kill() == false)
86 { }
87 else
88 {
89 return true;
90 }
91 }
92
93 return false;
94 }
95
96 void server_startup_st::shutdown_and_remove()
97 {
98 for (std::vector<Server *>::iterator iter= servers.begin(); iter != servers.end(); iter++)
99 {
100 delete *iter;
101 }
102 servers.clear();
103 }
104
105 bool server_startup_st::check() const
106 {
107 bool success= true;
108 for (std::vector<Server *>::const_iterator iter= servers.begin(); iter != servers.end(); iter++)
109 {
110 if ((*iter)->check() == false)
111 {
112 success= false;
113 }
114 }
115
116 return success;
117 }
118
119 bool server_startup_st::shutdown()
120 {
121 bool success= true;
122 for (std::vector<Server *>::iterator iter= servers.begin(); iter != servers.end(); iter++)
123 {
124 if ((*iter)->has_pid() and (*iter)->kill() == false)
125 {
126 Error << "Unable to kill:" << *(*iter);
127 success= false;
128 }
129 }
130
131 return success;
132 }
133
134 void server_startup_st::restart()
135 {
136 for (std::vector<Server *>::iterator iter= servers.begin(); iter != servers.end(); iter++)
137 {
138 (*iter)->start();
139 }
140 }
141
142 #define MAGIC_MEMORY 123575
143 server_startup_st::server_startup_st() :
144 _magic(MAGIC_MEMORY),
145 _socket(false),
146 _sasl(false),
147 _count(0),
148 udp(0),
149 _servers_to_run(5)
150 { }
151
152 server_startup_st::~server_startup_st()
153 {
154 shutdown_and_remove();
155 }
156
157 bool server_startup_st::validate()
158 {
159 return _magic == MAGIC_MEMORY;
160 }
161
162 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)
163 {
164 if (try_port <= 0)
165 {
166 throw libtest::fatal(LIBYATL_DEFAULT_PARAM, "was passed the invalid port number %d", int(try_port));
167 }
168
169 libtest::Server *server= NULL;
170 if (0)
171 { }
172 else if (server_type.compare("gearmand") == 0)
173 {
174 if (GEARMAND_BINARY)
175 {
176 if (HAVE_LIBGEARMAN)
177 {
178 server= build_gearmand("localhost", try_port);
179 }
180 }
181 }
182 else if (server_type.compare("drizzled") == 0)
183 {
184 if (DRIZZLED_BINARY)
185 {
186 if (HAVE_LIBDRIZZLE)
187 {
188 server= build_drizzled("localhost", try_port);
189 }
190 }
191 }
192 else if (server_type.compare("blobslap_worker") == 0)
193 {
194 if (GEARMAND_BINARY)
195 {
196 if (GEARMAND_BLOBSLAP_WORKER)
197 {
198 if (HAVE_LIBGEARMAN)
199 {
200 server= build_blobslap_worker(try_port);
201 }
202 }
203 }
204 }
205 else if (server_type.compare("memcached-sasl") == 0)
206 {
207 if (MEMCACHED_SASL_BINARY)
208 {
209 if (HAVE_LIBMEMCACHED)
210 {
211 server= build_memcached_sasl("localhost", try_port, construct.username(), construct.password());
212 }
213 }
214 }
215 else if (server_type.compare("memcached") == 0)
216 {
217 if (MEMCACHED_BINARY)
218 {
219 if (HAVE_LIBMEMCACHED)
220 {
221 server= build_memcached("localhost", try_port);
222 }
223 }
224 }
225 else if (server_type.compare("memcached-light") == 0)
226 {
227 if (MEMCACHED_LIGHT_BINARY)
228 {
229 if (HAVE_LIBMEMCACHED)
230 {
231 server= build_memcached_light("localhost", try_port);
232 }
233 }
234 }
235
236 if (server == NULL)
237 {
238 fatal_message("Launching of an unknown server was attempted");
239 }
240
241 /*
242 We will now cycle the server we have created.
243 */
244 if (server->cycle() == false)
245 {
246 Error << "Could not start up server " << *server;
247 delete server;
248 return false;
249 }
250
251 server->build(argc, argv);
252
253 if (false)
254 {
255 Out << "Pausing for startup, hit return when ready.";
256 std::string gdb_command= server->base_command();
257 std::string options;
258 #if 0
259 Out << "run " << server->args(options);
260 #endif
261 getchar();
262 }
263 else if (server->start() == false)
264 {
265 delete server;
266 return false;
267 }
268 else
269 {
270 if (opt_startup_message)
271 {
272 Outn();
273 Out << "STARTING SERVER(pid:" << server->pid() << "): " << server->running();
274 Outn();
275 }
276 }
277
278 construct.push_server(server);
279
280 return true;
281 }
282
283 bool server_startup_st::start_socket_server(const std::string& server_type, const in_port_t try_port, int argc, const char *argv[])
284 {
285 (void)try_port;
286 Outn();
287
288 Server *server= NULL;
289 if (0)
290 { }
291 else if (server_type.compare("gearmand") == 0)
292 {
293 Error << "Socket files are not supported for gearmand yet";
294 }
295 else if (server_type.compare("memcached-sasl") == 0)
296 {
297 if (MEMCACHED_SASL_BINARY)
298 {
299 if (HAVE_LIBMEMCACHED)
300 {
301 server= build_memcached_sasl_socket("localhost", try_port, username(), password());
302 }
303 else
304 {
305 Error << "Libmemcached was not found";
306 }
307 }
308 else
309 {
310 Error << "No memcached binary is available";
311 }
312 }
313 else if (server_type.compare("memcached") == 0)
314 {
315 if (MEMCACHED_BINARY)
316 {
317 if (HAVE_LIBMEMCACHED)
318 {
319 server= build_memcached_socket("localhost", try_port);
320 }
321 else
322 {
323 Error << "Libmemcached was not found";
324 }
325 }
326 else
327 {
328 Error << "No memcached binary is available";
329 }
330 }
331 else
332 {
333 Error << "Failed to start " << server_type << ", no support was found to be compiled in for it.";
334 }
335
336 if (server == NULL)
337 {
338 Error << "Failure occured while creating server: " << server_type;
339 return false;
340 }
341
342 /*
343 We will now cycle the server we have created.
344 */
345 if (server->cycle() == false)
346 {
347 Error << "Could not start up server " << *server;
348 delete server;
349 return false;
350 }
351
352 server->build(argc, argv);
353
354 if (false)
355 {
356 Out << "Pausing for startup, hit return when ready.";
357 std::string gdb_command= server->base_command();
358 std::string options;
359 #if 0
360 Out << "run " << server->args(options);
361 #endif
362 getchar();
363 }
364 else if (server->start() == false)
365 {
366 Error << "Failed to start " << *server;
367 delete server;
368 return false;
369 }
370 else
371 {
372 Out << "STARTING SERVER(pid:" << server->pid() << "): " << server->running();
373 }
374
375 push_server(server);
376
377 set_default_socket(server->socket().c_str());
378
379 Outn();
380
381 return true;
382 }
383
384 std::string server_startup_st::option_string() const
385 {
386 std::string temp= server_list;
387 rtrim(temp);
388 return temp;
389 }
390
391
392 } // namespace libtest