1 # Simple test harness infrastructure
3 # Copyright 2005 by Rob Landley
5 # This file defines two functions, "testing" and "optionflag"
7 # The following environment variables enable optional behavior in "testing":
8 # DEBUG - Show every command run by test script.
9 # VERBOSE - Print the diff -u of each failed test case.
10 # If equal to "fail", stop after first failed test.
11 # SKIP - do not perform this test (this is set by "optionflag")
13 # The "testing" function takes five arguments:
14 # $1) Description to display when running command
15 # $2) Command line arguments to command
16 # $3) Expected result (on stdout)
17 # $4) Data written to file "input"
18 # $5) Data written to stdin
20 # The exit value of testing is the exit value of the command it ran.
22 # The environment variable "FAILCOUNT" contains a cumulative total of the
23 # number of failed tests.
25 # The "optional" function is used to skip certain tests, ala:
26 # optionflag CFG_THINGY
28 # The "optional" function checks the environment variable "OPTIONFLAGS",
29 # which is either empty (in which case it always clears SKIP) or
30 # else contains a colon-separated list of features (in which case the function
31 # clears SKIP if the flag was found, or sets it to 1 if the flag was not found).
38 # Check config to see if option is enabled, set SKIP if not.
46 SHOWPASS="$(echo -e "\033[1;32m${SHOWPASS}\033[0m")"
47 SHOWFAIL="$(echo -e "\033[1;31m${SHOWFAIL}\033[0m")"
48 SHOWSKIP="$(echo -e "\033[1;33m${SHOWSKIP}\033[0m")"
53 option=`echo "$OPTIONFLAGS" | egrep "(^|:)$1(:|\$)"`
55 if [ -z "$1" ] || [ -z "$OPTIONFLAGS" ] || [ ${#option} -ne 0 ]
63 # The testing function
68 [ -z "$1" ] && NAME=$2
72 echo "Test $NAME has the wrong number of arguments ($# $*)" >&2
76 [ -n "$DEBUG" ] && set -x
78 if [ -n "$SKIP" ] || ( [ -n "$SKIP_HOST" ] && [ -n "$TEST_HOST" ])
80 [ ! -z "$VERBOSE" ] && echo "$SHOWSKIP: $NAME"
84 echo -ne "$3" > expected
86 echo -ne "$5" | ${EVAL:-eval} "$2" > actual
90 [ $RETVAL -gt 128 ] && [ $RETVAL -lt 255 ] &&
91 echo "exited with signal (or returned $RETVAL)" >> actual
93 DIFF="$(diff -au${NOSPACE:+b} expected actual)"
96 FAILCOUNT=$[$FAILCOUNT+1]
97 echo "$SHOWFAIL: $NAME"
100 [ ! -z "$4" ] && echo "echo -ne \"$4\" > input"
101 echo "echo -ne '$5' |$EVAL $2"
103 [ "$VERBOSE" == fail ] && exit 1
106 echo "$SHOWPASS: $NAME"
108 rm -f input expected actual
110 [ -n "$DEBUG" ] && set +x
115 # Recursively grab an executable and all the libraries needed to run it.
116 # Source paths beginning with / will be copied into destpath, otherwise
117 # the file is assumed to already be there and only its library dependencies
122 [ $# -lt 2 ] && return
130 [ "${i:0:1}" == "/" ] || i=$(which $i)
131 [ -f "$dest/$i" ] && continue
134 d=`echo "$i" | grep -o '.*/'` &&
135 mkdir -p "$dest/$d" &&
136 cat "$i" > "$dest/$i" &&
141 mkchroot "$dest" $(ldd "$i" | egrep -o '/.* ')
145 # Set up a chroot environment and run commands within it.
146 # Needed commands listed on command line
147 # Script fed to stdin.
152 mount -t ramfs tmpdir4chroot tmpdir4chroot
153 mkdir -p tmpdir4chroot/{etc,sys,proc,tmp,dev}
154 cp -L testing.sh tmpdir4chroot
156 # Copy utilities from command line arguments
158 echo -n "Setup chroot"
159 mkchroot tmpdir4chroot $*
162 mknod tmpdir4chroot/dev/tty c 5 0
163 mknod tmpdir4chroot/dev/null c 1 3
164 mknod tmpdir4chroot/dev/zero c 1 5
166 # Copy script from stdin
168 cat > tmpdir4chroot/test.sh
169 chmod +x tmpdir4chroot/test.sh
170 chroot tmpdir4chroot /test.sh
171 umount -l tmpdir4chroot