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