[dali_2.3.22] Merge branch 'devel/master'
[platform/core/uifw/dali-toolkit.git] / automated-tests / src / dali-toolkit / dali-toolkit-test-utils / test-harness.cpp
index b53630d..4237327 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2020 Samsung Electronics Co., Ltd.
+ * Copyright (c) 2023 Samsung Electronics Co., Ltd.
  *
  * Licensed under the Apache License, Version 2.0 (the "License");
  * you may not use this file except in compliance with the License.
 #include <sys/wait.h>
 #include <testcase.h>
 
-#include <time.h>
+#include <getopt.h>
 #include <unistd.h>
-
+#include <algorithm>
 #include <chrono>
+#include <cstdlib>
 #include <cstring>
+#include <ctime>
+#include <fstream>
 #include <map>
+#include <sstream>
 #include <vector>
 
+using std::chrono::steady_clock;
+using std::chrono::system_clock;
+
 namespace TestHarness
 {
 typedef std::map<int32_t, TestCase> RunningTestCases;
@@ -48,18 +55,197 @@ const char* basename(const char* path)
   return slash;
 }
 
-void SuppressLogOutput()
+std::vector<std::string> Split(const std::string& aString, char delimiter)
 {
-  // Close stdout and stderr to suppress the log output
-  close(STDOUT_FILENO); // File descriptor number for stdout is 1
-  close(STDERR_FILENO); // File descriptor number for stderr is 2
+  std::vector<std::string> tokens;
+  std::string              token;
+  std::istringstream       tokenStream(aString);
+  while(std::getline(tokenStream, token, delimiter))
+  {
+    tokens.push_back(token);
+  }
+  return tokens;
+}
 
