Home | History | Annotate | Line # | Download | only in test
      1  1.1  haad #!/bin/sh
      2  1.1  haad # Derived from git's t/test-lib.sh, which is Copyright (c) 2005 Junio C Hamano
      3  1.1  haad #
      4  1.1  haad # This copyrighted material is made available to anyone wishing to use,
      5  1.1  haad # modify, copy, or redistribute it subject to the terms and conditions
      6  1.1  haad # of the GNU General Public License v.2.
      7  1.1  haad #
      8  1.1  haad # You should have received a copy of the GNU General Public License
      9  1.1  haad # along with this program; if not, write to the Free Software Foundation,
     10  1.1  haad # Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
     11  1.1  haad 
     12  1.1  haad # For repeatability, reset the environment to known value.
     13  1.1  haad LANG=C
     14  1.1  haad LC_ALL=C
     15  1.1  haad TZ=UTC
     16  1.1  haad export LANG LC_ALL TZ
     17  1.1  haad 
     18  1.1  haad . ./init.sh || { echo >&2 you must run make first; exit 1; }
     19  1.1  haad 
     20  1.1  haad # Protect ourselves from common misconfiguration to export
     21  1.1  haad # CDPATH into the environment
     22  1.1  haad unset CDPATH
     23  1.1  haad 
     24  1.1  haad # Each test should start with something like this, after copyright notices:
     25  1.1  haad #
     26  1.1  haad # test_description='Description of this test...
     27  1.1  haad # This test checks if command xyzzy does the right thing...
     28  1.1  haad # '
     29  1.1  haad # . ./test-lib.sh
     30  1.1  haad 
     31  1.1  haad error () {
     32  1.1  haad 	echo "* error: $*"
     33  1.1  haad 	exit 1
     34  1.1  haad }
     35  1.1  haad 
     36  1.1  haad say () {
     37  1.1  haad 	echo "* $*"
     38  1.1  haad }
     39  1.1  haad 
     40  1.1  haad this_test_() { expr "./$0" : '.*/t-\([^/]*\)\.sh$'; }
     41  1.1  haad 
     42  1.1  haad test "${test_description}" != "" ||
     43  1.1  haad error "Test script did not set test_description."
     44  1.1  haad 
     45  1.1  haad verboselevel=0
     46  1.1  haad while test "$#" -ne 0
     47  1.1  haad do
     48  1.1  haad 	case "$1" in
     49  1.1  haad 	-d|--d|--de|--deb|--debu|--debug)
     50  1.1  haad 		debug=t ;;
     51  1.1  haad 	-i|--i|--im|--imm|--imme|--immed|--immedi|--immedia|--immediat|--immediate)
     52  1.1  haad 		immediate=t ;;
     53  1.1  haad 	-h|--h|--he|--hel|--help)
     54  1.1  haad 		echo "$test_description"
     55  1.1  haad 		exit 0 ;;
     56  1.1  haad 	-v|--v|--ve|--ver|--verb|--verbo|--verbos|--verbose)
     57  1.1  haad 		verbose=t ;;
     58  1.1  haad 	-vv|-vvv|-vvvv)
     59  1.1  haad 		verboselevel=${#1}
     60  1.1  haad 		verboselevel=$(($verboselevel - 1))
     61  1.1  haad 		verbose=t ;;
     62  1.1  haad 	*)
     63  1.1  haad 		echo "$0: unsupported option $1"
     64  1.1  haad 		exit 0 ;;
     65  1.1  haad 	esac
     66  1.1  haad         shift
     67  1.1  haad done
     68  1.1  haad 
     69  1.1  haad exec 5>&1
     70  1.1  haad if test "$verbose" = "t"
     71  1.1  haad then
     72  1.1  haad 	exec 4>&2 3>&1
     73  1.1  haad else
     74  1.1  haad 	exec 4>/dev/null 3>/dev/null
     75  1.1  haad fi
     76  1.1  haad 
     77  1.1  haad test_failure=0
     78  1.1  haad test_count=0
     79  1.1  haad 
     80  1.1  haad trap 'echo >&5 "FATAL: Unexpected exit with code $?"; exit 1' exit
     81  1.1  haad 
     82  1.1  haad # You are not expected to call test_ok_ and test_failure_ directly, use
     83  1.1  haad # the text_expect_* functions instead.
     84  1.1  haad 
     85  1.1  haad test_ok_ () {
     86  1.1  haad 	test_count=$(expr "$test_count" + 1)
     87  1.1  haad 	say "  ok $test_count: $@"
     88  1.1  haad }
     89  1.1  haad 
     90  1.1  haad test_failure_ () {
     91  1.1  haad 	test_count=$(expr "$test_count" + 1)
     92  1.1  haad 	test_failure=$(expr "$test_failure" + 1);
     93  1.1  haad 	say "FAIL $test_count: $1"
     94  1.1  haad 	shift
     95  1.1  haad 	echo "$@" | sed -e 's/^/	/'
     96  1.1  haad 	test "$immediate" = "" || exit 1
     97  1.1  haad }
     98  1.1  haad 
     99  1.1  haad test_debug () {
    100  1.1  haad 	test "$debug" = "" || eval "$1"
    101  1.1  haad }
    102  1.1  haad 
    103  1.1  haad test_run_ () {
    104  1.1  haad 	eval >&3 2>&4 "$1"
    105  1.1  haad 	eval_ret="$?"
    106  1.1  haad 	return 0
    107  1.1  haad }
    108  1.1  haad 
    109  1.1  haad test_skip () {
    110  1.1  haad 	this_test=$(this_test_)
    111  1.1  haad 	this_test="$this_test.$(expr "$test_count" + 1)"
    112  1.1  haad 	to_skip=
    113  1.1  haad 	for skp in $SKIP_TESTS
    114  1.1  haad 	do
    115  1.1  haad 		case "$this_test" in
    116  1.1  haad 		$skp)
    117  1.1  haad 			to_skip=t
    118  1.1  haad 		esac
    119  1.1  haad 	done
    120  1.1  haad 	case "$to_skip" in
    121  1.1  haad 	t)
    122  1.1  haad 		say >&3 "skipping test: $@"
    123  1.1  haad 		test_count=$(expr "$test_count" + 1)
    124  1.1  haad 		say "skip $test_count: $1"
    125  1.1  haad 		: true
    126  1.1  haad 		;;
    127  1.1  haad 	*)
    128  1.1  haad 		false
    129  1.1  haad 		;;
    130  1.1  haad 	esac
    131  1.1  haad }
    132  1.1  haad 
    133  1.1  haad test_expect_failure () {
    134  1.1  haad 	test "$#" = 2 ||
    135  1.1  haad 	error "bug in the test script: not 2 parameters to test-expect-failure"
    136  1.1  haad 	if ! test_skip "$@"
    137  1.1  haad 	then
    138  1.1  haad 		say >&3 "expecting failure: $2"
    139  1.1  haad 		test_run_ "$2"
    140  1.1  haad 		if [ "$?" = 0 -a "$eval_ret" != 0 -a "$eval_ret" -lt 129 ]
    141  1.1  haad 		then
    142  1.1  haad 			test_ok_ "$1"
    143  1.1  haad 		else
    144  1.1  haad 			test_failure_ "$@"
    145  1.1  haad 		fi
    146  1.1  haad 	fi
    147  1.1  haad 	echo >&3 ""
    148  1.1  haad }
    149  1.1  haad 
    150  1.1  haad test_expect_success () {
    151  1.1  haad 	test "$#" = 2 ||
    152  1.1  haad 	error "bug in the test script: not 2 parameters to test-expect-success"
    153  1.1  haad 	if ! test_skip "$@"
    154  1.1  haad 	then
    155  1.1  haad 		say >&3 "expecting success: $2"
    156  1.1  haad 		test_run_ "$2"
    157  1.1  haad 		if [ "$?" = 0 -a "$eval_ret" = 0 ]
    158  1.1  haad 		then
    159  1.1  haad 			test_ok_ "$1"
    160  1.1  haad 		else
    161  1.1  haad 			test_failure_ "$@"
    162  1.1  haad 		fi
    163  1.1  haad 	fi
    164  1.1  haad 	echo >&3 ""
    165  1.1  haad }
    166  1.1  haad 
    167  1.1  haad test_expect_code () {
    168  1.1  haad 	test "$#" = 3 ||
    169  1.1  haad 	error "bug in the test script: not 3 parameters to test-expect-code"
    170  1.1  haad 	if ! test_skip "$@"
    171  1.1  haad 	then
    172  1.1  haad 		say >&3 "expecting exit code $1: $3"
    173  1.1  haad 		test_run_ "$3"
    174  1.1  haad 		if [ "$?" = 0 -a "$eval_ret" = "$1" ]
    175  1.1  haad 		then
    176  1.1  haad 			test_ok_ "$2"
    177  1.1  haad 		else
    178  1.1  haad 			test_failure_ "$@"
    179  1.1  haad 		fi
    180  1.1  haad 	fi
    181  1.1  haad 	echo >&3 ""
    182  1.1  haad }
    183  1.1  haad 
    184  1.1  haad test_done () {
    185  1.1  haad 	case "$test_failure" in
    186  1.1  haad 	0)
    187  1.1  haad 		# We could:
    188  1.1  haad 		# cd .. && rm -fr trash
    189  1.1  haad 		# but that means we forbid any tests that use their own
    190  1.1  haad 		# subdirectory from calling test_done without coming back
    191  1.1  haad 		# to where they started from.
    192  1.1  haad 		# The Makefile provided will clean this test area so
    193  1.1  haad 		# we will leave things as they are.
    194  1.1  haad 
    195  1.1  haad 		say "passed all $test_count test(s)"
    196  1.1  haad 		exit 0 ;;
    197  1.1  haad 
    198  1.1  haad 	*)
    199  1.1  haad 		say "failed $test_failure among $test_count test(s)"
    200  1.1  haad 		exit 1 ;;
    201  1.1  haad 
    202  1.1  haad 	esac
    203  1.1  haad }
    204  1.1  haad 
    205  1.1  haad this_test=$(this_test_)
    206  1.1  haad 
    207  1.1  haad skip_=0
    208  1.1  haad # If $privileges_required_ is nonempty, non-root skips this test.
    209  1.1  haad if test "$privileges_required_" != ''; then
    210  1.1  haad     uid=`id -u` || error 'failed to run "id -u"'
    211  1.1  haad     if test "$uid" != 0; then
    212  1.1  haad 	SKIP_TESTS="$SKIP_TESTS $this_test"
    213  1.1  haad 	say "you have insufficient privileges for test $this_test"
    214  1.1  haad 	skip_=1
    215  1.1  haad     fi
    216  1.1  haad fi
    217  1.1  haad 
    218  1.1  haad pwd_=`pwd`
    219  1.1  haad 
    220  1.1  haad test_dir_=${LVM_TEST_DIR-.}
    221  1.1  haad test "$test_dir_" = . && test_dir_=$pwd_
    222  1.1  haad 
    223  1.1  haad # This is a stub function that is run upon trap (upon regular exit and
    224  1.1  haad # interrupt).  Override it with a per-test function, e.g., to unmount
    225  1.1  haad # a partition, or to undo any other global state changes.
    226  1.1  haad cleanup_() { :; }
    227  1.1  haad 
    228  1.1  haad for skp in $SKIP_TESTS
    229  1.1  haad do
    230  1.1  haad 	to_skip=
    231  1.1  haad 	for skp in $SKIP_TESTS
    232  1.1  haad 	do
    233  1.1  haad 		case "$this_test" in
    234  1.1  haad 		$skp)
    235  1.1  haad 			to_skip=t
    236  1.1  haad 		esac
    237  1.1  haad 	done
    238  1.1  haad 	case "$to_skip" in
    239  1.1  haad 	t)
    240  1.1  haad 		say >&3 "skipping test $this_test altogether"
    241  1.1  haad 		say "skip all tests in $this_test"
    242  1.1  haad 		trap - exit
    243  1.1  haad 		test_done
    244  1.1  haad 	esac
    245  1.1  haad done
    246  1.1  haad 
    247  1.1  haad test_dir_rand_=$($abs_srcdir/mkdtemp $test_dir_ lvm-$this_test.XXXXXXXXXX) \
    248  1.1  haad     || error "failed to create temporary directory in $test_dir_"
    249  1.1  haad 
    250  1.1  haad testlib_cleanup_() {
    251  1.1  haad     d="$test_dir_rand_";
    252  1.1  haad     cd "$test_dir_" && chmod -R u+rwx "$d" && rm -rf "$d"
    253  1.1  haad }
    254  1.1  haad 
    255  1.1  haad # Run each test from within a temporary sub-directory named after the
    256  1.1  haad # test itself, and arrange to remove it upon exception or normal exit.
    257  1.1  haad trap 'st=$?; cleanup_; testlib_cleanup_; exit $st' 0
    258  1.1  haad trap '(exit $?); exit $?' 1 2 13 15
    259  1.1  haad 
    260  1.1  haad cd $test_dir_rand_ || error "failed to cd to $test_dir_rand_"
    261  1.1  haad 
    262  1.1  haad if test $skip_ = 0; then
    263  1.1  haad   . $abs_srcdir/lvm-utils.sh || exit 1
    264  1.1  haad fi
    265  1.1  haad 
    266  1.1  haad if ( diff --version < /dev/null 2>&1 | grep GNU ) 2>&1 > /dev/null; then
    267  1.1  haad   compare='diff -u'
    268  1.1  haad elif ( cmp --version < /dev/null 2>&1 | grep GNU ) 2>&1 > /dev/null; then
    269  1.1  haad   compare='cmp -s'
    270  1.1  haad else
    271  1.1  haad   compare=cmp
    272  1.1  haad fi
    273