85307fcf0825b60a056fc41c46ba47d72fb2a77d
[awesomized/libmemcached] / libtest / gearmand.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
23 #include <config.h>
24 #include <libtest/common.h>
25
26 #include <libtest/gearmand.h>
27
28 #include "util/instance.hpp"
29 #include "util/operation.hpp"
30
31 using namespace datadifferential;
32 using namespace libtest;
33
34 #include <cassert>
35 #include <cerrno>
36 #include <cstdio>
37 #include <cstdlib>
38 #include <cstring>
39 #include <iostream>
40 #include <signal.h>
41 #include <sstream>
42 #include <sys/types.h>
43 #include <sys/wait.h>
44 #include <unistd.h>
45
46 #include <libgearman/gearman.h>
47
48 #ifndef __INTEL_COMPILER
49 #pragma GCC diagnostic ignored "-Wold-style-cast"
50 #endif
51
52 class GetPid : public util::Instance::Finish
53 {
54 private:
55 pid_t _pid;
56
57 public:
58 GetPid() :
59 _pid(-1)
60 { }
61
62 pid_t pid()
63 {
64 return _pid;
65 }
66
67
68 bool call(const bool success, const std::string &response)
69 {
70 _pid= -1;
71 if (success and response.size())
72 {
73 _pid= atoi(response.c_str());
74 }
75
76 if (is_pid_valid(_pid) == false)
77 {
78 _pid= -1;
79 return false;
80 }
81
82 return true;
83 }
84 };
85
86 using namespace libtest;
87
88 class Gearmand : public libtest::Server
89 {
90 private:
91 public:
92 Gearmand(const std::string& host_arg, in_port_t port_arg) :
93 libtest::Server(host_arg, port_arg)
94 {
95 set_pid_file();
96 }
97
98 pid_t get_pid(bool error_is_ok)
99 {
100 if (pid_file().empty() == false)
101 {
102 Wait wait(pid_file(), 0);
103
104 if (error_is_ok and not wait.successful())
105 {
106 Error << "Pidfile was not found:" << pid_file();
107 return -1;
108 }
109 }
110
111 GetPid *get_instance_pid;
112 util::Instance instance(hostname(), port());
113 instance.set_finish(get_instance_pid= new GetPid);
114
115 instance.push(new util::Operation(test_literal_param("getpid\r\n"), true));
116
117 if (error_is_ok and instance.run() == false)
118 {
119 Error << "Failed to obtain pid of server";
120 }
121
122 return get_instance_pid->pid();
123 }
124
125 bool ping()
126 {
127 gearman_client_st *client= gearman_client_create(NULL);
128 if (client == NULL)
129 {
130 Error << "Could not allocate memory for gearman_client_create()";
131 return false;
132 }
133 gearman_client_set_timeout(client, 3000);
134
135 if (gearman_success(gearman_client_add_server(client, hostname().c_str(), port())))
136 {
137 gearman_return_t rc= gearman_client_echo(client, test_literal_param("This is my echo test"));
138
139 if (gearman_success(rc))
140 {
141 gearman_client_free(client);
142 return true;
143 }
144 #if 0
145 Error << hostname().c_str() << ":" << port() << " was " << gearman_strerror(rc) << " extended: " << gearman_client_error(client);
146 #endif
147 }
148 else
149 {
150 Error << "gearman_client_add_server() " << gearman_client_error(client);
151 }
152
153 gearman_client_free(client);
154
155 return false;;
156 }
157
158 const char *name()
159 {
160 return "gearmand";
161 };
162
163 const char *executable()
164 {
165 return GEARMAND_BINARY;
166 }
167
168 const char *daemon_file_option()
169 {
170 return "--daemon";
171 }
172
173 void log_file_option(Application& app, const std::string& arg)
174 {
175 if (arg.empty() == false)
176 {
177 std::string buffer("--log-file=");
178 buffer+= arg;
179 app.add_option("--verbose=DEBUG");
180 app.add_option(buffer);
181 }
182 }
183
184 bool has_log_file_option() const
185 {
186 return true;
187 }
188
189 bool is_libtool()
190 {
191 return true;
192 }
193
194 bool has_syslog() const
195 {
196 return true;
197 }
198
199 bool has_port_option() const
200 {
201 return true;
202 }
203
204 bool build(size_t argc, const char *argv[]);
205 };
206
207 bool Gearmand::build(size_t argc, const char *argv[])
208 {
209 std::stringstream arg_buffer;
210
211 if (getuid() == 0 or geteuid() == 0)
212 {
213 add_option("-u", "root");
214 }
215
216 add_option("--listen=localhost");
217
218 for (size_t x= 0 ; x < argc ; x++)
219 {
220 add_option(argv[x]);
221 }
222
223 return true;
224 }
225
226 namespace libtest {
227
228 libtest::Server *build_gearmand(const char *hostname, in_port_t try_port)
229 {
230 return new Gearmand(hostname, try_port);
231 }
232
233 }