tests: fix failure tests
[awesomized/libmemcached] / libtest / server_container.cc
1 /* vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
2 *
3 * Data Differential YATL (i.e. libtest) library
4 *
5 * Copyright (C) 2012 Data Differential, http://datadifferential.com/
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions are
9 * met:
10 *
11 * * Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 *
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
17 * distribution.
18 *
19 * * The names of its contributors may not be used to endorse or
20 * promote products derived from this software without specific prior
21 * written permission.
22 *
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.
34 *
35 */
36
37 #include "libtest/yatlcon.h"
38
39 #include "libtest/common.h"
40
41 #include <cerrno>
42 #include <cstdlib>
43 #include <iostream>
44
45 #include <algorithm>
46 #include <functional>
47 #include <locale>
48
49 // trim from end
50 static inline std::string &rtrim(std::string &s)
51 {
52 s.erase(std::find_if(s.rbegin(), s.rend(), std::not1(std::ptr_fun<int, int>(std::isspace))).base(), s.end());
53 return s;
54 }
55
56 namespace libtest {
57
58 Server* server_startup_st::last()
59 {
60 return servers.back();
61 }
62
63 void server_startup_st::push_server(Server *arg)
64 {
65 servers.push_back(arg);
66
67 std::string server_config_string;
68 if (arg->has_socket())
69 {
70 server_config_string+= "--socket=";
71 server_config_string+= '"';
72 server_config_string+= arg->socket();
73 server_config_string+= '"';
74 server_config_string+= " ";
75 }
76 else
77 {
78 libtest::vchar_t port_str;
79 port_str.resize(NI_MAXSERV);
80 snprintf(&port_str[0], port_str.size(), "%u", int(arg->port()));
81
82 server_config_string+= "--server=";
83 server_config_string+= arg->hostname();
84 server_config_string+= ":";
85 server_config_string+= &port_str[0];
86 server_config_string+= " ";
87 }
88
89 server_list+= server_config_string;
90 }
91
92 Server* server_startup_st::pop_server()
93 {
94 Server *tmp= servers.back();
95 servers.pop_back();
96 return tmp;
97 }
98
99 // host_to_shutdown => host number to shutdown in array
100 bool server_startup_st::shutdown(uint32_t host_to_shutdown)
101 {
102 if (servers.size() > host_to_shutdown)
103 {
104 Server* tmp= servers[host_to_shutdown];
105
106 if (tmp and tmp->kill() == false)
107 { }
108 else
109 {
110 return true;
111 }
112 }
113
114 return false;
115 }
116
117 void server_startup_st::clear()
118 {
119 std::for_each(servers.begin(), servers.end(), DeleteFromVector());
120 servers.clear();
121 }
122
123 bool server_startup_st::check() const
124 {
125 bool success= true;
126 for (std::vector<Server *>::const_iterator iter= servers.begin(); iter != servers.end(); ++iter)
127 {
128 if ((*iter)->check() == false)
129 {
130 success= false;
131 }
132 }
133
134 return success;
135 }
136
137 bool server_startup_st::shutdown()
138 {
139 bool success= true;
140 for (std::vector<Server *>::iterator iter= servers.begin(); iter != servers.end(); ++iter)
141 {
142 if ((*iter)->has_pid() and (*iter)->kill() == false)
143 {
144 Error << "Unable to kill:" << *(*iter);
145 success= false;
146 }
147 }
148
149 return success;
150 }
151
152 void server_startup_st::restart()
153 {
154 for (std::vector<Server *>::iterator iter= servers.begin(); iter != servers.end(); ++iter)
155 {
156 Server *server = *iter;
157
158 if (server->check()) {
159 server->kill();
160 }
161 server->start();
162 }
163 }
164
165 #define MAGIC_MEMORY 123575
166 server_startup_st::server_startup_st() :
167 _magic(MAGIC_MEMORY),
168 _socket(false),
169 _sasl(false),
170 udp(0),
171 _servers_to_run(5)
172 { }
173
174 server_startup_st::~server_startup_st()
175 {
176 clear();
177 }
178
179 bool server_startup_st::validate()
180 {
181 return _magic == MAGIC_MEMORY;
182 }
183
184 bool server_startup(server_startup_st& construct, const std::string& server_type, in_port_t try_port, const char *argv[])
185 {
186 return construct.start_server(server_type, try_port, argv);
187 }
188
189 libtest::Server* server_startup_st::create(const std::string& server_type, in_port_t try_port, const bool is_socket)
190 {
191 libtest::Server *server= NULL;
192
193 if (is_socket == false)
194 {
195 if (try_port <= 0)
196 {
197 throw libtest::fatal(LIBYATL_DEFAULT_PARAM, "was passed the invalid port number %d", int(try_port));
198 }
199 }
200
201 if (is_socket)
202 {
203 if (server_type.compare("memcached") == 0)
204 {
205 server= build_memcached_socket("localhost", try_port);
206 }
207 else
208 {
209 Error << "Socket is not support for server: " << server_type;
210 return NULL;
211 }
212 }
213 else if (server_type.compare("gearmand") == 0)
214 {
215 server= build_gearmand("localhost", try_port);
216 }
217 else if (server_type.compare("hostile-gearmand") == 0)
218 {
219 server= build_gearmand("localhost", try_port, "gearmand/hostile_gearmand");
220 }
221 else if (server_type.compare("drizzled") == 0)
222 {
223 if (has_drizzled())
224 {
225 if (has_libdrizzle())
226 {
227 server= build_drizzled("localhost", try_port);
228 }
229 }
230 }
231 else if (server_type.compare("blobslap_worker") == 0)
232 {
233 if (has_gearmand())
234 {
235 #ifdef GEARMAND_BLOBSLAP_WORKER
236 if (GEARMAND_BLOBSLAP_WORKER)
237 {
238 if (HAVE_LIBGEARMAN)
239 {
240 server= build_blobslap_worker(try_port);
241 }
242 }
243 #endif // GEARMAND_BLOBSLAP_WORKER
244 }
245 }
246 else if (server_type.compare("memcached") == 0)
247 {
248 if (has_memcached())
249 {
250 server= build_memcached("localhost", try_port);
251 }
252 }
253
254 return server;
255 }
256
257 class ServerPtr {
258 public:
259 ServerPtr(libtest::Server* server_):
260 _server(server_)
261 { }
262
263 ~ServerPtr()
264 {
265 delete _server;
266 }
267
268 void reset()
269 {
270 delete _server;
271 _server= NULL;
272 }
273
274 libtest::Server* release(libtest::Server* server_= NULL)
275 {
276 libtest::Server* tmp= _server;
277 _server= server_;
278 return tmp;
279 }
280
281 libtest::Server* operator->() const
282 {
283 return _server;
284 }
285
286 libtest::Server* operator&() const
287 {
288 return _server;
289 }
290
291 private:
292 libtest::Server* _server;
293 };
294
295 bool server_startup_st::_start_server(const bool is_socket,
296 const std::string& server_type,
297 in_port_t try_port,
298 const char *argv[])
299 {
300 try {
301 ServerPtr server(create(server_type, try_port, is_socket));
302
303 if (&server == NULL)
304 {
305 Error << "Could not allocate server: " << server_type;
306 return false;
307 }
308
309 /*
310 We will now cycle the server we have created.
311 */
312 if (server->cycle() == false)
313 {
314 Error << "Could not start up server " << &server;
315 return false;
316 }
317
318 server->init(argv);
319
320 #if 0
321 if (false)
322 {
323 Out << "Pausing for startup, hit return when ready.";
324 std::string gdb_command= server->base_command();
325 getchar();
326 }
327 else
328 #endif
329
330 if (server->start() == false)
331 {
332 return false;
333 }
334 else
335 {
336 {
337 #ifdef DEBUG
338 if (DEBUG)
339 {
340 Outn();
341 Out << "STARTING SERVER(pid:" << server->pid() << "): " << server->running();
342 Outn();
343 }
344 #endif
345 }
346 }
347
348 push_server(server.release());
349
350 if (is_socket and &server)
351 {
352 set_default_socket(server->socket().c_str());
353 }
354 }
355 catch (const libtest::disconnected& err)
356 {
357 if (fatal::is_disabled() == false and try_port != LIBTEST_FAIL_PORT)
358 {
359 stream::cerr(err.file(), err.line(), err.func()) << err.what();
360 return false;
361 }
362 }
363 catch (const libtest::__test_result& err)
364 {
365 stream::cerr(err.file(), err.line(), err.func()) << err.what();
366 return false;
367 }
368 catch (const std::exception& err)
369 {
370 Error << err.what();
371 return false;
372 }
373 catch (...)
374 {
375 Error << "error occured while creating server: " << server_type;
376 return false;
377 }
378
379 return true;
380 }
381
382 bool server_startup_st::start_server(const std::string& server_type, in_port_t try_port, const char *argv[])
383 {
384 return _start_server(false, server_type, try_port, argv);
385 }
386
387 bool server_startup_st::start_socket_server(const std::string& server_type, const in_port_t try_port, const char *argv[])
388 {
389 return _start_server(true, server_type, try_port, argv);
390 }
391
392 std::string server_startup_st::option_string() const
393 {
394 std::string temp= server_list;
395 rtrim(temp);
396 return temp;
397 }
398
399
400 } // namespace libtest