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