framework.cc 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287
  1. /* vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
  2. *
  3. * Data Differential YATL (i.e. libtest) library
  4. *
  5. * Copyright (C) 2012 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 <libtest/collection.h>
  39. #include <libtest/signal.h>
  40. #include <libtest/stream.h>
  41. #include <algorithm>
  42. #include <cerrno>
  43. #include <fnmatch.h>
  44. #include <iostream>
  45. #include <set>
  46. #include <sys/stat.h>
  47. #include <sys/types.h>
  48. #include <unistd.h>
  49. namespace {
  50. #if defined(DEBUG) && DEBUG
  51. int open_file_descriptors(std::set<int>& pre_existing_fd, const bool report)
  52. {
  53. int max= getdtablesize();
  54. int counter= 0;
  55. for (int x= 3; x < max; ++x)
  56. {
  57. struct stat stats;
  58. if (fstat(x, &stats) == 0)
  59. {
  60. if (report)
  61. {
  62. std::set<int>::iterator it= pre_existing_fd.find(x);
  63. if (it!= pre_existing_fd.end())
  64. { }
  65. else
  66. {
  67. std::cerr << "FD: " << x;
  68. if (S_ISREG(stats.st_mode))
  69. {
  70. std::cerr << " regular file ";
  71. }
  72. else if (S_ISDIR(stats.st_mode))
  73. {
  74. std::cerr << " directory ";
  75. }
  76. else if (S_ISSOCK(stats.st_mode))
  77. {
  78. std::cerr << " socket ";
  79. }
  80. std::cerr << std::endl;
  81. }
  82. }
  83. else
  84. {
  85. pre_existing_fd.insert(x);
  86. }
  87. ++counter;
  88. }
  89. }
  90. return counter;
  91. }
  92. #endif // #if defined(DEBUG) && DEBUG
  93. } // namespace
  94. namespace libtest {
  95. Framework::Framework(libtest::SignalThread& signal_,
  96. const std::string& name_,
  97. const std::string& only_run_arg,
  98. const std::string& wildcard_arg) :
  99. _total(0),
  100. _success(0),
  101. _skipped(0),
  102. _failed(0),
  103. _create(NULL),
  104. _destroy(NULL),
  105. _on_error(NULL),
  106. _runner(NULL),
  107. _socket(false),
  108. _creators_ptr(NULL),
  109. _signal(signal_),
  110. _only_run(only_run_arg),
  111. _wildcard(wildcard_arg),
  112. _name(name_)
  113. {
  114. get_world(this);
  115. {
  116. std::string file_name;
  117. if (getenv("WORKSPACE"))
  118. {
  119. file_name.append(getenv("WORKSPACE"));
  120. file_name.append("/");
  121. }
  122. file_name.append(name());
  123. file_name.append(".xml");
  124. xml_file.open(file_name.c_str(), std::ios::trunc);
  125. _formatter.push_back(new libtest::Junit(this, xml_file));
  126. }
  127. {
  128. std::string file_name;
  129. if (getenv("WORKSPACE"))
  130. {
  131. file_name.append(getenv("WORKSPACE"));
  132. file_name.append("/");
  133. }
  134. file_name.append(name());
  135. file_name.append(".tap");
  136. tap_file.open(file_name.c_str(), std::ios::trunc);
  137. _formatter.push_back(new libtest::TAP(this, tap_file));
  138. }
  139. _formatter.push_back(new libtest::Legacy(this, std::cout));
  140. }
  141. void Framework::collections(collection_st collections_[])
  142. {
  143. for (collection_st *next= collections_; next and next->name; next++)
  144. {
  145. _collection.push_back(new Collection(this, next));
  146. }
  147. }
  148. Framework::~Framework()
  149. {
  150. if (_destroy and _destroy(_creators_ptr))
  151. {
  152. Error << "Failure in _destroy(), some resources may not have been cleaned up.";
  153. }
  154. _servers.shutdown();
  155. delete _runner;
  156. std::for_each(_collection.begin(), _collection.end(), DeleteFromVector());
  157. _collection.clear();
  158. std::for_each(_formatter.begin(), _formatter.end(), DeleteFromVector());
  159. _formatter.clear();
  160. }
  161. bool Framework::match(const char* arg)
  162. {
  163. if (_wildcard.empty() == false and fnmatch(_wildcard.c_str(), arg, 0))
  164. {
  165. return true;
  166. }
  167. return false;
  168. }
  169. void Framework::exec()
  170. {
  171. for (Suites::iterator iter= _collection.begin();
  172. iter != _collection.end() and (_signal.is_shutdown() == false);
  173. ++iter)
  174. {
  175. if (_only_run.empty() == false and
  176. fnmatch(_only_run.c_str(), (*iter)->name(), 0))
  177. {
  178. continue;
  179. }
  180. _total++;
  181. #if defined(DEBUG) && DEBUG
  182. std::set<int> pre_existing_fd;
  183. int open_fd= 0;
  184. if (DEBUG)
  185. {
  186. open_fd= open_file_descriptors(pre_existing_fd, false);
  187. }
  188. #endif
  189. try {
  190. switch ((*iter)->exec())
  191. {
  192. case TEST_FAILURE:
  193. _failed++;
  194. break;
  195. case TEST_SKIPPED:
  196. _skipped++;
  197. break;
  198. // exec() can return SUCCESS, but that doesn't mean that some tests did
  199. // not fail or get skipped.
  200. case TEST_SUCCESS:
  201. _success++;
  202. break;
  203. }
  204. }
  205. catch (const libtest::fatal& e)
  206. {
  207. _failed++;
  208. stream::cerr(e.file(), e.line(), e.func()) << e.what();
  209. }
  210. catch (const libtest::disconnected& e)
  211. {
  212. _failed++;
  213. Error << "Unhandled disconnection occurred:" << e.what();
  214. throw;
  215. }
  216. catch (...)
  217. {
  218. _failed++;
  219. throw;
  220. }
  221. #if defined(DEBUG) && DEBUG
  222. if (DEBUG)
  223. {
  224. int now_open_fd= open_file_descriptors(pre_existing_fd, true);
  225. if (open_fd != now_open_fd)
  226. {
  227. Error << "Growing number of file descriptors: " << int(now_open_fd - open_fd);
  228. }
  229. }
  230. #endif
  231. }
  232. }
  233. libtest::Runner *Framework::runner()
  234. {
  235. if (_runner == NULL)
  236. {
  237. _runner= new Runner;
  238. }
  239. _runner->set_servers(_servers);
  240. return _runner;
  241. }
  242. test_return_t Framework::create()
  243. {
  244. test_return_t rc= TEST_SUCCESS;
  245. if (_create)
  246. {
  247. _creators_ptr= _create(_servers, rc);
  248. }
  249. return rc;
  250. }
  251. } // namespace libtest