[API] If "SILENT", don't show stderr/stdout from gstTest
[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 not critical.${NC}"
88                 fi
89         fi
90
91         if [[ "$INDEPENDENT" -eq "1" ]]
92         then
93         # do nothing
94                 echo ""
95         else
96                 writef "${_cases},${_pass},${_fail}"
97                 echo "${ResultLog}" > ${_filename}
98                 printf "\n${_filename}\n"
99         fi
100
101         if (( ${_criticalFail} > 0 ))
102         then
103                 exit 1
104         else
105                 exit 0
106         fi
107 }
108
109 ## @fn testInit()
110 ## @brief Initialize runTest.sh shell test case
111 function testInit() {
112         _pass=0
113         _fail=0
114         _criticalFail=0
115         _cases=0
116         _filename=$(mktemp)
117         _group=`basename "$1"`
118         if [[ "${#_group}" -eq "0" ]]
119         then
120                 _group="(Unspecified)"
121         fi
122
123         writef "${Green}==================================================${NC}"
124         writef "    Test Group ${Green}$_group${NC} Starts."
125 }
126
127 ## @fn testResult()
128 ## @brief Write Test Log
129 ## @param $1 1 = success / 0 = fail ($5 is not 1)
130 ## @param $2 test case ID (short string)
131 ## @param $3 test case description
132 ## @param $4 set 1 if this is not critical (don't care if it's pass or fail)_
133 ## @param $5 set 1 if $1==0 is success and $1!=0 is fail.
134 function testResult() {
135         if [[ "${PROGRESS}" -eq "1" ]]
136         then
137                 echo "Case ${2}(${3}) report ${1}" > /dev/stderr
138         fi
139
140         _cases=$((_cases+1))
141         _good=0
142         if [[ "${5}" -eq "1" ]]; then
143                 if [[ "${1}" -eq "0" ]]; then
144                         _good=1
145                 fi
146         else
147                 if [[ "${1}" -eq "1" ]]; then
148                         _good=1
149                 fi
150         fi
151
152         if [[ "${_good}" -eq "1" ]]
153         then
154                 writef "${LightGreen}[PASSED]${NC} ${Green}$2${NC}:$3${NC}"
155                 _pass=$((_pass+1))
156         else
157                 _fail=$((_fail+1))
158                 if [[ "${4}" == "1" ]]
159                 then
160                         writef "${Purple}[FAILED][Ignorable] $2${NC}:${Purple}$3${NC}"
161                 else
162                         writef "${Red}[FAILED][Critical] $2${NC}:${Purple}$3${NC}"
163                         _criticalFail=$((_criticalFail+1))
164                 fi
165         fi
166 }
167
168 ## @fn callTestSuccess()
169 ## @brief Call Test Case (a shell script), expected exit = 0
170 ## @param $1 Full path to the executable (e.g., ~/script/a1.sh)
171 ## @param $2 Full string of the arguments to $1 (e.g., "-q -n --dryrun")
172 ## @param $3 test case ID
173 ## @param $4 test case description
174 ## @param $5 set 1 if this is not critical (don't care if it's pass or fail)_
175 function callTestSuccess() {
176         callutput=$(. $1 $2)
177         retcode=$?
178         if (( ${retcode} == 0 ))
179         then
180                 testResult 1 "$3" "$4" $5
181         else
182                 testResult 0 "$3" "$4 ret($retcode)" $5
183         fi
184 }
185
186 ## @fn callTestFail()
187 ## @brief Call Test Case (a shell script), expected exit != 0
188 ## @param $1 Full path to the executable (e.g., ~/script/a1.sh)
189 ## @param $2 Full string of the arguments to $1 (e.g., "-q -n --dryrun")
190 ## @param $3 test case ID
191 ## @param $4 test case description
192 ## @param $5 set 1 if this is not critical (don't care if it's pass or fail)_
193 function callTestFail() {
194         callutput=$(. $1 $2)
195         retcode=$?
196         if (( ${retcode} != 0 ))
197         then
198                 testResult 1 "$3" "$4 ret($retcode)" $5
199         else
200                 testResult 0 "$3" "$4" $5
201         fi
202 }
203
204 ## @fn callTestExitEq()
205 ## @brief Call Test Case (a shell script), expected exit == $5
206 ## @param $1 Full path to the executable (e.g., ~/script/a1.sh)
207 ## @param $2 Full string of the arguments to $1 (e.g., "-q -n --dryrun")
208 ## @param $3 test case ID
209 ## @param $4 test case description
210 ## @param $5 Expected exit code.
211 ## @param $6 set 1 if this is not critical (don't care if it's pass or fail)_
212 function callTestExitEq() {
213         callutput=$(. $1 $2)
214         retcode=$?
215         if (( ${retcode} == $5 ))
216         then
217                 testResult 1 "$3" "$4" $6
218         else
219                 testResult 0 "$3" "$4 ret($retcode)" $6
220         fi
221 }
222
223 ## @fn callCompareTest()
224 ## @brief Compare two result files expected to be equal
225 ## @param $1 Path to result 1 (golden)
226 ## @param $2 Path to result 2 (test run)
227 ## @param $3 test case ID
228 ## @param $4 test case description
229 ## @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)
230 ## @param $6 set 1 if this is not critical (don't care if it's pass or fail)_
231 function callCompareTest() {
232         # Try cmp.
233         output=0
234         command -v cmp
235         # If cmp is symlink, then it could be from busybox and it does not support "-n" option
236         if [[ $? == 0 && ! -L $(which cmp) ]]
237         then
238                 # use cmp
239                 if (( $5 == 0 )); then
240                         # Size should be same as well.
241                         cmp $1 $2
242                         output=$?
243                 elif (( $5 == 1 )); then
244                         # Compare up to the size of golden
245                         cmp -n `${StatCmd_GetSize} $1` $1 $2
246                         output=$?
247                 elif (( $5 == 2 )); then
248                         # Compare up to the size of test-run
249                         cmp -n `${StatCmd_GetSize} $2` $1 $2
250                         output=$?
251                 else
252                         # Compare up to $5 bytes.
253                         cmp -n `${StatCmd_GetSize} $5` $1 $2
254                         output=$?
255                 fi
256                 if (( ${output} == 0 )); then
257                         output=1
258                 else
259                         output=0
260                 fi
261                 testResult $output "$3" "$4" $6
262         else
263             # use internal logic (slower!)
264             bufsize=`${StatCmd_GetSize} $1`
265             if (( $5 == 2 )); then
266                 bufsize=`${StatCmd_GetSize} $2`
267             else
268                 bufsize=$5
269             fi
270             diff <(dd bs=1 count=$bufsize if=$1 &>/dev/null) <(dd bs=1 count=$bufsize if=$2 &>/dev/null)
271             output=$?
272             if (( ${output} == 0 )); then
273                 output=1
274             else
275                 output=0
276             fi
277             testResult $output "$3" "$4" $6
278         fi
279 }
280
281 ## @fn gstTest()
282 ## @brief Execute gst-launch with given arguments
283 ## @todo Separate this function to "gstreamer extension plugin"
284 ## @param $1 gst-launch-1.0 Arguments
285 ## @param $2 test case ID
286 ## @param $3 set 1 if this is not critical (don't care if it's pass or fail)
287 ## @param $4 set 1 if this passes if gstLaunch fails.
288 ## @param $5 set 1 to enable PERFORMANCE test.
289 function gstTest() {
290         if [[ "$VALGRIND" -eq "1" ]]; then
291                 calloutputprefix='valgrind --track-origins=yes'
292         fi
293         if [[ "${SILENT}" -eq "1" ]]; then
294                 calloutput=$(eval $calloutputprefix gst-launch-1.0 -f -q $1 &> /dev/null)
295         else
296                 calloutput=$(eval $calloutputprefix gst-launch-1.0 -f -q $1)
297         fi
298
299         retcode=$?
300         desired=0
301         if [[ "${4}" -eq "1" ]]; then
302                 if [[ "${retcode}" -ne "0" ]]; then
303                         desired=1
304                 fi
305         else
306                 if [[ "${retcode}" -eq "0" ]]; then
307                         desired=1
308                 fi
309         fi
310
311         if [[ "$desired" -eq "1" ]]; then
312                 testResult 1 "$2" "gst-launch of case $2" $3
313         else
314                 testResult 0 "$2" "gst-launch of case $2" $3
315         fi
316
317         if [[ "$5" -eq "1" ]]; then
318                 if (( ${#GST_DEBUG_DUMP_DOT_DIR} -le 1 )); then
319                         GST_DEBUG_DUMP_DOT_DIR="./performance"
320                 fi
321                 dot -Tpng $GST_DEBUG_DUMP_DOT_DIR/*.PLAYING_PAUSED.dot > $GST_DEBUG_DUMP_DOT_DIR/debug/$base/$2.png
322                 gst-report-1.0 --dot $GST_DEBUG_DUMP_DOT_DIR/*.gsttrace | dot -Tsvg > $GST_DEBUG_DUMP_DOT_DIR/profile/$base/$2.svg
323                 rm -f $GST_DEBUG_DUMP_DOT_DIR/*.dot
324                 rm -f $GST_DEBUG_DUMP_DOT_DIR/*.gsttrace
325         fi
326 }
327
328 ## @fn convertBMP2PNG()
329 ## @brief Convert all *.bmp to *.png in the current directory
330 ## @todo macronice "bmp2png" searching.
331 ## @todo Separate this function to "gstreamer extension plugin"
332 function convertBMP2PNG() {
333         tool="bmp2png"
334         if [ -x bmp2png ]; then
335                 tool="bmp2png"
336         else
337                 if [ -x ../bmp2png ]; then
338                         tool="../bmp2png"
339                 else
340                         if [ -x ../../bmp2png ]; then
341                                 tool="../../bmp2png"
342                         else
343                                 tool="../../../bmp2png"
344                                 # Try this and die if fails
345                         fi
346                 fi
347         fi
348         for X in `ls *.bmp`
349         do
350                 if [[ $X  = *"GRAY8"* ]]; then
351                         $tool $X --GRAY8
352                 else
353                         $tool $X
354                 fi
355         done
356 }
357
358 SSATAPILOADED=1