Updating for 1.0.2 release
[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 <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 (not pid_file().empty())
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 (not client)
128 {
129 Error << "Could not allocate memory for gearman_client_create()";
130 return false;
131 }
132 gearman_client_set_timeout(client, 1000);
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 }
144
145 gearman_client_free(client);
146
147 return false;;
148 }
149
150 const char *name()
151 {
152 return "gearmand";
153 };
154
155 const char *executable()
156 {
157 return GEARMAND_BINARY;
158 }
159
160 const char *pid_file_option()
161 {
162 return "--pid-file=";
163 }
164
165 const char *daemon_file_option()
166 {
167 return "--daemon";
168 }
169
170 const char *log_file_option()
171 {
172 return "-vvvvv --log-file=";
173 }
174
175 const char *port_option()
176 {
177 return "--port=";
178 }
179
180 bool is_libtool()
181 {
182 return true;
183 }
184
185 bool build(int argc, const char *argv[]);
186 };
187
188 bool Gearmand::build(int argc, const char *argv[])
189 {
190 std::stringstream arg_buffer;
191
192 if (getuid() == 0 or geteuid() == 0)
193 {
194 arg_buffer << " -u root ";
195 }
196
197 arg_buffer << " --listen=127.0.0.1 ";
198
199 for (int x= 1 ; x < argc ; x++)
200 {
201 arg_buffer << " " << argv[x] << " ";
202 }
203
204 set_extra_args(arg_buffer.str());
205
206 return true;
207 }
208
209 namespace libtest {
210
211 libtest::Server *build_gearmand(const char *hostname, in_port_t try_port)
212 {
213 return new Gearmand(hostname, try_port);
214 }
215
216 }