123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396 |
- #include <stdio.h>
- #include <algorithm> // std::find
- #include <vector> // std::vector
- #include <string>
- #include <errno.h>
- #include <ctime>
- #include <sstream>
- #include "TestManager.h"
- #include "TestsUtils.h"
- #include <fcntl.h>
- #include <unistd.h>
- #include "ipa_test_module.h"
- #include <sys/ioctl.h>
- using namespace std;
- TestManager* TestManager::m_instance = NULL;
- #ifdef HAVE_LIBXML
- TestsXMLResult::TestsXMLResult()
- {
- xmlNodePtr node;
-
- m_XML_doc_ptr = xmlNewDoc(BAD_CAST "1.0");
- if (m_XML_doc_ptr == NULL){
- printf("error on allocation xml doc\n");
- exit(-1);
- }
- node = xmlNewNode(NULL, BAD_CAST "testsuites");
- if (!node) {
- printf("failed to allocate XML node\n");
- exit (-1);
- }
- xmlDocSetRootElement(m_XML_doc_ptr, node);
- }
- TestsXMLResult::~TestsXMLResult()
- {
- if (m_XML_doc_ptr)
- xmlFreeDoc(m_XML_doc_ptr);
- xmlCleanupParser();
- }
- xmlNodePtr TestsXMLResult::GetSuiteElement(const string& suite_name)
- {
- xmlNodePtr root_node, suite_node, new_child_node;
- if (!m_XML_doc_ptr) {
- printf("no xml document\n");
- return NULL;
- }
- root_node = xmlDocGetRootElement(m_XML_doc_ptr);
- suite_node = xmlFirstElementChild(root_node);
- while (suite_node)
- {
-
- xmlChar *val = xmlGetProp(suite_node, BAD_CAST "name");
-
- string node_suite_name(reinterpret_cast<char*>(val));
- xmlFree(val);
- if (node_suite_name == suite_name)
- return suite_node;
- else suite_node = suite_node->next;
- }
-
- new_child_node = xmlNewChild(root_node, NULL, BAD_CAST "testsuite", BAD_CAST "");
- if (!new_child_node) {
- printf("failed creating new XML node\n");
- return NULL;
- }
- xmlSetProp(new_child_node, BAD_CAST "name", BAD_CAST suite_name.c_str());
- return xmlGetLastChild(root_node);
- }
- void TestsXMLResult::AddTestcase(const string &suite_nm, const string &test_nm,
- double runtime, bool pass)
- {
- xmlNodePtr suite_node, new_testcase, fail_node;
- ostringstream runtime_str;
- if (!suite_nm.size() || !test_nm.size()) {
- printf("Input error: suite_nm size %d , test_nm size %d",
- suite_nm.size(), test_nm.size());
- exit(-1);
- }
- suite_node = GetSuiteElement(suite_nm);
- if (!suite_node) {
- printf("failed getting suite element\n");
- exit(-1);
- }
-
- new_testcase = xmlNewChild(suite_node, NULL, BAD_CAST "testcase", NULL);
- if (!new_testcase) {
- printf("failed creating XML new child for testcase\n");
- exit(-1);
- }
- xmlSetProp(new_testcase, BAD_CAST "name", BAD_CAST test_nm.c_str());
- runtime_str << runtime;
- xmlSetProp(new_testcase, BAD_CAST "time", BAD_CAST runtime_str.str().c_str());
- if (!pass) {
- fail_node = xmlNewChild(new_testcase, NULL, BAD_CAST "failure", NULL);
- if (!fail_node) {
- printf("failed creating fail node\n");
- exit(-1);
- }
- }
- }
- void TestsXMLResult::GenerateXMLReport(void)
- {
- if (!m_XML_doc_ptr) {
- printf("no xml document\n");
- return;
- }
- xmlSaveFormatFileEnc(XUNIT_REPORT_PATH_AND_NAME, m_XML_doc_ptr, "UTF-8", 1);
- }
- #else
- TestsXMLResult::TestsXMLResult() {}
- TestsXMLResult::~TestsXMLResult() {}
- void TestsXMLResult::AddTestcase(const string &suite_nm, const string &test_nm,
- double runtime, bool pass) {}
- void TestsXMLResult::GenerateXMLReport(void)
- {
- printf("No XML support\n");
- }
- #endif
- TestManager::TestManager(
- const char* nat_mem_type_ptr)
- {
- m_testList.clear();
- m_failedTestsNames.clear();
- m_numTestsFailed = 0;
- m_numTestsRun = 0;
- FetchIPAHwType();
- m_nat_mem_type_ptr = nat_mem_type_ptr;
- }
- TestManager::~TestManager()
- {
- m_testList.clear();
- }
- TestManager* TestManager::GetInstance(
- const char* nat_mem_type_ptr)
- {
- if (!m_instance)
- m_instance = new TestManager(nat_mem_type_ptr);
- return m_instance;
- }
- void TestManager::Register(TestBase &test)
- {
- m_testList.push_back(&test);
- }
- bool TestManager::Run(vector<string> testSuiteList, vector<string> testNameList)
- {
- TestBase *test = NULL;
- bool pass = true;
- vector<string>::iterator testIter;
- vector<string>::iterator testSuiteIter;
- bool runTest = false;
- clock_t begin_test_clk, end_test_clk;
- double test_runtime_sec = 0, total_time_sec = 0;
- TestsXMLResult xml_res;
- if (m_testList.size() == 0)
- return false;
-
- for (unsigned int i = 0 ; i < m_testList.size() ; i++ , runTest = false) {
- pass = true;
- test = m_testList[i];
-
-
- if (testSuiteList.size() > 0) {
- for (unsigned int j = 0; j < test->m_testSuiteName.size(); j++) {
- testSuiteIter = find(testSuiteList.begin(), testSuiteList.end(), test->m_testSuiteName[j]);
- if (testSuiteIter != testSuiteList.end()) {
- runTest = true;
- }
- }
- }
-
- if (testNameList.size() > 0) {
- testIter = find(testNameList.begin(), testNameList.end(), test->m_name);
- if (testIter != testNameList.end())
- runTest = true;
- }
-
- if (runTest) {
- if (!(m_IPAHwType >= test->m_minIPAHwType && m_IPAHwType <= test->m_maxIPAHwType))
- runTest = false;
- }
- if (!runTest)
- continue;
- printf("\n\nExecuting test %s\n", test->m_name.c_str());
- printf("Description: %s\n", test->m_description.c_str());
- printf("Setup()\n");
- begin_test_clk = clock();
- test->SetMemType(GetMemType());
- pass &= test->Setup();
-
- if (true == pass)
- {
- printf("Run()\n");
- pass &= test->Run();
- }
- printf("Teardown()\n");
- pass &= test->Teardown();
- end_test_clk = clock();
- test_runtime_sec = double(end_test_clk - begin_test_clk) / CLOCKS_PER_SEC;
- total_time_sec += test_runtime_sec;
- if (pass)
- {
- m_numTestsRun++;
- PrintSeparator(test->m_name.size());
- printf("Test %s PASSED ! time:%g\n", test->m_name.c_str(), test_runtime_sec);
- PrintSeparator(test->m_name.size());
- }
- else
- {
- m_numTestsRun++;
- m_numTestsFailed++;
- m_failedTestsNames.push_back(test->m_name);
- PrintSeparator(test->m_name.size());
- printf("Test %s FAILED ! time:%g\n", test->m_name.c_str(), test_runtime_sec);
- PrintSeparator(test->m_name.size());
- }
- xml_res.AddTestcase(test->m_testSuiteName[0], test->m_name, test_runtime_sec, pass);
- }
-
- printf("\n\n");
- printf("==================== RESULTS SUMMARY ========================\n");
- printf("%zu tests were run, %zu failed, total time:%g.\n", m_numTestsRun, m_numTestsFailed, total_time_sec);
- if (0 != m_numTestsFailed) {
- printf("Failed tests list:\n");
- for (size_t i = 0; i < m_numTestsFailed; i++) {
- printf(" %s\n", m_failedTestsNames[i].c_str());
- m_failedTestsNames.pop_back();
- }
- }
- printf("=============================================================\n");
- xml_res.GenerateXMLReport();
- return pass;
- }
- void TestManager::PrintSeparator(size_t len)
- {
- string separator;
- for (size_t i = 0; i < len + 15; i++) {
- separator += "-";
- }
- printf("%s\n", separator.c_str());
- }
- TestManager::TestManager(TestManager const&)
- {
- }
- TestManager& TestManager::operator=(TestManager const&)
- {
- return *m_instance;
- }
- void TestManager::PrintRegisteredTests()
- {
- printf("Test list: (%zu registered)\n", m_testList.size());
- for (unsigned int i = 0; i < m_testList.size(); i++) {
- printf("%d) name = %s, suite name = %s, regression = %d\n", i, m_testList[i]->m_name.c_str(),
- m_testList[i]->m_testSuiteName[0].c_str(), m_testList[i]->m_runInRegression);
- }
- }
- void TestManager::FetchIPAHwType()
- {
- int fd;
-
- fd = open("/dev/ipa_test" , O_RDONLY);
- if (fd < 0) {
- printf("Failed opening %s. errno %d: %s\n", "/dev/ipa_test", errno, strerror(errno));
- m_IPAHwType = IPA_HW_None;
- return;
- }
- printf("%s(), fd is %d\n", __FUNCTION__, fd);
- m_IPAHwType = (enum ipa_hw_type)ioctl(fd, IPA_TEST_IOC_GET_HW_TYPE);
- if (-1 == m_IPAHwType) {
- printf("%s(), IPA_TEST_IOC_GET_HW_TYPE ioctl failed\n", __FUNCTION__);
- m_IPAHwType = IPA_HW_None;
- }
- printf("%s(), IPA HW type (version) = %d\n", __FUNCTION__, m_IPAHwType);
- close(fd);
- }
|