Merge lp:~tangent-org/libmemcached/1.0-build Build: jenkins-Libmemcached-1.0-95
[awesomized/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 "mem_config.h"
38
39 #include <libtest/common.h>
40
41 #include <libmemcached-1.0/memcached.h>
42 #include <libmemcachedutil-1.0/util.h>
43
44 #include <cassert>
45 #include <cerrno>
46 #include <cstdio>
47 #include <cstdlib>
48 #include <cstring>
49 #include <iostream>
50 #include <signal.h>
51 #include <sys/types.h>
52 #include <sys/wait.h>
53 #include <unistd.h>
54
55 #include <libtest/server.h>
56 #include <libtest/wait.h>
57
58 #include <libtest/memcached.h>
59
60 #ifndef __INTEL_COMPILER
61 #pragma GCC diagnostic ignored "-Wold-style-cast"
62 #endif
63
64 namespace libtest {
65
66 class Memcached : public libtest::Server
67 {
68 std::string _username;
69 std::string _password;
70
71 public:
72 Memcached(const std::string& host_arg,
73 const in_port_t port_arg,
74 const bool is_socket_arg,
75 const std::string& username_arg,
76 const std::string& password_arg) :
77 libtest::Server(host_arg, port_arg,
78 MEMCACHED_BINARY, false, is_socket_arg),
79 _username(username_arg),
80 _password(password_arg)
81 { }
82
83 Memcached(const std::string& host_arg, const in_port_t port_arg, const bool is_socket_arg) :
84 libtest::Server(host_arg, port_arg,
85 MEMCACHED_BINARY, false, is_socket_arg)
86 {
87 set_pid_file();
88 }
89
90 virtual const char *sasl() const
91 {
92 return NULL;
93 }
94
95 const std::string& password() const
96 {
97 return _password;
98 }
99
100 const std::string& username() const
101 {
102 return _username;
103 }
104
105 bool wait_for_pidfile() const
106 {
107 Wait wait(pid(), 4);
108
109 return wait.successful();
110 }
111
112 bool ping()
113 {
114 if (out_of_ban_killed())
115 {
116 return false;
117 }
118
119 if (is_socket())
120 {
121 return _app.check();
122 }
123
124 SimpleClient client(_hostname, _port);
125
126 std::string response;
127 return client.send_message("version", response);
128 }
129
130 const char *name()
131 {
132 return "memcached";
133 };
134
135 const char *executable()
136 {
137 return MEMCACHED_BINARY;
138 }
139
140 bool is_libtool()
141 {
142 return false;
143 }
144
145 virtual void pid_file_option(Application& app, const std::string& arg)
146 {
147 if (arg.empty() == false)
148 {
149 app.add_option("-P", arg);
150 }
151 }
152
153 const char *socket_file_option() const
154 {
155 return "-s ";
156 }
157
158 virtual void port_option(Application& app, in_port_t arg)
159 {
160 char buffer[30];
161 snprintf(buffer, sizeof(buffer), "%d", int(arg));
162 app.add_option("-p", buffer);
163 }
164
165 bool has_port_option() const
166 {
167 return true;
168 }
169
170 bool has_socket_file_option() const
171 {
172 return has_socket();
173 }
174
175 void socket_file_option(Application& app, const std::string& socket_arg)
176 {
177 if (socket_arg.empty() == false)
178 {
179 app.add_option("-s", socket_arg);
180 }
181 }
182
183 bool broken_socket_cleanup()
184 {
185 return true;
186 }
187
188 // Memcached's pidfile is broken
189 bool broken_pid_file()
190 {
191 return true;
192 }
193
194 bool build(size_t argc, const char *argv[]);
195 };
196
197
198 #include <sstream>
199
200 bool Memcached::build(size_t argc, const char *argv[])
201 {
202 if (getuid() == 0 or geteuid() == 0)
203 {
204 add_option("-u", "root");
205 }
206
207 add_option("-l", "localhost");
208 #ifndef TARGET_OS_OSX
209 add_option("-m", "128");
210 add_option("-M");
211 #endif
212
213 if (sasl())
214 {
215 add_option(sasl());
216 }
217
218 for (size_t x= 0 ; x < argc ; x++)
219 {
220 add_option(argv[x]);
221 }
222
223 return true;
224 }
225
226 libtest::Server *build_memcached(const std::string& hostname, const in_port_t try_port)
227 {
228 return new Memcached(hostname, try_port, false);
229 }
230
231 libtest::Server *build_memcached_socket(const std::string& socket_file, const in_port_t try_port)
232 {
233 return new Memcached(socket_file, try_port, true);
234 }
235
236 libtest::Server *build_memcached_sasl(const std::string& hostname, const in_port_t try_port, const std::string& username, const std::string &password)
237 {
238 if (username.empty())
239 {
240 return new Memcached(hostname, try_port, false, "memcached", "memcached");
241 }
242
243 return new Memcached(hostname, try_port, false, username, password);
244 }
245
246 } // namespace libtest
247