tests: run memcached verbosely, catch output and show it on failure
[awesomized/libmemcached] / libtest / server.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 <cassert>
42 #include <cerrno>
43 #include <climits>
44 #include <cstdlib>
45 #include <iostream>
46 #include <string>
47
48 #include <algorithm>
49 #include <functional>
50 #include <locale>
51 #include <unistd.h>
52
53 // trim from end
54 static inline std::string &rtrim(std::string &s)
55 {
56 s.erase(std::find_if(s.rbegin(), s.rend(), std::not1(std::ptr_fun<int, int>(std::isspace))).base(), s.end());
57 return s;
58 }
59
60 #include <libtest/server.h>
61 #include <libtest/stream.h>
62 #include <libtest/killpid.h>
63
64 namespace libtest {
65
66 std::ostream& operator<<(std::ostream& output, const Server &arg)
67 {
68 if (arg.is_socket())
69 {
70 output << arg.hostname();
71 }
72 else
73 {
74 output << arg.hostname() << ":" << arg.port();
75 }
76
77 if (arg.has_pid())
78 {
79 output << " Pid:" << arg.pid();
80 }
81
82 if (arg.has_socket())
83 {
84 output << " Socket:" << arg.socket();
85 }
86
87 if (arg.running().empty() == false)
88 {
89 output << " Exec:" << arg.running();
90 }
91
92 return output; // for multiple << operators
93 }
94
95 #ifdef __GLIBC__
96 namespace {
97
98 class Buffer
99 {
100 public:
101 Buffer(char *b) : b_(b) {}
102 ~Buffer() { if (b_) free(b_); }
103 char* buf() { return b_; }
104 private:
105 char *b_;
106 };
107
108 }
109 #endif // __GLIBC__
110
111 #define MAGIC_MEMORY 123570
112
113 Server::Server(const std::string& host_arg, const in_port_t port_arg,
114 const std::string& executable, const bool _is_libtool,
115 bool is_socket_arg) :
116 _magic(MAGIC_MEMORY),
117 _is_socket(is_socket_arg),
118 _port(port_arg),
119 _hostname(host_arg),
120 _app(executable, _is_libtool),
121 out_of_ban_killed_(false),
122 _timeout(40)
123 {
124 }
125
126 Server::~Server()
127 {
128 kill();
129 }
130
131 bool Server::check()
132 {
133 _app.clear();
134 return _app.check();
135 }
136
137 bool Server::validate()
138 {
139 return _magic == MAGIC_MEMORY;
140 }
141
142 // If the server exists, kill it
143 bool Server::cycle()
144 {
145 uint32_t limit= 3;
146
147 // Try to ping, and kill the server #limit number of times
148 while (--limit and
149 is_pid_valid(_app.pid()))
150 {
151 if (kill())
152 {
153 Log << "Killed existing server," << *this;
154 dream(0, 50000);
155 continue;
156 }
157 }
158
159 // For whatever reason we could not kill it, and we reached limit
160 if (limit == 0)
161 {
162 Error << "Reached limit, could not kill server";
163 return false;
164 }
165
166 return true;
167 }
168
169 bool Server::wait_for_pidfile() const
170 {
171 Wait wait(pid_file(), 4);
172
173 return wait.successful();
174 }
175
176 bool Server::init(const char *argv[])
177 {
178 if (argv)
179 {
180 for (const char **ptr= argv; *ptr ; ++ptr)
181 {
182 if (ptr)
183 {
184 add_option(*ptr);
185 }
186 }
187 }
188
189 return build();
190 }
191
192 bool Server::has_pid() const
193 {
194 return (_app.pid() > 1);
195 }
196
197
198 bool Server::start()
199 {
200 if (getenv("YATL_GDB_SERVER"))
201 {
202 _app.use_gdb(true);
203 }
204
205 if (port() == LIBTEST_FAIL_PORT)
206 {
207 throw libtest::disconnected(LIBYATL_DEFAULT_PARAM,
208 hostname(), port(), "Called failure");
209 }
210
211 if (getenv("YATL_PTRCHECK_SERVER"))
212 {
213 _app.use_ptrcheck(true);
214 }
215 else if (getenv("YATL_VALGRIND_SERVER"))
216 {
217 _app.use_valgrind(true);
218 }
219
220 out_of_ban_killed(false);
221 if (args(_app) == false)
222 {
223 throw libtest::disconnected(LIBYATL_DEFAULT_PARAM,
224 hostname(), port(), "Could not build command()");
225 }
226
227 libtest::release_port(_port);
228
229 Application::error_t ret;
230 if (Application::SUCCESS != (ret= _app.run()))
231 {
232 throw libtest::disconnected(LIBYATL_DEFAULT_PARAM,
233 hostname(), port(), "Application::run() %s", libtest::Application::toString(ret));
234 return false;
235 }
236 _running= _app.print();
237
238 if (valgrind_is_caller())
239 {
240 dream(5, 50000);
241 }
242
243 size_t repeat= 5;
244 _app.slurp();
245 while (--repeat)
246 {
247 if (pid_file().empty() == false)
248 {
249 Wait wait(pid_file(), 8);
250
251 if (wait.successful() == false)
252 {
253 if (_app.check())
254 {
255 _app.slurp();
256 continue;
257 }
258
259 #ifdef __GLIBC__
260 Buffer buf( get_current_dir_name());
261 char *getcwd_buf= buf.buf();
262 #else
263 libtest::vchar_t buf;
264 buf.resize(PATH_MAX);
265 char *getcwd_buf= getcwd(&buf[0], buf.size());
266 #endif // __GLIBC__
267 throw libtest::disconnected(LIBYATL_DEFAULT_PARAM,
268 hostname(), port(),
269 "Unable to open pidfile in %s for: %s stderr:%s",
270 getcwd_buf ? getcwd_buf : "",
271 _running.c_str(),
272 _app.stderr_c_str());
273 }
274 }
275 }
276
277 bool pinged= false;
278 uint32_t this_wait= 0;
279 {
280 uint32_t waited;
281 uint32_t retry;
282
283 for (waited= 0, retry= 1; ; retry++, waited+= this_wait)
284 {
285 if (_app.check() == false)
286 {
287 break;
288 }
289
290 if ((pinged= ping()) == true)
291 {
292 break;
293 }
294 else if (waited >= _timeout)
295 {
296 break;
297 }
298
299 this_wait= retry * retry / 3 + 1;
300 libtest::dream(this_wait, 0);
301 }
302 }
303
304 if (pinged == false)
305 {
306 #if 0
307 Error << "Failed to ping(" << _app.pid() << ") wait: " << this_wait << " " << hostname() << ":" << port() << " run:" << _running << " " << error();
308 #endif
309
310 // If we happen to have a pid file, lets try to kill it
311 if ((pid_file().empty() == false) and (access(pid_file().c_str(), R_OK) == 0))
312 {
313 _app.slurp();
314 if (kill_file(pid_file()) == false)
315 {
316 throw libtest::disconnected(LIBYATL_DEFAULT_PARAM,
317 hostname(), port(),
318 "Failed to kill off server, waited: %u after startup occurred, when pinging failed: %.*s stderr:%.*s",
319 this_wait,
320 int(_running.size()), _running.c_str(),
321 int(_app.stderr_result_length()), _app.stderr_c_str());
322 }
323 else
324 {
325 throw libtest::disconnected(LIBYATL_DEFAULT_PARAM,
326 hostname(), port(),
327 "Failed native ping(), pid: %d was alive: %s waited: %u server started, having pid_file. exec: %.*s stderr:%.*s",
328 int(_app.pid()),
329 _app.check() ? "true" : "false",
330 this_wait,
331 int(_running.size()), _running.c_str(),
332 int(_app.stderr_result_length()), _app.stderr_c_str());
333 }
334 }
335 else
336 {
337 throw libtest::disconnected(LIBYATL_DEFAULT_PARAM,
338 hostname(), port(),
339 "Failed native ping(), pid: %d is alive: %s waited: %u server started. exec: %.*s stderr:%.*s",
340 int(_app.pid()),
341 _app.check() ? "true" : "false",
342 this_wait,
343 int(_running.size()), _running.c_str(),
344 int(_app.stderr_result_length()), _app.stderr_c_str());
345 }
346 _running.clear();
347
348 return false;
349 }
350
351 return has_pid();
352 }
353
354 void Server::reset_pid()
355 {
356 _running.clear();
357 _pid_file.clear();
358 }
359
360 pid_t Server::pid() const
361 {
362 return _app.pid();
363 }
364
365 void Server::add_option(const std::string& arg)
366 {
367 _options.push_back(std::make_pair(arg, std::string()));
368 }
369
370 void Server::add_option(const std::string& name_, const std::string& value_)
371 {
372 _options.push_back(std::make_pair(name_, value_));
373 }
374
375 bool Server::set_socket_file()
376 {
377 libtest::vchar_t file_buffer;
378 file_buffer.resize(FILENAME_MAX);
379 file_buffer[0]= 0;
380
381 if (broken_pid_file())
382 {
383 snprintf(&file_buffer[0], file_buffer.size(), "/tmp/%s.socketXXXXXX", name());
384 }
385 else
386 {
387 snprintf(&file_buffer[0], file_buffer.size(), "var/run/%s.socketXXXXXX", name());
388 }
389
390 int fd;
391 if ((fd= mkstemp(&file_buffer[0])) == -1)
392 {
393 perror(&file_buffer[0]);
394 return false;
395 }
396 close(fd);
397 unlink(&file_buffer[0]);
398
399 _socket= &file_buffer[0];
400
401 return true;
402 }
403
404 bool Server::set_pid_file()
405 {
406 libtest::vchar_t file_buffer;
407 file_buffer.resize(FILENAME_MAX);
408 file_buffer[0]= 0;
409
410 if (broken_pid_file())
411 {
412 snprintf(&file_buffer[0], file_buffer.size(), "/tmp/%s.pidXXXXXX", name());
413 }
414 else
415 {
416 snprintf(&file_buffer[0], file_buffer.size(), "var/run/%s.pidXXXXXX", name());
417 }
418
419 int fd;
420 if ((fd= mkstemp(&file_buffer[0])) == -1)
421 {
422 throw libtest::fatal(LIBYATL_DEFAULT_PARAM, "mkstemp() failed on %s with %s", &file_buffer[0], strerror(errno));
423 }
424 close(fd);
425 unlink(&file_buffer[0]);
426
427 _pid_file= &file_buffer[0];
428
429 return true;
430 }
431
432 bool Server::set_log_file()
433 {
434 libtest::vchar_t file_buffer;
435 file_buffer.resize(FILENAME_MAX);
436 file_buffer[0]= 0;
437
438 snprintf(&file_buffer[0], file_buffer.size(), "var/log/%s.logXXXXXX", name());
439 int fd;
440 if ((fd= mkstemp(&file_buffer[0])) == -1)
441 {
442 throw libtest::fatal(LIBYATL_DEFAULT_PARAM, "mkstemp() failed on %s with %s", &file_buffer[0], strerror(errno));
443 }
444 close(fd);
445
446 _log_file= &file_buffer[0];
447
448 return true;
449 }
450
451 bool Server::args(Application& app)
452 {
453
454 // Set a log file if it was requested (and we can)
455 if (has_log_file_option())
456 {
457 set_log_file();
458 log_file_option(app, _log_file);
459 }
460
461 if (getenv("LIBTEST_SYSLOG") and has_syslog())
462 {
463 app.add_option("--syslog");
464 }
465
466 // Update pid_file
467 {
468 if (_pid_file.empty() and set_pid_file() == false)
469 {
470 return false;
471 }
472
473 pid_file_option(app, pid_file());
474 }
475
476 if (has_socket_file_option())
477 {
478 if (set_socket_file() == false)
479 {
480 return false;
481 }
482
483 socket_file_option(app, _socket);
484 }
485
486 if (has_port_option())
487 {
488 port_option(app, _port);
489 }
490
491 for (Options::const_iterator iter= _options.begin(); iter != _options.end(); ++iter)
492 {
493 if ((*iter).first.empty() == false)
494 {
495 if ((*iter).second.empty() == false)
496 {
497 app.add_option((*iter).first, (*iter).second);
498 }
499 else
500 {
501 app.add_option((*iter).first);
502 }
503 }
504 }
505
506 return true;
507 }
508
509 bool Server::kill()
510 {
511 if (check_pid(_app.pid())) // If we kill it, reset
512 {
513 _app.murder();
514 if (broken_pid_file() and pid_file().empty() == false)
515 {
516 unlink(pid_file().c_str());
517 }
518
519 if (broken_socket_cleanup() and has_socket() and not socket().empty())
520 {
521 unlink(socket().c_str());
522 }
523
524 reset_pid();
525
526 return true;
527 }
528
529 return false;
530 }
531
532 std::pair<std::string, std::string> Server::output()
533 {
534 _app.slurp();
535 return {
536 std::string {
537 _app.stdout_result().data(),
538 _app.stdout_result().size()
539 },
540 std::string {
541 _app.stderr_result().data(),
542 _app.stderr_result().size()
543 }
544 };
545 }
546
547
548 } // namespace libtest