jenkins-promote-staging-trunk-libmemcached-1
[awesomized/libmemcached] / libtest / memcached.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
23 #include <libtest/common.h>
24
25 #include <libmemcached-1.0/memcached.h>
26 #include <libmemcachedutil-1.0/util.h>
27
28 using namespace libtest;
29
30 #include <cassert>
31 #include <cerrno>
32 #include <cstdio>
33 #include <cstdlib>
34 #include <cstring>
35 #include <iostream>
36 #include <signal.h>
37 #include <sys/types.h>
38 #include <sys/wait.h>
39 #include <unistd.h>
40
41 #include <libtest/server.h>
42 #include <libtest/wait.h>
43
44 #include <libtest/memcached.h>
45
46 #ifndef __INTEL_COMPILER
47 #pragma GCC diagnostic ignored "-Wold-style-cast"
48 #endif
49
50 using namespace libtest;
51
52 class Memcached : public libtest::Server
53 {
54 std::string _username;
55 std::string _password;
56
57 public:
58 Memcached(const std::string& host_arg, const in_port_t port_arg, const bool is_socket_arg, const std::string& username_arg, const std::string& password_arg) :
59 libtest::Server(host_arg, port_arg, is_socket_arg),
60 _username(username_arg),
61 _password(password_arg)
62 { }
63
64 Memcached(const std::string& host_arg, const in_port_t port_arg, const bool is_socket_arg) :
65 libtest::Server(host_arg, port_arg, is_socket_arg)
66 {
67 set_pid_file();
68 }
69
70 virtual const char *sasl() const
71 {
72 return NULL;
73 }
74
75 const std::string& password() const
76 {
77 return _password;
78 }
79
80 const std::string& username() const
81 {
82 return _username;
83 }
84
85 pid_t get_pid(bool error_is_ok)
86 {
87 // Memcached is slow to start, so we need to do this
88 if (pid_file().empty() == false)
89 {
90 if (error_is_ok and not wait_for_pidfile())
91 {
92 Error << "Pidfile was not found:" << pid_file();
93 return -1;
94 }
95 }
96
97 pid_t local_pid;
98 memcached_return_t rc= MEMCACHED_SUCCESS;
99 if (has_socket())
100 {
101 if (socket().empty())
102 {
103 return -1;
104 }
105
106 local_pid= libmemcached_util_getpid(socket().c_str(), port(), &rc);
107 }
108 else
109 {
110 local_pid= libmemcached_util_getpid(hostname().c_str(), port(), &rc);
111 }
112
113 if (error_is_ok and ((memcached_failed(rc) or not is_pid_valid(local_pid))))
114 {
115 Error << "libmemcached_util_getpid(" << memcached_strerror(NULL, rc) << ") pid: " << local_pid << " for:" << *this;
116 }
117
118 return local_pid;
119 }
120
121 bool ping()
122 {
123 // Memcached is slow to start, so we need to do this
124 if (not pid_file().empty())
125 {
126 if (wait_for_pidfile() == false)
127 {
128 Error << "Pidfile was not found:" << pid_file();
129 return -1;
130 }
131 }
132
133 memcached_return_t rc;
134 bool ret;
135
136 if (has_socket())
137 {
138 ret= libmemcached_util_ping(socket().c_str(), 0, &rc);
139 }
140 else
141 {
142 ret= libmemcached_util_ping(hostname().c_str(), port(), &rc);
143 }
144
145 if (memcached_failed(rc) or not ret)
146 {
147 Error << "libmemcached_util_ping(" << hostname() << ", " << port() << ") error: " << memcached_strerror(NULL, rc);
148 }
149
150 return ret;
151 }
152
153 const char *name()
154 {
155 return "memcached";
156 };
157
158 const char *executable()
159 {
160 return MEMCACHED_BINARY;
161 }
162
163 virtual void pid_file_option(Application& app, const std::string& arg)
164 {
165 if (arg.empty() == false)
166 {
167 app.add_option("-P", arg);
168 }
169 }
170
171 const char *socket_file_option() const
172 {
173 return "-s ";
174 }
175
176 const char *daemon_file_option()
177 {
178 return "-d";
179 }
180
181 virtual void port_option(Application& app, in_port_t arg)
182 {
183 char buffer[30];
184 snprintf(buffer, sizeof(buffer), "%d", int(arg));
185 app.add_option("-p", buffer);
186 }
187
188 bool has_port_option() const
189 {
190 return true;
191 }
192
193 bool has_socket_file_option() const
194 {
195 return has_socket();
196 }
197
198 void socket_file_option(Application& app, const std::string& socket_arg)
199 {
200 if (socket_arg.empty() == false)
201 {
202 app.add_option("-s", socket_arg);
203 }
204 }
205
206 bool is_libtool()
207 {
208 return false;
209 }
210
211 bool broken_socket_cleanup()
212 {
213 return true;
214 }
215
216 // Memcached's pidfile is broken
217 bool broken_pid_file()
218 {
219 return true;
220 }
221
222 bool build(size_t argc, const char *argv[]);
223 };
224
225 class MemcachedLight : public libtest::Server
226 {
227
228 public:
229 MemcachedLight(const std::string& host_arg, const in_port_t port_arg):
230 libtest::Server(host_arg, port_arg)
231 {
232 set_pid_file();
233 }
234
235 pid_t get_pid(bool error_is_ok)
236 {
237 // Memcached is slow to start, so we need to do this
238 if (not pid_file().empty())
239 {
240 if (error_is_ok and not wait_for_pidfile())
241 {
242 Error << "Pidfile was not found:" << pid_file();
243 return -1;
244 }
245 }
246
247 bool success= false;
248 std::stringstream error_message;
249 pid_t local_pid= get_pid_from_file(pid_file(), error_message);
250 if (local_pid > 0)
251 {
252 if (::kill(local_pid, 0) > 0)
253 {
254 success= true;
255 }
256 }
257
258 if (error_is_ok and ((success or not is_pid_valid(local_pid))))
259 {
260 Error << "kill(" << " pid: " << local_pid << " errno:" << strerror(errno) << " for:" << *this;
261 }
262
263 return local_pid;
264 }
265
266 bool ping()
267 {
268 // Memcached is slow to start, so we need to do this
269 if (not pid_file().empty())
270 {
271 if (not wait_for_pidfile())
272 {
273 Error << "Pidfile was not found:" << pid_file();
274 return false;
275 }
276 }
277
278 std::stringstream error_message;
279 pid_t local_pid= get_pid_from_file(pid_file(), error_message);
280 if (local_pid > 0)
281 {
282 if (::kill(local_pid, 0) == 0)
283 {
284 return true;
285 }
286 }
287
288 return false;
289 }
290
291 const char *name()
292 {
293 return "memcached_light";
294 };
295
296 const char *executable()
297 {
298 return MEMCACHED_LIGHT_BINARY;
299 }
300
301 const char *daemon_file_option()
302 {
303 return "--daemon";
304 }
305
306 virtual void port_option(Application& app, in_port_t arg)
307 {
308 char buffer[1024];
309 snprintf(buffer, sizeof(buffer), "--port=%d", int(arg));
310 app.add_option(buffer);
311 }
312
313 bool has_port_option() const
314 {
315 return true;
316 }
317
318 bool is_libtool()
319 {
320 return true;
321 }
322
323 void log_file_option(Application& app, const std::string& arg)
324 {
325 if (arg.empty() == false)
326 {
327 std::string buffer("--log-file=");
328 buffer+= arg;
329 app.add_option("--verbose");
330 app.add_option(buffer);
331 }
332 }
333
334 bool has_log_file_option() const
335 {
336 return true;
337 }
338
339 bool build(size_t argc, const char *argv[]);
340 };
341
342 class MemcachedSaSL : public Memcached
343 {
344 public:
345 MemcachedSaSL(const std::string& host_arg, const in_port_t port_arg, const bool is_socket_arg, const std::string& username_arg, const std::string &password_arg) :
346 Memcached(host_arg, port_arg, is_socket_arg, username_arg, password_arg)
347 { }
348
349 const char *name()
350 {
351 return "memcached-sasl";
352 };
353
354 const char *sasl() const
355 {
356 return " -S -B binary ";
357 }
358
359 const char *executable()
360 {
361 return MEMCACHED_SASL_BINARY;
362 }
363
364 pid_t get_pid(bool error_is_ok)
365 {
366 // Memcached is slow to start, so we need to do this
367 if (not pid_file().empty())
368 {
369 if (error_is_ok and not wait_for_pidfile())
370 {
371 Error << "Pidfile was not found:" << pid_file();
372 return -1;
373 }
374 }
375
376 pid_t local_pid;
377 memcached_return_t rc;
378 if (has_socket())
379 {
380 local_pid= libmemcached_util_getpid2(socket().c_str(), 0, username().c_str(), password().c_str(), &rc);
381 }
382 else
383 {
384 local_pid= libmemcached_util_getpid2(hostname().c_str(), port(), username().c_str(), password().c_str(), &rc);
385 }
386
387 if (error_is_ok and ((memcached_failed(rc) or not is_pid_valid(local_pid))))
388 {
389 Error << "libmemcached_util_getpid2(" << memcached_strerror(NULL, rc) << ") username: " << username() << " password: " << password() << " pid: " << local_pid << " for:" << *this;
390 }
391
392 return local_pid;
393 }
394
395 bool ping()
396 {
397 // Memcached is slow to start, so we need to do this
398 if (not pid_file().empty())
399 {
400 if (not wait_for_pidfile())
401 {
402 Error << "Pidfile was not found:" << pid_file();
403 return -1;
404 }
405 }
406
407 memcached_return_t rc;
408 bool ret;
409
410 if (has_socket())
411 {
412 ret= libmemcached_util_ping2(socket().c_str(), 0, username().c_str(), password().c_str(), &rc);
413 }
414 else
415 {
416 ret= libmemcached_util_ping2(hostname().c_str(), port(), username().c_str(), password().c_str(), &rc);
417 }
418
419 if (memcached_failed(rc) or not ret)
420 {
421 Error << "libmemcached_util_ping2(" << hostname() << ", " << port() << ", " << username() << ", " << password() << ") error: " << memcached_strerror(NULL, rc);
422 }
423
424 return ret;
425 }
426
427 };
428
429
430 #include <sstream>
431
432 bool Memcached::build(size_t argc, const char *argv[])
433 {
434 std::stringstream arg_buffer;
435
436 if (getuid() == 0 or geteuid() == 0)
437 {
438 add_option("-u", "root");
439 }
440
441 add_option("-l", "localhost");
442 add_option("-m", "128");
443 add_option("-M");
444
445 if (sasl())
446 {
447 add_option(sasl());
448 }
449
450 for (int x= 0 ; x < argc ; x++)
451 {
452 add_option(argv[x]);
453 }
454
455 return true;
456 }
457
458 bool MemcachedLight::build(size_t argc, const char *argv[])
459 {
460 for (int x= 0 ; x < argc ; x++)
461 {
462 add_option(argv[x]);
463 }
464
465 return true;
466 }
467
468 namespace libtest {
469
470 libtest::Server *build_memcached(const std::string& hostname, const in_port_t try_port)
471 {
472 return new Memcached(hostname, try_port, false);
473 }
474
475 libtest::Server *build_memcached_socket(const std::string& socket_file, const in_port_t try_port)
476 {
477 return new Memcached(socket_file, try_port, true);
478 }
479
480 libtest::Server *build_memcached_light(const std::string& hostname, const in_port_t try_port)
481 {
482 return new MemcachedLight(hostname, try_port);
483 }
484
485
486 libtest::Server *build_memcached_sasl(const std::string& hostname, const in_port_t try_port, const std::string& username, const std::string &password)
487 {
488 if (username.empty())
489 {
490 return new MemcachedSaSL(hostname, try_port, false, "memcached", "memcached");
491 }
492
493 return new MemcachedSaSL(hostname, try_port, false, username, password);
494 }
495
496 libtest::Server *build_memcached_sasl_socket(const std::string& socket_file, const in_port_t try_port, const std::string& username, const std::string &password)
497 {
498 if (username.empty())
499 {
500 return new MemcachedSaSL(socket_file, try_port, true, "memcached", "memcached");
501 }
502
503 return new MemcachedSaSL(socket_file, try_port, true, username, password);
504 }
505
506 }
507