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