tests: run memcached verbosely, catch output and show it on failure
[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 app.add_option("-U", buffer);
161 }
162
163 bool has_port_option() const
164 {
165 return true;
166 }
167
168 bool has_socket_file_option() const
169 {
170 return has_socket();
171 }
172
173 void socket_file_option(Application& app, const std::string& socket_arg)
174 {
175 if (socket_arg.empty() == false)
176 {
177 app.add_option("-s", socket_arg);
178 }
179 }
180
181 bool broken_socket_cleanup()
182 {
183 return true;
184 }
185
186 // Memcached's pidfile is broken
187 bool broken_pid_file()
188 {
189 return true;
190 }
191
192 bool build();
193 };
194
195
196 #include <sstream>
197
198 bool Memcached::build()
199 {
200 if (getuid() == 0 or geteuid() == 0)
201 {
202 add_option("-u", "root");
203 }
204
205 add_option("-l", "localhost");
206 #ifdef __APPLE__
207 #else
208 add_option("-m", "128");
209 add_option("-M");
210 #endif
211
212 if (sasl())
213 {
214 add_option(sasl());
215 }
216
217 add_option("-vv");
218
219 return true;
220 }
221
222 libtest::Server *build_memcached(const std::string& hostname, const in_port_t try_port)
223 {
224 if (has_memcached())
225 {
226 return new Memcached(hostname, try_port, false);
227 }
228
229 return NULL;
230 }
231
232 libtest::Server *build_memcached_socket(const std::string& socket_file, const in_port_t try_port)
233 {
234 if (has_memcached())
235 {
236 return new Memcached(socket_file, try_port, true);
237 }
238
239 return NULL;
240 }
241
242 } // namespace libtest