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