a050242e7aab808fd2b5b328b10c4a72332a54e2
[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
41 #
42 SILENT=1
43 PROGRESS=0
44 VALGRIND=0
45 date=`date +"%b %d %Y"`
46
47 ## @fn createTemplate()
48 ## @brief Generate runTest template file
49 ##
50 ## Note that the generated template has no license.
51 ## The SSAT user may put their own license for the generated files.
52 ## I hereby grant the right to relicense the generated files.
53 function createTemplate() {
54         if [[ -f "runTest.sh" ]]
55         then
56                 printf "Cannot create runTest.sh here. The file already exists at $(pwd).\n\n"
57                 exit 1
58         fi
59
60         echo -e "#!/usr/bin/env bash\n\
61 ##\n\
62 ## @file runTest.sh\n\
63 ## @author MyungJoo Ham <myungjoo.ham@gmail.com>\n\
64 ## @date ${date}\n\
65 ## @brief This is a template file for SSAT test cases. You may designate your own license.\n\
66 #\n\
67 if [[ \"\$SSATAPILOADED\" != \"1\" ]]\n\
68 then\n\
69         SILENT=0\n\
70         INDEPENDENT=1\n\
71         search=\"ssat-api.sh\"\n\
72         source \$search\n\
73         printf \"\${Blue}Independent Mode\${NC}\\n\"\n\
74 fi\n\
75 testInit \$1 # You may replace this with Test Group Name\n\
76 \n\
77 #testResult 1 T1 \"Dummy Test1\"\n\
78 #callTestSuccess gst-launch-1.0 \"-q videotestsrc ! videoconvert ! autovideosink\" T2 \"This may run indefinitely\"\n\
79 #callCompareTest golden.log executeResult.log T3 \"The two files must be same\" 0\n\
80 \n\
81 report\n" > runTest.sh
82 chmod a+x runTest.sh
83
84         exit 0
85 }
86
87 # Handle arguments
88 POSITIONAL=()
89 while [[ $# -gt 0 ]]
90 do
91         key="$1"
92         case $key in
93         -h|--help)
94                 printf "usage: ${BASENAME} [--help] [<path>] [--testcase <filename>] [--nocolor] [--showstdout] [--createtemplate]\n\n"
95                 printf "These are common ${Red}ssat${NC} commands used:\n\n"
96                 printf "Test all test-groups in the current ($(pwd)) directory, recursively\n"
97                 printf "    (no options specified)\n"
98                 printf "    $ ${BASENAME}\n"
99                 printf "\n"
100                 printf "Test all test-groups in the specified directory, recursively\n"
101                 printf "    <path>\n"
102                 printf "    $ ${BASENAME} /home/username/test\n"
103                 printf "    If there are multiple paths, the last one will be used\n"
104                 printf "\n"
105                 printf "Search for \"filename\" as the testcase scripts\n"
106                 printf "    --testcase or -t\n"
107                 printf "    $ ${BASENAME} --testcase cases.sh\n"
108                 printf "    Search for cases.sh instead of runTest.sh\n"
109                 printf "\n"
110                 printf "Do not emit colored text\n"
111                 printf "    --nocolor or -n\n"
112                 printf "\n"
113                 printf "Show stdout of test cases\n"
114                 printf "    --showstdout or -s\n"
115                 printf "\n"
116                 printf "Create a template 'runTest.sh' test group at your current directory\n"
117                 printf "    --createtemplate or -c\n"
118                 printf "\n"
119                 printf "Show progress during execution\n"
120                 printf "    --progress or -p\n"
121                 printf "\n"
122                 printf "Enable valgrind to perform memcheck\n"
123                 printf "    --enable-valgrind or -vg\n"
124                 printf "\n"
125                 printf "Shows this message\n"
126                 printf "    --help or -h\n"
127                 printf "    $ ${BASENAME} --help \n"
128                 printf "\n\n"
129                 exit 0
130         ;;
131         -n|--nocolor)
132         nocolor=1
133         shift
134         ;;
135         -t|--testcase)
136         TESTCASE="$2"
137         shift
138         shift
139         ;;
140         -s|--showstdout)
141         SILENT=0
142         shift
143         ;;
144         -c|--createtemplate)
145         createTemplate
146         shift
147         ;;
148         -p|--progress)
149         PROGRESS=1
150         shift
151         ;;
152         -vg|--enable-valgrind)
153         VALGRIND=1
154         shift
155         ;;
156         *) # Unknown, which is probably target (the path to root-dir of test groups).
157         TARGET="$1"
158         shift
159         esac
160 done
161
162 source ${BASEPATH}/ssat-api.sh
163
164 if [[ "${#TARGET}" -eq "0" ]]
165 then
166         TARGET="."
167 fi
168
169 TNtc=0
170 TNtcpass=0
171 TNtcfail=0
172 TNtcignore=0
173 TNgroup=0
174 TNgrouppass=0
175 TNgroupfail=0
176 log=""
177 groupLog=""
178
179 while read -d $'\0' file
180 do
181         CASEBASEPATH=`dirname "$file"`
182         CASENAME=`basename "$CASEBASEPATH"`
183         Ntc=0
184         Npass=0
185         Nfail=0
186         tmpfile=$(mktemp)
187
188         if [[ "$PROGRESS" -eq "1" ]]; then
189                 printf "[Starting] $CASENAME\n"
190         fi
191         pushd $CASEBASEPATH > /dev/null
192         output=$(. $file $CASEBASEPATH)
193         retcode=$?
194         popd > /dev/null
195
196         logfile="${output##*$'\n'}"
197         resultlog=$(<$logfile)
198         effectiveOutput=`printf "$resultlog" | sed '$d'`
199         log="$log$effectiveOutput\n"
200
201         lastline=`printf "${resultlog}" | sed '$!d'`
202         IFS=,
203         set $lastline
204         Ntc=$1
205         Npass=$2
206         Nfail=$3
207         Nignore=$4
208         unset IFS
209
210         TNtc=$((TNtc+Ntc))
211         TNtcpass=$((TNtcpass+Npass))
212         TNtcfail=$((TNtcfail+Nfail))
213         TNtcignore=$((TNtcignore+Nignore))
214
215         TNgroup=$((TNgroup+1))
216         if [[ "$retcode" -eq "0" ]]
217         then
218                 TNgrouppass=$((TNgrouppass+1))
219                 groupLog="${groupLog}${LightGreen}[PASSED]${NC} ${Blue}${CASENAME}${NC} ($Npass passed among $Ntc cases)\n"
220         else
221                 TNgroupfail=$((TNgroupfail+1))
222                 groupLog="${groupLog}${Red}[FAILED]${NC} ${Blue}${CASENAME}${NC} ($Npass passed among $Ntc cases)\n"
223         fi
224
225         printf "$log\n"
226         log=""
227 done < <(find $TARGET -name $TESTCASE -print0)
228
229 printf "\n\n==================================================\n\n"
230
231 printf "==================================================\n\n"
232 printf "$groupLog"
233 printf "==================================================\n"
234
235 if (( ${TNgroupfail} == 0 ))
236 then
237         printf "${LightGreen}[PASSED] ${Blue}All Test Groups (${TNgroup}) Passed!${NC}\n"
238         printf "         TC Passed: ${TNtcpass} / Failed: ${TNtcfail} / Ignored: ${TNtcignore}\n\n";
239         exit 0
240 else
241         printf "${Red}[FAILED] ${Purple}There are failed test groups! (${TNgroupfail})${NC}\n"
242         printf "         TC Passed: ${TNtcpass} / Failed: ${TNtcfail} / Ignored: ${TNtcignore}\n\n";
243         exit 1
244 fi
245 # gather reports & publish them.