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