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