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