comparison scripts/test/testing.sh @ 103:417591818dcb

An old test suite wrapper I wrote, cleaned up a bit and checked for copyrights.
author Rob Landley <rob@landley.net>
date Sun, 18 Feb 2007 14:23:10 -0500
parents
children 1e8f4b05cb65
comparison
equal deleted inserted replaced
102:aa4fa2543a65 103:417591818dcb
1 # Simple test harness infrastructurei for BusyBox
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
72 [ -z "$VERBOSE" ] || echo "echo '$5' | $2"
73 echo -ne "$5" | eval "$2" > actual
74 RETVAL=$?
75
76 cmp expected actual > /dev/null
77 if [ $? -ne 0 ]
78 then
79 FAILCOUNT=$[$FAILCOUNT+1]
80 echo "FAIL: $NAME"
81 [ -n "$VERBOSE" ] && diff -u expected actual
82 else
83 echo "PASS: $NAME"
84 fi
85 rm -f input expected actual
86
87 [ -n "$DEBUG" ] && set +x
88
89 return $RETVAL
90 }
91
92 # Recursively grab an executable and all the libraries needed to run it.
93 # Source paths beginning with / will be copied into destpath, otherwise
94 # the file is assumed to already be there and only its library dependencies
95 # are copied.
96
97 function mkchroot
98 {
99 [ $# -lt 2 ] && return
100
101 echo -n .
102
103 dest=$1
104 shift
105 for i in "$@"
106 do
107 [ "${i:0:1}" == "/" ] || i=$(which $i)
108 [ -f "$dest/$i" ] && continue
109 if [ -e "$i" ]
110 then
111 d=`echo "$i" | grep -o '.*/'` &&
112 mkdir -p "$dest/$d" &&
113 cat "$i" > "$dest/$i" &&
114 chmod +x "$dest/$i"
115 else
116 echo "Not found: $i"
117 fi
118 mkchroot "$dest" $(ldd "$i" | egrep -o '/.* ')
119 done
120 }
121
122 # Set up a chroot environment and run commands within it.
123 # Needed commands listed on command line
124 # Script fed to stdin.
125
126 function dochroot
127 {
128 mkdir tmpdir4chroot
129 mount -t ramfs tmpdir4chroot tmpdir4chroot
130 mkdir -p tmpdir4chroot/{etc,sys,proc,tmp,dev}
131 cp -L testing.sh tmpdir4chroot
132
133 # Copy utilities from command line arguments
134
135 echo -n "Setup chroot"
136 mkchroot tmpdir4chroot $*
137 echo
138
139 mknod tmpdir4chroot/dev/tty c 5 0
140 mknod tmpdir4chroot/dev/null c 1 3
141 mknod tmpdir4chroot/dev/zero c 1 5
142
143 # Copy script from stdin
144
145 cat > tmpdir4chroot/test.sh
146 chmod +x tmpdir4chroot/test.sh
147 chroot tmpdir4chroot /test.sh
148 umount -l tmpdir4chroot
149 rmdir tmpdir4chroot
150 }
151