main.cc 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481
  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. case OPT_LIBYATL_LIST_COLLECTIONS:
  165. opt_list_collection= true;
  166. break;
  167. case OPT_LIBYATL_MATCH_COLLECTION:
  168. collection_to_run= optarg;
  169. break;
  170. case OPT_LIBYATL_MATCH_WILDCARD:
  171. wildcard= optarg;
  172. break;
  173. case OPT_LIBYATL_SSL:
  174. opt_ssl= true;
  175. break;
  176. case OPT_LIBYATL_MASSIVE:
  177. opt_massive= true;
  178. break;
  179. case '?':
  180. /* getopt_long already printed an error message. */
  181. Error << "unknown option to getopt_long()";
  182. exit(EXIT_FAILURE);
  183. default:
  184. break;
  185. }
  186. }
  187. }
  188. if (opt_verbose)
  189. {
  190. for (char** ptr= environ_; *ptr; ptr++)
  191. {
  192. Out << *ptr;
  193. }
  194. }
  195. srandom((unsigned int)time(NULL));
  196. errno= 0;
  197. if (bool(getenv("YATL_REPEAT")))
  198. {
  199. errno= 0;
  200. opt_repeat= strtoul(getenv("YATL_REPEAT"), (char **) NULL, 10);
  201. if (errno != 0)
  202. {
  203. Error << "ENV YATL_REPEAT passed an invalid value: `" << getenv("YATL_REPEAT") << "`";
  204. exit(EXIT_FAILURE);
  205. }
  206. }
  207. if ((bool(getenv("YATL_QUIET")) and (strcmp(getenv("YATL_QUIET"), "0") == 0)) or opt_quiet)
  208. {
  209. opt_quiet= true;
  210. }
  211. else if (getenv("JENKINS_URL"))
  212. {
  213. if (bool(getenv("YATL_QUIET")) and (strcmp(getenv("YATL_QUIET"), "1") == 0))
  214. { }
  215. else
  216. {
  217. opt_quiet= true;
  218. }
  219. }
  220. if ((bool(getenv("YATL_WILDCARD"))))
  221. {
  222. wildcard= getenv("YATL_WILDCARD");
  223. }
  224. if ((bool(getenv("YATL_RUN_MASSIVE_TESTS"))) or opt_massive)
  225. {
  226. opt_massive= true;
  227. }
  228. if ((bool(getenv("YATL_SSL"))) or opt_ssl)
  229. {
  230. opt_ssl= true;
  231. }
  232. if (opt_quiet)
  233. {
  234. close(STDOUT_FILENO);
  235. }
  236. if (opt_ssl)
  237. {
  238. is_ssl(opt_ssl);
  239. }
  240. if (opt_massive)
  241. {
  242. is_massive(opt_massive);
  243. }
  244. libtest::vchar_t tmp_directory;
  245. tmp_directory.resize(1024);
  246. if (getenv("LIBTEST_TMP"))
  247. {
  248. snprintf(&tmp_directory[0], tmp_directory.size(), "%s", getenv("LIBTEST_TMP"));
  249. }
  250. else
  251. {
  252. snprintf(&tmp_directory[0], tmp_directory.size(), "%s", LIBTEST_TEMP);
  253. }
  254. if (chdir(&tmp_directory[0]) == -1)
  255. {
  256. libtest::vchar_t getcwd_buffer;
  257. getcwd_buffer.resize(1024);
  258. char *dir= getcwd(&getcwd_buffer[0], getcwd_buffer.size());
  259. Error << "Unable to chdir() from " << dir << " to " << &tmp_directory[0] << " errno:" << strerror(errno);
  260. return EXIT_FAILURE;
  261. }
  262. if (libtest::libtool() == NULL)
  263. {
  264. Error << "Failed to locate libtool";
  265. return EXIT_FAILURE;
  266. }
  267. if (getenv("YATL_COLLECTION_TO_RUN"))
  268. {
  269. if (strlen(getenv("YATL_COLLECTION_TO_RUN")))
  270. {
  271. collection_to_run= getenv("YATL_COLLECTION_TO_RUN");
  272. }
  273. }
  274. if (collection_to_run.compare("none") == 0)
  275. {
  276. return EXIT_SUCCESS;
  277. }
  278. if (collection_to_run.empty() == false)
  279. {
  280. Out << "Only testing " << collection_to_run;
  281. }
  282. int exit_code;
  283. try
  284. {
  285. do
  286. {
  287. exit_code= EXIT_SUCCESS;
  288. fatal_assert(sigignore(SIGPIPE) == 0);
  289. libtest::SignalThread signal;
  290. if (signal.setup() == false)
  291. {
  292. Error << "Failed to setup signals";
  293. return EXIT_FAILURE;
  294. }
  295. std::unique_ptr<libtest::Framework> frame(new libtest::Framework(signal, binary_name, collection_to_run, wildcard));
  296. if (opt_list_collection)
  297. {
  298. for (Suites::iterator iter= frame->suites().begin();
  299. iter != frame->suites().end();
  300. ++iter)
  301. {
  302. if (opt_list_tests)
  303. {
  304. for (TestCases::iterator test_iter= (*iter)->tests().begin();
  305. test_iter != (*iter)->tests().end();
  306. ++test_iter)
  307. {
  308. Out << (*iter)->name() << "." << (*test_iter)->name();
  309. }
  310. }
  311. else
  312. {
  313. Out << (*iter)->name();
  314. }
  315. }
  316. continue;
  317. }
  318. // Run create(), bail on error.
  319. {
  320. switch (frame->create())
  321. {
  322. case TEST_SUCCESS:
  323. break;
  324. case TEST_SKIPPED:
  325. SKIP("SKIP was returned from framework create()");
  326. break;
  327. case TEST_FAILURE:
  328. std::cerr << "Could not call frame->create()" << std::endl;
  329. return EXIT_FAILURE;
  330. }
  331. }
  332. frame->exec();
  333. if (signal.is_shutdown() == false)
  334. {
  335. signal.set_shutdown(SHUTDOWN_GRACEFUL);
  336. }
  337. shutdown_t status= signal.get_shutdown();
  338. if (status == SHUTDOWN_FORCED)
  339. {
  340. // Tests were aborted
  341. exit_code= EXIT_FAILURE;
  342. }
  343. else if (frame->failed())
  344. {
  345. // Some test failed
  346. exit_code= EXIT_FAILURE;
  347. }
  348. else if (frame->skipped() and frame->failed() and frame->success())
  349. {
  350. // Some tests were skipped
  351. }
  352. else if (frame->success() and (frame->failed() == 0))
  353. {
  354. // Success
  355. }
  356. #if 0
  357. {
  358. std::ofstream xml_file;
  359. std::string file_name;
  360. if (getenv("WORKSPACE"))
  361. {
  362. file_name.append(getenv("WORKSPACE"));
  363. file_name.append("/");
  364. }
  365. file_name.append(frame->name());
  366. file_name.append(".xml");
  367. xml_file.open(file_name.c_str(), std::ios::trunc);
  368. libtest::Formatter::xml(*frame, xml_file);
  369. }
  370. #endif
  371. #if 0
  372. {
  373. std::ofstream tap_file;
  374. std::string file_name;
  375. if (getenv("WORKSPACE"))
  376. {
  377. file_name.append(getenv("WORKSPACE"));
  378. file_name.append("/");
  379. }
  380. file_name.append(frame->name());
  381. file_name.append(".tap");
  382. tap_file.open(file_name.c_str(), std::ios::trunc);
  383. libtest::Formatter::tap(*frame, tap_file);
  384. }
  385. #endif
  386. } while (exit_code == EXIT_SUCCESS and --opt_repeat);
  387. }
  388. catch (const libtest::__skipped& e)
  389. {
  390. exit_code= EXIT_SKIP;
  391. }
  392. catch (const libtest::__failure& e)
  393. {
  394. libtest::stream::make_cout(e.file(), e.line(), e.func()) << e.what();
  395. exit_code= EXIT_FAILURE;
  396. }
  397. catch (const libtest::fatal& e)
  398. {
  399. std::cerr << "FATAL:" << e.what() << std::endl;
  400. exit_code= EXIT_FAILURE;
  401. }
  402. catch (const libtest::disconnected& e)
  403. {
  404. std::cerr << "Unhandled disconnection occurred:" << e.what() << std::endl;
  405. exit_code= EXIT_FAILURE;
  406. }
  407. catch (const std::exception& e)
  408. {
  409. std::cerr << "std::exception:" << e.what() << std::endl;
  410. exit_code= EXIT_FAILURE;
  411. }
  412. catch (char const* s)
  413. {
  414. std::cerr << "Exception:" << s << std::endl;
  415. exit_code= EXIT_FAILURE;
  416. }
  417. catch (...)
  418. {
  419. std::cerr << "Unknown exception halted execution." << std::endl;
  420. exit_code= EXIT_FAILURE;
  421. }
  422. return exit_code;
  423. }