111be35a1SLionel Sambuc.\" 211be35a1SLionel Sambuc.\" Automated Testing Framework (atf) 311be35a1SLionel Sambuc.\" 411be35a1SLionel Sambuc.\" Copyright (c) 2008 The NetBSD Foundation, Inc. 511be35a1SLionel Sambuc.\" All rights reserved. 611be35a1SLionel Sambuc.\" 711be35a1SLionel Sambuc.\" Redistribution and use in source and binary forms, with or without 811be35a1SLionel Sambuc.\" modification, are permitted provided that the following conditions 911be35a1SLionel Sambuc.\" are met: 1011be35a1SLionel Sambuc.\" 1. Redistributions of source code must retain the above copyright 1111be35a1SLionel Sambuc.\" notice, this list of conditions and the following disclaimer. 1211be35a1SLionel Sambuc.\" 2. Redistributions in binary form must reproduce the above copyright 1311be35a1SLionel Sambuc.\" notice, this list of conditions and the following disclaimer in the 1411be35a1SLionel Sambuc.\" documentation and/or other materials provided with the distribution. 1511be35a1SLionel Sambuc.\" 1611be35a1SLionel Sambuc.\" THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND 1711be35a1SLionel Sambuc.\" CONTRIBUTORS ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, 1811be35a1SLionel Sambuc.\" INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF 1911be35a1SLionel Sambuc.\" MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 2011be35a1SLionel Sambuc.\" IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS BE LIABLE FOR ANY 2111be35a1SLionel Sambuc.\" DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 2211be35a1SLionel Sambuc.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE 2311be35a1SLionel Sambuc.\" GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 2411be35a1SLionel Sambuc.\" INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER 2511be35a1SLionel Sambuc.\" IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR 2611be35a1SLionel Sambuc.\" OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN 2711be35a1SLionel Sambuc.\" IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 2811be35a1SLionel Sambuc.\" 29*0a6a1f1dSLionel Sambuc.Dd October 13, 2013 3011be35a1SLionel Sambuc.Dt ATF-SH-API 3 3111be35a1SLionel Sambuc.Os 3211be35a1SLionel Sambuc.Sh NAME 3311be35a1SLionel Sambuc.Nm atf_add_test_case , 3411be35a1SLionel Sambuc.Nm atf_check , 3511be35a1SLionel Sambuc.Nm atf_check_equal , 3611be35a1SLionel Sambuc.Nm atf_config_get , 3711be35a1SLionel Sambuc.Nm atf_config_has , 3811be35a1SLionel Sambuc.Nm atf_expect_death , 3911be35a1SLionel Sambuc.Nm atf_expect_exit , 4011be35a1SLionel Sambuc.Nm atf_expect_fail , 4111be35a1SLionel Sambuc.Nm atf_expect_pass , 4211be35a1SLionel Sambuc.Nm atf_expect_signal , 4311be35a1SLionel Sambuc.Nm atf_expect_timeout , 4411be35a1SLionel Sambuc.Nm atf_fail , 4511be35a1SLionel Sambuc.Nm atf_get , 4611be35a1SLionel Sambuc.Nm atf_get_srcdir , 4711be35a1SLionel Sambuc.Nm atf_pass , 4811be35a1SLionel Sambuc.Nm atf_require_prog , 4911be35a1SLionel Sambuc.Nm atf_set , 5011be35a1SLionel Sambuc.Nm atf_skip , 5111be35a1SLionel Sambuc.Nm atf_test_case 5211be35a1SLionel Sambuc.Nd POSIX shell API to write ATF-based test programs 5311be35a1SLionel Sambuc.Sh SYNOPSIS 5411be35a1SLionel Sambuc.Fn atf_add_test_case "name" 5511be35a1SLionel Sambuc.Fn atf_check "command" 5611be35a1SLionel Sambuc.Fn atf_check_equal "expr1" "expr2" 5711be35a1SLionel Sambuc.Fn atf_config_get "var_name" 5811be35a1SLionel Sambuc.Fn atf_config_has "var_name" 5911be35a1SLionel Sambuc.Fn atf_expect_death "reason" "..." 6011be35a1SLionel Sambuc.Fn atf_expect_exit "exitcode" "reason" "..." 6111be35a1SLionel Sambuc.Fn atf_expect_fail "reason" "..." 6211be35a1SLionel Sambuc.Fn atf_expect_pass 6311be35a1SLionel Sambuc.Fn atf_expect_signal "signo" "reason" "..." 6411be35a1SLionel Sambuc.Fn atf_expect_timeout "reason" "..." 6511be35a1SLionel Sambuc.Fn atf_fail "reason" 6611be35a1SLionel Sambuc.Fn atf_get "var_name" 6711be35a1SLionel Sambuc.Fn atf_get_srcdir 6811be35a1SLionel Sambuc.Fn atf_pass 6911be35a1SLionel Sambuc.Fn atf_require_prog "prog_name" 7011be35a1SLionel Sambuc.Fn atf_set "var_name" "value" 7111be35a1SLionel Sambuc.Fn atf_skip "reason" 7211be35a1SLionel Sambuc.Fn atf_test_case "name" "cleanup" 7311be35a1SLionel Sambuc.Sh DESCRIPTION 7411be35a1SLionel SambucATF 7511be35a1SLionel Sambucprovides a simple but powerful interface to easily write test programs in 7611be35a1SLionel Sambucthe POSIX shell language. 7711be35a1SLionel SambucThese are extremely helpful given that they are trivial to write due to the 7811be35a1SLionel Sambuclanguage simplicity and the great deal of available external tools, so they 7911be35a1SLionel Sambucare often ideal to test other applications at the user level. 8011be35a1SLionel Sambuc.Pp 8111be35a1SLionel SambucTest programs written using this library must be run using the 8211be35a1SLionel Sambuc.Xr atf-sh 1 8311be35a1SLionel Sambucinterpreter by putting the following on their very first line: 8411be35a1SLionel Sambuc.Bd -literal -offset indent 8511be35a1SLionel Sambuc#! /usr/bin/env atf-sh 8611be35a1SLionel Sambuc.Ed 8711be35a1SLionel Sambuc.Pp 8811be35a1SLionel SambucShell-based test programs always follow this template: 8911be35a1SLionel Sambuc.Bd -literal -offset indent 9011be35a1SLionel Sambucatf_test_case tc1 9111be35a1SLionel Sambuctc1_head() { 9211be35a1SLionel Sambuc ... first test case's header ... 9311be35a1SLionel Sambuc} 9411be35a1SLionel Sambuctc1_body() { 9511be35a1SLionel Sambuc ... first test case's body ... 9611be35a1SLionel Sambuc} 9711be35a1SLionel Sambuc 9811be35a1SLionel Sambucatf_test_case tc2 cleanup 9911be35a1SLionel Sambuctc2_head() { 10011be35a1SLionel Sambuc ... second test case's header ... 10111be35a1SLionel Sambuc} 10211be35a1SLionel Sambuctc2_body() { 10311be35a1SLionel Sambuc ... second test case's body ... 10411be35a1SLionel Sambuc} 10511be35a1SLionel Sambuctc2_cleanup() { 10611be35a1SLionel Sambuc ... second test case's cleanup ... 10711be35a1SLionel Sambuc} 10811be35a1SLionel Sambuc 10911be35a1SLionel Sambuc.Ns ... additional test cases ... 11011be35a1SLionel Sambuc 11111be35a1SLionel Sambucatf_init_test_cases() { 11211be35a1SLionel Sambuc atf_add_test_case tc1 11311be35a1SLionel Sambuc atf_add_test_case tc2 11411be35a1SLionel Sambuc ... add additional test cases ... 11511be35a1SLionel Sambuc} 11611be35a1SLionel Sambuc.Ed 11711be35a1SLionel Sambuc.Ss Definition of test cases 11811be35a1SLionel SambucTest cases have an identifier and are composed of three different parts: 11911be35a1SLionel Sambucthe header, the body and an optional cleanup routine, all of which are 12011be35a1SLionel Sambucdescribed in 12111be35a1SLionel Sambuc.Xr atf-test-case 4 . 12211be35a1SLionel SambucTo define test cases, one can use the 12311be35a1SLionel Sambuc.Fn atf_test_case 12411be35a1SLionel Sambucfunction, which takes a first parameter specifiying the test case's 12511be35a1SLionel Sambucname and instructs the library to set things up to accept it as a valid 12611be35a1SLionel Sambuctest case. 12711be35a1SLionel SambucThe second parameter is optional and, if provided, must be 12811be35a1SLionel Sambuc.Sq cleanup ; 12911be35a1SLionel Sambucproviding this parameter allows defining a cleanup routine for the test 13011be35a1SLionel Sambuccase. 13111be35a1SLionel SambucIt is important to note that this function 13211be35a1SLionel Sambuc.Em does not 13311be35a1SLionel Sambucset the test case up for execution when the program is run. 13411be35a1SLionel SambucIn order to do so, a later registration is needed through the 13511be35a1SLionel Sambuc.Fn atf_add_test_case 13611be35a1SLionel Sambucfunction detailed in 13711be35a1SLionel Sambuc.Sx Program initialization . 13811be35a1SLionel Sambuc.Pp 13911be35a1SLionel SambucLater on, one must define the three parts of the body by providing two 14011be35a1SLionel Sambucor three functions (remember that the cleanup routine is optional). 14111be35a1SLionel SambucThese functions are named after the test case's identifier, and are 14211be35a1SLionel Sambuc.Fn <id>_head , 14311be35a1SLionel Sambuc.Fn <id>_body 14411be35a1SLionel Sambucand 14511be35a1SLionel Sambuc.Fn <id>_cleanup. 14611be35a1SLionel SambucNone of these take parameters when executed. 14711be35a1SLionel Sambuc.Ss Program initialization 14811be35a1SLionel SambucThe test program must define an 14911be35a1SLionel Sambuc.Fn atf_init_test_cases 15011be35a1SLionel Sambucfunction, which is in charge of registering the test cases that will be 15111be35a1SLionel Sambucexecuted at run time by using the 15211be35a1SLionel Sambuc.Fn atf_add_test_case 15311be35a1SLionel Sambucfunction, which takes the name of a test case as its single parameter. 15411be35a1SLionel SambucThis main function should not do anything else, except maybe sourcing 15511be35a1SLionel Sambucauxiliary source files that define extra variables and functions. 15611be35a1SLionel Sambuc.Ss Configuration variables 15711be35a1SLionel SambucThe test case has read-only access to the current configuration variables 15811be35a1SLionel Sambucthrough the 15911be35a1SLionel Sambuc.Fn atf_config_has 16011be35a1SLionel Sambucand 16111be35a1SLionel Sambuc.Fn atf_config_get 16211be35a1SLionel Sambucmethods. 16311be35a1SLionel SambucThe former takes a single parameter specifying a variable name and returns 16411be35a1SLionel Sambuca boolean indicating whether the variable is defined or not. 16511be35a1SLionel SambucThe latter can take one or two parameters. 16611be35a1SLionel SambucIf it takes only one, it specifies the variable from which to get the 16711be35a1SLionel Sambucvalue, and this variable must be defined. 16811be35a1SLionel SambucIf it takes two, the second one specifies a default value to be returned 16911be35a1SLionel Sambucif the variable is not available. 17011be35a1SLionel Sambuc.Ss Access to the source directory 17111be35a1SLionel SambucIt is possible to get the path to the test case's source directory from 17211be35a1SLionel Sambucanywhere in the test program by using the 17311be35a1SLionel Sambuc.Fn atf_get_srcdir 17411be35a1SLionel Sambucfunction. 17511be35a1SLionel SambucIt is interesting to note that this can be used inside 17611be35a1SLionel Sambuc.Fn atf_init_test_cases 17711be35a1SLionel Sambucto silently include additional helper files from the source directory. 17811be35a1SLionel Sambuc.Ss Requiring programs 17911be35a1SLionel SambucAside from the 18011be35a1SLionel Sambuc.Va require.progs 18111be35a1SLionel Sambucmeta-data variable available in the header only, one can also check for 18211be35a1SLionel Sambucadditional programs in the test case's body by using the 18311be35a1SLionel Sambuc.Fn atf_require_prog 18411be35a1SLionel Sambucfunction, which takes the base name or full path of a single binary. 18511be35a1SLionel SambucRelative paths are forbidden. 18611be35a1SLionel SambucIf it is not found, the test case will be automatically skipped. 18711be35a1SLionel Sambuc.Ss Test case finalization 18811be35a1SLionel SambucThe test case finalizes either when the body reaches its end, at which 18911be35a1SLionel Sambucpoint the test is assumed to have 19011be35a1SLionel Sambuc.Em passed , 19111be35a1SLionel Sambucor at any explicit call to 19211be35a1SLionel Sambuc.Fn atf_pass , 19311be35a1SLionel Sambuc.Fn atf_fail 19411be35a1SLionel Sambucor 19511be35a1SLionel Sambuc.Fn atf_skip . 19611be35a1SLionel SambucThese three functions terminate the execution of the test case immediately. 19711be35a1SLionel SambucThe cleanup routine will be processed afterwards in a completely automated 19811be35a1SLionel Sambucway, regardless of the test case's termination reason. 19911be35a1SLionel Sambuc.Pp 20011be35a1SLionel Sambuc.Fn atf_pass 20111be35a1SLionel Sambucdoes not take any parameters. 20211be35a1SLionel Sambuc.Fn atf_fail 20311be35a1SLionel Sambucand 20411be35a1SLionel Sambuc.Fn atf_skip 20511be35a1SLionel Sambuctake a single string parameter that describes why the test case failed or 20611be35a1SLionel Sambucwas skipped, respectively. 20711be35a1SLionel SambucIt is very important to provide a clear error message in both cases so that 20811be35a1SLionel Sambucthe user can quickly know why the test did not pass. 20911be35a1SLionel Sambuc.Ss Expectations 21011be35a1SLionel SambucEverything explained in the previous section changes when the test case 21111be35a1SLionel Sambucexpectations are redefined by the programmer. 21211be35a1SLionel Sambuc.Pp 21311be35a1SLionel SambucEach test case has an internal state called 21411be35a1SLionel Sambuc.Sq expect 21511be35a1SLionel Sambucthat describes what the test case expectations are at any point in time. 21611be35a1SLionel SambucThe value of this property can change during execution by any of: 21711be35a1SLionel Sambuc.Bl -tag -width indent 21811be35a1SLionel Sambuc.It Fn atf_expect_death "reason" "..." 21911be35a1SLionel SambucExpects the test case to exit prematurely regardless of the nature of the 22011be35a1SLionel Sambucexit. 22111be35a1SLionel Sambuc.It Fn atf_expect_exit "exitcode" "reason" "..." 22211be35a1SLionel SambucExpects the test case to exit cleanly. 22311be35a1SLionel SambucIf 22411be35a1SLionel Sambuc.Va exitcode 22511be35a1SLionel Sambucis not 22611be35a1SLionel Sambuc.Sq -1 , 22711be35a1SLionel Sambuc.Xr atf-run 1 22811be35a1SLionel Sambucwill validate that the exit code of the test case matches the one provided 22911be35a1SLionel Sambucin this call. 23011be35a1SLionel SambucOtherwise, the exact value will be ignored. 23111be35a1SLionel Sambuc.It Fn atf_expect_fail "reason" 23211be35a1SLionel SambucAny failure raised in this mode is recorded, but such failures do not report 23311be35a1SLionel Sambucthe test case as failed; instead, the test case finalizes cleanly and is 23411be35a1SLionel Sambucreported as 23511be35a1SLionel Sambuc.Sq expected failure ; 23611be35a1SLionel Sambucthis report includes the provided 23711be35a1SLionel Sambuc.Fa reason 23811be35a1SLionel Sambucas part of it. 23911be35a1SLionel SambucIf no error is raised while running in this mode, then the test case is 24011be35a1SLionel Sambucreported as 24111be35a1SLionel Sambuc.Sq failed . 24211be35a1SLionel Sambuc.Pp 24311be35a1SLionel SambucThis mode is useful to reproduce actual known bugs in tests. 24411be35a1SLionel SambucWhenever the developer fixes the bug later on, the test case will start 24511be35a1SLionel Sambucreporting a failure, signaling the developer that the test case must be 24611be35a1SLionel Sambucadjusted to the new conditions. 24711be35a1SLionel SambucIn this situation, it is useful, for example, to set 24811be35a1SLionel Sambuc.Fa reason 24911be35a1SLionel Sambucas the bug number for tracking purposes. 25011be35a1SLionel Sambuc.It Fn atf_expect_pass 25111be35a1SLionel SambucThis is the normal mode of execution. 25211be35a1SLionel SambucIn this mode, any failure is reported as such to the user and the test case 25311be35a1SLionel Sambucis marked as 25411be35a1SLionel Sambuc.Sq failed . 25511be35a1SLionel Sambuc.It Fn atf_expect_signal "signo" "reason" "..." 25611be35a1SLionel SambucExpects the test case to terminate due to the reception of a signal. 25711be35a1SLionel SambucIf 25811be35a1SLionel Sambuc.Va signo 25911be35a1SLionel Sambucis not 26011be35a1SLionel Sambuc.Sq -1 , 26111be35a1SLionel Sambuc.Xr atf-run 1 26211be35a1SLionel Sambucwill validate that the signal that terminated the test case matches the one 26311be35a1SLionel Sambucprovided in this call. 26411be35a1SLionel SambucOtherwise, the exact value will be ignored. 26511be35a1SLionel Sambuc.It Fn atf_expect_timeout "reason" "..." 26611be35a1SLionel SambucExpects the test case to execute for longer than its timeout. 26711be35a1SLionel Sambuc.El 26811be35a1SLionel Sambuc.Ss Helper functions for common checks 26911be35a1SLionel Sambuc.Fn atf_check [options] command [args] 27011be35a1SLionel Sambuc.Pp 27111be35a1SLionel SambucThis function wraps the execution of the 27211be35a1SLionel Sambuc.Nm atf-check 27311be35a1SLionel Sambuctool and makes the test case fail if the tool reports failure. 27411be35a1SLionel SambucYou should always use this function instead of the tool in your scripts. 27511be35a1SLionel SambucFor more details on the parameters of this function, refer to 27611be35a1SLionel Sambuc.Xr atf-check 1 . 27711be35a1SLionel Sambuc.Pp 27811be35a1SLionel Sambuc.Fn atf_check_equal expr1 expr2 27911be35a1SLionel Sambuc.Pp 28011be35a1SLionel SambucThis function takes two expressions, evaluates them and, if their 28111be35a1SLionel Sambucresults differ, aborts the test case with an appropriate failure message. 28211be35a1SLionel Sambuc.Sh EXAMPLES 28311be35a1SLionel SambucThe following shows a complete test program with a single test case that 28411be35a1SLionel Sambucvalidates the addition operator: 28511be35a1SLionel Sambuc.Bd -literal -offset indent 28611be35a1SLionel Sambucatf_test_case addition 28711be35a1SLionel Sambucaddition_head() { 28811be35a1SLionel Sambuc atf_set "descr" "Sample tests for the addition operator" 28911be35a1SLionel Sambuc} 29011be35a1SLionel Sambucaddition_body() { 29111be35a1SLionel Sambuc atf_check_equal $((0 + 0)) 0 29211be35a1SLionel Sambuc atf_check_equal $((0 + 1)) 1 29311be35a1SLionel Sambuc atf_check_equal $((1 + 0)) 0 29411be35a1SLionel Sambuc 29511be35a1SLionel Sambuc atf_check_equal $((1 + 1)) 2 29611be35a1SLionel Sambuc 29711be35a1SLionel Sambuc atf_check_equal $((100 + 200)) 300 29811be35a1SLionel Sambuc} 29911be35a1SLionel Sambuc 30011be35a1SLionel Sambucatf_init_test_cases() { 30111be35a1SLionel Sambuc atf_add_test_case addition 30211be35a1SLionel Sambuc} 30311be35a1SLionel Sambuc.Ed 30411be35a1SLionel Sambuc.Pp 30511be35a1SLionel SambucThis other example shows how to include a file with extra helper functions 30611be35a1SLionel Sambucin the test program: 30711be35a1SLionel Sambuc.Bd -literal -offset indent 30811be35a1SLionel Sambuc.Ns ... definition of test cases ... 30911be35a1SLionel Sambuc 31011be35a1SLionel Sambucatf_init_test_cases() { 31111be35a1SLionel Sambuc . $(atf_get_srcdir)/helper_functions.sh 31211be35a1SLionel Sambuc 31311be35a1SLionel Sambuc atf_add_test_case foo1 31411be35a1SLionel Sambuc atf_add_test_case foo2 31511be35a1SLionel Sambuc} 31611be35a1SLionel Sambuc.Ed 31711be35a1SLionel Sambuc.Pp 31811be35a1SLionel SambucThis example demonstrates the use of the very useful 31911be35a1SLionel Sambuc.Fn atf_check 32011be35a1SLionel Sambucfunction: 32111be35a1SLionel Sambuc.Bd -literal -offset indent 32211be35a1SLionel Sambuc# Check for silent output 323*0a6a1f1dSLionel Sambucatf_check -s exit:0 -o empty -e empty 'true' 32411be35a1SLionel Sambuc 32511be35a1SLionel Sambuc# Check for silent output and failure 326*0a6a1f1dSLionel Sambucatf_check -s exit:1 -o empty -e empty 'false' 32711be35a1SLionel Sambuc 32811be35a1SLionel Sambuc# Check for known stdout and silent stderr 32911be35a1SLionel Sambucecho foo >expout 330*0a6a1f1dSLionel Sambucatf_check -s exit:0 -o file:expout -e empty 'echo foo' 33111be35a1SLionel Sambuc 33211be35a1SLionel Sambuc# Generate a file for later inspection 333*0a6a1f1dSLionel Sambucatf_check -s exit:0 -o save:stdout -e empty 'ls' 33411be35a1SLionel Sambucgrep foo ls || atf_fail "foo file not found in listing" 335*0a6a1f1dSLionel Sambuc 336*0a6a1f1dSLionel Sambuc# Or just do the match along the way 337*0a6a1f1dSLionel Sambucatf_check -s exit:0 -o match:"^foo$" -e empty 'ls' 33811be35a1SLionel Sambuc.Ed 33911be35a1SLionel Sambuc.Sh SEE ALSO 34011be35a1SLionel Sambuc.Xr atf-sh 1 , 34111be35a1SLionel Sambuc.Xr atf-test-program 1 , 34211be35a1SLionel Sambuc.Xr atf-test-case 4 , 34311be35a1SLionel Sambuc.Xr atf 7 344