a9eba350ce266f5ee0278fa0b3c69c67782f3d56
[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         _cases=$((_cases+1))
126         _good=0
127         if [[ "${5}" -eq "1" ]]; then
128                 if [[ "${1}" -eq "0" ]]; then
129                         _good=1
130                 fi
131         else
132                 if [[ "${1}" -eq "1" ]]; then
133                         _good=1
134                 fi
135         fi
136
137         if [[ "${_good}" -eq "1" ]]
138         then
139                 writef "${LightGreen}[PASSED]${NC} ${Green}$2${NC}:$3${NC}"
140                 _pass=$((_pass+1))
141         else
142                 _fail=$((_fail+1))
143                 if [[ "${4}" == "1" ]]
144                 then
145                         writef "${Purple}[FAILED][Ignorable] $2${NC}:${Purple}$3${NC}"
146                 else
147                         writef "${Red}[FAILED][Critical] $2${NC}:${Purple}$3${NC}"
148                         _criticalFail=$((_criticalFail+1))
149                 fi
150         fi
151 }
152
153 ## @fn callTestSuccess()
154 ## @brief Call Test Case (a shell script), expected exit = 0
155 ## @param $1 Full path to the executable (e.g., ~/script/a1.sh)
156 ## @param $2 Full string of the arguments to $1 (e.g., "-q -n --dryrun")
157 ## @param $3 test case ID
158 ## @param $4 test case description
159 ## @param $5 set 1 if this is not critical (don't care if it's pass or fail)_
160 function callTestSuccess() {
161         callutput=$(. $1 $2)
162         retcode=$?
163         if (( ${retcode} == 0 ))
164         then
165                 testResult 1 "$3" "$4" $5
166         else
167                 testResult 0 "$3" "$4 ret($retcode)" $5
168         fi
169 }
170
171 ## @fn callTestFail()
172 ## @brief Call Test Case (a shell script), expected exit != 0
173 ## @param $1 Full path to the executable (e.g., ~/script/a1.sh)
174 ## @param $2 Full string of the arguments to $1 (e.g., "-q -n --dryrun")
175 ## @param $3 test case ID
176 ## @param $4 test case description
177 ## @param $5 set 1 if this is not critical (don't care if it's pass or fail)_
178 function callTestFail() {
179         callutput=$(. $1 $2)
180         retcode=$?
181         if (( ${retcode} != 0 ))
182         then
183                 testResult 1 "$3" "$4 ret($retcode)" $5
184         else
185                 testResult 0 "$3" "$4" $5
186         fi
187 }
188
189 ## @fn callTestExitEq()
190 ## @brief Call Test Case (a shell script), expected exit == $5
191 ## @param $1 Full path to the executable (e.g., ~/script/a1.sh)
192 ## @param $2 Full string of the arguments to $1 (e.g., "-q -n --dryrun")
193 ## @param $3 test case ID
194 ## @param $4 test case description
195 ## @param $5 Expected exit code.
196 ## @param $6 set 1 if this is not critical (don't care if it's pass or fail)_
197 function callTestExitEq() {
198         callutput=$(. $1 $2)
199         retcode=$?
200         if (( ${retcode} == $5 ))
201         then
202                 testResult 1 "$3" "$4" $6
203         else
204                 testResult 0 "$3" "$4 ret($retcode)" $6
205         fi
206 }
207
208 ## @fn callCompareTest()
209 ## @brief Compare two result files expected to be equal
210 ## @param $1 Path to result 1 (golden)
211 ## @param $2 Path to result 2 (test run)
212 ## @param $3 test case ID
213 ## @param $4 test case description
214 ## @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)
215 ## @param $6 set 1 if this is not critical (don't care if it's pass or fail)_
216 function callCompareTest() {
217         # Try cmp.
218         output=0
219         command -v cmp
220         if (( $? == 0 ))
221         then
222                 # use cmp
223                 if (( $5 == 0 )); then
224                         # Size should be same as well.
225                         cmp $1 $2
226                         output=$?
227                 elif (( $5 == 1 )); then
228                         # Compare up to the size of golden
229                         cmp -n `stat --printf="%s" $1` $1 $2
230                         output=$?
231                 elif (( $5 == 2 )); then
232                         # Compare up to the size of test-run
233                         cmp -n `stat --printf="%s" $2` $1 $2
234                         output=$?
235                 else
236                         # Compare up to $5 bytes.
237                         cmp -n `stat --printf="%s" $5` $1 $2
238                         output=$?
239                 fi
240                 if (( ${output} == 0 )); then
241                         output=1
242                 else
243                         output=0
244                 fi
245                 testResult $output "$3" "$4" $6
246         else
247                 # use internal logic (slower!)
248                 echo NYI
249                 testResult 0 "$3" "Cannot test. cmp not found." $6
250         fi
251 }
252
253 ## @fn gstTest()
254 ## @brief Execute gst-launch with given arguments
255 ## @todo Separate this function to "gstreamer extension plugin"
256 ## @param $1 gst-launch-1.0 Arguments
257 ## @param $2 test case ID
258 ## @param $3 set 1 if this is not critical (don't care if it's pass or fail)
259 ## @param $4 set 1 if this passes if gstLaunch fails.
260 ## @param $5 set 1 to enable PERFORMANCE test.
261 function gstTest() {
262         calloutput=$(gst-launch-1.0 -q $1)
263         retcode=$?
264         desired=0
265         if [[ "${4}" -eq "1" ]]; then
266                 if [[ "${retcode}" -ne "0" ]]; then
267                         desired=1
268                 fi
269         else
270                 if [[ "${retcode}" -eq "0" ]]; then
271                         desired=1
272                 fi
273         fi
274
275         if [[ "$desired" -eq "1" ]]; then
276                 testResult 1 "$2" "gst-launch of case $2" $3
277         else
278                 testResult 0 "$2" "gst-launch of case $2" $3
279         fi
280
281         if [[ "$5" -eq "1" ]]; then
282                 if (( ${#GST_DEBUG_DUMP_DOT_DIR} -le 1 )); then
283                         GST_DEBUG_DUMP_DOT_DIR="./performance"
284                 fi
285                 dot -Tpng $GST_DEBUG_DUMP_DOT_DIR/*.PLAYING_PAUSED.dot > $GST_DEBUG_DUMP_DOT_DIR/debug/$base/$2.png
286                 gst-report-1.0 --dot $GST_DEBUG_DUMP_DOT_DIR/*.gsttrace | dot -Tsvg > $GST_DEBUG_DUMP_DOT_DIR/profile/$base/$2.svg
287                 rm -f $GST_DEBUG_DUMP_DOT_DIR/*.dot
288                 rm -f $GST_DEBUG_DUMP_DOT_DIR/*.gsttrace
289         fi
290 }
291
292 ## @fn convertBMP2PNG()
293 ## @brief Convert all *.bmp to *.png in the current directory
294 ## @todo macronice "bmp2png" searching.
295 ## @todo Separate this function to "gstreamer extension plugin"
296 function convertBMP2PNG() {
297         tool="bmp2png"
298         if [ -x bmp2png ]; then
299                 tool="bmp2png"
300         else
301                 if [ -x ../bmp2png ]; then
302                         tool="../bmp2png"
303                 else
304                         if [ -x ../../bmp2png ]; then
305                                 tool="../../bmp2png"
306                         else
307                                 tool="../../../bmp2png"
308                                 # Try this and die if fails
309                         fi
310                 fi
311         fi
312         for X in `ls *.bmp`
313         do
314                 if [[ $X  = *"GRAY8"* ]]; then
315                         $tool $X --GRAY8
316                 else
317                         $tool $X
318                 fi
319         done
320 }
321
322 SSATAPILOADED=1