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
23 #include <libtest/common.h>
29 #include <sys/types.h>
39 #if defined(HAVE_CURL_CURL_H) && HAVE_CURL_CURL_H
40 #include <curl/curl.h>
43 #ifndef __INTEL_COMPILER
44 #pragma GCC diagnostic ignored "-Wold-style-cast"
47 using namespace libtest
;
49 static void stats_print(Stats
*stats
)
51 if (stats
->collection_failed
== 0 and stats
->collection_success
== 0)
56 Out
<< "\tTotal Collections\t\t\t\t" << stats
->collection_total
;
57 Out
<< "\tFailed Collections\t\t\t\t" << stats
->collection_failed
;
58 Out
<< "\tSkipped Collections\t\t\t\t" << stats
->collection_skipped
;
59 Out
<< "\tSucceeded Collections\t\t\t\t" << stats
->collection_success
;
61 Out
<< "Total\t\t\t\t" << stats
->total
;
62 Out
<< "\tFailed\t\t\t" << stats
->failed
;
63 Out
<< "\tSkipped\t\t\t" << stats
->skipped
;
64 Out
<< "\tSucceeded\t\t" << stats
->success
;
67 static long int timedif(struct timeval a
, struct timeval b
)
71 us
= (long)(a
.tv_usec
- b
.tv_usec
);
73 s
= (long)(a
.tv_sec
- b
.tv_sec
);
78 static void cleanup_curl(void)
80 #if defined(HAVE_CURL_CURL_H) && HAVE_CURL_CURL_H
81 curl_global_cleanup();
88 int main(int argc
, char *argv
[])
90 #if defined(HAVE_CURL_CURL_H) && HAVE_CURL_CURL_H
91 if (curl_global_init(CURL_GLOBAL_ALL
))
93 Error
<< "curl_global_init(CURL_GLOBAL_ALL) failed";
98 if (atexit(cleanup_curl
))
100 Error
<< "atexit() failed";
104 bool opt_repeat
= false;
105 std::string collection_to_run
;
111 OPT_LIBYATL_MATCH_COLLECTION
,
115 static struct option long_options
[]=
117 {"repeat", no_argument
, NULL
, OPT_LIBYATL_REPEAT
},
118 {"collection", required_argument
, NULL
, OPT_LIBYATL_MATCH_COLLECTION
},
125 int option_rv
= getopt_long(argc
, argv
, "", long_options
, &option_index
);
133 case OPT_LIBYATL_VERSION
:
136 case OPT_LIBYATL_REPEAT
:
140 case OPT_LIBYATL_MATCH_COLLECTION
:
141 collection_to_run
= optarg
;
145 /* getopt_long already printed an error message. */
146 Error
<< "unknown option to getopt_long()";
155 srandom((unsigned int)time(NULL
));
157 if (getenv("LIBTEST_QUIET") and strcmp(getenv("LIBTEST_QUIET"), "0") == 0)
159 close(STDOUT_FILENO
);
161 else if (getenv("JENKINS_URL"))
163 close(STDOUT_FILENO
);
167 if (getenv("LIBTEST_TMP"))
169 snprintf(buffer
, sizeof(buffer
), "%s", getenv("LIBTEST_TMP"));
173 snprintf(buffer
, sizeof(buffer
), "%s", LIBTEST_TEMP
);
176 if (chdir(buffer
) == -1)
178 char getcwd_buffer
[1024];
179 char *dir
= getcwd(getcwd_buffer
, sizeof(getcwd_buffer
));
181 Error
<< "Unable to chdir() from " << dir
<< " to " << buffer
<< " errno:" << strerror(errno
);
185 if (libtest::libtool() == NULL
)
187 Error
<< "Failed to locate libtool";
195 exit_code
= EXIT_SUCCESS
;
198 fatal_assert(sigignore(SIGPIPE
) == 0);
200 libtest::SignalThread signal
;
201 if (not signal
.setup())
203 Error
<< "Failed to setup signals";
212 void *creators_ptr
= world
.create(error
);
220 Out
<< "SKIP " << argv
[0];
227 if (getenv("TEST_COLLECTION"))
229 if (strlen(getenv("TEST_COLLECTION")))
231 collection_to_run
= getenv("TEST_COLLECTION");
235 if (collection_to_run
.empty() == false)
237 Out
<< "Only testing " << collection_to_run
;
240 char *wildcard
= NULL
;
246 for (collection_st
*next
= world
.collections
; next
and next
->name
and (not signal
.is_shutdown()); next
++)
251 if (collection_to_run
.empty() == false and fnmatch(collection_to_run
.c_str(), next
->name
, 0))
256 stats
.collection_total
++;
258 test_return_t collection_rc
= world
.startup(creators_ptr
);
260 if (collection_rc
== TEST_SUCCESS
and next
->pre
)
262 collection_rc
= world
.runner()->pre(next
->pre
, creators_ptr
);
265 switch (collection_rc
)
271 Out
<< next
->name
<< " [ failed ]";
273 signal
.set_shutdown(SHUTDOWN_GRACEFUL
);
277 Out
<< next
->name
<< " [ skipping ]";
282 throw fatal_message("invalid return code");
285 Out
<< "Collection: " << next
->name
;
287 for (test_st
*run
= next
->tests
; run
->name
; run
++)
289 struct timeval start_time
, end_time
;
290 long int load_time
= 0;
292 if (wildcard
&& fnmatch(wildcard
, run
->name
, 0))
297 test_return_t return_code
;
299 if (test_success(return_code
= world
.item
.startup(creators_ptr
)))
301 if (test_success(return_code
= world
.item
.flush(creators_ptr
, run
)))
303 // @note pre will fail is SKIPPED is returned
304 if (test_success(return_code
= world
.item
.pre(creators_ptr
)))
307 gettimeofday(&start_time
, NULL
);
308 assert(world
.runner());
309 assert(run
->test_fn
);
312 return_code
= world
.runner()->run(run
->test_fn
, creators_ptr
);
314 // Special case where check for the testing of the exception
316 catch (libtest::fatal
&e
)
318 if (fatal::is_disabled())
320 fatal::increment_disabled_counter();
321 return_code
= TEST_SUCCESS
;
329 gettimeofday(&end_time
, NULL
);
330 load_time
= timedif(end_time
, start_time
);
334 // @todo do something if post fails
335 (void)world
.item
.post(creators_ptr
);
337 else if (return_code
== TEST_SKIPPED
)
339 else if (return_code
== TEST_FAILURE
)
341 Error
<< " item.flush(failure)";
342 signal
.set_shutdown(SHUTDOWN_GRACEFUL
);
345 else if (return_code
== TEST_SKIPPED
)
347 else if (return_code
== TEST_FAILURE
)
349 Error
<< " item.startup(failure)";
350 signal
.set_shutdown(SHUTDOWN_GRACEFUL
);
354 catch (std::exception
&e
)
356 Error
<< "Exception was thrown: " << e
.what();
357 return_code
= TEST_FAILURE
;
361 Error
<< "Unknown exception occurred";
362 return_code
= TEST_FAILURE
;
370 Out
<< "\tTesting " << run
->name
<< "\t\t\t\t\t" << load_time
/ 1000 << "." << load_time
% 1000 << "[ " << test_strerror(return_code
) << " ]";
377 Out
<< "\tTesting " << run
->name
<< "\t\t\t\t\t" << "[ " << test_strerror(return_code
) << " ]";
383 Out
<< "\tTesting " << run
->name
<< "\t\t\t\t\t" << "[ " << test_strerror(return_code
) << " ]";
387 throw fatal_message("invalid return code");
390 if (test_failed(world
.on_error(return_code
, creators_ptr
)))
392 Error
<< "Failed while running on_error()";
393 signal
.set_shutdown(SHUTDOWN_GRACEFUL
);
398 (void) world
.runner()->post(next
->post
, creators_ptr
);
401 if (failed
== false and skipped
== false)
403 stats
.collection_success
++;
408 stats
.collection_failed
++;
413 stats
.collection_skipped
++;
416 world
.shutdown(creators_ptr
);
420 if (not signal
.is_shutdown())
422 signal
.set_shutdown(SHUTDOWN_GRACEFUL
);
425 shutdown_t status
= signal
.get_shutdown();
426 if (status
== SHUTDOWN_FORCED
)
428 Out
<< "Tests were aborted.";
429 exit_code
= EXIT_FAILURE
;
431 else if (stats
.collection_failed
)
433 Out
<< "Some test failed.";
434 exit_code
= EXIT_FAILURE
;
436 else if (stats
.collection_skipped
and stats
.collection_failed
and stats
.collection_success
)
438 Out
<< "Some tests were skipped.";
440 else if (stats
.collection_success
and stats
.collection_failed
== 0)
442 Out
<< "All tests completed successfully.";
447 Outn(); // Generate a blank to break up the messages if make check/test has been run
448 } while (exit_code
== EXIT_SUCCESS
and opt_repeat
);
450 catch (libtest::fatal
& e
)
452 std::cerr
<< e
.what() << std::endl
;
454 catch (std::bad_alloc
& e
)
456 std::cerr
<< e
.what() << std::endl
;
460 std::cerr
<< "Unknown exception halted execution" << std::endl
;