Merge lp:~tangent-org/libmemcached/1.0-build/ Build: jenkins-Libmemcached-187
[m6w6/libmemcached] / libtest / cmdline.h
1 /* vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
2 *
3 * Data Differential YATL (i.e. libtest) library
4 *
5 * Copyright (C) 2012 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 #pragma once
38
39 #include <spawn.h>
40
41 // http://www.gnu.org/software/automake/manual/automake.html#Using-the-TAP-test-protocol
42 #ifndef EXIT_SKIP
43 # define EXIT_SKIP 77
44 #endif
45
46 #ifndef EXIT_FATAL
47 # define EXIT_FATAL 99
48 #endif
49
50 #ifndef EX_NOEXEC
51 # define EX_NOEXEC 126
52 #endif
53
54 #ifndef EX_NOTFOUND
55 # define EX_NOTFOUND 127
56 #endif
57
58 namespace libtest {
59
60 class Application {
61 private:
62 typedef std::vector< std::pair<std::string, std::string> > Options;
63
64 public:
65
66 enum error_t {
67 SUCCESS= EXIT_SUCCESS,
68 FAILURE= EXIT_FAILURE,
69 UNINITIALIZED,
70 SIGTERM_KILLED,
71 UNKNOWN,
72 UNKNOWN_SIGNAL,
73 INVALID_POSIX_SPAWN= 127
74 };
75
76 static const char* toString(error_t arg)
77 {
78 switch (arg)
79 {
80 case Application::SUCCESS:
81 return "EXIT_SUCCESS";
82
83 case Application::UNINITIALIZED:
84 return "UNINITIALIZED";
85
86 case Application::SIGTERM_KILLED:
87 return "Exit happened via SIGTERM";
88
89 case Application::FAILURE:
90 return "EXIT_FAILURE";
91
92 case Application::UNKNOWN_SIGNAL:
93 return "Exit happened via a signal which was not SIGTERM";
94
95 case Application::INVALID_POSIX_SPAWN:
96 return "127: Invalid call to posix_spawn()";
97
98 case Application::UNKNOWN:
99 default:
100 break;
101 }
102
103 return "EXIT_UNKNOWN";
104 }
105
106 class Pipe {
107 public:
108 Pipe(int);
109 ~Pipe();
110
111 int fd();
112
113 enum close_t {
114 READ= 0,
115 WRITE= 1
116 };
117
118 void reset();
119 void close(const close_t& arg);
120 void dup_for_spawn(posix_spawn_file_actions_t& file_actions);
121
122 void nonblock();
123 void cloexec();
124 bool read(libtest::vchar_t&);
125
126 private:
127 const int _std_fd;
128 int _pipe_fd[2];
129 bool _open[2];
130 };
131
132 public:
133 Application(const std::string& arg, const bool _use_libtool_arg= false);
134
135 virtual ~Application();
136
137 void add_option(const std::string&);
138 void add_option(const std::string&, const std::string&);
139 void add_long_option(const std::string& option_name, const std::string& option_value);
140 error_t run(const char *args[]= NULL);
141 Application::error_t join();
142
143 libtest::vchar_t stdout_result() const
144 {
145 return _stdout_buffer;
146 }
147
148 size_t stdout_result_length() const
149 {
150 return _stdout_buffer.size();
151 }
152
153 libtest::vchar_t stderr_result() const
154 {
155 return _stderr_buffer;
156 }
157
158 const char* stderr_c_str() const
159 {
160 return &_stderr_buffer[0];
161 }
162
163 size_t stderr_result_length() const
164 {
165 return _stderr_buffer.size();
166 }
167
168 std::string print();
169
170 void use_valgrind(bool arg)
171 {
172 _use_valgrind= arg;
173 }
174
175 bool check() const;
176
177 bool slurp();
178 void murder();
179
180 void use_gdb(bool arg)
181 {
182 _use_gdb= arg;
183 }
184
185 void use_ptrcheck(bool arg)
186 {
187 _use_ptrcheck= arg;
188 }
189
190 std::string arguments();
191
192 std::string gdb_filename()
193 {
194 return _gdb_filename;
195 }
196
197 pid_t pid() const
198 {
199 return _pid;
200 }
201
202 void will_fail()
203 {
204 _will_fail= true;
205 }
206
207 private:
208 void create_argv(const char *args[]);
209 void delete_argv();
210 void add_to_build_argv(const char*);
211
212 private:
213 const bool _use_libtool;
214 bool _use_valgrind;
215 bool _use_gdb;
216 bool _use_ptrcheck;
217 bool _will_fail;
218 size_t _argc;
219 std::string _exectuble_name;
220 std::string _exectuble;
221 std::string _exectuble_with_path;
222 std::string _gdb_filename;
223 Options _options;
224 Pipe stdin_fd;
225 Pipe stdout_fd;
226 Pipe stderr_fd;
227 libtest::vchar_ptr_t built_argv;
228 pid_t _pid;
229 libtest::vchar_t _stdout_buffer;
230 libtest::vchar_t _stderr_buffer;
231 int _status;
232 pthread_t _thread;
233 error_t _app_exit_state;
234 };
235
236 static inline std::ostream& operator<<(std::ostream& output, const enum Application::error_t &arg)
237 {
238 return output << Application::toString(arg);
239 }
240
241 int exec_cmdline(const std::string& executable, const char *args[], bool use_libtool= false);
242
243 const char *gearmand_binary();
244 const char *drizzled_binary();
245
246 }