[API] Add timeout mode to gstTest command
[platform/upstream/SSAT.git] / ssat-api.sh
1 #!/usr/bin/env bash
2 ##
3 ## @file ssat-api.sh
4 ## @author MyungJoo Ham <myungjoo.ham@gmail.com>
5 ## @date Jun 22 2018
6 ## @brief This is API set for SSAT (Shell Script Automated Tester)
7 ##
8
9 if [[ "$nocolor" != "1" ]]
10 then
11         Black='\033[0;30m'
12         DarkGray='\033[1;30m'
13         Red='\033[0;31m'
14         LightRed='\033[1;31m'
15         Green='\033[0;32m'
16         LightGreen='\033[1;32m'
17         Orange='\033[0;33m'
18         Yellow='\033[1;33m'
19         Blue='\033[0;34m'
20         LightBlue='\033[1;34m'
21         Purple='\033[0;35m'
22         LightPurple='\033[1;35m'
23         Cyan='\033[0;36m'
24         LightCyan='\033[1;36m'
25         LightGray='\033[0;37m'
26         White='\033[1;37m'
27         NC='\033[0m'
28 else
29         Black=''
30         DarkGray=''
31         Red=''
32         LightRed=''
33         Green=''
34         LightGreen=''
35         Orange=''
36         Yellow=''
37         Blue=''
38         LightBlue=''
39         Purple=''
40         LightPurple=''
41         Cyan=''
42         LightCyan=''
43         LightGray=''
44         White=''
45         NC=''
46 fi
47
48
49 ResultLog=""
50
51 # Platform dependent variables
52 KernelName=$(uname -s)
53 if [[ "${KernelName}" == "Darwin" ]]; then
54         StatCmd_GetSize="stat -f %z"
55         SO_EXT="dylib"
56 else
57         StatCmd_GetSize="stat --printf=%s"
58         SO_EXT="so"
59 fi
60
61 ## @fn writef()
62 ## @private
63 ## @param $1 the string to be printed.
64 ## @brief Prepare report result
65 function writef() {
66         if [[ "${SILENT}" == "0" ]]
67         then
68                 printf "$1\n"
69         fi
70         ResultLog="$ResultLog$1\n"
71 }
72
73 ## @fn report()
74 ## @brief Report results of a test group (a "runTest.sh" in a testee directory)
75 function report() {
76         if (( ${_fail} == 0 && ${_criticalFail} == 0 ))
77         then
78                 writef "${Green}==================================================${NC}"
79                 writef "${LightGreen}[PASSED]${NC} Test Group $_group ${Green}Passed${NC}"
80         else
81                 if (( ${_criticalFail} > 0 ))
82                 then
83                         writef "${Green}==================================================${NC}"
84                         writef "${Red}[FAILED]${NC} Test Group $_group has ${Red}failed cases ($_fail)${NC}"
85                 else
86                         writef "${Green}==================================================${NC}"
87                         writef "${LightGreen}[PASSED]${NC} Test Group $_group has ${Red}failed cases ($_fail), but they are ignorable cases and not critical.${NC}"
88                 fi
89         fi
90
91         if [[ "$INDEPENDENT" -eq "1" ]]
92         then
93         # do nothing
94                 echo ""
95         else
96                 _ignore=$((_fail-_criticalFail))
97                 _fail=${_criticalFail}
98                 if [[ "${COUNTNEGATIVE}" -eq "1" ]]
99                 then
100                         writef "${_cases},${_pass},${_fail},${_ignore},${_neg}"
101                 else
102                         writef "${_cases},${_pass},${_fail},${_ignore}"
103                 fi
104                 echo "${ResultLog}" > ${_filename}
105                 printf "\n${_filename}\n"
106         fi
107
108         if (( ${_criticalFail} > 0 ))
109         then
110                 exit 1
111         else
112                 exit 0
113         fi
114 }
115
116 ## @fn testInit()
117 ## @brief Initialize runTest.sh shell test case
118 function testInit() {
119         _pass=0
120         _fail=0
121         _criticalFail=0
122         _cases=0
123         _neg=0
124         _filename=$(mktemp)
125         _group=`basename "$1"`
126         if [[ "${#_group}" -eq "0" ]]
127         then
128                 _group="(Unspecified)"
129         fi
130
131         writef "${Green}==================================================${NC}"
132         writef "    Test Group ${Green}$_group${NC} Starts."
133 }
134
135 ## @fn testResult()
136 ## @brief Write Test Log
137 ## @param $1 1 = success / 0 = fail ($5 is not 1)
138 ## @param $2 test case ID (short string)
139 ## @param $3 test case description
140 ## @param $4 set 1 if this is not critical (don't care if it's pass or fail)_
141 ## @param $5 set 1 if $1==0 is success and $1!=0 is fail.
142 function testResult() {
143         if [[ ${PROGRESSLOGLEVEL} -gt 1 ]]
144         then
145                 echo "Case ${2}(${3}) report ${1}" >&2
146         fi
147
148         _cases=$((_cases+1))
149         _good=0
150         if [[ "${5}" -eq "1" ]]; then
151                 if [[ "${1}" -eq "0" ]]; then
152                         _good=1
153                 fi
154         else
155                 if [[ "${1}" -eq "1" ]]; then
156                         _good=1
157                 fi
158         fi
159
160         if [[ "${COUNTNEGATIVE}" -eq "1" ]]
161         then
162                 if [[ "${2}\n" =~ "${COUNTNEGATIVEPOSTFIX}\n" ]]
163                 then
164                         _neg=$((_neg+1))
165                 fi
166         fi
167
168         if [[ "${_good}" -eq "1" ]]
169         then
170                 writef "${LightGreen}[PASSED]${NC} ${Green}$2${NC}:$3${NC}"
171                 _pass=$((_pass+1))
172         else
173                 _fail=$((_fail+1))
174                 if [[ "${4}" == "1" ]]
175                 then
176                         writef "${Purple}[IGNORED] $2${NC}:${Purple}$3${NC}"
177                 else
178                         writef "${Red}[FAILED][Critical] $2${NC}:${Purple}$3${NC}"
179                         _criticalFail=$((_criticalFail+1))
180                 fi
181         fi
182 }
183
184 ## @fn callTestSuccess()
185 ## @brief Call Test Case (a shell script), expected exit = 0
186 ## @param $1 Full path to the executable (e.g., ~/script/a1.sh)
187 ## @param $2 Full string of the arguments to $1 (e.g., "-q -n --dryrun")
188 ## @param $3 test case ID
189 ## @param $4 test case description
190 ## @param $5 set 1 if this is not critical (don't care if it's pass or fail)_
191 function callTestSuccess() {
192         callutput=$(. $1 $2)
193         retcode=$?
194         if (( ${retcode} == 0 ))
195         then
196                 testResult 1 "$3" "$4" $5
197         else
198                 testResult 0 "$3" "$4 ret($retcode)" $5
199         fi
200 }
201
202 ## @fn callTestFail()
203 ## @brief Call Test Case (a shell script), expected exit != 0
204 ## @param $1 Full path to the executable (e.g., ~/script/a1.sh)
205 ## @param $2 Full string of the arguments to $1 (e.g., "-q -n --dryrun")
206 ## @param $3 test case ID
207 ## @param $4 test case description
208 ## @param $5 set 1 if this is not critical (don't care if it's pass or fail)_
209 function callTestFail() {
210         callutput=$(. $1 $2)
211         retcode=$?
212         if (( ${retcode} != 0 ))
213         then
214                 testResult 1 "$3" "$4 ret($retcode)" $5
215         else
216                 testResult 0 "$3" "$4" $5
217         fi
218 }
219
220 ## @fn callTestExitEq()
221 ## @brief Call Test Case (a shell script), expected exit == $5
222 ## @param $1 Full path to the executable (e.g., ~/script/a1.sh)
223 ## @param $2 Full string of the arguments to $1 (e.g., "-q -n --dryrun")
224 ## @param $3 test case ID
225 ## @param $4 test case description
226 ## @param $5 Expected exit code.
227 ## @param $6 set 1 if this is not critical (don't care if it's pass or fail)_
228 function callTestExitEq() {
229         callutput=$(. $1 $2)
230         retcode=$?
231         if (( ${retcode} == $5 ))
232         then
233                 testResult 1 "$3" "$4" $6
234         else
235                 testResult 0 "$3" "$4 ret($retcode)" $6
236         fi
237 }
238
239 ## @fn callCompareTest()
240 ## @brief Compare two result files expected to be equal
241 ## @param $1 Path to result 1 (golden)
242 ## @param $2 Path to result 2 (test run)
243 ## @param $3 test case ID
244 ## @param $4 test case description
245 ## @param $5 0 if the size is expected to be equal as well. 1 if golden (result 1) might be smaller (will ignore rest of result 2). 2 if the opposite of 1. If $5 > 2, it denotes the max size of compared bytes. (compare the first $5 bytes only)
246 ## @param $6 set 1 if this is not critical (don't care if it's pass or fail)_
247 function callCompareTest() {
248         # Try cmp.
249         output=0
250         if [[ ! -f "$1" || ! -f "$2" ]]; then
251                 testResult $output "$3" "$4" $6
252                 return
253         fi
254         command -v cmp
255         # If cmp is symlink, then it could be from busybox and it does not support "-n" option
256         if [[ $? == 0 && ! -L $(which cmp) ]]
257         then
258                 # use cmp
259                 if (( $5 == 0 )); then
260                         # Size should be same as well.
261                         cmp $1 $2
262                         output=$?
263                 elif (( $5 == 1 )); then
264                         # Compare up to the size of golden
265                         cmp -n `${StatCmd_GetSize} $1` $1 $2
266                         output=$?
267                 elif (( $5 == 2 )); then
268                         # Compare up to the size of test-run
269                         cmp -n `${StatCmd_GetSize} $2` $1 $2
270                         output=$?
271                 else
272                         # Compare up to $5 bytes.
273                         cmp -n `${StatCmd_GetSize} $5` $1 $2
274                         output=$?
275                 fi
276                 if (( ${output} == 0 )); then
277                         output=1
278                 else
279                         output=0
280                 fi
281                 testResult $output "$3" "$4" $6
282         else
283             # use internal logic (slower!)
284             bufsize=`${StatCmd_GetSize} $1`
285             if (( $5 == 2 )); then
286                 bufsize=`${StatCmd_GetSize} $2`
287             else
288                 bufsize=$5
289             fi
290             diff <(dd bs=1 count=$bufsize if=$1 &>/dev/null) <(dd bs=1 count=$bufsize if=$2 &>/dev/null)
291             output=$?
292             if (( ${output} == 0 )); then
293                 output=1
294             else
295                 output=0
296             fi
297             testResult $output "$3" "$4" $6
298         fi
299 }
300
301 ## @fn gstTest()
302 ## @brief Execute gst-launch with given arguments
303 ## @todo Separate this function to "gstreamer extension plugin"
304 ## @param $1 gst-launch-1.0 Arguments
305 ## @param $2 test case ID
306 ## @param $3 set 1 if this is not critical (don't care if it's pass or fail)
307 ## @param $4 set 1 if this passes if gstLaunch fails.
308 ## @param $5 set 1 to enable PERFORMANCE test.
309 ## @param $6 set a positive value (seconds) to enable timeout mode.
310 function gstTest() {
311         if [[ "$VALGRIND" -eq "1" ]]; then
312                 calloutputprefix='valgrind --track-origins=yes'
313         fi
314
315         if [[ "${6}" -gt "0" ]]; then
316                 if [[ "${SILENT}" -eq "1" ]]; then
317                         calloutput=$(eval timeout ${6} $calloutputprefix gst-launch-1.0 -f -q $1 &> /dev/null)
318                 else
319                         calloutput=$(eval timeout ${6} $calloutputprefix gst-launch-1.0 -f -q $1)
320                 fi
321         else
322                 if [[ "${SILENT}" -eq "1" ]]; then
323                         calloutput=$(eval $calloutputprefix gst-launch-1.0 -f -q $1 &> /dev/null)
324                 else
325                         calloutput=$(eval $calloutputprefix gst-launch-1.0 -f -q $1)
326                 fi
327         fi
328
329         retcode=$?
330         desired=0
331         if [[ "${4}" -eq "1" ]]; then
332                 if [[ "${retcode}" -ne "0" ]]; then
333                         desired=1
334                 fi
335         else
336                 if [[ "${retcode}" -eq "0" ]]; then
337                         desired=1
338                 fi
339         fi
340
341         if [[ "$desired" -eq "1" ]]; then
342                 testResult 1 "$2" "gst-launch of case $2" $3
343         else
344                 testResult 0 "$2" "gst-launch of case $2" $3
345         fi
346
347         if [[ "$5" -eq "1" ]]; then
348                 if (( ${#GST_DEBUG_DUMP_DOT_DIR} -le 1 )); then
349                         GST_DEBUG_DUMP_DOT_DIR="./performance"
350                 fi
351                 dot -Tpng $GST_DEBUG_DUMP_DOT_DIR/*.PLAYING_PAUSED.dot > $GST_DEBUG_DUMP_DOT_DIR/debug/$base/$2.png
352                 gst-report-1.0 --dot $GST_DEBUG_DUMP_DOT_DIR/*.gsttrace | dot -Tsvg > $GST_DEBUG_DUMP_DOT_DIR/profile/$base/$2.svg
353                 rm -f $GST_DEBUG_DUMP_DOT_DIR/*.dot
354                 rm -f $GST_DEBUG_DUMP_DOT_DIR/*.gsttrace
355         fi
356 }
357
358 ## @fn convertBMP2PNG()
359 ## @brief Convert all *.bmp to *.png in the current directory
360 ## @todo macronice "bmp2png" searching.
361 ## @todo Separate this function to "gstreamer extension plugin"
362 function convertBMP2PNG() {
363         tool="bmp2png"
364         if [ -x bmp2png ]; then
365                 tool="bmp2png"
366         else
367                 if [ -x ../bmp2png ]; then
368                         tool="../bmp2png"
369                 else
370                         if [ -x ../../bmp2png ]; then
371                                 tool="../../bmp2png"
372                         else
373                                 tool="../../../bmp2png"
374                                 # Try this and die if fails
375                         fi
376                 fi
377         fi
378         for X in `ls *.bmp`
379         do
380                 if [[ $X  = *"GRAY8"* ]]; then
381                         $tool $X --GRAY8
382                 else
383                         $tool $X
384                 fi
385         done
386 }
387
388 SSATAPILOADED=1