main.cc 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485
  1. /* vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
  2. *
  3. * Data Differential YATL (i.e. libtest) library
  4. *
  5. * Copyright (C) 2012-2013 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. #include "libtest/yatlcon.h"
  37. #include <libtest/common.h>
  38. #include <cassert>
  39. #include <cstddef>
  40. #include <cstdlib>
  41. #include <cstring>
  42. #include <ctime>
  43. #include <fnmatch.h>
  44. #include <fstream>
  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. #include <signal.h>
  53. #ifndef __INTEL_COMPILER
  54. #pragma GCC diagnostic ignored "-Wold-style-cast"
  55. #endif
  56. using namespace libtest;
  57. #include <getopt.h>
  58. #include <unistd.h>
  59. #if __GNUC__ >= 7
  60. #pragma GCC diagnostic warning "-Wimplicit-fallthrough"
  61. #endif
  62. int main(int argc, char *argv[], char* environ_[])
  63. {
  64. bool opt_massive= false;
  65. bool opt_ssl= false;
  66. unsigned long int opt_repeat= 1; // Run all tests once
  67. bool opt_verbose= false;
  68. bool opt_quiet= false;
  69. bool opt_list_collection= false;
  70. bool opt_list_tests= false;
  71. std::string collection_to_run;
  72. std::string wildcard;
  73. std::string binary_name;
  74. const char *just_filename= rindex(argv[0], '/');
  75. if (just_filename)
  76. {
  77. just_filename++;
  78. }
  79. else
  80. {
  81. just_filename= argv[0];
  82. }
  83. if (just_filename[0] == 'l' and just_filename[1] == 't' and just_filename[2] == '-')
  84. {
  85. just_filename+= 3;
  86. }
  87. binary_name.append(just_filename);
  88. /*
  89. Valgrind does not currently work reliably, or sometimes at all, on OSX
  90. - Fri Jun 15 11:24:07 EDT 2012
  91. */
  92. #if defined(__APPLE__) && __APPLE__
  93. if (valgrind_is_caller())
  94. {
  95. return EXIT_SKIP;
  96. }
  97. #endif
  98. // Options parsing
  99. {
  100. enum long_option_t {
  101. OPT_LIBYATL_HELP,
  102. OPT_LIBYATL_VERBOSE,
  103. OPT_LIBYATL_VERSION,
  104. OPT_LIBYATL_MATCH_COLLECTION,
  105. OPT_LIBYATL_LIST_COLLECTIONS,
  106. OPT_LIBYATL_LIST_TESTS,
  107. OPT_LIBYATL_MASSIVE,
  108. OPT_LIBYATL_QUIET,
  109. OPT_LIBYATL_MATCH_WILDCARD,
  110. OPT_LIBYATL_REPEAT,
  111. OPT_LIBYATL_SSL,
  112. OPT_LIBYATL_MAX
  113. };
  114. static struct option long_options[]=
  115. {
  116. { "help", no_argument, NULL, OPT_LIBYATL_HELP },
  117. { "verbose", no_argument, NULL, OPT_LIBYATL_VERBOSE },
  118. { "version", no_argument, NULL, OPT_LIBYATL_VERSION },
  119. { "quiet", no_argument, NULL, OPT_LIBYATL_QUIET },
  120. { "repeat", required_argument, NULL, OPT_LIBYATL_REPEAT },
  121. { "collection", required_argument, NULL, OPT_LIBYATL_MATCH_COLLECTION },
  122. { "list-collections", no_argument, NULL, OPT_LIBYATL_LIST_COLLECTIONS },
  123. { "list-tests", no_argument, NULL, OPT_LIBYATL_LIST_TESTS },
  124. { "wildcard", required_argument, NULL, OPT_LIBYATL_MATCH_WILDCARD },
  125. { "massive", no_argument, NULL, OPT_LIBYATL_MASSIVE },
  126. { "ssl", no_argument, NULL, OPT_LIBYATL_SSL },
  127. { 0, 0, 0, 0 }
  128. };
  129. int option_index= 0;
  130. while (1)
  131. {
  132. int option_rv= getopt_long(argc, argv, "", long_options, &option_index);
  133. if (option_rv == -1)
  134. {
  135. break;
  136. }
  137. switch (option_rv)
  138. {
  139. case OPT_LIBYATL_HELP:
  140. for (struct option *opt= long_options; opt->name; ++opt)
  141. {
  142. Out << "--" << opt->name;
  143. }
  144. exit(EXIT_SUCCESS);
  145. case OPT_LIBYATL_VERBOSE:
  146. opt_verbose= true;
  147. break;
  148. case OPT_LIBYATL_VERSION:
  149. break;
  150. case OPT_LIBYATL_QUIET:
  151. opt_quiet= true;
  152. break;
  153. case OPT_LIBYATL_REPEAT:
  154. errno= 0;
  155. opt_repeat= strtoul(optarg, (char **) NULL, 10);
  156. if (errno != 0)
  157. {
  158. Error << "unknown value passed to --repeat: `" << optarg << "`";
  159. exit(EXIT_FAILURE);
  160. }
  161. break;
  162. case OPT_LIBYATL_LIST_TESTS:
  163. opt_list_tests= true;
  164. opt_list_collection= true;
  165. break;
  166. case OPT_LIBYATL_LIST_COLLECTIONS:
  167. opt_list_collection= true;
  168. break;
  169. case OPT_LIBYATL_MATCH_COLLECTION:
  170. collection_to_run= optarg;
  171. break;
  172. case OPT_LIBYATL_MATCH_WILDCARD:
  173. wildcard= optarg;
  174. break;
  175. case OPT_LIBYATL_SSL:
  176. opt_ssl= true;
  177. break;
  178. case OPT_LIBYATL_MASSIVE:
  179. opt_massive= true;
  180. break;
  181. case '?':
  182. /* getopt_long already printed an error message. */
  183. Error << "unknown option to getopt_long()";
  184. exit(EXIT_FAILURE);
  185. default:
  186. break;
  187. }
  188. }
  189. }
  190. if (opt_verbose)
  191. {
  192. for (char** ptr= environ_; *ptr; ptr++)
  193. {
  194. Out << *ptr;
  195. }
  196. }
  197. srandom((unsigned int)time(NULL));
  198. errno= 0;
  199. if (bool(getenv("YATL_REPEAT")))
  200. {
  201. errno= 0;
  202. opt_repeat= strtoul(getenv("YATL_REPEAT"), (char **) NULL, 10);
  203. if (errno != 0)
  204. {
  205. Error << "ENV YATL_REPEAT passed an invalid value: `" << getenv("YATL_REPEAT") << "`";
  206. exit(EXIT_FAILURE);
  207. }
  208. }
  209. if ((bool(getenv("YATL_QUIET")) and (strcmp(getenv("YATL_QUIET"), "0") == 0)) or opt_quiet)
  210. {
  211. opt_quiet= true;
  212. }
  213. else if (getenv("JENKINS_URL"))
  214. {
  215. if (bool(getenv("YATL_QUIET")) and (strcmp(getenv("YATL_QUIET"), "1") == 0))
  216. { }
  217. else
  218. {
  219. opt_quiet= true;
  220. }
  221. }
  222. if (getenv("YATL_WILDCARD"))
  223. {
  224. wildcard= getenv("YATL_WILDCARD");
  225. }
  226. if (getenv("YATL_RUN_MASSIVE_TESTS"))
  227. {
  228. opt_massive= true;
  229. }
  230. if (getenv("YATL_SSL"))
  231. {
  232. opt_ssl= true;
  233. }
  234. if (opt_quiet)
  235. {
  236. close(STDOUT_FILENO);
  237. }
  238. if (opt_ssl)
  239. {
  240. is_ssl(opt_ssl);
  241. }
  242. if (opt_massive)
  243. {
  244. is_massive(opt_massive);
  245. }
  246. libtest::vchar_t tmp_directory;
  247. tmp_directory.resize(1024);
  248. if (getenv("LIBTEST_TMP"))
  249. {
  250. snprintf(&tmp_directory[0], tmp_directory.size(), "%s", getenv("LIBTEST_TMP"));
  251. }
  252. else
  253. {
  254. snprintf(&tmp_directory[0], tmp_directory.size(), "%s", LIBTEST_TEMP);
  255. }
  256. if (chdir(&tmp_directory[0]) == -1)
  257. {
  258. libtest::vchar_t getcwd_buffer;
  259. getcwd_buffer.resize(1024);
  260. char *dir= getcwd(&getcwd_buffer[0], getcwd_buffer.size());
  261. Error << "Unable to chdir() from " << dir << " to " << &tmp_directory[0] << " errno:" << strerror(errno);
  262. return EXIT_FAILURE;
  263. }
  264. if (libtest::libtool() == NULL)
  265. {
  266. Error << "Failed to locate libtool";
  267. return EXIT_FAILURE;
  268. }
  269. if (getenv("YATL_COLLECTION_TO_RUN"))
  270. {
  271. if (strlen(getenv("YATL_COLLECTION_TO_RUN")))
  272. {
  273. collection_to_run= getenv("YATL_COLLECTION_TO_RUN");
  274. }
  275. }
  276. if (collection_to_run.compare("none") == 0)
  277. {
  278. return EXIT_SUCCESS;
  279. }
  280. if (collection_to_run.empty() == false)
  281. {
  282. Out << "Only testing " << collection_to_run;
  283. }
  284. int exit_code;
  285. try
  286. {
  287. do
  288. {
  289. exit_code= EXIT_SUCCESS;
  290. struct sigaction sa;
  291. sa.sa_handler= SIG_IGN;
  292. fatal_assert(sigaction(SIGPIPE, &sa, NULL) == 0);
  293. libtest::SignalThread signal;
  294. if (signal.setup() == false)
  295. {
  296. Error << "Failed to setup signals";
  297. return EXIT_FAILURE;
  298. }
  299. std::unique_ptr<libtest::Framework> frame(new libtest::Framework(signal, binary_name, collection_to_run, wildcard));
  300. if (opt_list_collection)
  301. {
  302. for (Suites::iterator iter= frame->suites().begin();
  303. iter != frame->suites().end();
  304. ++iter)
  305. {
  306. if (opt_list_tests)
  307. {
  308. for (TestCases::iterator test_iter= (*iter)->tests().begin();
  309. test_iter != (*iter)->tests().end();
  310. ++test_iter)
  311. {
  312. Out << (*iter)->name() << "." << (*test_iter)->name();
  313. }
  314. }
  315. else
  316. {
  317. Out << (*iter)->name();
  318. }
  319. }
  320. continue;
  321. }
  322. // Run create(), bail on error.
  323. {
  324. switch (frame->create())
  325. {
  326. case TEST_SUCCESS:
  327. break;
  328. case TEST_SKIPPED:
  329. SKIP("SKIP was returned from framework create()");
  330. break;
  331. case TEST_FAILURE:
  332. std::cerr << "Could not call frame->create()" << std::endl;
  333. return EXIT_FAILURE;
  334. }
  335. }
  336. frame->exec();
  337. if (signal.is_shutdown() == false)
  338. {
  339. signal.set_shutdown(SHUTDOWN_GRACEFUL);
  340. }
  341. shutdown_t status= signal.get_shutdown();
  342. if (status == SHUTDOWN_FORCED)
  343. {
  344. // Tests were aborted
  345. exit_code= EXIT_FAILURE;
  346. }
  347. else if (frame->failed())
  348. {
  349. // Some test failed
  350. exit_code= EXIT_FAILURE;
  351. }
  352. else if (frame->skipped() and frame->failed() and frame->success())
  353. {
  354. // Some tests were skipped
  355. }
  356. else if (frame->success() and (frame->failed() == 0))
  357. {
  358. // Success
  359. }
  360. #if 0
  361. {
  362. std::ofstream xml_file;
  363. std::string file_name;
  364. if (getenv("WORKSPACE"))
  365. {
  366. file_name.append(getenv("WORKSPACE"));
  367. file_name.append("/");
  368. }
  369. file_name.append(frame->name());
  370. file_name.append(".xml");
  371. xml_file.open(file_name.c_str(), std::ios::trunc);
  372. libtest::Formatter::xml(*frame, xml_file);
  373. }
  374. #endif
  375. #if 0
  376. {
  377. std::ofstream tap_file;
  378. std::string file_name;
  379. if (getenv("WORKSPACE"))
  380. {
  381. file_name.append(getenv("WORKSPACE"));
  382. file_name.append("/");
  383. }
  384. file_name.append(frame->name());
  385. file_name.append(".tap");
  386. tap_file.open(file_name.c_str(), std::ios::trunc);
  387. libtest::Formatter::tap(*frame, tap_file);
  388. }
  389. #endif
  390. } while (exit_code == EXIT_SUCCESS and --opt_repeat);
  391. }
  392. catch (const libtest::__skipped& e)
  393. {
  394. exit_code= EXIT_SKIP;
  395. }
  396. catch (const libtest::__failure& e)
  397. {
  398. libtest::stream::make_cout(e.file(), e.line(), e.func()) << e.what();
  399. exit_code= EXIT_FAILURE;
  400. }
  401. catch (const libtest::fatal& e)
  402. {
  403. std::cerr << "FATAL:" << e.what() << std::endl;
  404. exit_code= EXIT_FAILURE;
  405. }
  406. catch (const libtest::disconnected& e)
  407. {
  408. std::cerr << "Unhandled disconnection occurred:" << e.what() << std::endl;
  409. exit_code= EXIT_FAILURE;
  410. }
  411. catch (const std::exception& e)
  412. {
  413. std::cerr << "std::exception:" << e.what() << std::endl;
  414. exit_code= EXIT_FAILURE;
  415. }
  416. catch (char const* s)
  417. {
  418. std::cerr << "Exception:" << s << std::endl;
  419. exit_code= EXIT_FAILURE;
  420. }
  421. catch (...)
  422. {
  423. std::cerr << "Unknown exception halted execution." << std::endl;
  424. exit_code= EXIT_FAILURE;
  425. }
  426. return exit_code;
  427. }