Refactor out item specific bits of the testing framework.
[m6w6/libmemcached] / libtest / main.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 #include <config.h>
23 #include <libtest/common.h>
24
25 #include <cassert>
26 #include <cstdlib>
27 #include <cstring>
28 #include <sys/time.h>
29 #include <sys/types.h>
30 #include <sys/stat.h>
31 #include <sys/wait.h>
32 #include <unistd.h>
33 #include <ctime>
34 #include <fnmatch.h>
35 #include <iostream>
36
37 #include <signal.h>
38
39 #ifndef __INTEL_COMPILER
40 #pragma GCC diagnostic ignored "-Wold-style-cast"
41 #endif
42
43 using namespace libtest;
44
45 static void stats_print(Stats *stats)
46 {
47 if (stats->collection_failed == 0 and stats->collection_success == 0)
48 {
49 return;
50 }
51
52 Out << "\tTotal Collections\t\t\t\t" << stats->collection_total;
53 Out << "\tFailed Collections\t\t\t\t" << stats->collection_failed;
54 Out << "\tSkipped Collections\t\t\t\t" << stats->collection_skipped;
55 Out << "\tSucceeded Collections\t\t\t\t" << stats->collection_success;
56 Outn();
57 Out << "Total\t\t\t\t" << stats->total;
58 Out << "\tFailed\t\t\t" << stats->failed;
59 Out << "\tSkipped\t\t\t" << stats->skipped;
60 Out << "\tSucceeded\t\t" << stats->success;
61 }
62
63 static long int timedif(struct timeval a, struct timeval b)
64 {
65 long us, s;
66
67 us = (long)(a.tv_usec - b.tv_usec);
68 us /= 1000;
69 s = (long)(a.tv_sec - b.tv_sec);
70 s *= 1000;
71 return s + us;
72 }
73
74 #include <getopt.h>
75 #include <unistd.h>
76
77 int main(int argc, char *argv[])
78 {
79 bool opt_massive= false;
80 unsigned long int opt_repeat= 1; // Run all tests once
81 bool opt_quiet= false;
82 std::string collection_to_run;
83
84 // Options parsing
85 {
86 enum long_option_t {
87 OPT_LIBYATL_VERSION,
88 OPT_LIBYATL_MATCH_COLLECTION,
89 OPT_LIBYATL_MASSIVE,
90 OPT_LIBYATL_QUIET,
91 OPT_LIBYATL_REPEAT
92 };
93
94 static struct option long_options[]=
95 {
96 { "version", no_argument, NULL, OPT_LIBYATL_VERSION },
97 { "quiet", no_argument, NULL, OPT_LIBYATL_QUIET },
98 { "repeat", no_argument, NULL, OPT_LIBYATL_REPEAT },
99 { "collection", required_argument, NULL, OPT_LIBYATL_MATCH_COLLECTION },
100 { "massive", no_argument, NULL, OPT_LIBYATL_MASSIVE },
101 { 0, 0, 0, 0 }
102 };
103
104 int option_index= 0;
105 while (1)
106 {
107 int option_rv= getopt_long(argc, argv, "", long_options, &option_index);
108 if (option_rv == -1)
109 {
110 break;
111 }
112
113 switch (option_rv)
114 {
115 case OPT_LIBYATL_VERSION:
116 break;
117
118 case OPT_LIBYATL_QUIET:
119 opt_quiet= true;
120 break;
121
122 case OPT_LIBYATL_REPEAT:
123 opt_repeat= strtoul(optarg, (char **) NULL, 10);
124 break;
125
126 case OPT_LIBYATL_MATCH_COLLECTION:
127 collection_to_run= optarg;
128 break;
129
130 case OPT_LIBYATL_MASSIVE:
131 opt_massive= true;
132 break;
133
134 case '?':
135 /* getopt_long already printed an error message. */
136 Error << "unknown option to getopt_long()";
137 exit(EXIT_FAILURE);
138
139 default:
140 break;
141 }
142 }
143 }
144
145 srandom((unsigned int)time(NULL));
146
147 if (bool(getenv("YATL_REPEAT")) and (strtoul(getenv("YATL_REPEAT"), (char **) NULL, 10) > 1))
148 {
149 opt_repeat= strtoul(getenv("YATL_REPEAT"), (char **) NULL, 10);
150 }
151
152 if ((bool(getenv("YATL_QUIET")) and (strcmp(getenv("YATL_QUIET"), "0") == 0)) or opt_quiet)
153 {
154 opt_quiet= true;
155 }
156 else if (getenv("JENKINS_URL"))
157 {
158 if (bool(getenv("YATL_QUIET")) and (strcmp(getenv("YATL_QUIET"), "1") == 0))
159 { }
160 else
161 {
162 opt_quiet= true;
163 }
164 }
165
166 if (opt_quiet)
167 {
168 close(STDOUT_FILENO);
169 }
170
171 char buffer[1024];
172 if (getenv("LIBTEST_TMP"))
173 {
174 snprintf(buffer, sizeof(buffer), "%s", getenv("LIBTEST_TMP"));
175 }
176 else
177 {
178 snprintf(buffer, sizeof(buffer), "%s", LIBTEST_TEMP);
179 }
180
181 if (chdir(buffer) == -1)
182 {
183 char getcwd_buffer[1024];
184 char *dir= getcwd(getcwd_buffer, sizeof(getcwd_buffer));
185
186 Error << "Unable to chdir() from " << dir << " to " << buffer << " errno:" << strerror(errno);
187 return EXIT_FAILURE;
188 }
189
190 if (libtest::libtool() == NULL)
191 {
192 Error << "Failed to locate libtool";
193 return EXIT_FAILURE;
194 }
195
196 int exit_code;
197
198 try {
199 do {
200 exit_code= EXIT_SUCCESS;
201 Framework world;
202
203 fatal_assert(sigignore(SIGPIPE) == 0);
204
205 libtest::SignalThread signal;
206 if (signal.setup() == false)
207 {
208 Error << "Failed to setup signals";
209 return EXIT_FAILURE;
210 }
211
212 Stats stats;
213
214 get_world(&world);
215
216 test_return_t error;
217 void *creators_ptr= world.create(error);
218
219 switch (error)
220 {
221 case TEST_SUCCESS:
222 break;
223
224 case TEST_SKIPPED:
225 Out << "SKIP " << argv[0];
226 return EXIT_SUCCESS;
227
228 case TEST_FAILURE:
229 return EXIT_FAILURE;
230 }
231
232 if (getenv("YATL_COLLECTION_TO_RUN"))
233 {
234 if (strlen(getenv("YATL_COLLECTION_TO_RUN")))
235 {
236 collection_to_run= getenv("YATL_COLLECTION_TO_RUN");
237 }
238 }
239
240 if (collection_to_run.compare("none") == 0)
241 {
242 return EXIT_SUCCESS;
243 }
244
245 if (collection_to_run.empty() == false)
246 {
247 Out << "Only testing " << collection_to_run;
248 }
249
250 char *wildcard= NULL;
251 if (argc == 3)
252 {
253 wildcard= argv[2];
254 }
255
256 for (collection_st *next= world.collections; next and next->name and (not signal.is_shutdown()); next++)
257 {
258 bool failed= false;
259 bool skipped= false;
260
261 if (collection_to_run.empty() == false and fnmatch(collection_to_run.c_str(), next->name, 0))
262 {
263 continue;
264 }
265
266 stats.collection_total++;
267
268 test_return_t collection_rc= world.startup(creators_ptr);
269
270 if (collection_rc == TEST_SUCCESS and next->pre)
271 {
272 collection_rc= world.runner()->pre(next->pre, creators_ptr);
273 }
274
275 switch (collection_rc)
276 {
277 case TEST_SUCCESS:
278 break;
279
280 case TEST_FAILURE:
281 Out << next->name << " [ failed ]";
282 failed= true;
283 signal.set_shutdown(SHUTDOWN_GRACEFUL);
284 goto cleanup;
285
286 case TEST_SKIPPED:
287 Out << next->name << " [ skipping ]";
288 skipped= true;
289 goto cleanup;
290
291 default:
292 fatal_message("invalid return code");
293 }
294
295 Out << "Collection: " << next->name;
296
297 for (test_st *run= next->tests; run->name; run++)
298 {
299 struct timeval start_time, end_time;
300 long int load_time= 0;
301
302 if (wildcard && fnmatch(wildcard, run->name, 0))
303 {
304 continue;
305 }
306
307 test_return_t return_code;
308 try {
309 if (test_success(return_code= world.item.startup(creators_ptr)))
310 {
311 if (test_success(return_code= world.item.flush(creators_ptr, run)))
312 {
313 { // Runner Code
314 gettimeofday(&start_time, NULL);
315 assert(world.runner());
316 assert(run->test_fn);
317 try
318 {
319 return_code= world.runner()->run(run->test_fn, creators_ptr);
320 }
321 // Special case where check for the testing of the exception
322 // system.
323 catch (libtest::fatal &e)
324 {
325 if (fatal::is_disabled())
326 {
327 fatal::increment_disabled_counter();
328 return_code= TEST_SUCCESS;
329 }
330 else
331 {
332 throw;
333 }
334 }
335
336 gettimeofday(&end_time, NULL);
337 load_time= timedif(end_time, start_time);
338 }
339 }
340 else if (return_code == TEST_SKIPPED)
341 { }
342 else if (return_code == TEST_FAILURE)
343 {
344 Error << " item.flush(failure)";
345 signal.set_shutdown(SHUTDOWN_GRACEFUL);
346 }
347 }
348 else if (return_code == TEST_SKIPPED)
349 { }
350 else if (return_code == TEST_FAILURE)
351 {
352 Error << " item.startup(failure)";
353 signal.set_shutdown(SHUTDOWN_GRACEFUL);
354 }
355 }
356
357 catch (libtest::fatal &e)
358 {
359 Error << "Fatal exception was thrown: " << e.what();
360 return_code= TEST_FAILURE;
361 }
362 catch (std::exception &e)
363 {
364 Error << "Exception was thrown: " << e.what();
365 return_code= TEST_FAILURE;
366 }
367 catch (...)
368 {
369 Error << "Unknown exception occurred";
370 return_code= TEST_FAILURE;
371 }
372
373 stats.total++;
374
375 switch (return_code)
376 {
377 case TEST_SUCCESS:
378 Out << "\tTesting " << run->name << "\t\t\t\t\t" << load_time / 1000 << "." << load_time % 1000 << "[ " << test_strerror(return_code) << " ]";
379 stats.success++;
380 break;
381
382 case TEST_FAILURE:
383 stats.failed++;
384 failed= true;
385 Out << "\tTesting " << run->name << "\t\t\t\t\t" << "[ " << test_strerror(return_code) << " ]";
386 break;
387
388 case TEST_SKIPPED:
389 stats.skipped++;
390 skipped= true;
391 Out << "\tTesting " << run->name << "\t\t\t\t\t" << "[ " << test_strerror(return_code) << " ]";
392 break;
393
394 default:
395 fatal_message("invalid return code");
396 }
397
398 if (test_failed(world.on_error(return_code, creators_ptr)))
399 {
400 Error << "Failed while running on_error()";
401 signal.set_shutdown(SHUTDOWN_GRACEFUL);
402 break;
403 }
404 }
405
406 (void) world.runner()->post(next->post, creators_ptr);
407
408 cleanup:
409 if (failed == false and skipped == false)
410 {
411 stats.collection_success++;
412 }
413
414 if (failed)
415 {
416 stats.collection_failed++;
417 }
418
419 if (skipped)
420 {
421 stats.collection_skipped++;
422 }
423
424 world.shutdown(creators_ptr);
425 Outn();
426 }
427
428 if (not signal.is_shutdown())
429 {
430 signal.set_shutdown(SHUTDOWN_GRACEFUL);
431 }
432
433 shutdown_t status= signal.get_shutdown();
434 if (status == SHUTDOWN_FORCED)
435 {
436 Out << "Tests were aborted.";
437 exit_code= EXIT_FAILURE;
438 }
439 else if (stats.collection_failed)
440 {
441 Out << "Some test failed.";
442 exit_code= EXIT_FAILURE;
443 }
444 else if (stats.collection_skipped and stats.collection_failed and stats.collection_success)
445 {
446 Out << "Some tests were skipped.";
447 }
448 else if (stats.collection_success and stats.collection_failed == 0)
449 {
450 Out << "All tests completed successfully.";
451 }
452
453 stats_print(&stats);
454
455 Outn(); // Generate a blank to break up the messages if make check/test has been run
456 } while (exit_code == EXIT_SUCCESS and --opt_repeat);
457 }
458 catch (libtest::fatal& e)
459 {
460 std::cerr << e.what() << std::endl;
461 }
462 catch (libtest::disconnected& e)
463 {
464 std::cerr << "Unhandled disconnection occurred:" << e.what() << std::endl;
465 }
466 catch (std::exception& e)
467 {
468 std::cerr << e.what() << std::endl;
469 }
470 catch (...)
471 {
472 std::cerr << "Unknown exception halted execution." << std::endl;
473 }
474
475 return exit_code;
476 }