10677dfd1SJulio Merino.\" Copyright (c) 2008 The NetBSD Foundation, Inc. 20677dfd1SJulio Merino.\" All rights reserved. 30677dfd1SJulio Merino.\" 40677dfd1SJulio Merino.\" Redistribution and use in source and binary forms, with or without 50677dfd1SJulio Merino.\" modification, are permitted provided that the following conditions 60677dfd1SJulio Merino.\" are met: 70677dfd1SJulio Merino.\" 1. Redistributions of source code must retain the above copyright 80677dfd1SJulio Merino.\" notice, this list of conditions and the following disclaimer. 90677dfd1SJulio Merino.\" 2. Redistributions in binary form must reproduce the above copyright 100677dfd1SJulio Merino.\" notice, this list of conditions and the following disclaimer in the 110677dfd1SJulio Merino.\" documentation and/or other materials provided with the distribution. 120677dfd1SJulio Merino.\" 130677dfd1SJulio Merino.\" THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND 140677dfd1SJulio Merino.\" CONTRIBUTORS ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, 150677dfd1SJulio Merino.\" INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF 160677dfd1SJulio Merino.\" MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 170677dfd1SJulio Merino.\" IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS BE LIABLE FOR ANY 180677dfd1SJulio Merino.\" DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 190677dfd1SJulio Merino.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE 200677dfd1SJulio Merino.\" GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 210677dfd1SJulio Merino.\" INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER 220677dfd1SJulio Merino.\" IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR 230677dfd1SJulio Merino.\" OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN 240677dfd1SJulio Merino.\" IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 25*ba31e895SEnji Cooper.Dd March 6, 2017 260677dfd1SJulio Merino.Dt ATF-SH 3 270677dfd1SJulio Merino.Os 280677dfd1SJulio Merino.Sh NAME 290677dfd1SJulio Merino.Nm atf_add_test_case , 300677dfd1SJulio Merino.Nm atf_check , 310677dfd1SJulio Merino.Nm atf_check_equal , 320677dfd1SJulio Merino.Nm atf_config_get , 330677dfd1SJulio Merino.Nm atf_config_has , 340677dfd1SJulio Merino.Nm atf_expect_death , 350677dfd1SJulio Merino.Nm atf_expect_exit , 360677dfd1SJulio Merino.Nm atf_expect_fail , 370677dfd1SJulio Merino.Nm atf_expect_pass , 380677dfd1SJulio Merino.Nm atf_expect_signal , 390677dfd1SJulio Merino.Nm atf_expect_timeout , 400677dfd1SJulio Merino.Nm atf_fail , 410677dfd1SJulio Merino.Nm atf_get , 420677dfd1SJulio Merino.Nm atf_get_srcdir , 430677dfd1SJulio Merino.Nm atf_pass , 440677dfd1SJulio Merino.Nm atf_require_prog , 450677dfd1SJulio Merino.Nm atf_set , 460677dfd1SJulio Merino.Nm atf_skip , 470677dfd1SJulio Merino.Nm atf_test_case 480677dfd1SJulio Merino.Nd POSIX shell API to write ATF-based test programs 490677dfd1SJulio Merino.Sh SYNOPSIS 500677dfd1SJulio Merino.Nm atf_add_test_case 510677dfd1SJulio Merino.Qq name 520677dfd1SJulio Merino.Nm atf_check 530677dfd1SJulio Merino.Qq command 540677dfd1SJulio Merino.Nm atf_check_equal 550677dfd1SJulio Merino.Qq expected_expression 560677dfd1SJulio Merino.Qq actual_expression 570677dfd1SJulio Merino.Nm atf_config_get 580677dfd1SJulio Merino.Qq var_name 590677dfd1SJulio Merino.Nm atf_config_has 600677dfd1SJulio Merino.Qq var_name 610677dfd1SJulio Merino.Nm atf_expect_death 620677dfd1SJulio Merino.Qq reason 630677dfd1SJulio Merino.Qq ... 640677dfd1SJulio Merino.Nm atf_expect_exit 650677dfd1SJulio Merino.Qq exitcode 660677dfd1SJulio Merino.Qq reason 670677dfd1SJulio Merino.Qq ... 680677dfd1SJulio Merino.Nm atf_expect_fail 690677dfd1SJulio Merino.Qq reason 700677dfd1SJulio Merino.Qq ... 710677dfd1SJulio Merino.Nm atf_expect_pass 720677dfd1SJulio Merino.Qq 730677dfd1SJulio Merino.Nm atf_expect_signal 740677dfd1SJulio Merino.Qq signo 750677dfd1SJulio Merino.Qq reason 760677dfd1SJulio Merino.Qq ... 770677dfd1SJulio Merino.Nm atf_expect_timeout 780677dfd1SJulio Merino.Qq reason 790677dfd1SJulio Merino.Qq ... 800677dfd1SJulio Merino.Nm atf_fail 810677dfd1SJulio Merino.Qq reason 820677dfd1SJulio Merino.Nm atf_get 830677dfd1SJulio Merino.Qq var_name 840677dfd1SJulio Merino.Nm atf_get_srcdir 850677dfd1SJulio Merino.Nm atf_pass 860677dfd1SJulio Merino.Nm atf_require_prog 870677dfd1SJulio Merino.Qq prog_name 880677dfd1SJulio Merino.Nm atf_set 890677dfd1SJulio Merino.Qq var_name 900677dfd1SJulio Merino.Qq value 910677dfd1SJulio Merino.Nm atf_skip 920677dfd1SJulio Merino.Qq reason 930677dfd1SJulio Merino.Nm atf_test_case 940677dfd1SJulio Merino.Qq name 950677dfd1SJulio Merino.Qq cleanup 960677dfd1SJulio Merino.Sh DESCRIPTION 970677dfd1SJulio MerinoATF 980677dfd1SJulio Merinoprovides a simple but powerful interface to easily write test programs in 990677dfd1SJulio Merinothe POSIX shell language. 1000677dfd1SJulio MerinoThese are extremely helpful given that they are trivial to write due to the 1010677dfd1SJulio Merinolanguage simplicity and the great deal of available external tools, so they 1020677dfd1SJulio Merinoare often ideal to test other applications at the user level. 1030677dfd1SJulio Merino.Pp 1040677dfd1SJulio MerinoTest programs written using this library must be run using the 1050677dfd1SJulio Merino.Xr atf-sh 1 1060677dfd1SJulio Merinointerpreter by putting the following on their very first line: 1070677dfd1SJulio Merino.Bd -literal -offset indent 1080677dfd1SJulio Merino#! /usr/bin/env atf-sh 1090677dfd1SJulio Merino.Ed 1100677dfd1SJulio Merino.Pp 1110677dfd1SJulio MerinoShell-based test programs always follow this template: 1120677dfd1SJulio Merino.Bd -literal -offset indent 1130677dfd1SJulio Merinoatf_test_case tc1 1140677dfd1SJulio Merinotc1_head() { 1150677dfd1SJulio Merino ... first test case's header ... 1160677dfd1SJulio Merino} 1170677dfd1SJulio Merinotc1_body() { 1180677dfd1SJulio Merino ... first test case's body ... 1190677dfd1SJulio Merino} 1200677dfd1SJulio Merino 1210677dfd1SJulio Merinoatf_test_case tc2 cleanup 1220677dfd1SJulio Merinotc2_head() { 1230677dfd1SJulio Merino ... second test case's header ... 1240677dfd1SJulio Merino} 1250677dfd1SJulio Merinotc2_body() { 1260677dfd1SJulio Merino ... second test case's body ... 1270677dfd1SJulio Merino} 1280677dfd1SJulio Merinotc2_cleanup() { 1290677dfd1SJulio Merino ... second test case's cleanup ... 1300677dfd1SJulio Merino} 1310677dfd1SJulio Merino 132*ba31e895SEnji Cooper\&... additional test cases ... 1330677dfd1SJulio Merino 1340677dfd1SJulio Merinoatf_init_test_cases() { 1350677dfd1SJulio Merino atf_add_test_case tc1 1360677dfd1SJulio Merino atf_add_test_case tc2 1370677dfd1SJulio Merino ... add additional test cases ... 1380677dfd1SJulio Merino} 1390677dfd1SJulio Merino.Ed 1400677dfd1SJulio Merino.Ss Definition of test cases 1410677dfd1SJulio MerinoTest cases have an identifier and are composed of three different parts: 1420677dfd1SJulio Merinothe header, the body and an optional cleanup routine, all of which are 1430677dfd1SJulio Merinodescribed in 1440677dfd1SJulio Merino.Xr atf-test-case 4 . 1450677dfd1SJulio MerinoTo define test cases, one can use the 1460677dfd1SJulio Merino.Nm atf_test_case 147*ba31e895SEnji Cooperfunction, which takes a first parameter specifying the test case's 1480677dfd1SJulio Merinoname and instructs the library to set things up to accept it as a valid 1490677dfd1SJulio Merinotest case. 1500677dfd1SJulio MerinoThe second parameter is optional and, if provided, must be 1510677dfd1SJulio Merino.Sq cleanup ; 1520677dfd1SJulio Merinoproviding this parameter allows defining a cleanup routine for the test 1530677dfd1SJulio Merinocase. 1540677dfd1SJulio MerinoIt is important to note that this function 1550677dfd1SJulio Merino.Em does not 1560677dfd1SJulio Merinoset the test case up for execution when the program is run. 1570677dfd1SJulio MerinoIn order to do so, a later registration is needed through the 1580677dfd1SJulio Merino.Nm atf_add_test_case 1590677dfd1SJulio Merinofunction detailed in 1600677dfd1SJulio Merino.Sx Program initialization . 1610677dfd1SJulio Merino.Pp 1620677dfd1SJulio MerinoLater on, one must define the three parts of the body by providing two 1630677dfd1SJulio Merinoor three functions (remember that the cleanup routine is optional). 1640677dfd1SJulio MerinoThese functions are named after the test case's identifier, and are 1650677dfd1SJulio Merino.Nm \*(Ltid\*(Gt_head , 1660677dfd1SJulio Merino.Nm \*(Ltid\*(Gt_body 1670677dfd1SJulio Merinoand 1680677dfd1SJulio Merino.Nm \*(Ltid\*(Gt_cleanup . 1690677dfd1SJulio MerinoNone of these take parameters when executed. 1700677dfd1SJulio Merino.Ss Program initialization 1710677dfd1SJulio MerinoThe test program must define an 1720677dfd1SJulio Merino.Nm atf_init_test_cases 1730677dfd1SJulio Merinofunction, which is in charge of registering the test cases that will be 1740677dfd1SJulio Merinoexecuted at run time by using the 1750677dfd1SJulio Merino.Nm atf_add_test_case 1760677dfd1SJulio Merinofunction, which takes the name of a test case as its single parameter. 1770677dfd1SJulio MerinoThis main function should not do anything else, except maybe sourcing 1780677dfd1SJulio Merinoauxiliary source files that define extra variables and functions. 1790677dfd1SJulio Merino.Ss Configuration variables 1800677dfd1SJulio MerinoThe test case has read-only access to the current configuration variables 1810677dfd1SJulio Merinothrough the 1820677dfd1SJulio Merino.Nm atf_config_has 1830677dfd1SJulio Merinoand 1840677dfd1SJulio Merino.Nm atf_config_get 1850677dfd1SJulio Merinomethods. 1860677dfd1SJulio MerinoThe former takes a single parameter specifying a variable name and returns 1870677dfd1SJulio Merinoa boolean indicating whether the variable is defined or not. 1880677dfd1SJulio MerinoThe latter can take one or two parameters. 1890677dfd1SJulio MerinoIf it takes only one, it specifies the variable from which to get the 1900677dfd1SJulio Merinovalue, and this variable must be defined. 1910677dfd1SJulio MerinoIf it takes two, the second one specifies a default value to be returned 1920677dfd1SJulio Merinoif the variable is not available. 1930677dfd1SJulio Merino.Ss Access to the source directory 1940677dfd1SJulio MerinoIt is possible to get the path to the test case's source directory from 1950677dfd1SJulio Merinoanywhere in the test program by using the 1960677dfd1SJulio Merino.Nm atf_get_srcdir 1970677dfd1SJulio Merinofunction. 1980677dfd1SJulio MerinoIt is interesting to note that this can be used inside 1990677dfd1SJulio Merino.Nm atf_init_test_cases 2000677dfd1SJulio Merinoto silently include additional helper files from the source directory. 2010677dfd1SJulio Merino.Ss Requiring programs 2020677dfd1SJulio MerinoAside from the 2030677dfd1SJulio Merino.Va require.progs 2040677dfd1SJulio Merinometa-data variable available in the header only, one can also check for 2050677dfd1SJulio Merinoadditional programs in the test case's body by using the 2060677dfd1SJulio Merino.Nm atf_require_prog 2070677dfd1SJulio Merinofunction, which takes the base name or full path of a single binary. 2080677dfd1SJulio MerinoRelative paths are forbidden. 2090677dfd1SJulio MerinoIf it is not found, the test case will be automatically skipped. 2100677dfd1SJulio Merino.Ss Test case finalization 2110677dfd1SJulio MerinoThe test case finalizes either when the body reaches its end, at which 2120677dfd1SJulio Merinopoint the test is assumed to have 2130677dfd1SJulio Merino.Em passed , 2140677dfd1SJulio Merinoor at any explicit call to 2150677dfd1SJulio Merino.Nm atf_pass , 2160677dfd1SJulio Merino.Nm atf_fail 2170677dfd1SJulio Merinoor 2180677dfd1SJulio Merino.Nm atf_skip . 2190677dfd1SJulio MerinoThese three functions terminate the execution of the test case immediately. 2200677dfd1SJulio MerinoThe cleanup routine will be processed afterwards in a completely automated 2210677dfd1SJulio Merinoway, regardless of the test case's termination reason. 2220677dfd1SJulio Merino.Pp 2230677dfd1SJulio Merino.Nm atf_pass 2240677dfd1SJulio Merinodoes not take any parameters. 2250677dfd1SJulio Merino.Nm atf_fail 2260677dfd1SJulio Merinoand 2270677dfd1SJulio Merino.Nm atf_skip 2280677dfd1SJulio Merinotake a single string parameter that describes why the test case failed or 2290677dfd1SJulio Merinowas skipped, respectively. 2300677dfd1SJulio MerinoIt is very important to provide a clear error message in both cases so that 2310677dfd1SJulio Merinothe user can quickly know why the test did not pass. 2320677dfd1SJulio Merino.Ss Expectations 2330677dfd1SJulio MerinoEverything explained in the previous section changes when the test case 2340677dfd1SJulio Merinoexpectations are redefined by the programmer. 2350677dfd1SJulio Merino.Pp 2360677dfd1SJulio MerinoEach test case has an internal state called 2370677dfd1SJulio Merino.Sq expect 2380677dfd1SJulio Merinothat describes what the test case expectations are at any point in time. 2390677dfd1SJulio MerinoThe value of this property can change during execution by any of: 2400677dfd1SJulio Merino.Bl -tag -width indent 2410677dfd1SJulio Merino.It Nm atf_expect_death Qo reason Qc Qo ... Qc 2420677dfd1SJulio MerinoExpects the test case to exit prematurely regardless of the nature of the 2430677dfd1SJulio Merinoexit. 2440677dfd1SJulio Merino.It Nm atf_expect_exit Qo exitcode Qc Qo reason Qc Qo ... Qc 2450677dfd1SJulio MerinoExpects the test case to exit cleanly. 2460677dfd1SJulio MerinoIf 2470677dfd1SJulio Merino.Va exitcode 2480677dfd1SJulio Merinois not 2490677dfd1SJulio Merino.Sq -1 , 2500677dfd1SJulio Merinothe runtime engine will validate that the exit code of the test case 2510677dfd1SJulio Merinomatches the one provided in this call. 2520677dfd1SJulio MerinoOtherwise, the exact value will be ignored. 2530677dfd1SJulio Merino.It Nm atf_expect_fail Qo reason Qc 2540677dfd1SJulio MerinoAny failure raised in this mode is recorded, but such failures do not report 2550677dfd1SJulio Merinothe test case as failed; instead, the test case finalizes cleanly and is 2560677dfd1SJulio Merinoreported as 2570677dfd1SJulio Merino.Sq expected failure ; 2580677dfd1SJulio Merinothis report includes the provided 2590677dfd1SJulio Merino.Fa reason 2600677dfd1SJulio Merinoas part of it. 2610677dfd1SJulio MerinoIf no error is raised while running in this mode, then the test case is 2620677dfd1SJulio Merinoreported as 2630677dfd1SJulio Merino.Sq failed . 2640677dfd1SJulio Merino.Pp 2650677dfd1SJulio MerinoThis mode is useful to reproduce actual known bugs in tests. 2660677dfd1SJulio MerinoWhenever the developer fixes the bug later on, the test case will start 2670677dfd1SJulio Merinoreporting a failure, signaling the developer that the test case must be 2680677dfd1SJulio Merinoadjusted to the new conditions. 2690677dfd1SJulio MerinoIn this situation, it is useful, for example, to set 2700677dfd1SJulio Merino.Fa reason 2710677dfd1SJulio Merinoas the bug number for tracking purposes. 2720677dfd1SJulio Merino.It Nm atf_expect_pass 2730677dfd1SJulio MerinoThis is the normal mode of execution. 2740677dfd1SJulio MerinoIn this mode, any failure is reported as such to the user and the test case 2750677dfd1SJulio Merinois marked as 2760677dfd1SJulio Merino.Sq failed . 2770677dfd1SJulio Merino.It Nm atf_expect_signal Qo signo Qc Qo reason Qc Qo ... Qc 2780677dfd1SJulio MerinoExpects the test case to terminate due to the reception of a signal. 2790677dfd1SJulio MerinoIf 2800677dfd1SJulio Merino.Va signo 2810677dfd1SJulio Merinois not 2820677dfd1SJulio Merino.Sq -1 , 2830677dfd1SJulio Merinothe runtime engine will validate that the signal that terminated the test 2840677dfd1SJulio Merinocase matches the one provided in this call. 2850677dfd1SJulio MerinoOtherwise, the exact value will be ignored. 2860677dfd1SJulio Merino.It Nm atf_expect_timeout Qo reason Qc Qo ... Qc 2870677dfd1SJulio MerinoExpects the test case to execute for longer than its timeout. 2880677dfd1SJulio Merino.El 2890677dfd1SJulio Merino.Ss Helper functions for common checks 2900677dfd1SJulio Merino.Bl -tag -width indent 2910677dfd1SJulio Merino.It Nm atf_check Qo [options] Qc Qo command Qc Qo [args] Qc 2920677dfd1SJulio MerinoExecutes a command, performs checks on its exit code and its output, and 2930677dfd1SJulio Merinofails the test case if any of the checks is not successful. 2940677dfd1SJulio MerinoThis function is particularly useful in integration tests that verify the 2950677dfd1SJulio Merinocorrect functioning of a binary. 2960677dfd1SJulio Merino.Pp 2970677dfd1SJulio MerinoInternally, this function is just a wrapper over the 2980677dfd1SJulio Merino.Xr atf-check 1 2990677dfd1SJulio Merinotool (whose manual page provides all details on the calling syntax). 3000677dfd1SJulio MerinoYou should always use the 3010677dfd1SJulio Merino.Nm atf_check 3020677dfd1SJulio Merinofunction instead of the 3030677dfd1SJulio Merino.Xr atf-check 1 3040677dfd1SJulio Merinotool in your scripts; the latter is not even in the path. 3050677dfd1SJulio Merino.It Nm atf_check_equal Qo expected_expression Qc Qo actual_expression Qc 3060677dfd1SJulio MerinoThis function takes two expressions, evaluates them and, if their 3070677dfd1SJulio Merinoresults differ, aborts the test case with an appropriate failure message. 3080677dfd1SJulio MerinoThe common style is to put the expected value in the first parameter and the 3090677dfd1SJulio Merinoactual value in the second parameter. 3100677dfd1SJulio Merino.El 3110677dfd1SJulio Merino.Sh EXAMPLES 3120677dfd1SJulio MerinoThe following shows a complete test program with a single test case that 3130677dfd1SJulio Merinovalidates the addition operator: 3140677dfd1SJulio Merino.Bd -literal -offset indent 3150677dfd1SJulio Merinoatf_test_case addition 3160677dfd1SJulio Merinoaddition_head() { 3170677dfd1SJulio Merino atf_set "descr" "Sample tests for the addition operator" 3180677dfd1SJulio Merino} 3190677dfd1SJulio Merinoaddition_body() { 3200677dfd1SJulio Merino atf_check_equal 0 $((0 + 0)) 3210677dfd1SJulio Merino atf_check_equal 1 $((0 + 1)) 3220677dfd1SJulio Merino atf_check_equal 1 $((1 + 0)) 3230677dfd1SJulio Merino 3240677dfd1SJulio Merino atf_check_equal 2 $((1 + 1)) 3250677dfd1SJulio Merino 3260677dfd1SJulio Merino atf_check_equal 300 $((100 + 200)) 3270677dfd1SJulio Merino} 3280677dfd1SJulio Merino 3290677dfd1SJulio Merinoatf_init_test_cases() { 3300677dfd1SJulio Merino atf_add_test_case addition 3310677dfd1SJulio Merino} 3320677dfd1SJulio Merino.Ed 3330677dfd1SJulio Merino.Pp 3340677dfd1SJulio MerinoThis other example shows how to include a file with extra helper functions 3350677dfd1SJulio Merinoin the test program: 3360677dfd1SJulio Merino.Bd -literal -offset indent 337*ba31e895SEnji Cooper\&... definition of test cases ... 3380677dfd1SJulio Merino 3390677dfd1SJulio Merinoatf_init_test_cases() { 3400677dfd1SJulio Merino . $(atf_get_srcdir)/helper_functions.sh 3410677dfd1SJulio Merino 3420677dfd1SJulio Merino atf_add_test_case foo1 3430677dfd1SJulio Merino atf_add_test_case foo2 3440677dfd1SJulio Merino} 3450677dfd1SJulio Merino.Ed 3460677dfd1SJulio Merino.Pp 3470677dfd1SJulio MerinoThis example demonstrates the use of the very useful 3480677dfd1SJulio Merino.Nm atf_check 3490677dfd1SJulio Merinofunction: 3500677dfd1SJulio Merino.Bd -literal -offset indent 3510677dfd1SJulio Merino# Check for silent output 3520677dfd1SJulio Merinoatf_check -s exit:0 -o empty -e empty 'true' 3530677dfd1SJulio Merino 3540677dfd1SJulio Merino# Check for silent output and failure 3550677dfd1SJulio Merinoatf_check -s exit:1 -o empty -e empty 'false' 3560677dfd1SJulio Merino 3570677dfd1SJulio Merino# Check for known stdout and silent stderr 3580677dfd1SJulio Merinoecho foo >expout 3590677dfd1SJulio Merinoatf_check -s exit:0 -o file:expout -e empty 'echo foo' 3600677dfd1SJulio Merino 3610677dfd1SJulio Merino# Generate a file for later inspection 3620677dfd1SJulio Merinoatf_check -s exit:0 -o save:stdout -e empty 'ls' 3630677dfd1SJulio Merinogrep foo ls || atf_fail "foo file not found in listing" 3640677dfd1SJulio Merino 3650677dfd1SJulio Merino# Or just do the match along the way 3660677dfd1SJulio Merinoatf_check -s exit:0 -o match:"^foo$" -e empty 'ls' 3670677dfd1SJulio Merino.Ed 3680677dfd1SJulio Merino.Sh SEE ALSO 3690677dfd1SJulio Merino.Xr atf-check 1 , 3700677dfd1SJulio Merino.Xr atf-sh 1 , 3710677dfd1SJulio Merino.Xr atf-test-program 1 , 3720677dfd1SJulio Merino.Xr atf-test-case 4 373