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