Rob Landley | 2c22685 | 2007-11-15 18:30:30 -0600 | [diff] [blame] | 1 | # Simple test harness infrastructure |
Rob Landley | 5f1d7e2 | 2007-02-18 14:23:10 -0500 | [diff] [blame] | 2 | # |
| 3 | # Copyright 2005 by Rob Landley |
| 4 | |
| 5 | # This file defines two functions, "testing" and "optionflag" |
| 6 | |
| 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 | # SKIP - do not perform this test (this is set by "optionflag") |
| 11 | # |
| 12 | # The "testing" function takes five arguments: |
| 13 | # $1) Description to display when running command |
| 14 | # $2) Command line arguments to command |
| 15 | # $3) Expected result (on stdout) |
| 16 | # $4) Data written to file "input" |
| 17 | # $5) Data written to stdin |
| 18 | # |
| 19 | # The exit value of testing is the exit value of the command it ran. |
| 20 | # |
| 21 | # The environment variable "FAILCOUNT" contains a cumulative total of the |
| 22 | # number of failed tests. |
| 23 | |
| 24 | # The "optional" function is used to skip certain tests, ala: |
| 25 | # optionflag CFG_THINGY |
| 26 | # |
| 27 | # The "optional" function checks the environment variable "OPTIONFLAGS", |
| 28 | # which is either empty (in which case it always clears SKIP) or |
| 29 | # else contains a colon-separated list of features (in which case the function |
| 30 | # clears SKIP if the flag was found, or sets it to 1 if the flag was not found). |
| 31 | |
| 32 | export FAILCOUNT=0 |
| 33 | export SKIP= |
| 34 | |
| 35 | # Helper functions |
| 36 | |
| 37 | optional() |
| 38 | { |
| 39 | option=`echo "$OPTIONFLAGS" | egrep "(^|:)$1(:|\$)"` |
| 40 | # Not set? |
| 41 | if [ -z "$1" ] || [ -z "$OPTIONFLAGS" ] || [ ${#option} -ne 0 ] |
| 42 | then |
| 43 | SKIP="" |
| 44 | return |
| 45 | fi |
| 46 | SKIP=1 |
| 47 | } |
| 48 | |
| 49 | # The testing function |
| 50 | |
| 51 | testing () |
| 52 | { |
| 53 | NAME="$1" |
| 54 | [ -z "$1" ] && NAME=$2 |
| 55 | |
| 56 | if [ $# -ne 5 ] |
| 57 | then |
| 58 | echo "Test $NAME has the wrong number of arguments ($# $*)" >&2 |
| 59 | exit |
| 60 | fi |
| 61 | |
| 62 | [ -n "$DEBUG" ] && set -x |
| 63 | |
| 64 | if [ -n "$SKIP" ] |
| 65 | then |
| 66 | echo "SKIPPED: $NAME" |
| 67 | return 0 |
| 68 | fi |
| 69 | |
| 70 | echo -ne "$3" > expected |
| 71 | echo -ne "$4" > input |
Rob Landley | 5f1d7e2 | 2007-02-18 14:23:10 -0500 | [diff] [blame] | 72 | echo -ne "$5" | eval "$2" > actual |
| 73 | RETVAL=$? |
| 74 | |
| 75 | cmp expected actual > /dev/null |
| 76 | if [ $? -ne 0 ] |
| 77 | then |
| 78 | FAILCOUNT=$[$FAILCOUNT+1] |
| 79 | echo "FAIL: $NAME" |
Rob Landley | 273f278 | 2007-12-16 17:56:31 -0600 | [diff] [blame^] | 80 | if [ -n "$VERBOSE" ] |
| 81 | then |
| 82 | echo "echo '$5' | $2" |
| 83 | diff -u expected actual |
| 84 | fi |
Rob Landley | 5f1d7e2 | 2007-02-18 14:23:10 -0500 | [diff] [blame] | 85 | else |
| 86 | echo "PASS: $NAME" |
| 87 | fi |
| 88 | rm -f input expected actual |
| 89 | |
| 90 | [ -n "$DEBUG" ] && set +x |
| 91 | |
| 92 | return $RETVAL |
| 93 | } |
| 94 | |
| 95 | # Recursively grab an executable and all the libraries needed to run it. |
| 96 | # Source paths beginning with / will be copied into destpath, otherwise |
| 97 | # the file is assumed to already be there and only its library dependencies |
| 98 | # are copied. |
| 99 | |
| 100 | function mkchroot |
| 101 | { |
| 102 | [ $# -lt 2 ] && return |
| 103 | |
| 104 | echo -n . |
| 105 | |
| 106 | dest=$1 |
| 107 | shift |
| 108 | for i in "$@" |
| 109 | do |
| 110 | [ "${i:0:1}" == "/" ] || i=$(which $i) |
| 111 | [ -f "$dest/$i" ] && continue |
| 112 | if [ -e "$i" ] |
| 113 | then |
| 114 | d=`echo "$i" | grep -o '.*/'` && |
| 115 | mkdir -p "$dest/$d" && |
| 116 | cat "$i" > "$dest/$i" && |
| 117 | chmod +x "$dest/$i" |
| 118 | else |
| 119 | echo "Not found: $i" |
| 120 | fi |
| 121 | mkchroot "$dest" $(ldd "$i" | egrep -o '/.* ') |
| 122 | done |
| 123 | } |
| 124 | |
| 125 | # Set up a chroot environment and run commands within it. |
| 126 | # Needed commands listed on command line |
| 127 | # Script fed to stdin. |
| 128 | |
| 129 | function dochroot |
| 130 | { |
| 131 | mkdir tmpdir4chroot |
| 132 | mount -t ramfs tmpdir4chroot tmpdir4chroot |
| 133 | mkdir -p tmpdir4chroot/{etc,sys,proc,tmp,dev} |
| 134 | cp -L testing.sh tmpdir4chroot |
| 135 | |
| 136 | # Copy utilities from command line arguments |
| 137 | |
| 138 | echo -n "Setup chroot" |
| 139 | mkchroot tmpdir4chroot $* |
| 140 | echo |
| 141 | |
| 142 | mknod tmpdir4chroot/dev/tty c 5 0 |
| 143 | mknod tmpdir4chroot/dev/null c 1 3 |
| 144 | mknod tmpdir4chroot/dev/zero c 1 5 |
| 145 | |
| 146 | # Copy script from stdin |
| 147 | |
| 148 | cat > tmpdir4chroot/test.sh |
| 149 | chmod +x tmpdir4chroot/test.sh |
| 150 | chroot tmpdir4chroot /test.sh |
| 151 | umount -l tmpdir4chroot |
| 152 | rmdir tmpdir4chroot |
| 153 | } |
| 154 | |