-  // The POSIX specification requires that /dev/null must be provided,
-  // The open function always chooses the lowest unused file descriptor
-  // It is sufficient for stdout to be writable.
-  open("/dev/null", O_WRONLY); // Redirect file descriptor number 1 (i.e. stdout) to /dev/null
-  // When stderr is opened it must be both readable and writable.
-  open("/dev/null", O_RDWR); // Redirect file descriptor number 2 (i.e. stderr) to /dev/null
+std::string Join(const std::vector<std::string>& tokens, char delimiter)
+{
+  std::ostringstream oss;
+
+  unsigned int delimiterCount = 0;
+  for(auto& token : tokens)
+  {
+    oss << token;
+    if(delimiterCount < tokens.size() - 1)
+    {
+      oss << delimiter;
+    }
+    ++delimiterCount;
+  }
+  return oss.str();
+}
+
+std::string ChildOutputFilename(int pid)
+{
+  std::ostringstream os;
+  os << "/tmp/tct-child." << pid;
+  return os.str();
+}
+
+std::string TestModuleFilename(const char* processName)
+{
+  auto pathComponents = Split(processName, '/');
+  auto aModule        = pathComponents.back();
+  aModule += "-tests.xml";
+  return aModule;
+}
+
+std::string TestModuleName(const char* processName)
+{
+  auto pathComponents   = Split(processName, '/');
+  auto aModule          = pathComponents.back();
+  auto moduleComponents = Split(aModule, '-');
+
+  moduleComponents[1][0] = std::toupper(moduleComponents[1][0]);
+  moduleComponents[2][0] = std::toupper(moduleComponents[2][0]);
+
+  std::ostringstream oss;
+  for(unsigned int i = 1; i < moduleComponents.size() - 1; ++i) // [0]=tct, [n-1]=core
+  {
+    oss << moduleComponents[i];
+
+    if(i > 1 && i < moduleComponents.size() - 2) // skip first and last delimiter
+    {
+      oss << '-';
+    }
+  }
+
+  return oss.str();
+}
+
+std::string GetWChan(int pid)
+{
+  std::ostringstream procwchan;
+  procwchan << "/proc/" << pid << "/wchan";
+  std::ifstream ifs;
+  ifs.open(procwchan.str(), std::ifstream::in);
+  std::string line;
+  std::getline(ifs, line);
+  ifs.close();
+  return line;
+}
+
+std::string ReadAndEscape(std::string filename)
+{
+  std::ostringstream os;
+  std::ifstream      ifs;
+  ifs.open(filename, std::ifstream::in);
+  while(ifs.good())
+  {
+    std::string line;
+    std::getline(ifs, line);
+    for(auto c : line)
+    {
+      switch(c)
+      {
+        case '<':
+          os << "&lt;";
+          break;
+        case '>':
+          os << "&gt;";
+          break;
+        case '&':
+          os << "&amp;";
+          break;
+        default:
+          os << c;
+          break;
+      }
+    }
+    os << "\\"
+       << "n";
+  }
+  ifs.close();
+  return os.str();
+}
+
+void OutputTestResult(
+  std::ofstream& ofs,
+  const char*    pathToExecutable,
+  std::string    testSuiteName,
+  TestCase&      testCase,
+  std::string    startTime,
+  std::string    endTime)
+{
+  std::string outputFilename = ChildOutputFilename(testCase.childPid);
+  std::string testOutput     = ReadAndEscape(outputFilename);
+
+  ofs << "<testcase component=\"CoreAPI/" << testSuiteName << "/default\" execution_type=\"auto\" id=\""
+      << testCase.name << "\" purpose=\"\" result=\"" << (testCase.result == 0 ? "PASS" : "FAIL") << "\">" << std::endl
+      << "<description><test_script_entry test_script_expected_result=\"0\">"
+      << pathToExecutable << testCase.name << "</test_script_entry>" << std::endl
+      << "</description>"
+      << "<result_info><actual_result>" << (testCase.result == 0 ? "PASS" : "FAIL") << "</actual_result>" << std::endl
+      << "<start>" << startTime << "</start>"
+      << "<end>" << endTime << "</end>"
+      << "<stdout><![CDATA[]]></stdout>"
+      << "<stderr><![CDATA[" << testOutput << "]]></stderr></result_info></testcase>" << std::endl;
+
+  unlink(outputFilename.c_str());
+}
+
+void OutputTestResults(const char* processName, RunningTestCases& children)
+{
+  std::ofstream ofs;
+  std::string   filename   = TestModuleFilename(processName);
+  std::string   moduleName = TestModuleName(processName);
+  ofs.open(filename, std::ofstream::out | std::ofstream::app);
+
+  // Sort completed cases by original test case id
+  std::vector<TestCase> childTestCases;
+  childTestCases.reserve(children.size());
+  for(auto& element : children) childTestCases.push_back(element.second);
+  std::sort(childTestCases.begin(), childTestCases.end(), [](const TestCase& a, const TestCase& b) {
+    return a.testCase < b.testCase;
+  });
+
+  const int BUFSIZE = 256;
+  char      buffer[BUFSIZE];
+  for(auto& testCase : childTestCases)
+  {
+    auto tt = system_clock::to_time_t(testCase.startSystemTime);
+    strftime(buffer, BUFSIZE, "%c", localtime(&tt));
+    std::string startTime(buffer);
+    OutputTestResult(ofs, processName, moduleName, testCase, startTime, startTime);
+  }
+
+  ofs.close();
+}
+
+void OutputStatistics(const char* processName, int32_t numPasses, int32_t numFailures)
+{
+  FILE* fp = fopen("summary.xml", "a");
+  if(fp != NULL)
+  {
+    fprintf(fp,
+            "  <suite name=\"%s-tests\">\n"
+            "    <total_case>%d</total_case>\n"
+            "    <pass_case>%d</pass_case>\n"
+            "    <pass_rate>%5.2f</pass_rate>\n"
+            "    <fail_case>%d</fail_case>\n"
+            "    <fail_rate>%5.2f</fail_rate>\n"
+            "    <block_case>0</block_case>\n"
+            "    <block_rate>0.00</block_rate>\n"
+            "    <na_case>0</na_case>\n"
+            "    <na_rate>0.00</na_rate>\n"
+            "  </suite>\n",
+            basename(processName),
+            numPasses + numFailures,
+            numPasses,
+            numPasses > 0 ? (float)numPasses * 100.0f / (numPasses + numFailures) : 0.0f,
+            numFailures,
+            numPasses > 0 ? (float)numFailures * 100.0f / (numPasses + numFailures) : 0.0f);
+    fclose(fp);
+  }
 }
 
 int32_t RunTestCase(struct ::testcase_s& testCase)
@@ -89,35 +275,55 @@ int32_t RunTestCase(struct ::testcase_s& testCase)
   return result;
 }
 
