1 /* vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
5 * Copyright (C) 2011 Data Differential, http://datadifferential.com/
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.
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.
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
22 #include <libtest/common.h>
28 #include <sys/types.h>
38 #if defined(HAVE_CURL_CURL_H) && HAVE_CURL_CURL_H
39 #include <curl/curl.h>
42 #ifndef __INTEL_COMPILER
43 #pragma GCC diagnostic ignored "-Wold-style-cast"
46 using namespace libtest
;
48 static void stats_print(Stats
*stats
)
50 if (stats
->collection_failed
== 0 and stats
->collection_success
== 0)
55 Out
<< "\tTotal Collections\t\t\t\t" << stats
->collection_total
;
56 Out
<< "\tFailed Collections\t\t\t\t" << stats
->collection_failed
;
57 Out
<< "\tSkipped Collections\t\t\t\t" << stats
->collection_skipped
;
58 Out
<< "\tSucceeded Collections\t\t\t\t" << stats
->collection_success
;
60 Out
<< "Total\t\t\t\t" << stats
->total
;
61 Out
<< "\tFailed\t\t\t" << stats
->failed
;
62 Out
<< "\tSkipped\t\t\t" << stats
->skipped
;
63 Out
<< "\tSucceeded\t\t" << stats
->success
;
66 static long int timedif(struct timeval a
, struct timeval b
)
70 us
= (long)(a
.tv_usec
- b
.tv_usec
);
72 s
= (long)(a
.tv_sec
- b
.tv_sec
);
77 static void cleanup_curl(void)
79 #if defined(HAVE_CURL_CURL_H) && HAVE_CURL_CURL_H
80 curl_global_cleanup();
87 int main(int argc
, char *argv
[])
89 #if defined(HAVE_CURL_CURL_H) && HAVE_CURL_CURL_H
90 if (curl_global_init(CURL_GLOBAL_ALL
))
92 Error
<< "curl_global_init(CURL_GLOBAL_ALL) failed";
97 if (atexit(cleanup_curl
))
99 Error
<< "atexit() failed";
103 bool opt_repeat
= false;
104 std::string collection_to_run
;
110 OPT_LIBYATL_MATCH_COLLECTION
,
114 static struct option long_options
[]=
116 {"repeat", no_argument
, NULL
, OPT_LIBYATL_REPEAT
},
117 {"collection", required_argument
, NULL
, OPT_LIBYATL_MATCH_COLLECTION
},
124 int option_rv
= getopt_long(argc
, argv
, "", long_options
, &option_index
);
132 case OPT_LIBYATL_VERSION
:
135 case OPT_LIBYATL_REPEAT
:
139 case OPT_LIBYATL_MATCH_COLLECTION
:
140 collection_to_run
= optarg
;
144 /* getopt_long already printed an error message. */
145 Error
<< "unknown option to getopt_long()";
154 srandom((unsigned int)time(NULL
));
156 if (getenv("LIBTEST_QUIET") and strcmp(getenv("LIBTEST_QUIET"), "0") == 0)
158 close(STDOUT_FILENO
);
160 else if (getenv("JENKINS_URL"))
162 close(STDOUT_FILENO
);
166 if (getenv("LIBTEST_TMP"))
168 snprintf(buffer
, sizeof(buffer
), "%s", getenv("LIBTEST_TMP"));
172 snprintf(buffer
, sizeof(buffer
), "%s", LIBTEST_TEMP
);
175 if (chdir(buffer
) == -1)
177 char getcwd_buffer
[1024];
178 char *dir
= getcwd(getcwd_buffer
, sizeof(getcwd_buffer
));
180 Error
<< "Unable to chdir() from " << dir
<< " to " << buffer
<< " errno:" << strerror(errno
);
184 if (libtest::libtool() == NULL
)
186 Error
<< "Failed to locate libtool";
194 exit_code
= EXIT_SUCCESS
;
195 Framework
*world
= new Framework();
199 Error
<< "Failed to create Framework()";
203 assert(sigignore(SIGPIPE
) == 0);
205 libtest::SignalThread signal
;
206 if (not signal
.setup())
208 Error
<< "Failed to setup signals";
217 void *creators_ptr
= world
->create(error
);
225 Out
<< "SKIP " << argv
[0];
234 if (getenv("TEST_COLLECTION"))
236 if (strlen(getenv("TEST_COLLECTION")))
238 collection_to_run
= getenv("TEST_COLLECTION");
242 if (collection_to_run
.empty() == false)
244 Out
<< "Only testing " << collection_to_run
;
247 char *wildcard
= NULL
;
253 for (collection_st
*next
= world
->collections
; next
and next
->name
and (not signal
.is_shutdown()); next
++)
258 if (collection_to_run
.empty() == false and fnmatch(collection_to_run
.c_str(), next
->name
, 0))
263 stats
.collection_total
++;
265 test_return_t collection_rc
= world
->startup(creators_ptr
);
267 if (collection_rc
== TEST_SUCCESS
and next
->pre
)
269 collection_rc
= world
->runner()->pre(next
->pre
, creators_ptr
);
272 switch (collection_rc
)
278 Out
<< next
->name
<< " [ failed ]";
280 signal
.set_shutdown(SHUTDOWN_GRACEFUL
);
284 Out
<< next
->name
<< " [ skipping ]";
289 throw fatal_message("invalid return code");
292 Out
<< "Collection: " << next
->name
;
294 for (test_st
*run
= next
->tests
; run
->name
; run
++)
296 struct timeval start_time
, end_time
;
297 long int load_time
= 0;
299 if (wildcard
&& fnmatch(wildcard
, run
->name
, 0))
304 test_return_t return_code
;
306 if (test_success(return_code
= world
->item
.startup(creators_ptr
)))
308 if (test_success(return_code
= world
->item
.flush(creators_ptr
, run
)))
310 // @note pre will fail is SKIPPED is returned
311 if (test_success(return_code
= world
->item
.pre(creators_ptr
)))
314 gettimeofday(&start_time
, NULL
);
315 assert(world
->runner());
316 assert(run
->test_fn
);
319 return_code
= world
->runner()->run(run
->test_fn
, creators_ptr
);
321 // Special case where check for the testing of the exception
323 catch (libtest::fatal
&e
)
325 if (fatal::is_disabled())
327 fatal::increment_disabled_counter();
328 return_code
= TEST_SUCCESS
;
336 gettimeofday(&end_time
, NULL
);
337 load_time
= timedif(end_time
, start_time
);
341 // @todo do something if post fails
342 (void)world
->item
.post(creators_ptr
);
344 else if (return_code
== TEST_SKIPPED
)
346 else if (return_code
== TEST_FAILURE
)
348 Error
<< " item.flush(failure)";
349 signal
.set_shutdown(SHUTDOWN_GRACEFUL
);
352 else if (return_code
== TEST_SKIPPED
)
354 else if (return_code
== TEST_FAILURE
)
356 Error
<< " item.startup(failure)";
357 signal
.set_shutdown(SHUTDOWN_GRACEFUL
);
361 catch (std::exception
&e
)
363 Error
<< "Exception was thrown: " << e
.what();
364 return_code
= TEST_FAILURE
;
368 Error
<< "Unknown exception occurred";
369 return_code
= TEST_FAILURE
;
377 Out
<< "\tTesting " << run
->name
<< "\t\t\t\t\t" << load_time
/ 1000 << "." << load_time
% 1000 << "[ " << test_strerror(return_code
) << " ]";
384 Out
<< "\tTesting " << run
->name
<< "\t\t\t\t\t" << "[ " << test_strerror(return_code
) << " ]";
390 Out
<< "\tTesting " << run
->name
<< "\t\t\t\t\t" << "[ " << test_strerror(return_code
) << " ]";
394 throw fatal_message("invalid return code");
397 if (test_failed(world
->on_error(return_code
, creators_ptr
)))
399 Error
<< "Failed while running on_error()";
400 signal
.set_shutdown(SHUTDOWN_GRACEFUL
);
405 (void) world
->runner()->post(next
->post
, creators_ptr
);
408 if (failed
== false and skipped
== false)
410 stats
.collection_success
++;
415 stats
.collection_failed
++;
420 stats
.collection_skipped
++;
423 world
->shutdown(creators_ptr
);
427 if (not signal
.is_shutdown())
429 signal
.set_shutdown(SHUTDOWN_GRACEFUL
);
432 shutdown_t status
= signal
.get_shutdown();
433 if (status
== SHUTDOWN_FORCED
)
435 Out
<< "Tests were aborted.";
436 exit_code
= EXIT_FAILURE
;
438 else if (stats
.collection_failed
)
440 Out
<< "Some test failed.";
441 exit_code
= EXIT_FAILURE
;
443 else if (stats
.collection_skipped
and stats
.collection_failed
and stats
.collection_success
)
445 Out
<< "Some tests were skipped.";
447 else if (stats
.collection_success
and stats
.collection_failed
== 0)
449 Out
<< "All tests completed successfully.";
456 Outn(); // Generate a blank to break up the messages if make check/test has been run
457 } while (exit_code
== EXIT_SUCCESS
and opt_repeat
);
459 catch (libtest::fatal
& e
)
461 std::cerr
<< e
.what() << std::endl
;
463 catch (std::bad_alloc
& e
)
465 std::cerr
<< e
.what() << std::endl
;
469 std::cerr
<< "Unknown exception halted execution" << std::endl
;