[Main] Options added, test group features added
[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         ResultLog="$ResultLog$1\n"
54 }
55
56 ##
57 # @brief Report results of a test group (a "runTest.sh" in a testee directory)
58 #
59 function report {
60         if (( ${_fail} == 0 ))
61         then
62                 writef "${Green}==================================================${NC}"
63                 writef "${LightGreen}[PASSED]${NC} Test Group ${Green}Passed${NC}"
64         else
65                 if (( ${_criticalFail} > 0 ))
66                 then
67                         writef "${Green}==================================================${NC}"
68                         writef "${Red}[FAILED]${NC} Test Group has ${Red}failed cases ($_fail)${NC}"
69                 else
70                         writef "${Green}==================================================${NC}"
71                         writef "${LightGreen}[PASSED]${NC} Test Group has ${Red}failed cases ($_fail), but they are not critical.${NC}"
72                 fi
73         fi
74
75         writef "${_cases}/${_pass}/${_fail}"
76
77         echo "${ResultLog}" > $_filename
78         printf "$_filename\n"
79
80         if (( ${_criticalFail} > 0 ))
81         then
82                 exit 1
83         else
84                 exit 0
85         fi
86 }
87
88 function testInit {
89         _pass=0
90         _fail=0
91         _criticalFail=0
92         _cases=0
93         _filename=$(mktemp)
94 }
95
96 ##
97 # @brief Write Test Log
98 # @param $1 1 = success / 0 = fail
99 # @param $2 test case ID (short string)
100 # @param $3 test case description
101 # @param $4 set 1 if this is not critical (don't care if it's pass or fail)_
102 function testResult {
103         _cases=$((_cases+1))
104         if [[ "${1}" == "1" ]]
105         then
106                 writef "${LightGreen}[PASSED]${NC} ${Green}$2${NC}:$3${NC}"
107                 _pass=$((_pass+1))
108         else
109                 _fail=$((_fail+1))
110                 if [[ "${4}" == "1" ]]
111                 then
112                         writef "${Purple}[FAILED][Ignorable] $2${NC}:${Purple}$3${NC}"
113                 else
114                         writef "${Red}[FAILED][Critical] $2${NC}:${Purple}$3${NC}"
115                         _criticalFail=$((_criticalFail+1))
116                 fi
117         fi
118 }
119
120 ##
121 # @brief Call Test Case (a shell script), expected exit = 0
122 # @param $1 Full path to the executable (e.g., ~/script/a1.sh)
123 # @param $2 Full string of the arguments to $1 (e.g., "-q -n --dryrun")
124 # @param $3 test case ID
125 # @param $4 test case description
126 # @param $5 set 1 if this is not critical (don't care if it's pass or fail)_
127 function callTestSuccess {
128         echo NYI
129 }
130
131 ##
132 # @brief Call Test Case (a shell script), expected exit != 0
133 # @param $1 Full path to the executable (e.g., ~/script/a1.sh)
134 # @param $2 Full string of the arguments to $1 (e.g., "-q -n --dryrun")
135 # @param $3 test case ID
136 # @param $4 test case description
137 # @param $5 set 1 if this is not critical (don't care if it's pass or fail)_
138 function callTestFail {
139         echo NYI
140 }
141
142 ##
143 # @brief Call Test Case (a shell script), expected exit == $5
144 # @param $1 Full path to the executable (e.g., ~/script/a1.sh)
145 # @param $2 Full string of the arguments to $1 (e.g., "-q -n --dryrun")
146 # @param $3 test case ID
147 # @param $4 test case description
148 # @param $5 Expected exit code.
149 # @param $6 set 1 if this is not critical (don't care if it's pass or fail)_
150 function callTestExitEq {
151         echo NYI
152 }
153
154 ##
155 # @brief Compare two result files expected to be equal
156 # @param $1 Path to result 1 (golden)
157 # @param $2 Path to result 2 (test run)
158 # @param $3 test case ID
159 # @param $4 test case description
160 # @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)
161 # @param $6 set 1 if this is not critical (don't care if it's pass or fail)_
162 function callCompareTest {
163         echo NYI
164 }
165
166 SSATAPILOADED=1