1 /* vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
3 * Data Differential YATL (i.e. libtest) library
5 * Copyright (C) 2012 Data Differential, http://datadifferential.com/
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions are
11 * * Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
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
19 * * The names of its contributors may not be used to endorse or
20 * promote products derived from this software without specific prior
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.
37 #include "mem_config.h"
38 #include <libtest/common.h>
40 #include <libmemcached-1.0/memcached.h>
41 #include <libmemcachedutil-1.0/util.h>
43 using namespace libtest
;
52 #include <sys/types.h>
56 #include <libtest/server.h>
57 #include <libtest/wait.h>
59 #include <libtest/memcached.h>
61 #ifndef __INTEL_COMPILER
62 #pragma GCC diagnostic ignored "-Wold-style-cast"
65 using namespace libtest
;
68 bool is_memcached_libtool()
70 if (MEMCACHED_BINARY
and strcmp(MEMCACHED_BINARY
, "memcached/memcached") == 0)
79 class Memcached
: public libtest::Server
81 std::string _username
;
82 std::string _password
;
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
)
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
)
103 virtual const char *sasl() const
108 const std::string
& password() const
113 const std::string
& username() const
118 bool wait_for_pidfile() const
122 return wait
.successful();
127 if (out_of_ban_killed())
132 SimpleClient
client(_hostname
, _port
);
134 std::string response
;
135 return client
.send_message("version", response
);
143 const char *executable()
145 return MEMCACHED_BINARY
;
150 return is_memcached_libtool();
153 virtual void pid_file_option(Application
& app
, const std::string
& arg
)
155 if (arg
.empty() == false)
157 app
.add_option("-P", arg
);
161 const char *socket_file_option() const
166 virtual void port_option(Application
& app
, in_port_t arg
)
169 snprintf(buffer
, sizeof(buffer
), "%d", int(arg
));
170 app
.add_option("-p", buffer
);
173 bool has_port_option() const
178 bool has_socket_file_option() const
183 void socket_file_option(Application
& app
, const std::string
& socket_arg
)
185 if (socket_arg
.empty() == false)
187 app
.add_option("-s", socket_arg
);
191 bool broken_socket_cleanup()
196 // Memcached's pidfile is broken
197 bool broken_pid_file()
202 bool build(size_t argc
, const char *argv
[]);
205 class MemcachedLight
: public libtest::Server
209 MemcachedLight(const std::string
& host_arg
, const in_port_t port_arg
) :
210 libtest::Server(host_arg
, port_arg
, MEMCACHED_LIGHT_BINARY
, true)
217 // Memcached is slow to start, so we need to do this
218 if (not pid_file().empty())
220 if (not wait_for_pidfile())
222 Error
<< "Pidfile was not found:" << pid_file();
227 std::stringstream error_message
;
228 pid_t local_pid
= get_pid_from_file(pid_file(), error_message
);
231 if (::kill(local_pid
, 0) == 0)
242 return "memcached_light";
245 const char *executable()
247 return MEMCACHED_LIGHT_BINARY
;
250 virtual void port_option(Application
& app
, in_port_t arg
)
253 snprintf(buffer
, sizeof(buffer
), "--port=%d", int(arg
));
254 app
.add_option(buffer
);
257 bool has_port_option() const
267 void log_file_option(Application
& app
, const std::string
& arg
)
269 if (arg
.empty() == false)
271 std::string
buffer("--log-file=");
273 app
.add_option("--verbose");
274 app
.add_option(buffer
);
278 bool has_log_file_option() const
283 bool build(size_t argc
, const char *argv
[]);
286 class MemcachedSaSL
: public Memcached
289 MemcachedSaSL(const std::string
& host_arg
,
290 const in_port_t port_arg
,
291 const bool is_socket_arg
,
292 const std::string
& username_arg
,
293 const std::string
&password_arg
) :
294 Memcached(host_arg
, port_arg
, is_socket_arg
, username_arg
, password_arg
)
299 return "memcached-sasl";
302 const char *sasl() const
304 return " -S -B binary ";
307 const char *executable()
309 return MEMCACHED_SASL_BINARY
;
314 memcached_return_t rc
;
319 ret
= libmemcached_util_ping2(socket().c_str(), 0, username().c_str(), password().c_str(), &rc
);
323 ret
= libmemcached_util_ping2(hostname().c_str(), port(), username().c_str(), password().c_str(), &rc
);
326 if (memcached_failed(rc
) or ret
== false)
328 error(memcached_strerror(NULL
, rc
));
339 bool Memcached::build(size_t argc
, const char *argv
[])
341 if (getuid() == 0 or geteuid() == 0)
343 add_option("-u", "root");
346 add_option("-l", "localhost");
347 #ifndef TARGET_OS_OSX
348 add_option("-m", "128");
357 for (size_t x
= 0 ; x
< argc
; x
++)
365 bool MemcachedLight::build(size_t argc
, const char *argv
[])
367 for (size_t x
= 0 ; x
< argc
; x
++)
377 libtest::Server
*build_memcached(const std::string
& hostname
, const in_port_t try_port
)
379 return new Memcached(hostname
, try_port
, false);
382 libtest::Server
*build_memcached_socket(const std::string
& socket_file
, const in_port_t try_port
)
384 return new Memcached(socket_file
, try_port
, true);
387 libtest::Server
*build_memcached_light(const std::string
& hostname
, const in_port_t try_port
)
389 return new MemcachedLight(hostname
, try_port
);
393 libtest::Server
*build_memcached_sasl(const std::string
& hostname
, const in_port_t try_port
, const std::string
& username
, const std::string
&password
)
395 if (username
.empty())
397 return new MemcachedSaSL(hostname
, try_port
, false, "memcached", "memcached");
400 return new MemcachedSaSL(hostname
, try_port
, false, username
, password
);
403 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
)
405 if (username
.empty())
407 return new MemcachedSaSL(socket_file
, try_port
, true, "memcached", "memcached");
410 return new MemcachedSaSL(socket_file
, try_port
, true, username
, password
);