Merge in version/etc libtest
[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 <sys/types.h>
41 #include <sys/wait.h>
42 #include <unistd.h>
43
44 #include <libgearman/gearman.h>
45
46 #ifndef __INTEL_COMPILER
47 #pragma GCC diagnostic ignored "-Wold-style-cast"
48 #endif
49
50 class GetPid : public util::Instance::Finish
51 {
52 private:
53 pid_t _pid;
54
55 public:
56 GetPid() :
57 _pid(-1)
58 { }
59
60 pid_t pid()
61 {
62 return _pid;
63 }
64
65
66 bool call(const bool success, const std::string &response)
67 {
68 _pid= -1;
69
70 if (success and response.size())
71 {
72 _pid= atoi(response.c_str());
73 }
74
75 if (_pid < 1)
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
95 pid_t get_pid(bool error_is_ok)
96 {
97 if (not pid_file().empty())
98 {
99 Wait wait(pid_file(), 0);
100
101 if (error_is_ok and not wait.successful())
102 {
103 Error << "Pidfile was not found:" << pid_file();
104 return -1;
105 }
106 }
107
108 GetPid *get_instance_pid;
109 util::Instance instance(hostname(), port());
110 instance.set_finish(get_instance_pid= new GetPid);
111
112 instance.push(new util::Operation(test_literal_param("getpid\r\n"), true));
113
114 if (error_is_ok and instance.run() == false)
115 {
116 Error << "Failed to obtain pid of server";
117 }
118
119 return get_instance_pid->pid();
120 }
121
122 bool ping()
123 {
124 gearman_client_st *client= gearman_client_create(NULL);
125 if (not client)
126 {
127 Error << "Could not allocate memory for gearman_client_create()";
128 return false;
129 }
130 gearman_client_set_timeout(client, 1000);
131
132 if (gearman_success(gearman_client_add_server(client, hostname().c_str(), port())))
133 {
134 gearman_return_t rc= gearman_client_echo(client, test_literal_param("This is my echo test"));
135
136 if (gearman_success(rc))
137 {
138 gearman_client_free(client);
139 return true;
140 }
141 }
142
143 gearman_client_free(client);
144
145 return false;;
146 }
147
148 const char *name()
149 {
150 return "gearmand";
151 };
152
153 const char *executable()
154 {
155 return GEARMAND_BINARY;
156 }
157
158 const char *pid_file_option()
159 {
160 return "--pid-file=";
161 }
162
163 const char *daemon_file_option()
164 {
165 return "--daemon";
166 }
167
168 const char *log_file_option()
169 {
170 return "-vvvvv --log-file=";
171 }
172
173 const char *port_option()
174 {
175 return "--port=";
176 }
177
178 bool is_libtool()
179 {
180 return true;
181 }
182
183 bool build(int argc, const char *argv[]);
184 };
185
186
187 #include <sstream>
188
189 bool Gearmand::build(int argc, const char *argv[])
190 {
191 std::stringstream arg_buffer;
192
193 if (getuid() == 0 or geteuid() == 0)
194 {
195 arg_buffer << " -u root ";
196 }
197
198 arg_buffer << " --listen=127.0.0.1 ";
199
200 for (int x= 1 ; x < argc ; x++)
201 {
202 arg_buffer << " " << argv[x] << " ";
203 }
204
205 set_extra_args(arg_buffer.str());
206
207 return true;
208 }
209
210 namespace libtest {
211
212 libtest::Server *build_gearmand(const char *hostname, in_port_t try_port)
213 {
214 return new Gearmand(hostname, try_port);
215 }
216
217 }