]> CyberLeo.Net >> Repos - FreeBSD/releng/10.2.git/blob - contrib/atf/atf-sh/atf-sh-api.3
- Copy stable/10@285827 to releng/10.2 in preparation for 10.2-RC1
[FreeBSD/releng/10.2.git] / contrib / atf / atf-sh / atf-sh-api.3
1 .\"
2 .\" Automated Testing Framework (atf)
3 .\"
4 .\" Copyright (c) 2008 The NetBSD Foundation, Inc.
5 .\" All rights reserved.
6 .\"
7 .\" Redistribution and use in source and binary forms, with or without
8 .\" modification, are permitted provided that the following conditions
9 .\" are met:
10 .\" 1. Redistributions of source code must retain the above copyright
11 .\"    notice, this list of conditions and the following disclaimer.
12 .\" 2. Redistributions in binary form must reproduce the above copyright
13 .\"    notice, this list of conditions and the following disclaimer in the
14 .\"    documentation and/or other materials provided with the distribution.
15 .\"
16 .\" THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND
17 .\" CONTRIBUTORS ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
18 .\" INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
19 .\" MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20 .\" IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS BE LIABLE FOR ANY
21 .\" DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 .\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
23 .\" GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24 .\" INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
25 .\" IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
26 .\" OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
27 .\" IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 .\"
29 .Dd March 2, 2014
30 .Dt ATF-SH-API 3
31 .Os
32 .Sh NAME
33 .Nm atf_add_test_case ,
34 .Nm atf_check ,
35 .Nm atf_check_equal ,
36 .Nm atf_config_get ,
37 .Nm atf_config_has ,
38 .Nm atf_expect_death ,
39 .Nm atf_expect_exit ,
40 .Nm atf_expect_fail ,
41 .Nm atf_expect_pass ,
42 .Nm atf_expect_signal ,
43 .Nm atf_expect_timeout ,
44 .Nm atf_fail ,
45 .Nm atf_get ,
46 .Nm atf_get_srcdir ,
47 .Nm atf_pass ,
48 .Nm atf_require_prog ,
49 .Nm atf_set ,
50 .Nm atf_skip ,
51 .Nm atf_test_case
52 .Nd POSIX shell API to write ATF-based test programs
53 .Sh SYNOPSIS
54 .Fn atf_add_test_case "name"
55 .Fn atf_check "command"
56 .Fn atf_check_equal "expr1" "expr2"
57 .Fn atf_config_get "var_name"
58 .Fn atf_config_has "var_name"
59 .Fn atf_expect_death "reason" "..."
60 .Fn atf_expect_exit "exitcode" "reason" "..."
61 .Fn atf_expect_fail "reason" "..."
62 .Fn atf_expect_pass
63 .Fn atf_expect_signal "signo" "reason" "..."
64 .Fn atf_expect_timeout "reason" "..."
65 .Fn atf_fail "reason"
66 .Fn atf_get "var_name"
67 .Fn atf_get_srcdir
68 .Fn atf_pass
69 .Fn atf_require_prog "prog_name"
70 .Fn atf_set "var_name" "value"
71 .Fn atf_skip "reason"
72 .Fn atf_test_case "name" "cleanup"
73 .Sh DESCRIPTION
74 ATF
75 provides a simple but powerful interface to easily write test programs in
76 the POSIX shell language.
77 These are extremely helpful given that they are trivial to write due to the
78 language simplicity and the great deal of available external tools, so they
79 are often ideal to test other applications at the user level.
80 .Pp
81 Test programs written using this library must be run using the
82 .Xr atf-sh 1
83 interpreter by putting the following on their very first line:
84 .Bd -literal -offset indent
85 #! /usr/bin/env atf-sh
86 .Ed
87 .Pp
88 Shell-based test programs always follow this template:
89 .Bd -literal -offset indent
90 atf_test_case tc1
91 tc1_head() {
92     ... first test case's header ...
93 }
94 tc1_body() {
95     ... first test case's body ...
96 }
97
98 atf_test_case tc2 cleanup
99 tc2_head() {
100     ... second test case's header ...
101 }
102 tc2_body() {
103     ... second test case's body ...
104 }
105 tc2_cleanup() {
106     ... second test case's cleanup ...
107 }
108
109 .Ns ... additional test cases ...
110
111 atf_init_test_cases() {
112     atf_add_test_case tc1
113     atf_add_test_case tc2
114     ... add additional test cases ...
115 }
116 .Ed
117 .Ss Definition of test cases
118 Test cases have an identifier and are composed of three different parts:
119 the header, the body and an optional cleanup routine, all of which are
120 described in
121 .Xr atf-test-case 4 .
122 To define test cases, one can use the
123 .Fn atf_test_case
124 function, which takes a first parameter specifiying the test case's
125 name and instructs the library to set things up to accept it as a valid
126 test case.
127 The second parameter is optional and, if provided, must be
128 .Sq cleanup ;
129 providing this parameter allows defining a cleanup routine for the test
130 case.
131 It is important to note that this function
132 .Em does not
133 set the test case up for execution when the program is run.
134 In order to do so, a later registration is needed through the
135 .Fn atf_add_test_case
136 function detailed in
137 .Sx Program initialization .
138 .Pp
139 Later on, one must define the three parts of the body by providing two
140 or three functions (remember that the cleanup routine is optional).
141 These functions are named after the test case's identifier, and are
142 .Fn <id>_head ,
143 .Fn <id>_body
144 and
145 .Fn <id>_cleanup.
146 None of these take parameters when executed.
147 .Ss Program initialization
148 The test program must define an
149 .Fn atf_init_test_cases
150 function, which is in charge of registering the test cases that will be
151 executed at run time by using the
152 .Fn atf_add_test_case
153 function, which takes the name of a test case as its single parameter.
154 This main function should not do anything else, except maybe sourcing
155 auxiliary source files that define extra variables and functions.
156 .Ss Configuration variables
157 The test case has read-only access to the current configuration variables
158 through the
159 .Fn atf_config_has
160 and
161 .Fn atf_config_get
162 methods.
163 The former takes a single parameter specifying a variable name and returns
164 a boolean indicating whether the variable is defined or not.
165 The latter can take one or two parameters.
166 If it takes only one, it specifies the variable from which to get the
167 value, and this variable must be defined.
168 If it takes two, the second one specifies a default value to be returned
169 if the variable is not available.
170 .Ss Access to the source directory
171 It is possible to get the path to the test case's source directory from
172 anywhere in the test program by using the
173 .Fn atf_get_srcdir
174 function.
175 It is interesting to note that this can be used inside
176 .Fn atf_init_test_cases
177 to silently include additional helper files from the source directory.
178 .Ss Requiring programs
179 Aside from the
180 .Va require.progs
181 meta-data variable available in the header only, one can also check for
182 additional programs in the test case's body by using the
183 .Fn atf_require_prog
184 function, which takes the base name or full path of a single binary.
185 Relative paths are forbidden.
186 If it is not found, the test case will be automatically skipped.
187 .Ss Test case finalization
188 The test case finalizes either when the body reaches its end, at which
189 point the test is assumed to have
190 .Em passed ,
191 or at any explicit call to
192 .Fn atf_pass ,
193 .Fn atf_fail
194 or
195 .Fn atf_skip .
196 These three functions terminate the execution of the test case immediately.
197 The cleanup routine will be processed afterwards in a completely automated
198 way, regardless of the test case's termination reason.
199 .Pp
200 .Fn atf_pass
201 does not take any parameters.
202 .Fn atf_fail
203 and
204 .Fn atf_skip
205 take a single string parameter that describes why the test case failed or
206 was skipped, respectively.
207 It is very important to provide a clear error message in both cases so that
208 the user can quickly know why the test did not pass.
209 .Ss Expectations
210 Everything explained in the previous section changes when the test case
211 expectations are redefined by the programmer.
212 .Pp
213 Each test case has an internal state called
214 .Sq expect
215 that describes what the test case expectations are at any point in time.
216 The value of this property can change during execution by any of:
217 .Bl -tag -width indent
218 .It Fn atf_expect_death "reason" "..."
219 Expects the test case to exit prematurely regardless of the nature of the
220 exit.
221 .It Fn atf_expect_exit "exitcode" "reason" "..."
222 Expects the test case to exit cleanly.
223 If
224 .Va exitcode
225 is not
226 .Sq -1 ,
227 the runtime engine will validate that the exit code of the test case
228 matches the one provided in this call.
229 Otherwise, the exact value will be ignored.
230 .It Fn atf_expect_fail "reason"
231 Any failure raised in this mode is recorded, but such failures do not report
232 the test case as failed; instead, the test case finalizes cleanly and is
233 reported as
234 .Sq expected failure ;
235 this report includes the provided
236 .Fa reason
237 as part of it.
238 If no error is raised while running in this mode, then the test case is
239 reported as
240 .Sq failed .
241 .Pp
242 This mode is useful to reproduce actual known bugs in tests.
243 Whenever the developer fixes the bug later on, the test case will start
244 reporting a failure, signaling the developer that the test case must be
245 adjusted to the new conditions.
246 In this situation, it is useful, for example, to set
247 .Fa reason
248 as the bug number for tracking purposes.
249 .It Fn atf_expect_pass
250 This is the normal mode of execution.
251 In this mode, any failure is reported as such to the user and the test case
252 is marked as
253 .Sq failed .
254 .It Fn atf_expect_signal "signo" "reason" "..."
255 Expects the test case to terminate due to the reception of a signal.
256 If
257 .Va signo
258 is not
259 .Sq -1 ,
260 the runtime engine will validate that the signal that terminated the test
261 case matches the one provided in this call.
262 Otherwise, the exact value will be ignored.
263 .It Fn atf_expect_timeout "reason" "..."
264 Expects the test case to execute for longer than its timeout.
265 .El
266 .Ss Helper functions for common checks
267 .Fn atf_check [options] command [args]
268 .Pp
269 This function wraps the execution of the
270 .Nm atf-check
271 tool and makes the test case fail if the tool reports failure.
272 You should always use this function instead of the tool in your scripts.
273 For more details on the parameters of this function, refer to
274 .Xr atf-check 1 .
275 .Pp
276 .Fn atf_check_equal expr1 expr2
277 .Pp
278 This function takes two expressions, evaluates them and, if their
279 results differ, aborts the test case with an appropriate failure message.
280 .Sh EXAMPLES
281 The following shows a complete test program with a single test case that
282 validates the addition operator:
283 .Bd -literal -offset indent
284 atf_test_case addition
285 addition_head() {
286     atf_set "descr" "Sample tests for the addition operator"
287 }
288 addition_body() {
289     atf_check_equal $((0 + 0)) 0
290     atf_check_equal $((0 + 1)) 1
291     atf_check_equal $((1 + 0)) 0
292
293     atf_check_equal $((1 + 1)) 2
294
295     atf_check_equal $((100 + 200)) 300
296 }
297
298 atf_init_test_cases() {
299     atf_add_test_case addition
300 }
301 .Ed
302 .Pp
303 This other example shows how to include a file with extra helper functions
304 in the test program:
305 .Bd -literal -offset indent
306 .Ns ... definition of test cases ...
307
308 atf_init_test_cases() {
309     . $(atf_get_srcdir)/helper_functions.sh
310
311     atf_add_test_case foo1
312     atf_add_test_case foo2
313 }
314 .Ed
315 .Pp
316 This example demonstrates the use of the very useful
317 .Fn atf_check
318 function:
319 .Bd -literal -offset indent
320 # Check for silent output
321 atf_check -s exit:0 -o empty -e empty 'true'
322
323 # Check for silent output and failure
324 atf_check -s exit:1 -o empty -e empty 'false'
325
326 # Check for known stdout and silent stderr
327 echo foo >expout
328 atf_check -s exit:0 -o file:expout -e empty 'echo foo'
329
330 # Generate a file for later inspection
331 atf_check -s exit:0 -o save:stdout -e empty 'ls'
332 grep foo ls || atf_fail "foo file not found in listing"
333
334 # Or just do the match along the way
335 atf_check -s exit:0 -o match:"^foo$" -e empty 'ls'
336 .Ed
337 .Sh SEE ALSO
338 .Xr atf-sh 1 ,
339 .Xr atf-test-program 1 ,
340 .Xr atf-test-case 4