framework.cc 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  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 <config.h>
  37. #include <libtest/common.h>
  38. #include <libtest/collection.h>
  39. #include <libtest/signal.h>
  40. #include <fnmatch.h>
  41. #include <iostream>
  42. namespace libtest {
  43. Framework::Framework(libtest::SignalThread& signal,
  44. const std::string& name_,
  45. const std::string& only_run_arg,
  46. const std::string& wildcard_arg) :
  47. _total(0),
  48. _success(0),
  49. _skipped(0),
  50. _failed(0),
  51. _create(NULL),
  52. _destroy(NULL),
  53. _on_error(NULL),
  54. _runner(NULL),
  55. _socket(false),
  56. _creators_ptr(NULL),
  57. _signal(signal),
  58. _only_run(only_run_arg),
  59. _wildcard(wildcard_arg),
  60. _name(name_)
  61. {
  62. get_world(this);
  63. }
  64. void Framework::collections(collection_st* collections_)
  65. {
  66. for (collection_st *next= collections_; next and next->name; next++)
  67. {
  68. _collection.push_back(new Collection(this, next));
  69. }
  70. }
  71. Framework::~Framework()
  72. {
  73. if (_destroy and _destroy(_creators_ptr))
  74. {
  75. Error << "Failure in _destroy(), some resources may not have been cleaned up.";
  76. }
  77. _servers.shutdown();
  78. delete _runner;
  79. for (std::vector<Collection*>::iterator iter= _collection.begin();
  80. iter != _collection.end();
  81. ++iter)
  82. {
  83. delete *iter;
  84. }
  85. }
  86. bool Framework::match(const char* arg)
  87. {
  88. if (_wildcard.empty() == false and fnmatch(_wildcard.c_str(), arg, 0))
  89. {
  90. return true;
  91. }
  92. return false;
  93. }
  94. void Framework::exec()
  95. {
  96. for (std::vector<Collection*>::iterator iter= _collection.begin();
  97. iter != _collection.end() and (_signal.is_shutdown() == false);
  98. ++iter)
  99. {
  100. if (_only_run.empty() == false and
  101. fnmatch(_only_run.c_str(), (*iter)->name(), 0))
  102. {
  103. continue;
  104. }
  105. _total++;
  106. try {
  107. switch ((*iter)->exec())
  108. {
  109. case TEST_FAILURE:
  110. _failed++;
  111. break;
  112. case TEST_SKIPPED:
  113. _skipped++;
  114. break;
  115. // exec() can return SUCCESS, but that doesn't mean that some tests did
  116. // not fail or get skipped.
  117. case TEST_SUCCESS:
  118. _success++;
  119. break;
  120. }
  121. }
  122. catch (libtest::fatal& e)
  123. {
  124. _failed++;
  125. stream::cerr(e.file(), e.line(), e.func()) << e.mesg();
  126. }
  127. catch (libtest::disconnected& e)
  128. {
  129. _failed++;
  130. Error << "Unhandled disconnection occurred:" << e.what();
  131. throw;
  132. }
  133. catch (...)
  134. {
  135. _failed++;
  136. throw;
  137. }
  138. }
  139. void xml(const std::string& testsuites_name, std::ostream& output);
  140. }
  141. uint32_t Framework::sum_total()
  142. {
  143. uint32_t count= 0;
  144. for (std::vector<Collection*>::iterator iter= _collection.begin();
  145. iter != _collection.end();
  146. ++iter)
  147. {
  148. count+= (*iter)->total();
  149. }
  150. return count;
  151. }
  152. uint32_t Framework::sum_success()
  153. {
  154. uint32_t count= 0;
  155. for (std::vector<Collection*>::iterator iter= _collection.begin();
  156. iter != _collection.end();
  157. ++iter)
  158. {
  159. count+= (*iter)->success();
  160. }
  161. return count;
  162. }
  163. uint32_t Framework::sum_skipped()
  164. {
  165. uint32_t count= 0;
  166. for (std::vector<Collection*>::iterator iter= _collection.begin();
  167. iter != _collection.end();
  168. ++iter)
  169. {
  170. count+= (*iter)->skipped();
  171. }
  172. return count;
  173. }
  174. uint32_t Framework::sum_failed()
  175. {
  176. uint32_t count= 0;
  177. for (std::vector<Collection*>::iterator iter= _collection.begin();
  178. iter != _collection.end();
  179. ++iter)
  180. {
  181. count+= (*iter)->failed();
  182. }
  183. return count;
  184. }
  185. libtest::Runner *Framework::runner()
  186. {
  187. if (_runner == NULL)
  188. {
  189. _runner= new Runner;
  190. }
  191. _runner->set_servers(_servers);
  192. return _runner;
  193. }
  194. test_return_t Framework::create()
  195. {
  196. test_return_t rc= TEST_SUCCESS;
  197. if (_create)
  198. {
  199. _creators_ptr= _create(_servers, rc);
  200. }
  201. return rc;
  202. }
  203. } // namespace libtest