Feature: write summary with negative cases
[platform/upstream/SSAT.git] / ssat.sh
1 #!/usr/bin/env bash
2 ##
3 ## @file ssat.sh
4 ## @author MyungJoo Ham <myungjoo.ham@gmail.com>
5 ## @date Jun 22 2018
6 ## @brief This executes test groups and reports aggregated test results.
7 ## @return 0 if all PASSED. Positive if some FAILED.
8 ## @todo Separate GStreamer related functions as plugins
9 ##
10 ## This uses sed, date, cmp
11 ##
12 ## If there is no arguments specified, this will search for all "runTest.sh" in
13 ## the subdirectory of this file and regard them as the test groups.
14 ##
15 ## If a testgroup (runTest.sh) returns 0 while there are failed testcase,
16 ## it implies that the failed testcases may be ignored and it's good to go.
17 ##
18 ## If --help or -h is given, this will show detailed description.
19
20 ##
21 ## @mainpage SSAT
22 ## @section intro        Introduction
23 ## - Introduction     :  Shell Script Automated Tester
24 ## @section Program      Program Name
25 ## - Program Name     :  ssat
26 ## - Program Details  :  SSAT is a software testing framework for test cases written in BASH shell scripts.
27 ##   It can search for test scripts recursively from a given path and summarize the test results.
28 ##   If there is any "critical" fail, ssat will return non-zero values on its exit.
29 ## @section INOUTPUT     Input/output data
30 ## - INPUT            :  Test Cases (If not supplied, the current path is the root of test cases)
31 ## - OUTPUT           :  Summary of test results to stdout. Exit code of 0 if success, non-zero if not success.
32 ## @section CREATEINFO   Code information
33 ## - Initial date     :  2018/06/22
34 ## - Version          :  1.0.0
35
36 TARGET=$(pwd)
37 BASEPATH=`dirname "$0"`
38 BASENAME=`basename "$0"`
39 TESTCASE="runTest.sh"
40 SUMMARYFILENAME=""
41
42 #
43 SILENT=1
44 PROGRESS=0
45 COUNTNEGATIVE=0
46 COUNTNEGATIVEPOSTFIX=""
47 VALGRIND=0
48 date=`date +"%b %d %Y"`
49
50 ## @fn createTemplate()
51 ## @brief Generate runTest template file
52 ##
53 ## Note that the generated template has no license.
54 ## The SSAT user may put their own license for the generated files.
55 ## I hereby grant the right to relicense the generated files.
56 function createTemplate() {
57         if [[ -f "runTest.sh" ]]
58         then
59                 printf "Cannot create runTest.sh here. The file already exists at $(pwd).\n\n"
60                 exit 1
61         fi
62
63         echo -e "#!/usr/bin/env bash\n\
64 ##\n\
65 ## @file runTest.sh\n\
66 ## @author MyungJoo Ham <myungjoo.ham@gmail.com>\n\
67 ## @date ${date}\n\
68 ## @brief This is a template file for SSAT test cases. You may designate your own license.\n\
69 #\n\
70 if [[ \"\$SSATAPILOADED\" != \"1\" ]]\n\
71 then\n\
72         SILENT=0\n\
73         INDEPENDENT=1\n\
74         search=\"ssat-api.sh\"\n\
75         source \$search\n\
76         printf \"\${Blue}Independent Mode\${NC}\\n\"\n\
77 fi\n\
78 testInit \$1 # You may replace this with Test Group Name\n\
79 \n\
80 #testResult 1 T1 \"Dummy Test1\"\n\
81 #callTestSuccess gst-launch-1.0 \"-q videotestsrc ! videoconvert ! autovideosink\" T2 \"This may run indefinitely\"\n\
82 #callCompareTest golden.log executeResult.log T3 \"The two files must be same\" 0\n\
83 \n\
84 report\n" > runTest.sh
85 chmod a+x runTest.sh
86
87         exit 0
88 }
89
90 # Handle arguments
91 POSITIONAL=()
92 while [[ $# -gt 0 ]]
93 do
94         key="$1"
95         case $key in
96         -h|--help)
97                 printf "usage: ${BASENAME} [--help] [<path>] [--testcase <filename>] [--nocolor] [--showstdout] [--createtemplate] [--countnegative <postfix>] \n\n"
98                 printf "These are common ${Red}ssat${NC} commands used:\n\n"
99                 printf "Test all test-groups in the current ($(pwd)) directory, recursively\n"
100                 printf "    (no options specified)\n"
101                 printf "    $ ${BASENAME}\n"
102                 printf "\n"
103                 printf "Test all test-groups in the specified directory, recursively\n"
104                 printf "    <path>\n"
105                 printf "    $ ${BASENAME} /home/username/test\n"
106                 printf "    If there are multiple paths, the last one will be used\n"
107                 printf "\n"
108                 printf "Search for \"filename\" as the testcase scripts\n"
109                 printf "    --testcase or -t\n"
110                 printf "    $ ${BASENAME} --testcase cases.sh\n"
111                 printf "    Search for cases.sh instead of runTest.sh\n"
112                 printf "\n"
113                 printf "Do not emit colored text\n"
114                 printf "    --nocolor or -n\n"
115                 printf "\n"
116                 printf "Show stdout of test cases\n"
117                 printf "    --showstdout or -s\n"
118                 printf "\n"
119                 printf "Create a template 'runTest.sh' test group at your current directory\n"
120                 printf "    --createtemplate or -c\n"
121                 printf "\n"
122                 printf "Show progress during execution\n"
123                 printf "    --progress or -p\n"
124                 printf "\n"
125                 printf "Enable valgrind to perform memcheck\n"
126                 printf "    --enable-valgrind or -vg\n"
127                 printf "\n"
128                 printf "Shows this message\n"
129                 printf "    --help or -h\n"
130                 printf "    $ ${BASENAME} --help \n"
131                 printf "\n"
132                 printf "Count negative test cases with the given postfix\n"
133                 printf "    --countnegative or -cn\n"
134                 printf "    $ ${BASENAME} --countnegative _n\n"
135                 printf "    $ ${BASENAME} -cn _n\n"
136                 printf "\n"
137                 printf "Write result summary as a file\n"
138                 printf "    --summary <filename>\n"
139                 printf "\n\n"
140                 exit 0
141         ;;
142         -n|--nocolor)
143         nocolor=1
144         shift
145         ;;
146         -t|--testcase)
147         TESTCASE="$2"
148         shift
149         shift
150         ;;
151         -cn|--countnegative)
152         COUNTNEGATIVE=1
153         COUNTNEGATIVEPOSTFIX="$2"
154         if [[ "${COUNTNEGATIVEPOSTFIX}" == "" ]]
155         then
156                 printf "${BASENAME} -cn or --countnegative requires postfix.\n\n"
157                 exit -2
158         fi
159         shift
160         shift
161         ;;
162         -s|--showstdout)
163         SILENT=0
164         shift
165         ;;
166         -c|--createtemplate)
167         createTemplate
168         shift
169         ;;
170         -p|--progress)
171         PROGRESS=1
172         shift
173         ;;
174         -vg|--enable-valgrind)
175         VALGRIND=1
176         shift
177         ;;
178         --summary)
179         SUMMARYFILENAME="$2"
180         shift
181         shift
182         ;;
183         *) # Unknown, which is probably target (the path to root-dir of test groups).
184         TARGET="$1"
185         shift
186         esac
187 done
188
189 source ${BASEPATH}/ssat-api.sh
190
191 if [[ "${#TARGET}" -eq "0" ]]
192 then
193         TARGET="."
194 fi
195
196 TNtc=0
197 TNtcpass=0
198 TNtcfail=0
199 TNtcignore=0
200 TNtcneg=0
201 TNgroup=0
202 TNgrouppass=0
203 TNgroupfail=0
204 log=""
205 groupLog=""
206
207 while read -d $'\0' file
208 do
209         CASEBASEPATH=`dirname "$file"`
210         CASENAME=`basename "$CASEBASEPATH"`
211         Ntc=0
212         Npass=0
213         Nfail=0
214         Nneg=0
215         tmpfile=$(mktemp)
216
217         if [[ "$PROGRESS" -eq "1" ]]; then
218                 printf "[Starting] $CASENAME\n"
219         fi
220         pushd $CASEBASEPATH > /dev/null
221         output=$(. $file $CASEBASEPATH)
222         retcode=$?
223         popd > /dev/null
224
225         logfile="${output##*$'\n'}"
226         resultlog=$(<$logfile)
227         effectiveOutput=`printf "$resultlog" | sed '$d'`
228         log="$log$effectiveOutput\n"
229
230         lastline=`printf "${resultlog}" | sed '$!d'`
231         IFS=,
232         set $lastline
233         Ntc=$1
234         Npass=$2
235         Nfail=$3
236         Nignore=$4
237         Nneg=$5
238         unset IFS
239
240         TNtc=$((TNtc+Ntc))
241         TNtcpass=$((TNtcpass+Npass))
242         TNtcfail=$((TNtcfail+Nfail))
243         TNtcignore=$((TNtcignore+Nignore))
244         TNtcneg=$((TNtcneg+Nneg))
245
246         TNgroup=$((TNgroup+1))
247         if [[ "$retcode" -eq "0" ]]
248         then
249                 TNgrouppass=$((TNgrouppass+1))
250                 groupLog="${groupLog}${LightGreen}[PASSED]${NC} ${Blue}${CASENAME}${NC} ($Npass passed among $Ntc cases)\n"
251         else
252                 TNgroupfail=$((TNgroupfail+1))
253                 groupLog="${groupLog}${Red}[FAILED]${NC} ${Blue}${CASENAME}${NC} ($Npass passed among $Ntc cases)\n"
254         fi
255
256         printf "$log\n"
257         log=""
258 done < <(find $TARGET -name $TESTCASE -print0)
259
260 printf "\n\n==================================================\n\n"
261
262 printf "==================================================\n\n"
263 printf "$groupLog"
264 printf "==================================================\n"
265
266 ADDITIONALSTRING=""
267 ADDITIONALSUMMARY=""
268 if (( ${COUNTNEGATIVE} == 1 ))
269 then
270         total=$((TNtcpass+TNtcfail+TNtcignore))
271         pos=$((total-TNtcneg))
272         ADDITIONALSTRING="${ADDITIONALSTRING} | Positive: ${pos} / Negative: ${TNtcneg}"
273         ADDITIONALSUMMARY="${ADDITIONALSUMMARY}, negative=${TNtcneg}"
274 fi
275
276 if [ "${SUMMARYFILENAME}" != "" ]
277 then
278         echo "passed=${TNtcpass}, failed=${TNtcfail}, ignored=${TNtcignore}${ADDITIONALSUMMARY}" > "${SUMMARYFILENAME}"
279 fi
280 if (( ${TNgroupfail} == 0 ))
281 then
282         printf "${LightGreen}[PASSED] ${Blue}All Test Groups (${TNgroup}) Passed!${NC}\n"
283         printf "         TC Passed: ${TNtcpass} / Failed: ${TNtcfail} / Ignored: ${TNtcignore} ${ADDITIONALSTRING}\n\n";
284         exit 0
285 else
286         printf "${Red}[FAILED] ${Purple}There are failed test groups! (${TNgroupfail})${NC}\n"
287         printf "         TC Passed: ${TNtcpass} / Failed: ${TNtcfail} / Ignored: ${TNtcignore} ${ADDITIONALSTRING}\n\n";
288         exit 1
289 fi
290 # gather reports & publish them.