Merge in tests for SASL
[m6w6/libmemcached] / libtest / server_container.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 #include <libtest/common.h>
23
24 #include <cassert>
25 #include <cerrno>
26 #include <cstdlib>
27 #include <iostream>
28
29 #include <algorithm>
30 #include <functional>
31 #include <locale>
32
33 // trim from end
34 static inline std::string &rtrim(std::string &s)
35 {
36 s.erase(std::find_if(s.rbegin(), s.rend(), std::not1(std::ptr_fun<int, int>(std::isspace))).base(), s.end());
37 return s;
38 }
39
40 namespace libtest {
41
42 void server_startup_st::push_server(Server *arg)
43 {
44 servers.push_back(arg);
45
46 char port_str[NI_MAXSERV];
47 snprintf(port_str, sizeof(port_str), "%u", int(arg->port()));
48
49 std::string server_config_string;
50 if (arg->has_socket())
51 {
52 server_config_string+= "--socket=";
53 server_config_string+= '"';
54 server_config_string+= arg->socket();
55 server_config_string+= '"';
56 server_config_string+= " ";
57 }
58 else
59 {
60 server_config_string+= "--server=";
61 server_config_string+= arg->hostname();
62 server_config_string+= ":";
63 server_config_string+= port_str;
64 server_config_string+= " ";
65 }
66
67 server_list+= server_config_string;
68
69 }
70
71 Server* server_startup_st::pop_server()
72 {
73 Server *tmp= servers.back();
74 servers.pop_back();
75 return tmp;
76 }
77
78 void server_startup_st::shutdown(bool remove)
79 {
80 if (remove)
81 {
82 for (std::vector<Server *>::iterator iter= servers.begin(); iter != servers.end(); iter++)
83 {
84 delete *iter;
85 }
86 servers.clear();
87 }
88 else
89 {
90 for (std::vector<Server *>::iterator iter= servers.begin(); iter != servers.end(); iter++)
91 {
92 if ((*iter)->has_pid() and not (*iter)->kill((*iter)->pid()))
93 {
94 Error << "Unable to kill:" << *(*iter);
95 }
96 }
97 }
98 }
99
100 server_startup_st::~server_startup_st()
101 {
102 shutdown(true);
103 }
104
105 bool server_startup_st::is_debug() const
106 {
107 return bool(getenv("LIBTEST_MANUAL_GDB"));
108 }
109
110 bool server_startup_st::is_valgrind() const
111 {
112 return bool(getenv("LIBTEST_MANUAL_VALGRIND"));
113 }
114
115 bool server_startup_st::is_helgrind() const
116 {
117 return bool(getenv("LIBTEST_MANUAL_HELGRIND"));
118 }
119
120
121 bool server_startup(server_startup_st& construct, const std::string& server_type, in_port_t try_port, int argc, const char *argv[])
122 {
123 Outn();
124 (void)try_port;
125
126 // Look to see if we are being provided ports to use
127 {
128 char variable_buffer[1024];
129 snprintf(variable_buffer, sizeof(variable_buffer), "LIBTEST_PORT_%lu", (unsigned long)construct.count());
130
131 char *var;
132 if ((var= getenv(variable_buffer)))
133 {
134 in_port_t tmp= in_port_t(atoi(var));
135
136 if (tmp > 0)
137 try_port= tmp;
138 }
139 }
140
141 libtest::Server *server= NULL;
142 if (0)
143 { }
144 else if (server_type.compare("gearmand") == 0)
145 {
146 if (GEARMAND_BINARY)
147 {
148 if (HAVE_LIBGEARMAN)
149 {
150 server= build_gearmand("localhost", try_port);
151 }
152 else
153 {
154 Error << "Libgearman was not found";
155 }
156 }
157 else
158 {
159 Error << "No gearmand binary is available";
160 }
161 }
162 else if (server_type.compare("blobslap_worker") == 0)
163 {
164 if (GEARMAND_BINARY and GEARMAND_BLOBSLAP_WORKER)
165 {
166 if (HAVE_LIBGEARMAN)
167 {
168 server= build_blobslap_worker(try_port);
169 }
170 else
171 {
172 Error << "Libgearman was not found";
173 }
174 }
175 else
176 {
177 Error << "No gearmand binary is available";
178 }
179 }
180 else if (server_type.compare("memcached-sasl") == 0)
181 {
182 if (MEMCACHED_SASL_BINARY)
183 {
184 if (HAVE_LIBMEMCACHED)
185 {
186 server= build_memcached_sasl("localhost", try_port, construct.username(), construct.password());
187 }
188 else
189 {
190 Error << "Libmemcached was not found";
191 }
192 }
193 else
194 {
195 Error << "No memcached binary that was compiled with sasl is available";
196 }
197 }
198 else if (server_type.compare("memcached") == 0)
199 {
200 if (MEMCACHED_BINARY)
201 {
202 if (HAVE_LIBMEMCACHED)
203 {
204 server= build_memcached("localhost", try_port);
205 }
206 else
207 {
208 Error << "Libmemcached was not found";
209 }
210 }
211 else
212 {
213 Error << "No memcached binary is available";
214 }
215 }
216 else
217 {
218 Error << "Failed to start " << server_type << ", no support was found to be compiled in for it.";
219 }
220
221 if (server == NULL)
222 {
223 Error << "Failure occured while creating server: " << server_type;
224 return false;
225 }
226
227 /*
228 We will now cycle the server we have created.
229 */
230 if (not server->cycle())
231 {
232 Error << "Could not start up server " << *server;
233 delete server;
234 return false;
235 }
236
237 server->build(argc, argv);
238
239 if (construct.is_debug())
240 {
241 Out << "Pausing for startup, hit return when ready.";
242 std::string gdb_command= server->base_command();
243 std::string options;
244 Out << "run " << server->args(options);
245 getchar();
246 }
247 else if (not server->start())
248 {
249 Error << "Failed to start " << *server;
250 delete server;
251 return false;
252 }
253 else
254 {
255 Out << "STARTING SERVER(pid:" << server->pid() << "): " << server->running();
256 }
257
258 construct.push_server(server);
259
260 if (default_port() == 0)
261 {
262 assert(server->has_port());
263 set_default_port(server->port());
264 }
265
266 Outn();
267
268 return true;
269 }
270
271 bool server_startup_st::start_socket_server(const std::string& server_type, const in_port_t try_port, int argc, const char *argv[])
272 {
273 (void)try_port;
274 Outn();
275
276 Server *server= NULL;
277 if (0)
278 { }
279 else if (server_type.compare("gearmand") == 0)
280 {
281 Error << "Socket files are not supported for gearmand yet";
282 }
283 else if (server_type.compare("memcached-sasl") == 0)
284 {
285 if (MEMCACHED_SASL_BINARY)
286 {
287 if (HAVE_LIBMEMCACHED)
288 {
289 server= build_memcached_sasl_socket("localhost", try_port, username(), password());
290 }
291 else
292 {
293 Error << "Libmemcached was not found";
294 }
295 }
296 else
297 {
298 Error << "No memcached binary is available";
299 }
300 }
301 else if (server_type.compare("memcached") == 0)
302 {
303 if (MEMCACHED_BINARY)
304 {
305 if (HAVE_LIBMEMCACHED)
306 {
307 server= build_memcached_socket("localhost", try_port);
308 }
309 else
310 {
311 Error << "Libmemcached was not found";
312 }
313 }
314 else
315 {
316 Error << "No memcached binary is available";
317 }
318 }
319 else
320 {
321 Error << "Failed to start " << server_type << ", no support was found to be compiled in for it.";
322 }
323
324 if (server == NULL)
325 {
326 Error << "Failure occured while creating server: " << server_type;
327 return false;
328 }
329
330 /*
331 We will now cycle the server we have created.
332 */
333 if (not server->cycle())
334 {
335 Error << "Could not start up server " << *server;
336 delete server;
337 return false;
338 }
339
340 server->build(argc, argv);
341
342 if (is_debug())
343 {
344 Out << "Pausing for startup, hit return when ready.";
345 std::string gdb_command= server->base_command();
346 std::string options;
347 Out << "run " << server->args(options);
348 getchar();
349 }
350 else if (not server->start())
351 {
352 Error << "Failed to start " << *server;
353 delete server;
354 return false;
355 }
356 else
357 {
358 Out << "STARTING SERVER(pid:" << server->pid() << "): " << server->running();
359 }
360
361 push_server(server);
362
363 set_default_socket(server->socket().c_str());
364
365 Outn();
366
367 return true;
368 }
369
370 std::string server_startup_st::option_string() const
371 {
372 std::string temp= server_list;
373 rtrim(temp);
374 return temp;
375 }
376
377
378 } // namespace libtest