c124192292c8440bc663de7fced4077d8ad0c07d
[m6w6/libmemcached] / libtest / drizzled.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 <config.h>
24 #include <libtest/common.h>
25
26 #include <libtest/drizzled.h>
27
28 #include "util/instance.hpp"
29 #include "util/operation.hpp"
30
31 using namespace datadifferential;
32 using namespace libtest;
33
34 #include <cassert>
35 #include <cerrno>
36 #include <cstdio>
37 #include <cstdlib>
38 #include <cstring>
39 #include <iostream>
40 #include <signal.h>
41 #include <sstream>
42 #include <sys/types.h>
43 #include <sys/wait.h>
44 #include <unistd.h>
45
46 #ifndef __INTEL_COMPILER
47 #pragma GCC diagnostic ignored "-Wold-style-cast"
48 #endif
49
50 #if defined(HAVE_LIBDRIZZLE) && HAVE_LIBDRIZZLE
51 #include <libdrizzle-1.0/drizzle_client.h>
52 #endif
53
54 using namespace libtest;
55
56 namespace libtest {
57 bool ping_drizzled(const in_port_t _port)
58 {
59 #if defined(HAVE_LIBDRIZZLE) && HAVE_LIBDRIZZLE
60 {
61 drizzle_st *drizzle= drizzle_create(NULL);
62
63 if (drizzle == NULL)
64 {
65 return false;
66 }
67
68 drizzle_con_st *con;
69
70 if ((con= drizzle_con_create(drizzle, NULL)) == NULL)
71 {
72 drizzle_free(drizzle);
73 return false;
74 }
75
76 drizzle_con_set_tcp(con, "localhost", _port);
77 drizzle_con_set_auth(con, "root", 0);
78
79 bool success= false;
80
81 drizzle_return_t rc;
82 if ((rc= drizzle_con_connect(con)) == DRIZZLE_RETURN_OK)
83 {
84 drizzle_result_st *result= drizzle_ping(con, NULL, &rc);
85 success= bool(result);
86 drizzle_result_free(result);
87 }
88
89 if (success == true)
90 { }
91 else if (rc != DRIZZLE_RETURN_OK)
92 {
93 Error << drizzle_error(drizzle) << " localhost:" << _port;
94 }
95
96 drizzle_con_free(con);
97 drizzle_free(drizzle);
98
99 return success;
100 }
101 #endif
102
103 return false;
104 }
105 } // namespace libtest
106
107 class Drizzle : public libtest::Server
108 {
109 private:
110 public:
111 Drizzle(const std::string& host_arg, in_port_t port_arg) :
112 libtest::Server(host_arg, port_arg, DRIZZLED_BINARY, false)
113 {
114 set_pid_file();
115 }
116
117 bool ping()
118 {
119 size_t limit= 5;
120 while (_app.check() and --limit)
121 {
122 if (ping_drizzled(_port))
123 {
124 return true;
125 }
126 libtest::dream(1, 0);
127 }
128
129 return false;
130 }
131
132 const char *name()
133 {
134 return "drizzled";
135 };
136
137 void log_file_option(Application& app, const std::string& arg)
138 {
139 }
140
141 bool has_log_file_option() const
142 {
143 return true;
144 }
145
146 bool broken_pid_file()
147 {
148 return true;
149 }
150
151 bool is_libtool()
152 {
153 return false;
154 }
155
156 bool has_syslog() const
157 {
158 return true;
159 }
160
161 bool has_port_option() const
162 {
163 return true;
164 }
165
166 void port_option(Application& app, in_port_t arg)
167 {
168 if (arg > 0)
169 {
170 char buffer[1024];
171 snprintf(buffer, sizeof(buffer), "--drizzle-protocol.port=%d", int(arg));
172 app.add_option(buffer);
173 }
174 }
175
176 bool build(size_t argc, const char *argv[]);
177 };
178
179 bool Drizzle::build(size_t argc, const char *argv[])
180 {
181 if (getuid() == 0 or geteuid() == 0)
182 {
183 add_option("--user=root");
184 }
185
186 add_option("--verbose=INSPECT");
187 #if 0
188 add_option("--datadir=var/drizzle");
189 #endif
190
191 for (size_t x= 0 ; x < argc ; x++)
192 {
193 add_option(argv[x]);
194 }
195
196 return true;
197 }
198
199 namespace libtest {
200
201 libtest::Server *build_drizzled(const char *hostname, in_port_t try_port)
202 {
203 return new Drizzle(hostname, try_port);
204 }
205
206 }