[dali_2.3.20] Merge branch 'devel/master'
[platform/core/uifw/dali-toolkit.git] / automated-tests / src / dali-toolkit / dali-toolkit-test-utils / test-harness.cpp
1 /*
2  * Copyright (c) 2021 Samsung Electronics Co., Ltd.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 #include "test-harness.h"
18
19 #include <fcntl.h>
20 #include <stdlib.h>
21 #include <sys/types.h>
22 #include <sys/wait.h>
23 #include <testcase.h>
24
25 #include <getopt.h>
26 #include <unistd.h>
27 #include <algorithm>
28 #include <chrono>
29 #include <cstdlib>
30 #include <cstring>
31 #include <ctime>
32 #include <fstream>
33 #include <map>
34 #include <sstream>
35 #include <vector>
36
37 using std::chrono::steady_clock;
38 using std::chrono::system_clock;
39
40 namespace TestHarness
41 {
42 typedef std::map<int32_t, TestCase> RunningTestCases;
43
44 const double MAXIMUM_CHILD_LIFETIME(60.0f); // 1 minute
45
46 const char* basename(const char* path)
47 {
48   const char* ptr   = path;
49   const char* slash = NULL;
50   for(; *ptr != '\0'; ++ptr)
51   {
52     if(*ptr == '/') slash = ptr;
53   }
54   if(slash != NULL) ++slash;
55   return slash;
56 }
57
58 std::vector<std::string> Split(const std::string& aString, char delimiter)
59 {
60   std::vector<std::string> tokens;
61   std::string              token;
62   std::istringstream       tokenStream(aString);
63   while(std::getline(tokenStream, token, delimiter))
64   {
65     tokens.push_back(token);
66   }
67   return tokens;
68 }
69
70 std::string Join(const std::vector<std::string>& tokens, char delimiter)
71 {
72   std::ostringstream oss;
73
74   unsigned int delimiterCount = 0;
75   for(auto& token : tokens)
76   {
77     oss << token;
78     if(delimiterCount < tokens.size() - 1)
79     {
80       oss << delimiter;
81     }
82     ++delimiterCount;
83   }
84   return oss.str();
85 }
86
87 std::string ChildOutputFilename(int pid)
88 {
89   std::ostringstream os;
90   os << "/tmp/tct-child." << pid;
91   return os.str();
92 }
93
94 std::string TestModuleFilename(const char* processName)
95 {
96   auto pathComponents = Split(processName, '/');
97   auto aModule        = pathComponents.back();
98   aModule += "-tests.xml";
99   return aModule;
100 }
101
102 std::string TestModuleName(const char* processName)
103 {
104   auto pathComponents   = Split(processName, '/');
105   auto aModule          = pathComponents.back();
106   auto moduleComponents = Split(aModule, '-');
107
108   moduleComponents[1][0] = std::toupper(moduleComponents[1][0]);
109   moduleComponents[2][0] = std::toupper(moduleComponents[2][0]);
110
111   std::ostringstream oss;
112   for(unsigned int i = 1; i < moduleComponents.size() - 1; ++i) // [0]=tct, [n-1]=core
113   {
114     oss << moduleComponents[i];
115
116     if(i > 1 && i < moduleComponents.size() - 2) // skip first and last delimiter
117     {
118       oss << '-';
119     }
120   }
121
122   return oss.str();
123 }
124
125 std::string ReadAndEscape(std::string filename)
126 {
127   std::ostringstream os;
128   std::ifstream      ifs;
129   ifs.open(filename, std::ifstream::in);
130   while(ifs.good())
131   {
132     std::string line;
133     std::getline(ifs, line);
134     for(auto c : line)
135     {
136       switch(c)
137       {
138         case '<':
139           os << "&lt;";
140           break;
141         case '>':
142           os << "&gt;";
143           break;
144         case '&':
145           os << "&amp;";
146           break;
147         default:
148           os << c;
149           break;
150       }
151     }
152     os << "\\"
153        << "n";
154   }
155   ifs.close();
156   return os.str();
157 }
158
159 void OutputTestResult(
160   std::ofstream& ofs,
161   const char*    pathToExecutable,
162   std::string    testSuiteName,
163   TestCase&      testCase,
164   std::string    startTime,
165   std::string    endTime)
166 {
167   std::string outputFilename = ChildOutputFilename(testCase.childPid);
168   std::string testOutput     = ReadAndEscape(outputFilename);
169
170   ofs << "<testcase component=\"CoreAPI/" << testSuiteName << "/default\" execution_type=\"auto\" id=\""
171       << testCase.name << "\" purpose=\"\" result=\"" << (testCase.result == 0 ? "PASS" : "FAIL") << "\">" << std::endl
172       << "<description><test_script_entry test_script_expected_result=\"0\">"
173       << pathToExecutable << testCase.name << "</test_script_entry>" << std::endl
174       << "</description>"
175       << "<result_info><actual_result>" << (testCase.result == 0 ? "PASS" : "FAIL") << "</actual_result>" << std::endl
176       << "<start>" << startTime << "</start>"
177       << "<end>" << endTime << "</end>"
178       << "<stdout><![CDATA[]]></stdout>"
179       << "<stderr><![CDATA[" << testOutput << "]]></stderr></result_info></testcase>" << std::endl;
180
181   unlink(outputFilename.c_str());
182 }
183
184 void OutputTestResults(const char* processName, RunningTestCases& children)
185 {
186   std::ofstream ofs;
187   std::string   filename   = TestModuleFilename(processName);
188   std::string   moduleName = TestModuleName(processName);
189   ofs.open(filename, std::ofstream::out | std::ofstream::app);
190
191   // Sort completed cases by original test case id
192   std::vector<TestCase> childTestCases;
193   childTestCases.reserve(children.size());
194   for(auto& element : children) childTestCases.push_back(element.second);
195   std::sort(childTestCases.begin(), childTestCases.end(), [](const TestCase& a, const TestCase& b) {
196     return a.testCase < b.testCase;
197   });
198
199   const int BUFSIZE = 256;
200   char      buffer[BUFSIZE];
201   for(auto& testCase : childTestCases)
202   {
203     auto tt = system_clock::to_time_t(testCase.startSystemTime);
204     strftime(buffer, BUFSIZE, "%c", localtime(&tt));
205     std::string startTime(buffer);
206     OutputTestResult(ofs, processName, moduleName, testCase, startTime, startTime);
207   }
208
209   ofs.close();
210 }
211
212 void OutputStatistics(const char* processName, int32_t numPasses, int32_t numFailures)
213 {
214   FILE* fp = fopen("summary.xml", "a");
215   if(fp != NULL)
216   {
217     fprintf(fp,
218             "  <suite name=\"%s-tests\">\n"
219             "    <total_case>%d</total_case>\n"
220             "    <pass_case>%d</pass_case>\n"
221             "    <pass_rate>%5.2f</pass_rate>\n"
222             "    <fail_case>%d</fail_case>\n"
223             "    <fail_rate>%5.2f</fail_rate>\n"
224             "    <block_case>0</block_case>\n"
225             "    <block_rate>0.00</block_rate>\n"
226             "    <na_case>0</na_case>\n"
227             "    <na_rate>0.00</na_rate>\n"
228             "  </suite>\n",
229             basename(processName),
230             numPasses + numFailures,
231             numPasses,
232             (float)numPasses * 100.0f / (numPasses + numFailures),
233             numFailures,
234             (float)numFailures * 100.0f / (numPasses + numFailures));
235     fclose(fp);
236   }
237 }
238
239 int32_t RunTestCase(struct ::testcase_s& testCase)
240 {
241   int32_t result = EXIT_STATUS_TESTCASE_FAILED;
242
243   // dont want to catch exception as we want to be able to get
244   // gdb stack trace from the first error
245   // by default tests should all always pass with no exceptions
246   if(testCase.startup)
247   {
248     testCase.startup();
249   }
250   try
251   {
252     result = testCase.function();
253   }
254   catch(const char*)
255   {
256     // just catch test fail exception, return is already set to EXIT_STATUS_TESTCASE_FAILED
257   }
258   if(testCase.cleanup)
259   {
260     testCase.cleanup();
261   }
262
263   return result;
264 }
265
266 int32_t RunTestCaseRedirectOutput(TestCase& testCase, bool suppressOutput)
267 {
268   // Executing in child process
269   // Close stdout and stderr to suppress the log output
270   close(STDOUT_FILENO); // File descriptor number for stdout is 1
271
272   // The POSIX specification requires that /dev/null must be provided,
273   // The open function always chooses the lowest unused file descriptor
274   // It is sufficient for stdout to be writable.
275   open("/dev/null", O_WRONLY); // Redirect file descriptor number 1 (i.e. stdout) to /dev/null
276
277   fflush(stderr);
278   close(STDERR_FILENO);
279   if(suppressOutput)
280   {
281     stderr = fopen("/dev/null", "w+"); // Redirect fd 2 to /dev/null
282   }
283   else
284   {
285     // When stderr is opened it must be both readable and writable.
286     std::string childOutputFilename = ChildOutputFilename(getpid());
287     stderr                          = fopen(childOutputFilename.c_str(), "w+");
288   }
289
290   int32_t status = RunTestCase(*testCase.tctPtr);
291
292   fflush(stderr);
293   fclose(stderr);
294
295   return status;
296 }
297
298 int32_t RunTestCaseInChildProcess(TestCase& testCase, bool redirect)
299 {
300   int32_t testResult = EXIT_STATUS_TESTCASE_FAILED;
301
302   int32_t pid = fork();
303   if(pid == 0) // Child process
304   {
305     if(redirect)
306     {
307       int status = RunTestCaseRedirectOutput(testCase, false);
308       exit(status);
309     }
310     else
311     {
312       int status = RunTestCase(*testCase.tctPtr);
313       exit(status);
314     }
315   }
316   else if(pid == -1)
317   {
318     perror("fork");
319     exit(EXIT_STATUS_FORK_FAILED);
320   }
321   else // Parent process
322   {
323     int32_t status    = 0;
324     int32_t childPid  = waitpid(pid, &status, 0);
325     testCase.childPid = childPid;
326     if(childPid == -1)
327     {
328       perror("waitpid");
329       exit(EXIT_STATUS_WAITPID_FAILED);
330     }
331     if(WIFEXITED(status))
332     {
333       if(childPid > 0)
334       {
335         testResult = WEXITSTATUS(status);
336         if(testResult)
337         {
338           printf("Test case %s failed: %d\n", testCase.name, testResult);
339         }
340       }
341     }
342     else if(WIFSIGNALED(status))
343     {
344       int32_t signal = WTERMSIG(status);
345       testResult     = EXIT_STATUS_TESTCASE_ABORTED;
346       if(signal == SIGABRT)
347       {
348         printf("Test case %s failed: test case asserted\n", testCase.name);
349       }
350       else
351       {
352         printf("Test case %s failed: exit with signal %s\n", testCase.name, strsignal(WTERMSIG(status)));
353       }
354     }
355     else if(WIFSTOPPED(status))
356     {
357       printf("Test case %s failed: stopped with signal %s\n", testCase.name, strsignal(WSTOPSIG(status)));
358     }
359   }
360   fflush(stdout);
361   fflush(stderr);
362   return testResult;
363 }
364
365 int32_t RunAll(const char* processName, ::testcase tc_array[], bool quiet)
366 {
367   int32_t       numFailures = 0;
368   int32_t       numPasses   = 0;
369   std::ofstream ofs;
370   std::string   filename   = TestModuleFilename(processName);
371   std::string   moduleName = TestModuleName(processName);
372   ofs.open(filename, std::ofstream::out | std::ofstream::app);
373   const int BUFSIZE = 256;
374   char      buffer[BUFSIZE];
375
376   // Run test cases in child process( to handle signals ), but run serially.
377   for(uint32_t i = 0; tc_array[i].name; i++)
378   {
379     auto tt = system_clock::to_time_t(system_clock::now());
380     strftime(buffer, BUFSIZE, "%c", localtime(&tt));
381     std::string startTime(buffer);
382
383     TestCase testCase(i, &tc_array[i]);
384     testCase.result = RunTestCaseInChildProcess(testCase, quiet);
385
386     tt = system_clock::to_time_t(system_clock::now());
387     strftime(buffer, BUFSIZE, "%c", localtime(&tt));
388     std::string endTime(buffer);
389
390     if(testCase.result == 0)
391     {
392       numPasses++;
393     }
394     else
395     {
396       numFailures++;
397     }
398     if(!quiet)
399     {
400       OutputTestResult(ofs, processName, moduleName, testCase, startTime, endTime);
401     }
402   }
403   ofs.close();
404
405   OutputStatistics(processName, numPasses, numFailures);
406
407   return numFailures;
408 }
409
410 // Constantly runs up to MAX_NUM_CHILDREN processes
411 int32_t RunAllInParallel(const char* processName, ::testcase tc_array[], bool reRunFailed, bool quiet)
412 {
413   int32_t numFailures = 0;
414   int32_t numPasses   = 0;
415
416   RunningTestCases     children;
417   std::vector<int32_t> failedTestCases;
418
419   // Fork up to MAX_NUM_CHILDREN processes, then
420   // wait. As soon as a proc completes, fork the next.
421
422   int32_t nextTestCase       = 0;
423   int32_t numRunningChildren = 0;
424
425   while(tc_array[nextTestCase].name || numRunningChildren > 0)
426   {
427     // Create more children (up to the max number or til the end of the array)
428     while(numRunningChildren < MAX_NUM_CHILDREN && tc_array[nextTestCase].name)
429     {
430       int32_t pid = fork();
431       if(pid == 0) // Child process
432       {
433         TestCase testCase(nextTestCase, &tc_array[nextTestCase]);
434         int      status = RunTestCaseRedirectOutput(testCase, quiet);
435         exit(status);
436       }
437       else if(pid == -1)
438       {
439         perror("fork");
440         exit(EXIT_STATUS_FORK_FAILED);
441       }
442       else // Parent process
443       {
444         TestCase tc(nextTestCase, tc_array[nextTestCase].name);
445         tc.startTime       = steady_clock::now();
446         tc.startSystemTime = system_clock::now();
447         tc.childPid        = pid;
448
449         children[pid] = tc;
450         nextTestCase++;
451         numRunningChildren++;
452       }
453     }
454
455     // Check to see if any children have finished yet
456     int32_t status   = 0;
457     int32_t childPid = waitpid(-1, &status, WNOHANG);
458     if(childPid == 0)
459     {
460       // No children have finished.
461       // Check if any have exceeded execution time
462       auto endTime = std::chrono::steady_clock::now();
463
464       for(auto& tc : children)
465       {
466         std::chrono::steady_clock::duration timeSpan = endTime - tc.second.startTime;
467         std::chrono::duration<double>       seconds  = std::chrono::duration_cast<std::chrono::duration<double>>(timeSpan);
468         if(seconds.count() > MAXIMUM_CHILD_LIFETIME)
469         {
470           // Kill the child process. A subsequent call to waitpid will process signal result below.
471           kill(tc.first, SIGKILL);
472         }
473       }
474     }
475     else if(childPid == -1) // waitpid errored
476     {
477       perror("waitpid");
478       exit(EXIT_STATUS_WAITPID_FAILED);
479     }
480     else // a child has finished
481     {
482       if(WIFEXITED(status))
483       {
484         auto& testCase  = children[childPid];
485         testCase.result = WEXITSTATUS(status);
486         if(testCase.result)
487         {
488           printf("Test case %s failed: %d\n", testCase.name, testCase.result);
489           failedTestCases.push_back(testCase.testCase);
490           numFailures++;
491         }
492         else
493         {
494           numPasses++;
495         }
496         numRunningChildren--;
497       }
498
499       else if(WIFSIGNALED(status) || WIFSTOPPED(status))
500       {
501         status = WIFSIGNALED(status) ? WTERMSIG(status) : WSTOPSIG(status);
502
503         RunningTestCases::iterator iter = children.find(childPid);
504         if(iter != children.end())
505         {
506           printf("Test case %s exited with signal %s\n", iter->second.name, strsignal(status));
507           iter->second.result = 1;
508           failedTestCases.push_back(iter->second.testCase);
509         }
510         else
511         {
512           printf("Unknown child process: %d signaled %s\n", childPid, strsignal(status));
513         }
514
515         numFailures++;
516         numRunningChildren--;
517       }
518     }
519   }
520
521   if(!quiet)
522   {
523     OutputTestResults(processName, children);
524   }
525
526   OutputStatistics(processName, numPasses, numFailures);
527
528   if(reRunFailed)
529   {
530     for(uint32_t i = 0; i < failedTestCases.size(); i++)
531     {
532       char*   testCaseStrapline;
533       int32_t numChars = asprintf(&testCaseStrapline, "Test case %s", tc_array[failedTestCases[i]].name);
534       printf("\n%s\n", testCaseStrapline);
535       for(int32_t j = 0; j < numChars; j++)
536       {
537         printf("=");
538       }
539       printf("\n");
540       int      index = failedTestCases[i];
541       TestCase testCase(index, &tc_array[index]);
542       RunTestCaseInChildProcess(testCase, false);
543     }
544   }
545
546   return numFailures;
547 }
548
549 int32_t FindAndRunTestCase(::testcase tc_array[], const char* testCaseName)
550 {
551   int32_t result = EXIT_STATUS_TESTCASE_NOT_FOUND;
552
553   for(int32_t i = 0; tc_array[i].name; i++)
554   {
555     if(!strcmp(testCaseName, tc_array[i].name))
556     {
557       return RunTestCase(tc_array[i]);
558     }
559   }
560
561   printf("Unknown testcase name: \"%s\"\n", testCaseName);
562   return result;
563 }
564
565 void Usage(const char* program)
566 {
567   printf(
568     "Usage: \n"
569     "   %s <testcase name>\t\t Execute a test case\n"
570     "   %s \t\t Execute all test cases in parallel\n"
571     "   %s -r\t\t Execute all test cases in parallel, rerunning failed test cases\n"
572     "   %s -s\t\t Execute all test cases serially\n"
573     "   %s -q\t\t Run without output\n",
574     program,
575     program,
576     program,
577     program,
578     program);
579 }
580
581 int RunTests(int argc, char* const argv[], ::testcase tc_array[])
582 {
583   int         result    = TestHarness::EXIT_STATUS_BAD_ARGUMENT;
584   const char* optString = "sfq";
585   bool        optRerunFailed(true);
586   bool        optRunSerially(false);
587   bool        optQuiet(false);
588
589   int nextOpt = 0;
590   do
591   {
592     nextOpt = getopt(argc, argv, optString);
593     switch(nextOpt)
594     {
595       case 'f':
596         optRerunFailed = false;
597         break;
598       case 's':
599         optRunSerially = true;
600         break;
601       case 'q':
602         optQuiet = true;
603         break;
604       case '?':
605         TestHarness::Usage(argv[0]);
606         exit(TestHarness::EXIT_STATUS_BAD_ARGUMENT);
607         break;
608     }
609   } while(nextOpt != -1);
610
611   if(optind == argc) // no testcase name in argument list
612   {
613     if(optRunSerially)
614     {
615       result = TestHarness::RunAll(argv[0], tc_array, optQuiet);
616     }
617     else
618     {
619       result = TestHarness::RunAllInParallel(argv[0], tc_array, optRerunFailed, optQuiet);
620     }
621   }
622   else
623   {
624     // optind is index of next argument - interpret as testcase name
625     result = TestHarness::FindAndRunTestCase(tc_array, argv[optind]);
626   }
627   return result;
628 }
629
630 } // namespace TestHarness