-int32_t RunTestCaseInChildProcess(struct ::testcase_s& testCase, bool suppressOutput)
+int32_t RunTestCaseRedirectOutput(TestCase& testCase, bool suppressOutput)
+{
+  // Executing in child process
+  // Close stdout and stderr to suppress the log output
+  close(STDOUT_FILENO); // File descriptor number for stdout is 1
+
+  // The POSIX specification requires that /dev/null must be provided,
+  // The open function always chooses the lowest unused file descriptor
+  // It is sufficient for stdout to be writable.
+  open("/dev/null", O_WRONLY); // Redirect file descriptor number 1 (i.e. stdout) to /dev/null
+
+  fflush(stderr);
+  close(STDERR_FILENO);
+  if(suppressOutput)
+  {
+    stderr = fopen("/dev/null", "w+"); // Redirect fd 2 to /dev/null
+  }
+  else
+  {
+    // When stderr is opened it must be both readable and writable.
+    std::string childOutputFilename = ChildOutputFilename(getpid());
+    stderr                          = fopen(childOutputFilename.c_str(), "w+");
+  }
+
+  int32_t status = RunTestCase(*testCase.tctPtr);
+
+  fflush(stderr);
+  fclose(stderr);
+
+  return status;
+}
+
+int32_t RunTestCaseInChildProcess(TestCase& testCase, bool redirect)
 {
   int32_t testResult = EXIT_STATUS_TESTCASE_FAILED;
 
   int32_t pid = fork();
   if(pid == 0) // Child process
   {
-    if(suppressOutput)
+    if(redirect)
     {
-      SuppressLogOutput();
+      int status = RunTestCaseRedirectOutput(testCase, false);
+      exit(status);
     }
     else
     {
-      printf("\n");
-      for(int32_t i = 0; i < 80; ++i) printf("#");
-      printf("\nTC: %s\n", testCase.name);
-      fflush(stdout);
-    }
-
-    int32_t status = RunTestCase(testCase);
-
-    if(!suppressOutput)
-    {
-      fflush(stdout);
-      fflush(stderr);
-      fclose(stdout);
-      fclose(stderr);
+      int status = RunTestCase(*testCase.tctPtr);
+      exit(status);
     }
-    exit(status);
   }
   else if(pid == -1)
   {
@@ -126,8 +332,9 @@ int32_t RunTestCaseInChildProcess(struct ::testcase_s& testCase, bool suppressOu
   }
   else // Parent process
   {
-    int32_t status   = 0;
-    int32_t childPid = waitpid(pid, &status, 0);
+    int32_t status    = 0;
+    int32_t childPid  = waitpid(pid, &status, 0);
+    testCase.childPid = childPid;
     if(childPid == -1)
     {
       perror("waitpid");
@@ -167,51 +374,57 @@ int32_t RunTestCaseInChildProcess(struct ::testcase_s& testCase, bool suppressOu
   return testResult;
 }
 
-void OutputStatistics(const char* processName, int32_t numPasses, int32_t numFailures)
-{
-  FILE* fp = fopen("summary.xml", "a");
-  if(fp != NULL)
-  {
-    fprintf(fp,
-            "  <suite name=\"%s\">\n"
-            "    <total_case>%d</total_case>\n"
-            "    <pass_case>%d</pass_case>\n"
-            "    <pass_rate>%5.2f</pass_rate>\n"
-            "    <fail_case>%d</fail_case>\n"
-            "    <fail_rate>%5.2f</fail_rate>\n"
-            "    <block_case>0</block_case>\n"
-            "    <block_rate>0.00</block_rate>\n"
-            "    <na_case>0</na_case>\n"
-            "    <na_rate>0.00</na_rate>\n"
-            "  </suite>\n",
-            basename(processName),
-            numPasses + numFailures,
-            numPasses,
-            (float)numPasses / (numPasses + numFailures),
-            numFailures,
-            (float)numFailures / (numPasses + numFailures));
-    fclose(fp);
-  }
-}
-
-int32_t RunAll(const char* processName, ::testcase tc_array[])
+int32_t RunAll(const char* processName, ::testcase tc_array[], std::string match, bool quiet)
 {
-  int32_t numFailures = 0;
-  int32_t numPasses   = 0;
-
-  // Run test cases in child process( to kill output/handle signals ), but run serially.
+  int32_t       numFailures = 0;
+  int32_t       numPasses   = 0;
+  std::ofstream ofs;
+  std::string   filename   = TestModuleFilename(processName);
+  std::string   moduleName = TestModuleName(processName);
+  ofs.open(filename, std::ofstream::out | std::ofstream::app);
+  const int BUFSIZE = 256;
+  char      buffer[BUFSIZE];
+
+  // Run test cases in child process( to handle signals ), but run serially.
   for(uint32_t i = 0; tc_array[i].name; i++)
   {
-    int32_t result = RunTestCaseInChildProcess(tc_array[i], false);
-    if(result == 0)
+    auto tt = system_clock::to_time_t(system_clock::now());
+    strftime(buffer, BUFSIZE, "%c", localtime(&tt));
+    std::string startTime(buffer);
+
+    TestCase testCase(i, &tc_array[i]);
+    bool     run = true;
+    if(!match.empty())
     {
-      numPasses++;
+      if(match.compare(0, match.size(), testCase.name, match.size()))
+      {
+        run = false;
+      }
     }
-    else
+
+    if(run)
     {
-      numFailures++;
+      testCase.result = RunTestCaseInChildProcess(testCase, quiet);
+
+      tt = system_clock::to_time_t(system_clock::now());
+      strftime(buffer, BUFSIZE, "%c", localtime(&tt));
+      std::string endTime(buffer);
+
+      if(testCase.result == 0)
+      {
+        numPasses++;
+      }
+      else
+      {
+        numFailures++;
+      }
+      if(!quiet)
+      {
+        OutputTestResult(ofs, processName, moduleName, testCase, startTime, endTime);
+      }
     }
   }
+  ofs.close();
 
   OutputStatistics(processName, numPasses, numFailures);
 
@@ -219,7 +432,7 @@ int32_t RunAll(const char* processName, ::testcase tc_array[])
 }
 
 // Constantly runs up to MAX_NUM_CHILDREN processes
-int32_t RunAllInParallel(const char* processName, ::testcase tc_array[], bool reRunFailed)
+int32_t RunAllInParallel(const char* processName, ::testcase tc_array[], std::string match, bool reRunFailed, bool quiet)
 {
   int32_t numFailures = 0;
   int32_t numPasses   = 0;
@@ -238,31 +451,54 @@ int32_t RunAllInParallel(const char* processName, ::testcase tc_array[], bool re
     // Create more children (up to the max number or til the end of the array)
     while(numRunningChildren < MAX_NUM_CHILDREN && tc_array[nextTestCase].name)
     {
-      int32_t pid = fork();
-      if(pid == 0) // Child process
+      bool run = true;
+      if(!match.empty())
       {
-        SuppressLogOutput();
-        exit(RunTestCase(tc_array[nextTestCase]));
+        if(match.compare(0, match.size(), tc_array[nextTestCase].name, match.size()))
+        {
+          run = false;
+        }
       }
-      else if(pid == -1)
+      if(run)
       {
-        perror("fork");
-        exit(EXIT_STATUS_FORK_FAILED);
+        int32_t pid = fork();
+        if(pid == 0) // Child process
+        {
+          TestCase testCase(nextTestCase, &tc_array[nextTestCase]);
+          int      status = RunTestCaseRedirectOutput(testCase, quiet);
+          exit(status);
+        }
+        else if(pid == -1)
+        {
+          perror("fork");
+          exit(EXIT_STATUS_FORK_FAILED);
+        }
+        else // Parent process
+        {
+          TestCase tc(nextTestCase, tc_array[nextTestCase].name);
+          tc.startTime       = steady_clock::now();
+          tc.startSystemTime = system_clock::now();
+          tc.childPid        = pid;
+
+          children[pid] = tc;
+          nextTestCase++;
+          numRunningChildren++;
+        }
       }
-      else // Parent process
+      else
       {
-        TestCase tc(nextTestCase, tc_array[nextTestCase].name);
-        tc.startTime = std::chrono::steady_clock::now();
-
-        children[pid] = tc;
         nextTestCase++;
-        numRunningChildren++;
       }
     }
 
-    // Check to see if any children have finished yet
     int32_t status   = 0;
-    int32_t childPid = waitpid(-1, &status, WNOHANG);
+    int32_t childPid = 0;
+    if(numRunningChildren > 0) // Only wait if there are running children.
+    {
+      // Check to see if any children have finished yet
+      childPid = waitpid(-1, &status, WNOHANG);
+    }
+
     if(childPid == 0)
     {
       // No children have finished.
@@ -272,11 +508,21 @@ int32_t RunAllInParallel(const char* processName, ::testcase tc_array[], bool re
       for(auto& tc : children)
       {
         std::chrono::steady_clock::duration timeSpan = endTime - tc.second.startTime;
-        std::chrono::duration<double>       seconds  = std::chrono::duration_cast<std::chrono::duration<double>>(timeSpan);
-        if(seconds.count() > MAXIMUM_CHILD_LIFETIME)
+        double                              seconds  = double(timeSpan.count()) * std::chrono::steady_clock::period::num / std::chrono::steady_clock::period::den;
+
+        if(4.9999 < seconds && seconds < 5 && !tc.second.finished)
+        {
+          printf("Child process %s is delayed: WCHAN:%s\n", tc.second.name, GetWChan(tc.first).c_str());
+        }
+        else if(seconds > MAXIMUM_CHILD_LIFETIME)
         {
           // Kill the child process. A subsequent call to waitpid will process signal result below.
-          kill(tc.first, SIGKILL);
+          if(!tc.second.finished)
+          {
+            printf("Child process %s WCHAN:%s\n", tc.second.name, GetWChan(tc.first).c_str());
+            kill(tc.first, SIGKILL);
+            tc.second.finished = true; // Only send kill signal once.
+          }
         }
       }
     }
@@ -289,11 +535,13 @@ int32_t RunAllInParallel(const char* processName, ::testcase tc_array[], bool re
     {
       if(WIFEXITED(status))
       {
-        int32_t testResult = WEXITSTATUS(status);
-        if(testResult)
+        auto& testCase    = children[childPid];
+        testCase.result   = WEXITSTATUS(status);
+        testCase.finished = true;
+        if(testCase.result)
         {
-          printf("Test case %s failed: %d\n", children[childPid].testCaseName, testResult);
-          failedTestCases.push_back(children[childPid].testCase);
+          printf("Test case %s failed: %d\n", testCase.name, testCase.result);
+          failedTestCases.push_back(testCase.testCase);
           numFailures++;
         }
         else
@@ -310,7 +558,9 @@ int32_t RunAllInParallel(const char* processName, ::testcase tc_array[], bool re
         RunningTestCases::iterator iter = children.find(childPid);
         if(iter != children.end())
         {
-          printf("Test case %s exited with signal %s\n", iter->second.testCaseName, strsignal(status));
+          iter->second.finished = true;
+          printf("Test case %s exited with signal %s\n", iter->second.name, strsignal(status));
+          iter->second.result = 1;
           failedTestCases.push_back(iter->second.testCase);
         }
         else
@@ -324,6 +574,11 @@ int32_t RunAllInParallel(const char* processName, ::testcase tc_array[], bool re
     }
   }
 
+  if(!quiet)
+  {
+    OutputTestResults(processName, children);
+  }
+
   OutputStatistics(processName, numPasses, numFailures);
 
   if(reRunFailed)
@@ -338,7 +593,9 @@ int32_t RunAllInParallel(const char* processName, ::testcase tc_array[], bool re
         printf("=");
       }
       printf("\n");
-      RunTestCaseInChildProcess(tc_array[failedTestCases[i]], false);
+      int      index = failedTestCases[i];
+      TestCase testCase(index, &tc_array[index]);
+      RunTestCaseInChildProcess(testCase, false);
     }
   }
 
@@ -368,11 +625,66 @@ void Usage(const char* program)
     "   %s <testcase name>\t\t Execute a test case\n"
     "   %s \t\t Execute all test cases in parallel\n"
     "   %s -r\t\t Execute all test cases in parallel, rerunning failed test cases\n"
-    "   %s -s\t\t Execute all test cases serially\n",
+    "   %s -s\t\t Execute all test cases serially\n"
+    "   %s -q\t\t Run without output\n",
+    program,
     program,
     program,
     program,
     program);
 }
 
+int RunTests(int argc, char* const argv[], ::testcase tc_array[])
+{
+  int         result    = TestHarness::EXIT_STATUS_BAD_ARGUMENT;
+  const char* optString = "sfqm:";
+  bool        optRerunFailed(true);
+  bool        optRunSerially(false);
+  bool        optQuiet(false);
+  std::string optMatch;
+
+  int nextOpt = 0;
+  do
+  {
+    nextOpt = getopt(argc, argv, optString);
+    switch(nextOpt)
+    {
+      case 'f':
+        optRerunFailed = false;
+        break;
+      case 's':
+        optRunSerially = true;
+        break;
+      case 'q':
+        optQuiet = true;
+        break;
+      case 'm':
+        optMatch = optarg;
+        break;
+      case '?':
+        TestHarness::Usage(argv[0]);
+        exit(TestHarness::EXIT_STATUS_BAD_ARGUMENT);
+        break;
+    }
+  } while(nextOpt != -1);
+
+  if(optind == argc) // no testcase name in argument list
+  {
+    if(optRunSerially)
+    {
+      result = TestHarness::RunAll(argv[0], tc_array, optMatch, optQuiet);
+    }
+    else
+    {
+      result = TestHarness::RunAllInParallel(argv[0], tc_array, optMatch, optRerunFailed, optQuiet);
+    }
+  }
+  else
+  {
+    // optind is index of next argument - interpret as testcase name
+    result = TestHarness::FindAndRunTestCase(tc_array, argv[optind]);
+  }
+  return result;
+}
+
 } // namespace TestHarness