test.cc 11 KB

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