TestManager.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396
  1. /*
  2. * Copyright (c) 2017-2018,2020 The Linux Foundation. All rights reserved.
  3. *
  4. * Redistribution and use in source and binary forms, with or without
  5. * modification, are permitted provided that the following conditions are
  6. * met:
  7. * * Redistributions of source code must retain the above copyright
  8. * notice, this list of conditions and the following disclaimer.
  9. * * Redistributions in binary form must reproduce the above
  10. * copyright notice, this list of conditions and the following
  11. * disclaimer in the documentation and/or other materials provided
  12. * with the distribution.
  13. * * Neither the name of The Linux Foundation nor the names of its
  14. * contributors may be used to endorse or promote products derived
  15. * from this software without specific prior written permission.
  16. *
  17. * THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESS OR IMPLIED
  18. * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
  19. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT
  20. * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS
  21. * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  22. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  23. * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
  24. * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
  25. * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
  26. * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
  27. * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  28. */
  29. #include <stdio.h>
  30. #include <algorithm> // std::find
  31. #include <vector> // std::vector
  32. #include <string>
  33. #include <errno.h>
  34. #include <ctime>
  35. #include <sstream>
  36. #include "TestManager.h"
  37. #include "TestsUtils.h"
  38. #include <fcntl.h>
  39. #include <unistd.h>
  40. #include "ipa_test_module.h"
  41. #include <sys/ioctl.h>
  42. using namespace std;
  43. /* Global static pointer used to ensure a single instance of the class. */
  44. TestManager* TestManager::m_instance = NULL;
  45. #ifdef HAVE_LIBXML
  46. TestsXMLResult::TestsXMLResult()
  47. {
  48. xmlNodePtr node;
  49. // initialize xml report document and add a root to node it
  50. m_XML_doc_ptr = xmlNewDoc(BAD_CAST "1.0");
  51. if (m_XML_doc_ptr == NULL){
  52. printf("error on allocation xml doc\n");
  53. exit(-1);
  54. }
  55. node = xmlNewNode(NULL, BAD_CAST "testsuites");
  56. if (!node) {
  57. printf("failed to allocate XML node\n");
  58. exit (-1);
  59. }
  60. xmlDocSetRootElement(m_XML_doc_ptr, node);
  61. }
  62. TestsXMLResult::~TestsXMLResult()
  63. {
  64. if (m_XML_doc_ptr)
  65. xmlFreeDoc(m_XML_doc_ptr);
  66. xmlCleanupParser();
  67. }
  68. /*
  69. * Returns xmlPtr to testsuite element node, if doesn't exist
  70. * creates one by that name
  71. */
  72. xmlNodePtr TestsXMLResult::GetSuiteElement(const string& suite_name)
  73. {
  74. xmlNodePtr root_node, suite_node, new_child_node;
  75. if (!m_XML_doc_ptr) {
  76. printf("no xml document\n");
  77. return NULL;
  78. }
  79. root_node = xmlDocGetRootElement(m_XML_doc_ptr);
  80. suite_node = xmlFirstElementChild(root_node);
  81. while (suite_node)
  82. {
  83. /* get suite name */
  84. xmlChar *val = xmlGetProp(suite_node, BAD_CAST "name");
  85. /* change xmlCHar* to string */
  86. string node_suite_name(reinterpret_cast<char*>(val));
  87. xmlFree(val); //free val allocated memory
  88. if (node_suite_name == suite_name)
  89. return suite_node;
  90. else suite_node = suite_node->next;
  91. }
  92. /* If we got here no suitable suite name was found,
  93. * so we create a new suite element and return it
  94. */
  95. new_child_node = xmlNewChild(root_node, NULL, BAD_CAST "testsuite", BAD_CAST "");
  96. if (!new_child_node) {
  97. printf("failed creating new XML node\n");
  98. return NULL;
  99. }
  100. xmlSetProp(new_child_node, BAD_CAST "name", BAD_CAST suite_name.c_str());
  101. return xmlGetLastChild(root_node);
  102. }
  103. /*
  104. * Creates new testcase element
  105. */
  106. void TestsXMLResult::AddTestcase(const string &suite_nm, const string &test_nm,
  107. double runtime, bool pass)
  108. {
  109. xmlNodePtr suite_node, new_testcase, fail_node;
  110. ostringstream runtime_str;
  111. if (!suite_nm.size() || !test_nm.size()) {
  112. printf("Input error: suite_nm size %d , test_nm size %d",
  113. suite_nm.size(), test_nm.size());
  114. exit(-1);
  115. }
  116. suite_node = GetSuiteElement(suite_nm);
  117. if (!suite_node) {
  118. printf("failed getting suite element\n");
  119. exit(-1);
  120. }
  121. /* Create new testcase element as son to suite element */
  122. new_testcase = xmlNewChild(suite_node, NULL, BAD_CAST "testcase", NULL);
  123. if (!new_testcase) {
  124. printf("failed creating XML new child for testcase\n");
  125. exit(-1);
  126. }
  127. xmlSetProp(new_testcase, BAD_CAST "name", BAD_CAST test_nm.c_str());
  128. runtime_str << runtime;
  129. xmlSetProp(new_testcase, BAD_CAST "time", BAD_CAST runtime_str.str().c_str());
  130. if (!pass) {
  131. fail_node = xmlNewChild(new_testcase, NULL, BAD_CAST "failure", NULL);
  132. if (!fail_node) {
  133. printf("failed creating fail node\n");
  134. exit(-1);
  135. }
  136. }
  137. }
  138. /*
  139. * Prints the XML tree to file
  140. */
  141. void TestsXMLResult::GenerateXMLReport(void)
  142. {
  143. if (!m_XML_doc_ptr) {
  144. printf("no xml document\n");
  145. return;
  146. }
  147. xmlSaveFormatFileEnc(XUNIT_REPORT_PATH_AND_NAME, m_XML_doc_ptr, "UTF-8", 1);
  148. }
  149. #else /* HAVE_LIBXML */
  150. TestsXMLResult::TestsXMLResult() {}
  151. TestsXMLResult::~TestsXMLResult() {}
  152. void TestsXMLResult::AddTestcase(const string &suite_nm, const string &test_nm,
  153. double runtime, bool pass) {}
  154. void TestsXMLResult::GenerateXMLReport(void)
  155. {
  156. printf("No XML support\n");
  157. }
  158. #endif /* HAVE_LIBXML */
  159. TestManager::TestManager(
  160. const char* nat_mem_type_ptr)
  161. {
  162. m_testList.clear();
  163. m_failedTestsNames.clear();
  164. m_numTestsFailed = 0;
  165. m_numTestsRun = 0;
  166. FetchIPAHwType();
  167. m_nat_mem_type_ptr = nat_mem_type_ptr;
  168. }
  169. ////////////////////////////////////////////////////////////////////////////////////////////
  170. TestManager::~TestManager()
  171. {
  172. m_testList.clear();
  173. }
  174. ////////////////////////////////////////////////////////////////////////////////////////////
  175. TestManager* TestManager::GetInstance(
  176. const char* nat_mem_type_ptr)
  177. {
  178. if (!m_instance) // Only allow one instance of class to be generated.
  179. m_instance = new TestManager(nat_mem_type_ptr);
  180. return m_instance;
  181. }
  182. ////////////////////////////////////////////////////////////////////////////////////////////
  183. void TestManager::Register(TestBase &test)
  184. {
  185. m_testList.push_back(&test);
  186. }
  187. ////////////////////////////////////////////////////////////////////////////////////////////
  188. bool TestManager::Run(vector<string> testSuiteList, vector<string> testNameList)
  189. {
  190. TestBase *test = NULL;
  191. bool pass = true;
  192. vector<string>::iterator testIter;
  193. vector<string>::iterator testSuiteIter;
  194. bool runTest = false;
  195. clock_t begin_test_clk, end_test_clk;
  196. double test_runtime_sec = 0, total_time_sec = 0;
  197. TestsXMLResult xml_res;
  198. if (m_testList.size() == 0)
  199. return false;
  200. /* PrintRegisteredTests(); */
  201. for (unsigned int i = 0 ; i < m_testList.size() ; i++ , runTest = false) {
  202. pass = true;
  203. test = m_testList[i];
  204. // Run only tests from the list of test suites which is stated in the command
  205. // line. In case the list is empty, run all tests.
  206. if (testSuiteList.size() > 0) {
  207. for (unsigned int j = 0; j < test->m_testSuiteName.size(); j++) {
  208. testSuiteIter = find(testSuiteList.begin(), testSuiteList.end(), test->m_testSuiteName[j]);
  209. if (testSuiteIter != testSuiteList.end()) {
  210. runTest = true;
  211. }
  212. }
  213. }
  214. // We also support test by name
  215. if (testNameList.size() > 0) {
  216. testIter = find(testNameList.begin(), testNameList.end(), test->m_name);
  217. if (testIter != testNameList.end())
  218. runTest = true;
  219. }
  220. // Run the test only if it's applicable to the current IPA HW type / version
  221. if (runTest) {
  222. if (!(m_IPAHwType >= test->m_minIPAHwType && m_IPAHwType <= test->m_maxIPAHwType))
  223. runTest = false;
  224. }
  225. if (!runTest)
  226. continue;
  227. printf("\n\nExecuting test %s\n", test->m_name.c_str());
  228. printf("Description: %s\n", test->m_description.c_str());
  229. printf("Setup()\n");
  230. begin_test_clk = clock();
  231. test->SetMemType(GetMemType());
  232. pass &= test->Setup();
  233. //In case the test's setup did not go well it will be a bad idea to try and run it.
  234. if (true == pass)
  235. {
  236. printf("Run()\n");
  237. pass &= test->Run();
  238. }
  239. printf("Teardown()\n");
  240. pass &= test->Teardown();
  241. end_test_clk = clock();
  242. test_runtime_sec = double(end_test_clk - begin_test_clk) / CLOCKS_PER_SEC;
  243. total_time_sec += test_runtime_sec;
  244. if (pass)
  245. {
  246. m_numTestsRun++;
  247. PrintSeparator(test->m_name.size());
  248. printf("Test %s PASSED ! time:%g\n", test->m_name.c_str(), test_runtime_sec);
  249. PrintSeparator(test->m_name.size());
  250. }
  251. else
  252. {
  253. m_numTestsRun++;
  254. m_numTestsFailed++;
  255. m_failedTestsNames.push_back(test->m_name);
  256. PrintSeparator(test->m_name.size());
  257. printf("Test %s FAILED ! time:%g\n", test->m_name.c_str(), test_runtime_sec);
  258. PrintSeparator(test->m_name.size());
  259. }
  260. xml_res.AddTestcase(test->m_testSuiteName[0], test->m_name, test_runtime_sec, pass);
  261. } // for
  262. // Print summary
  263. printf("\n\n");
  264. printf("==================== RESULTS SUMMARY ========================\n");
  265. printf("%zu tests were run, %zu failed, total time:%g.\n", m_numTestsRun, m_numTestsFailed, total_time_sec);
  266. if (0 != m_numTestsFailed) {
  267. printf("Failed tests list:\n");
  268. for (size_t i = 0; i < m_numTestsFailed; i++) {
  269. printf(" %s\n", m_failedTestsNames[i].c_str());
  270. m_failedTestsNames.pop_back();
  271. }
  272. }
  273. printf("=============================================================\n");
  274. xml_res.GenerateXMLReport();
  275. return pass;
  276. }
  277. ////////////////////////////////////////////////////////////////////////////////////////////
  278. void TestManager::PrintSeparator(size_t len)
  279. {
  280. string separator;
  281. for (size_t i = 0; i < len + 15; i++) {
  282. separator += "-";
  283. }
  284. printf("%s\n", separator.c_str());
  285. }
  286. ////////////////////////////////////////////////////////////////////////////////////////////
  287. TestManager::TestManager(TestManager const&)
  288. {
  289. }
  290. ////////////////////////////////////////////////////////////////////////////////////////////
  291. TestManager& TestManager::operator=(TestManager const&)
  292. {
  293. return *m_instance;
  294. }
  295. ////////////////////////////////////////////////////////////////////////////////////////////
  296. void TestManager::PrintRegisteredTests()
  297. {
  298. printf("Test list: (%zu registered)\n", m_testList.size());
  299. for (unsigned int i = 0; i < m_testList.size(); i++) {
  300. printf("%d) name = %s, suite name = %s, regression = %d\n", i, m_testList[i]->m_name.c_str(),
  301. m_testList[i]->m_testSuiteName[0].c_str(), m_testList[i]->m_runInRegression);
  302. }
  303. }
  304. ////////////////////////////////////////////////////////////////////////////////////////////
  305. void TestManager::FetchIPAHwType()
  306. {
  307. int fd;
  308. // Open ipa_test device node
  309. fd = open("/dev/ipa_test" , O_RDONLY);
  310. if (fd < 0) {
  311. printf("Failed opening %s. errno %d: %s\n", "/dev/ipa_test", errno, strerror(errno));
  312. m_IPAHwType = IPA_HW_None;
  313. return;
  314. }
  315. printf("%s(), fd is %d\n", __FUNCTION__, fd);
  316. m_IPAHwType = (enum ipa_hw_type)ioctl(fd, IPA_TEST_IOC_GET_HW_TYPE);
  317. if (-1 == m_IPAHwType) {
  318. printf("%s(), IPA_TEST_IOC_GET_HW_TYPE ioctl failed\n", __FUNCTION__);
  319. m_IPAHwType = IPA_HW_None;
  320. }
  321. printf("%s(), IPA HW type (version) = %d\n", __FUNCTION__, m_IPAHwType);
  322. close(fd);
  323. }
  324. ////////////////////////////////////////////////////////////////////////////////////////////