Merge in merge of 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 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions are
9 * met:
10 *
11 * * Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 *
14 * * Redistributions in binary form must reproduce the above
15 * copyright notice, this list of conditions and the following disclaimer
16 * in the documentation and/or other materials provided with the
17 * distribution.
18 *
19 * * The names of its contributors may not be used to endorse or
20 * promote products derived from this software without specific prior
21 * written permission.
22 *
23 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
24 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
25 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
26 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
27 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
28 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
29 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
30 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
31 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
32 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
33 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
34 *
35 */
36
37 #include <libtest/common.h>
38 #include <libtest/gearmand.h>
39
40 #include "util/instance.hpp"
41 #include "util/operation.hpp"
42
43 using namespace datadifferential;
44 using namespace libtest;
45
46 #include <cassert>
47 #include <cerrno>
48 #include <cstdio>
49 #include <cstdlib>
50 #include <cstring>
51 #include <iostream>
52 #include <signal.h>
53 #include <sys/types.h>
54 #include <sys/wait.h>
55 #include <unistd.h>
56
57 #include <libgearman/gearman.h>
58
59 #ifndef __INTEL_COMPILER
60 #pragma GCC diagnostic ignored "-Wold-style-cast"
61 #endif
62
63 class GetPid : public util::Instance::Finish
64 {
65 private:
66 pid_t _pid;
67
68 public:
69 GetPid() :
70 _pid(-1)
71 { }
72
73 pid_t pid()
74 {
75 return _pid;
76 }
77
78
79 bool call(const bool success, const std::string &response)
80 {
81 _pid= -1;
82
83 if (success and response.size())
84 {
85 _pid= atoi(response.c_str());
86 }
87
88 if (_pid < 1)
89 {
90 _pid= -1;
91 return false;
92 }
93
94 return true;
95 }
96 };
97
98 using namespace libtest;
99
100 class Gearmand : public Server
101 {
102 private:
103 public:
104 Gearmand(const std::string& host_arg, in_port_t port_arg) :
105 Server(host_arg, port_arg)
106 { }
107
108 pid_t get_pid(bool error_is_ok)
109 {
110 if (not pid_file().empty())
111 {
112 Wait wait(pid_file(), 0);
113
114 if (error_is_ok and not wait.successful())
115 {
116 Error << "Pidfile was not found:" << pid_file();
117 return -1;
118 }
119 }
120
121 GetPid *get_instance_pid;
122 util::Instance instance(hostname(), port());
123 instance.set_finish(get_instance_pid= new GetPid);
124
125 instance.push(new util::Operation(test_literal_param("getpid\r\n"), true));
126
127 if (error_is_ok and instance.run() == false)
128 {
129 Error << "Failed to obtain pid of server";
130 }
131
132 return get_instance_pid->pid();
133 }
134
135 bool ping()
136 {
137 gearman_client_st *client= gearman_client_create(NULL);
138 if (not client)
139 {
140 Error << "Could not allocate memory for gearman_client_create()";
141 return false;
142 }
143 gearman_client_set_timeout(client, 1000);
144
145 if (gearman_success(gearman_client_add_server(client, hostname().c_str(), port())))
146 {
147 gearman_return_t rc= gearman_client_echo(client, test_literal_param("This is my echo test"));
148
149 if (gearman_success(rc))
150 {
151 gearman_client_free(client);
152 return true;
153 }
154 }
155
156 gearman_client_free(client);
157
158 return false;;
159 }
160
161 const char *name()
162 {
163 return "gearmand";
164 };
165
166 const char *executable()
167 {
168 return GEARMAND_BINARY;
169 }
170
171 const char *pid_file_option()
172 {
173 return "--pid-file=";
174 }
175
176 const char *daemon_file_option()
177 {
178 return "--daemon";
179 }
180
181 const char *log_file_option()
182 {
183 return "-vvvvv --log-file=";
184 }
185
186 const char *port_option()
187 {
188 return "--port=";
189 }
190
191 bool is_libtool()
192 {
193 return true;
194 }
195
196 bool build(int argc, const char *argv[]);
197 };
198
199
200 #include <sstream>
201
202 bool Gearmand::build(int argc, const char *argv[])
203 {
204 std::stringstream arg_buffer;
205
206 if (getuid() == 0 or geteuid() == 0)
207 {
208 arg_buffer << " -u root ";
209 }
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 Server *build_gearmand(const char *hostname, in_port_t try_port)
224 {
225 return new Gearmand(hostname, try_port);
226 }
227
228 }