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