main.cc 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435
  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. int main(int argc, char *argv[], char* environ_[])
  60. {
  61. bool opt_massive= false;
  62. bool opt_ssl= false;
  63. unsigned long int opt_repeat= 1; // Run all tests once
  64. bool opt_verbose= false;
  65. bool opt_quiet= false;
  66. std::string collection_to_run;
  67. std::string wildcard;
  68. std::string binary_name;
  69. const char *just_filename= rindex(argv[0], '/');
  70. if (just_filename)
  71. {
  72. just_filename++;
  73. }
  74. else
  75. {
  76. just_filename= argv[0];
  77. }
  78. if (just_filename[0] == 'l' and just_filename[1] == 't' and just_filename[2] == '-')
  79. {
  80. just_filename+= 3;
  81. }
  82. binary_name.append(just_filename);
  83. /*
  84. Valgrind does not currently work reliably, or sometimes at all, on OSX
  85. - Fri Jun 15 11:24:07 EDT 2012
  86. */
  87. #if defined(TARGET_OS_OSX) && TARGET_OS_OSX
  88. if (valgrind_is_caller())
  89. {
  90. return EXIT_SKIP;
  91. }
  92. #endif
  93. // Options parsing
  94. {
  95. enum long_option_t {
  96. OPT_LIBYATL_VERBOSE,
  97. OPT_LIBYATL_VERSION,
  98. OPT_LIBYATL_MATCH_COLLECTION,
  99. OPT_LIBYATL_MASSIVE,
  100. OPT_LIBYATL_QUIET,
  101. OPT_LIBYATL_MATCH_WILDCARD,
  102. OPT_LIBYATL_REPEAT,
  103. OPT_LIBYATL_SSL,
  104. OPT_LIBYATL_MAX
  105. };
  106. static struct option long_options[]=
  107. {
  108. { "verbose", no_argument, NULL, OPT_LIBYATL_VERBOSE },
  109. { "version", no_argument, NULL, OPT_LIBYATL_VERSION },
  110. { "quiet", no_argument, NULL, OPT_LIBYATL_QUIET },
  111. { "repeat", required_argument, NULL, OPT_LIBYATL_REPEAT },
  112. { "collection", required_argument, NULL, OPT_LIBYATL_MATCH_COLLECTION },
  113. { "wildcard", required_argument, NULL, OPT_LIBYATL_MATCH_WILDCARD },
  114. { "massive", no_argument, NULL, OPT_LIBYATL_MASSIVE },
  115. { "ssl", no_argument, NULL, OPT_LIBYATL_SSL },
  116. { 0, 0, 0, 0 }
  117. };
  118. int option_index= 0;
  119. while (1)
  120. {
  121. int option_rv= getopt_long(argc, argv, "", long_options, &option_index);
  122. if (option_rv == -1)
  123. {
  124. break;
  125. }
  126. switch (option_rv)
  127. {
  128. case OPT_LIBYATL_VERBOSE:
  129. opt_verbose= true;
  130. break;
  131. case OPT_LIBYATL_VERSION:
  132. break;
  133. case OPT_LIBYATL_QUIET:
  134. opt_quiet= true;
  135. break;
  136. case OPT_LIBYATL_REPEAT:
  137. errno= 0;
  138. opt_repeat= strtoul(optarg, (char **) NULL, 10);
  139. if (errno != 0)
  140. {
  141. Error << "unknown value passed to --repeat: `" << optarg << "`";
  142. exit(EXIT_FAILURE);
  143. }
  144. break;
  145. case OPT_LIBYATL_MATCH_COLLECTION:
  146. collection_to_run= optarg;
  147. break;
  148. case OPT_LIBYATL_MATCH_WILDCARD:
  149. wildcard= optarg;
  150. break;
  151. case OPT_LIBYATL_SSL:
  152. opt_ssl= true;
  153. break;
  154. case OPT_LIBYATL_MASSIVE:
  155. opt_massive= true;
  156. break;
  157. case '?':
  158. /* getopt_long already printed an error message. */
  159. Error << "unknown option to getopt_long()";
  160. exit(EXIT_FAILURE);
  161. default:
  162. break;
  163. }
  164. }
  165. }
  166. if (opt_verbose)
  167. {
  168. for (char** ptr= environ_; *ptr; ptr++)
  169. {
  170. Out << *ptr;
  171. }
  172. }
  173. srandom((unsigned int)time(NULL));
  174. errno= 0;
  175. if (bool(getenv("YATL_REPEAT")))
  176. {
  177. errno= 0;
  178. opt_repeat= strtoul(getenv("YATL_REPEAT"), (char **) NULL, 10);
  179. if (errno != 0)
  180. {
  181. Error << "ENV YATL_REPEAT passed an invalid value: `" << getenv("YATL_REPEAT") << "`";
  182. exit(EXIT_FAILURE);
  183. }
  184. }
  185. if ((bool(getenv("YATL_QUIET")) and (strcmp(getenv("YATL_QUIET"), "0") == 0)) or opt_quiet)
  186. {
  187. opt_quiet= true;
  188. }
  189. else if (getenv("JENKINS_URL"))
  190. {
  191. if (bool(getenv("YATL_QUIET")) and (strcmp(getenv("YATL_QUIET"), "1") == 0))
  192. { }
  193. else
  194. {
  195. opt_quiet= true;
  196. }
  197. }
  198. if ((bool(getenv("YATL_WILDCARD"))))
  199. {
  200. wildcard= getenv("YATL_WILDCARD");
  201. }
  202. if ((bool(getenv("YATL_RUN_MASSIVE_TESTS"))) or opt_massive)
  203. {
  204. opt_massive= true;
  205. }
  206. if ((bool(getenv("YATL_SSL"))) or opt_ssl)
  207. {
  208. opt_ssl= true;
  209. }
  210. if (opt_quiet)
  211. {
  212. close(STDOUT_FILENO);
  213. }
  214. if (opt_ssl)
  215. {
  216. is_ssl(opt_ssl);
  217. }
  218. if (opt_massive)
  219. {
  220. is_massive(opt_massive);
  221. }
  222. libtest::vchar_t tmp_directory;
  223. tmp_directory.resize(1024);
  224. if (getenv("LIBTEST_TMP"))
  225. {
  226. snprintf(&tmp_directory[0], tmp_directory.size(), "%s", getenv("LIBTEST_TMP"));
  227. }
  228. else
  229. {
  230. snprintf(&tmp_directory[0], tmp_directory.size(), "%s", LIBTEST_TEMP);
  231. }
  232. if (chdir(&tmp_directory[0]) == -1)
  233. {
  234. libtest::vchar_t getcwd_buffer;
  235. getcwd_buffer.resize(1024);
  236. char *dir= getcwd(&getcwd_buffer[0], getcwd_buffer.size());
  237. Error << "Unable to chdir() from " << dir << " to " << &tmp_directory[0] << " errno:" << strerror(errno);
  238. return EXIT_FAILURE;
  239. }
  240. if (libtest::libtool() == NULL)
  241. {
  242. Error << "Failed to locate libtool";
  243. return EXIT_FAILURE;
  244. }
  245. if (getenv("YATL_COLLECTION_TO_RUN"))
  246. {
  247. if (strlen(getenv("YATL_COLLECTION_TO_RUN")))
  248. {
  249. collection_to_run= getenv("YATL_COLLECTION_TO_RUN");
  250. }
  251. }
  252. if (collection_to_run.compare("none") == 0)
  253. {
  254. return EXIT_SUCCESS;
  255. }
  256. if (collection_to_run.empty() == false)
  257. {
  258. Out << "Only testing " << collection_to_run;
  259. }
  260. int exit_code;
  261. try
  262. {
  263. do
  264. {
  265. exit_code= EXIT_SUCCESS;
  266. fatal_assert(sigignore(SIGPIPE) == 0);
  267. libtest::SignalThread signal;
  268. if (signal.setup() == false)
  269. {
  270. Error << "Failed to setup signals";
  271. return EXIT_FAILURE;
  272. }
  273. std::auto_ptr<libtest::Framework> frame(new libtest::Framework(signal, binary_name, collection_to_run, wildcard));
  274. // Run create(), bail on error.
  275. {
  276. switch (frame->create())
  277. {
  278. case TEST_SUCCESS:
  279. break;
  280. case TEST_SKIPPED:
  281. SKIP("SKIP was returned from framework create()");
  282. break;
  283. case TEST_FAILURE:
  284. std::cerr << "Could not call frame->create()" << std::endl;
  285. return EXIT_FAILURE;
  286. }
  287. }
  288. frame->exec();
  289. if (signal.is_shutdown() == false)
  290. {
  291. signal.set_shutdown(SHUTDOWN_GRACEFUL);
  292. }
  293. shutdown_t status= signal.get_shutdown();
  294. if (status == SHUTDOWN_FORCED)
  295. {
  296. Out << "Tests were aborted.";
  297. exit_code= EXIT_FAILURE;
  298. }
  299. else if (frame->failed())
  300. {
  301. Out << "Some test failed.";
  302. exit_code= EXIT_FAILURE;
  303. }
  304. else if (frame->skipped() and frame->failed() and frame->success())
  305. {
  306. Out << "Some tests were skipped.";
  307. }
  308. else if (frame->success() and (frame->failed() == 0))
  309. {
  310. Out;
  311. Out << "All tests completed successfully.";
  312. }
  313. #if 0
  314. {
  315. std::ofstream xml_file;
  316. std::string file_name;
  317. if (getenv("WORKSPACE"))
  318. {
  319. file_name.append(getenv("WORKSPACE"));
  320. file_name.append("/");
  321. }
  322. file_name.append(frame->name());
  323. file_name.append(".xml");
  324. xml_file.open(file_name.c_str(), std::ios::trunc);
  325. libtest::Formatter::xml(*frame, xml_file);
  326. }
  327. #endif
  328. #if 0
  329. {
  330. std::ofstream tap_file;
  331. std::string file_name;
  332. if (getenv("WORKSPACE"))
  333. {
  334. file_name.append(getenv("WORKSPACE"));
  335. file_name.append("/");
  336. }
  337. file_name.append(frame->name());
  338. file_name.append(".tap");
  339. tap_file.open(file_name.c_str(), std::ios::trunc);
  340. libtest::Formatter::tap(*frame, tap_file);
  341. }
  342. #endif
  343. Outn(); // Generate a blank to break up the messages if make check/test has been run
  344. } while (exit_code == EXIT_SUCCESS and --opt_repeat);
  345. }
  346. catch (const libtest::__skipped& e)
  347. {
  348. return EXIT_SKIP;
  349. }
  350. catch (const libtest::__failure& e)
  351. {
  352. libtest::stream::make_cout(e.file(), e.line(), e.func()) << e.what();
  353. exit_code= EXIT_FAILURE;
  354. }
  355. catch (const libtest::fatal& e)
  356. {
  357. std::cerr << "FATAL:" << e.what() << std::endl;
  358. exit_code= EXIT_FAILURE;
  359. }
  360. catch (const libtest::disconnected& e)
  361. {
  362. std::cerr << "Unhandled disconnection occurred:" << e.what() << std::endl;
  363. exit_code= EXIT_FAILURE;
  364. }
  365. catch (const std::exception& e)
  366. {
  367. std::cerr << "std::exception:" << e.what() << std::endl;
  368. exit_code= EXIT_FAILURE;
  369. }
  370. catch (char const* s)
  371. {
  372. std::cerr << "Exception:" << s << std::endl;
  373. exit_code= EXIT_FAILURE;
  374. }
  375. catch (...)
  376. {
  377. std::cerr << "Unknown exception halted execution." << std::endl;
  378. exit_code= EXIT_FAILURE;
  379. }
  380. return exit_code;
  381. }