2 * Copyright (c) 2017 Samsung Electronics Co., Ltd.
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 #include "test-harness.h"
19 #include <sys/types.h>
30 typedef std::map<int32_t, TestCase> RunningTestCases;
32 const char* basename(const char* path)
35 const char* slash=NULL;
36 for( ; *ptr != '\0' ; ++ptr )
38 if(*ptr == '/') slash=ptr;
40 if(slash != NULL) ++slash;
44 int32_t RunTestCase( struct ::testcase_s& testCase )
46 int32_t result = EXIT_STATUS_TESTCASE_FAILED;
48 // dont want to catch exception as we want to be able to get
49 // gdb stack trace from the first error
50 // by default tests should all always pass with no exceptions
51 if( testCase.startup )
57 result = testCase.function();
61 // just catch test fail exception, return is already set to EXIT_STATUS_TESTCASE_FAILED
63 if( testCase.cleanup )
72 int32_t RunTestCaseInChildProcess( struct ::testcase_s& testCase, bool suppressOutput )
74 int32_t testResult = EXIT_STATUS_TESTCASE_FAILED;
77 if( pid == 0 ) // Child process
87 for(int32_t i=0; i<80; ++i) printf("#");
88 printf("\nTC: %s\n", testCase.name);
92 int32_t status = RunTestCase( testCase );
94 if( ! suppressOutput )
106 exit(EXIT_STATUS_FORK_FAILED);
108 else // Parent process
111 int32_t childPid = waitpid(pid, &status, 0);
115 exit(EXIT_STATUS_WAITPID_FAILED);
117 if( WIFEXITED(status) )
121 testResult = WEXITSTATUS(status);
124 printf("Test case %s failed: %d\n", testCase.name, testResult);
128 else if(WIFSIGNALED(status) )
130 int32_t signal = WTERMSIG(status);
131 testResult = EXIT_STATUS_TESTCASE_ABORTED;
132 if( signal == SIGABRT )
134 printf("Test case %s failed: test case asserted\n", testCase.name );
138 printf("Test case %s failed: exit with signal %s\n", testCase.name, strsignal(WTERMSIG(status)));
141 else if(WIFSTOPPED(status))
143 printf("Test case %s failed: stopped with signal %s\n", testCase.name, strsignal(WSTOPSIG(status)));
151 void OutputStatistics( const char* processName, int32_t numPasses, int32_t numFailures )
153 FILE* fp=fopen("summary.xml", "a");
157 " <suite name=\"%s\">\n"
158 " <total_case>%d</total_case>\n"
159 " <pass_case>%d</pass_case>\n"
160 " <pass_rate>%5.2f</pass_rate>\n"
161 " <fail_case>%d</fail_case>\n"
162 " <fail_rate>%5.2f</fail_rate>\n"
163 " <block_case>0</block_case>\n"
164 " <block_rate>0.00</block_rate>\n"
165 " <na_case>0</na_case>\n"
166 " <na_rate>0.00</na_rate>\n"
168 basename(processName),
169 numPasses+numFailures,
171 (float)numPasses/(numPasses+numFailures),
173 (float)numFailures/(numPasses+numFailures) );
178 int32_t RunAll( const char* processName, ::testcase tc_array[] )
180 int32_t numFailures = 0;
181 int32_t numPasses = 0;
183 // Run test cases in child process( to kill output/handle signals ), but run serially.
184 for( uint32_t i=0; tc_array[i].name; i++)
186 int32_t result = RunTestCaseInChildProcess( tc_array[i], false );
197 OutputStatistics( processName, numPasses, numFailures);
202 // Constantly runs up to MAX_NUM_CHILDREN processes
203 int32_t RunAllInParallel( const char* processName, ::testcase tc_array[], bool reRunFailed)
205 int32_t numFailures = 0;
206 int32_t numPasses = 0;
208 RunningTestCases children;
209 std::vector<int32_t> failedTestCases;
211 // Fork up to MAX_NUM_CHILDREN processes, then
212 // wait. As soon as a proc completes, fork the next.
214 int32_t nextTestCase = 0;
215 int32_t numRunningChildren = 0;
217 while( tc_array[nextTestCase].name || numRunningChildren > 0)
219 // Create more children (up to the max number or til the end of the array)
220 while( numRunningChildren < MAX_NUM_CHILDREN && tc_array[nextTestCase].name )
222 int32_t pid = fork();
223 if( pid == 0 ) // Child process
225 close(STDOUT_FILENO);
226 close(STDERR_FILENO);
227 exit( RunTestCase( tc_array[nextTestCase] ) );
232 exit(EXIT_STATUS_FORK_FAILED);
234 else // Parent process
236 TestCase tc(nextTestCase, tc_array[nextTestCase].name);
239 numRunningChildren++;
243 // Wait for the next child to finish
246 int32_t childPid = waitpid(-1, &status, 0);
250 exit(EXIT_STATUS_WAITPID_FAILED);
253 if( WIFEXITED(status) )
257 int32_t testResult = WEXITSTATUS(status);
260 printf("Test case %s failed: %d\n", children[childPid].testCaseName, testResult);
261 failedTestCases.push_back(children[childPid].testCase);
268 numRunningChildren--;
272 else if( WIFSIGNALED(status) || WIFSTOPPED(status))
274 status = WIFSIGNALED(status)?WTERMSIG(status):WSTOPSIG(status);
278 RunningTestCases::iterator iter = children.find(childPid);
279 if( iter != children.end() )
281 printf("Test case %s exited with signal %s\n", iter->second.testCaseName, strsignal(status));
282 failedTestCases.push_back(iter->second.testCase);
286 printf("Unknown child process: %d signaled %s\n", childPid, strsignal(status));
290 numRunningChildren--;
295 OutputStatistics( processName, numPasses, numFailures );
299 for( uint32_t i=0; i<failedTestCases.size(); i++)
301 char* testCaseStrapline;
302 int32_t numChars = asprintf(&testCaseStrapline, "Test case %s", tc_array[failedTestCases[i]].name );
303 printf("\n%s\n", testCaseStrapline);
304 for(int32_t j=0; j<numChars; j++)
309 RunTestCaseInChildProcess( tc_array[failedTestCases[i] ], false );
318 int32_t FindAndRunTestCase(::testcase tc_array[], const char* testCaseName)
320 int32_t result = EXIT_STATUS_TESTCASE_NOT_FOUND;
322 for( int32_t i = 0; tc_array[i].name; i++ )
324 if( !strcmp(testCaseName, tc_array[i].name) )
326 return RunTestCase( tc_array[i] );
330 printf("Unknown testcase name: \"%s\"\n", testCaseName);
334 void Usage(const char* program)
337 " %s <testcase name>\t\t Execute a test case\n"
338 " %s \t\t Execute all test cases in parallel\n"
339 " %s -r\t\t Execute all test cases in parallel, rerunning failed test cases\n"
340 " %s -s\t\t Execute all test cases serially\n",
341 program, program, program, program);