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