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