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