Add alternative semantics for testResult reporting
[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 # @license Apache-2.0
7 # @brief This is API set for SSAT (Shell Script Automated Tester)
8 #
9
10 if [[ "$nocolor" != "1" ]]
11 then
12         Black='\033[0;30m'
13         DarkGray='\033[1;30m'
14         Red='\033[0;31m'
15         LightRed='\033[1;31m'
16         Green='\033[0;32m'
17         LightGreen='\033[1;32m'
18         Orange='\033[0;33m'
19         Yellow='\033[1;33m'
20         Blue='\033[0;34m'
21         LightBlue='\033[1;34m'
22         Purple='\033[0;35m'
23         LightPurple='\033[1;35m'
24         Cyan='\033[0;36m'
25         LightCyan='\033[1;36m'
26         LightGray='\033[0;37m'
27         White='\033[1;37m'
28         NC='\033[0m'
29 else
30         Black=''
31         DarkGray=''
32         Red=''
33         LightRed=''
34         Green=''
35         LightGreen=''
36         Orange=''
37         Yellow=''
38         Blue=''
39         LightBlue=''
40         Purple=''
41         LightPurple=''
42         Cyan=''
43         LightCyan=''
44         LightGray=''
45         White=''
46         NC=''
47 fi
48
49
50 ResultLog=""
51
52 function writef {
53         if [[ "${SILENT}" == "0" ]]
54         then
55                 printf "$1\n"
56         fi
57         ResultLog="$ResultLog$1\n"
58 }
59
60 ##
61 # @brief Report results of a test group (a "runTest.sh" in a testee directory)
62 #
63 function report {
64         if (( ${_fail} == 0 ))
65         then
66                 writef "${Green}==================================================${NC}"
67                 writef "${LightGreen}[PASSED]${NC} Test Group $_group ${Green}Passed${NC}"
68         else
69                 if (( ${_criticalFail} > 0 ))
70                 then
71                         writef "${Green}==================================================${NC}"
72                         writef "${Red}[FAILED]${NC} Test Group $_group has ${Red}failed cases ($_fail)${NC}"
73                 else
74                         writef "${Green}==================================================${NC}"
75                         writef "${LightGreen}[PASSED]${NC} Test Group $_group has ${Red}failed cases ($_fail), but they are not critical.${NC}"
76                 fi
77         fi
78
79         if [[ "$INDEPENDENT" -eq "1" ]]
80         then
81         # do nothing
82                 echo ""
83         else
84                 writef "${_cases},${_pass},${_fail}"
85                 echo "${ResultLog}" > ${_filename}
86                 printf "${_filename}\n"
87         fi
88
89         if (( ${_criticalFail} > 0 ))
90         then
91                 exit 1
92         else
93                 exit 0
94         fi
95 }
96
97 function testInit {
98         _pass=0
99         _fail=0
100         _criticalFail=0
101         _cases=0
102         _filename=$(mktemp)
103         _group=`basename "$1"`
104         if [[ "${#_group}" -eq "0" ]]
105         then
106                 _group="(Unspecified)"
107         fi
108
109         writef "${Green}==================================================${NC}"
110         writef "    Test Group ${Green}$_group${NC} Starts."
111 }
112
113 ##
114 # @brief Write Test Log
115 # @param $1 1 = success / 0 = fail ($5 is not 1)
116 # @param $2 test case ID (short string)
117 # @param $3 test case description
118 # @param $4 set 1 if this is not critical (don't care if it's pass or fail)_
119 # @param $5 set 1 if $1==0 is success and $1!=0 is fail.
120 function testResult {
121         _cases=$((_cases+1))
122         _good=0
123         if [[ "${5}" -eq "1" ]]; then
124                 if [[ "${1}" -eq "0" ]]; then
125                         _good=1
126                 fi
127         else
128                 if [[ "${1}" -eq "1" ]]; then
129                         _good=1
130                 fi
131         fi
132
133         if [[ "${_good}" -eq "1" ]]
134         then
135                 writef "${LightGreen}[PASSED]${NC} ${Green}$2${NC}:$3${NC}"
136                 _pass=$((_pass+1))
137         else
138                 _fail=$((_fail+1))
139                 if [[ "${4}" == "1" ]]
140                 then
141                         writef "${Purple}[FAILED][Ignorable] $2${NC}:${Purple}$3${NC}"
142                 else
143                         writef "${Red}[FAILED][Critical] $2${NC}:${Purple}$3${NC}"
144                         _criticalFail=$((_criticalFail+1))
145                 fi
146         fi
147 }
148
149 ##
150 # @brief Call Test Case (a shell script), expected exit = 0
151 # @param $1 Full path to the executable (e.g., ~/script/a1.sh)
152 # @param $2 Full string of the arguments to $1 (e.g., "-q -n --dryrun")
153 # @param $3 test case ID
154 # @param $4 test case description
155 # @param $5 set 1 if this is not critical (don't care if it's pass or fail)_
156 function callTestSuccess {
157         callutput=$(. $1 $2)
158         retcode=$?
159         if (( ${retcode} == 0 ))
160         then
161                 testResult 1 "$3" "$4" $5
162         else
163                 testResult 0 "$3" "$4 ret($retcode)" $5
164         fi
165 }
166
167 ##
168 # @brief Call Test Case (a shell script), expected exit != 0
169 # @param $1 Full path to the executable (e.g., ~/script/a1.sh)
170 # @param $2 Full string of the arguments to $1 (e.g., "-q -n --dryrun")
171 # @param $3 test case ID
172 # @param $4 test case description
173 # @param $5 set 1 if this is not critical (don't care if it's pass or fail)_
174 function callTestFail {
175         callutput=$(. $1 $2)
176         retcode=$?
177         if (( ${retcode} != 0 ))
178         then
179                 testResult 1 "$3" "$4 ret($retcode)" $5
180         else
181                 testResult 0 "$3" "$4" $5
182         fi
183 }
184
185 ##
186 # @brief Call Test Case (a shell script), expected exit == $5
187 # @param $1 Full path to the executable (e.g., ~/script/a1.sh)
188 # @param $2 Full string of the arguments to $1 (e.g., "-q -n --dryrun")
189 # @param $3 test case ID
190 # @param $4 test case description
191 # @param $5 Expected exit code.
192 # @param $6 set 1 if this is not critical (don't care if it's pass or fail)_
193 function callTestExitEq {
194         callutput=$(. $1 $2)
195         retcode=$?
196         if (( ${retcode} == $5 ))
197         then
198                 testResult 1 "$3" "$4" $6
199         else
200                 testResult 0 "$3" "$4 ret($retcode)" $6
201         fi
202 }
203
204 ##
205 # @brief Compare two result files expected to be equal
206 # @param $1 Path to result 1 (golden)
207 # @param $2 Path to result 2 (test run)
208 # @param $3 test case ID
209 # @param $4 test case description
210 # @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)
211 # @param $6 set 1 if this is not critical (don't care if it's pass or fail)_
212 function callCompareTest {
213         # Try cmp.
214         command -v cmp
215         output=0
216         if (( $? == 0 ))
217         then
218                 # use cmp
219                 echo NYI
220                 if (( $5 == 0 )); then
221                         # Size should be same as well.
222                         cmp $1 $2
223                         output=$?
224                 elif (( $5 == 1 )); then
225                         # Compare up to the size of golden
226                         cmp -n `stat --printf="%s" $1` $1 $2
227                         output=$?
228                 elif (( $5 == 2 )); then
229                         # Compare up to the size of test-run
230                         cmp -n `stat --printf="%s" $2` $1 $2
231                         output=$?
232                 else
233                         # Compare up to $5 bytes.
234                         cmp -n `stat --printf="%s" $5` $1 $2
235                         output=$?
236                 fi
237                 if (( ${output} == 0 )); then
238                         output=1
239                 else
240                         output=0
241                 fi
242                 testResult $output "$3" "$4" $6
243         else
244                 # use internal logic (slower!)
245                 echo NYI
246                 testResult 0 "$3" "Cannot test. cmp not found." $6
247         fi
248 }
249
250 ########################################################
251 ## EXTENSION. GStreamer
252 ## @todo How to separate such "plugins"?
253 ########################################################
254
255 ##
256 # @brief Execute gst-launch with given arguments
257 # @param $1 gst-launch-1.0 Arguments
258 # @param $2 test case ID
259 # @param $3 set 1 if this is not critical (don't care if it's pass or fail)
260 # @param $4 set 1 if this passes if gstLaunch fails.
261 # @param $5 set 1 to enable PERFORMANCE test.
262 function gstTest {
263         calloutput=$(gst-launch-1.0 -q $1)
264         retcode=$?
265         desired=0
266         if [[ "${4}" -eq "1" ]]; then
267                 if [[ "${retcode}" -ne "0" ]]; then
268                         desired=1
269                 fi
270         else
271                 if [[ "${retcode}" -eq "0" ]]; then
272                         desired=1
273                 fi
274         fi
275
276         if [[ "$desired" -eq "1" ]]; then
277                 testResult 1 "$2" "gst-launch of case $2" $3
278         else
279                 testResult 0 "$2" "gst-launch of case $2" $3
280         fi
281
282         if [[ "$5" -eq "1" ]]; then
283                 if (( ${#GST_DEBUG_DUMP_DOT_DIR} -le 1 )); then
284                         GST_DEBUG_DUMP_DOT_DIR="./performance"
285                 fi
286                 dot -Tpng $GST_DEBUG_DUMP_DOT_DIR/*.PLAYING_PAUSED.dot > $GST_DEBUG_DUMP_DOT_DIR/debug/$base/$2.png
287                 gst-report-1.0 --dot $GST_DEBUG_DUMP_DOT_DIR/*.gsttrace | dot -Tsvg > $GST_DEBUG_DUMP_DOT_DIR/profile/$base/$2.svg
288                 rm -f $GST_DEBUG_DUMP_DOT_DIR/*.dot
289                 rm -f $GST_DEBUG_DUMP_DOT_DIR/*.gsttrace
290         fi
291 }
292
293 ##
294 # @brief Convert all *.bmp to *.png
295 #
296 # @todo macronice "bmp2png" searching.
297 function convertBMP2PNG {
298         tool="bmp2png"
299         if [ -x bmp2png ]; then
300                 tool="bmp2png"
301         else
302                 if [ -x ../bmp2png ]; then
303                         tool="../bmp2png"
304                 else
305                         if [ -x ../../bmp2png ]; then
306                                 tool="../../bmp2png"
307                         else
308                                 tool="../../../bmp2png"
309                                 # Try this and die if fails
310                         fi
311                 fi
312         fi
313         for X in `ls *.bmp`
314         do
315                 if [[ $X  = *"GRAY8"* ]]; then
316                         $tool $X --GRAY8
317                 else
318                         $tool $X
319                 fi
320         done
321 }
322
323 SSATAPILOADED=1