Merge of cppccheck fixes.
[m6w6/libmemcached] / libtest / main.cc
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 #include <config.h>
38 #include <libtest/common.h>
39
40 #include <cassert>
41 #include <cstdlib>
42 #include <cstring>
43 #include <ctime>
44 #include <fnmatch.h>
45 #include <iostream>
46 #include <memory>
47 #include <sys/stat.h>
48 #include <sys/time.h>
49 #include <sys/types.h>
50 #include <sys/wait.h>
51 #include <unistd.h>
52
53 #include <signal.h>
54
55 #ifndef __INTEL_COMPILER
56 #pragma GCC diagnostic ignored "-Wold-style-cast"
57 #endif
58
59 using namespace libtest;
60
61 static void stats_print(Framework *frame)
62 {
63 if (frame->failed() == 0 and frame->success() == 0)
64 {
65 return;
66 }
67
68 Outn();
69 Out << "Collections\t\t\t\t\t" << frame->total();
70 Out << "\tFailed\t\t\t\t\t" << frame->failed();
71 Out << "\tSkipped\t\t\t\t\t" << frame->skipped();
72 Out << "\tSucceeded\t\t\t\t" << frame->success();
73 Outn();
74 Out << "Tests\t\t\t\t\t" << frame->sum_total();
75 Out << "\tFailed\t\t\t\t" << frame->sum_failed();
76 Out << "\tSkipped\t\t\t\t" << frame->sum_skipped();
77 Out << "\tSucceeded\t\t\t" << frame->sum_success();
78 }
79
80 #include <getopt.h>
81 #include <unistd.h>
82
83 int main(int argc, char *argv[])
84 {
85 bool opt_massive= false;
86 unsigned long int opt_repeat= 1; // Run all tests once
87 bool opt_quiet= false;
88 std::string collection_to_run;
89 std::string wildcard;
90
91 /*
92 Valgrind does not currently work reliably, or sometimes at all, on OSX
93 - Fri Jun 15 11:24:07 EDT 2012
94 */
95 #if defined(TARGET_OS_OSX) && TARGET_OS_OSX
96 if (valgrind_is_caller())
97 {
98 return EXIT_SKIP;
99 }
100 #endif
101
102 // Options parsing
103 {
104 enum long_option_t {
105 OPT_LIBYATL_VERSION,
106 OPT_LIBYATL_MATCH_COLLECTION,
107 OPT_LIBYATL_MASSIVE,
108 OPT_LIBYATL_QUIET,
109 OPT_LIBYATL_MATCH_WILDCARD,
110 OPT_LIBYATL_REPEAT
111 };
112
113 static struct option long_options[]=
114 {
115 { "version", no_argument, NULL, OPT_LIBYATL_VERSION },
116 { "quiet", no_argument, NULL, OPT_LIBYATL_QUIET },
117 { "repeat", no_argument, NULL, OPT_LIBYATL_REPEAT },
118 { "collection", required_argument, NULL, OPT_LIBYATL_MATCH_COLLECTION },
119 { "wildcard", required_argument, NULL, OPT_LIBYATL_MATCH_WILDCARD },
120 { "massive", no_argument, NULL, OPT_LIBYATL_MASSIVE },
121 { 0, 0, 0, 0 }
122 };
123
124 int option_index= 0;
125 while (1)
126 {
127 int option_rv= getopt_long(argc, argv, "", long_options, &option_index);
128 if (option_rv == -1)
129 {
130 break;
131 }
132
133 switch (option_rv)
134 {
135 case OPT_LIBYATL_VERSION:
136 break;
137
138 case OPT_LIBYATL_QUIET:
139 opt_quiet= true;
140 break;
141
142 case OPT_LIBYATL_REPEAT:
143 opt_repeat= strtoul(optarg, (char **) NULL, 10);
144 break;
145
146 case OPT_LIBYATL_MATCH_COLLECTION:
147 collection_to_run= optarg;
148 break;
149
150 case OPT_LIBYATL_MATCH_WILDCARD:
151 wildcard= optarg;
152 break;
153
154 case OPT_LIBYATL_MASSIVE:
155 opt_massive= true;
156 break;
157
158 case '?':
159 /* getopt_long already printed an error message. */
160 Error << "unknown option to getopt_long()";
161 exit(EXIT_FAILURE);
162
163 default:
164 break;
165 }
166 }
167 }
168
169 srandom((unsigned int)time(NULL));
170
171 if (bool(getenv("YATL_REPEAT")) and (strtoul(getenv("YATL_REPEAT"), (char **) NULL, 10) > 1))
172 {
173 opt_repeat= strtoul(getenv("YATL_REPEAT"), (char **) NULL, 10);
174 }
175
176 if ((bool(getenv("YATL_QUIET")) and (strcmp(getenv("YATL_QUIET"), "0") == 0)) or opt_quiet)
177 {
178 opt_quiet= true;
179 }
180 else if (getenv("JENKINS_URL"))
181 {
182 if (bool(getenv("YATL_QUIET")) and (strcmp(getenv("YATL_QUIET"), "1") == 0))
183 { }
184 else
185 {
186 opt_quiet= true;
187 }
188 }
189
190 if ((bool(getenv("YATL_RUN_MASSIVE_TESTS"))) or opt_massive)
191 {
192 opt_massive= true;
193 }
194
195 if (opt_quiet)
196 {
197 close(STDOUT_FILENO);
198 }
199
200 if (opt_massive)
201 {
202 is_massive(opt_massive);
203 }
204
205 char buffer[1024];
206 if (getenv("LIBTEST_TMP"))
207 {
208 snprintf(buffer, sizeof(buffer), "%s", getenv("LIBTEST_TMP"));
209 }
210 else
211 {
212 snprintf(buffer, sizeof(buffer), "%s", LIBTEST_TEMP);
213 }
214
215 if (chdir(buffer) == -1)
216 {
217 char getcwd_buffer[1024];
218 char *dir= getcwd(getcwd_buffer, sizeof(getcwd_buffer));
219
220 Error << "Unable to chdir() from " << dir << " to " << buffer << " errno:" << strerror(errno);
221 return EXIT_FAILURE;
222 }
223
224 if (libtest::libtool() == NULL)
225 {
226 Error << "Failed to locate libtool";
227 return EXIT_FAILURE;
228 }
229
230 if (getenv("YATL_COLLECTION_TO_RUN"))
231 {
232 if (strlen(getenv("YATL_COLLECTION_TO_RUN")))
233 {
234 collection_to_run= getenv("YATL_COLLECTION_TO_RUN");
235 }
236 }
237
238 if (collection_to_run.compare("none") == 0)
239 {
240 return EXIT_SUCCESS;
241 }
242
243 if (collection_to_run.empty() == false)
244 {
245 Out << "Only testing " << collection_to_run;
246 }
247
248 int exit_code;
249
250 try
251 {
252 do
253 {
254 exit_code= EXIT_SUCCESS;
255 fatal_assert(sigignore(SIGPIPE) == 0);
256
257 libtest::SignalThread signal;
258 if (signal.setup() == false)
259 {
260 Error << "Failed to setup signals";
261 return EXIT_FAILURE;
262 }
263
264 std::auto_ptr<Framework> frame(new Framework(signal, collection_to_run, wildcard));
265
266 // Run create(), bail on error.
267 {
268 switch (frame->create())
269 {
270 case TEST_SUCCESS:
271 break;
272
273 case TEST_SKIPPED:
274 return EXIT_SKIP;
275
276 case TEST_FAILURE:
277 std::cerr << "frame->create()" << std::endl;
278 return EXIT_FAILURE;
279 }
280 }
281
282 frame->exec();
283
284 if (signal.is_shutdown() == false)
285 {
286 signal.set_shutdown(SHUTDOWN_GRACEFUL);
287 }
288
289 shutdown_t status= signal.get_shutdown();
290 if (status == SHUTDOWN_FORCED)
291 {
292 Out << "Tests were aborted.";
293 exit_code= EXIT_FAILURE;
294 }
295 else if (frame->failed())
296 {
297 Out << "Some test failed.";
298 exit_code= EXIT_FAILURE;
299 }
300 else if (frame->skipped() and frame->failed() and frame->success())
301 {
302 Out << "Some tests were skipped.";
303 }
304 else if (frame->success() and (frame->failed() == 0))
305 {
306 Out << "All tests completed successfully.";
307 }
308
309 stats_print(frame.get());
310
311 Outn(); // Generate a blank to break up the messages if make check/test has been run
312 } while (exit_code == EXIT_SUCCESS and --opt_repeat);
313 }
314 catch (libtest::fatal& e)
315 {
316 std::cerr << "FATAL:" << e.what() << std::endl;
317 exit_code= EXIT_FAILURE;
318 }
319 catch (libtest::disconnected& e)
320 {
321 std::cerr << "Unhandled disconnection occurred:" << e.what() << std::endl;
322 exit_code= EXIT_FAILURE;
323 }
324 catch (std::exception& e)
325 {
326 std::cerr << "std::exception:" << e.what() << std::endl;
327 exit_code= EXIT_FAILURE;
328 }
329 catch (char const*)
330 {
331 std::cerr << "Exception:" << std::endl;
332 exit_code= EXIT_FAILURE;
333 }
334 catch (...)
335 {
336 std::cerr << "Unknown exception halted execution." << std::endl;
337 exit_code= EXIT_FAILURE;
338 }
339
340 return exit_code;
341 }