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