source: MondoRescue/branches/3.3/mindi-busybox/testsuite/testing.sh@ 3647

Last change on this file since 3647 was 3621, checked in by Bruno Cornec, 10 years ago

New 3?3 banch for incorporation of latest busybox 1.25. Changing minor version to handle potential incompatibilities.

  • Property svn:executable set to *
File size: 3.9 KB
RevLine 
[2725]1# Simple test harness infrastructure for BusyBox
[821]2#
3# Copyright 2005 by Rob Landley
4#
5# License is GPLv2, see LICENSE in the busybox tarball for full license text.
6
[2725]7# This file defines two functions, "testing" and "optional"
8# and a couple more...
[821]9
10# The following environment variables may be set to enable optional behavior
11# in "testing":
12# VERBOSE - Print the diff -u of each failed test case.
13# DEBUG - Enable command tracing.
[2725]14# SKIP - do not perform this test (this is set by "optional")
[821]15#
16# The "testing" function takes five arguments:
[2725]17# $1) Test description
18# $2) Command(s) to run. May have pipes, redirects, etc
19# $3) Expected result on stdout
20# $4) Data to be written to file "input"
21# $5) Data to be written to stdin
[821]22#
[2725]23# The exit value of testing is the exit value of $2 it ran.
[821]24#
25# The environment variable "FAILCOUNT" contains a cumulative total of the
26# number of failed tests.
27
28# The "optional" function is used to skip certain tests, ala:
[2725]29# optional FEATURE_THINGY
[821]30#
31# The "optional" function checks the environment variable "OPTIONFLAGS",
32# which is either empty (in which case it always clears SKIP) or
33# else contains a colon-separated list of features (in which case the function
34# clears SKIP if the flag was found, or sets it to 1 if the flag was not found).
35
36export FAILCOUNT=0
37export SKIP=
38
[2725]39# Helper for helpers. Oh my...
40
41test x"$ECHO" != x"" || {
42 ECHO="echo"
43 test x"`echo -ne`" = x"" || {
44 # Compile and use a replacement 'echo' which understands -e -n
45 ECHO="$PWD/echo-ne"
46 test -x "$ECHO" || {
47 gcc -Os -o "$ECHO" ../scripts/echo.c || exit 1
48 }
49 }
50 export ECHO
51}
52
[821]53# Helper functions
54
55optional()
56{
[2725]57 SKIP=
58 while test "$1"; do
[3621]59 case "${OPTIONFLAGS}" in
60 *:$1:*) ;;
61 *) SKIP=1; return ;;
62 esac
[2725]63 shift
64 done
[821]65}
66
67# The testing function
68
[1765]69testing()
[821]70{
71 NAME="$1"
[2725]72 [ -n "$1" ] || NAME="$2"
[821]73
74 if [ $# -ne 5 ]
75 then
[2725]76 echo "Test $NAME has wrong number of arguments: $# (must be 5)" >&2
77 exit 1
[821]78 fi
79
[2725]80 [ -z "$DEBUG" ] || set -x
[821]81
82 if [ -n "$SKIP" ]
83 then
84 echo "SKIPPED: $NAME"
85 return 0
86 fi
87
[2725]88 $ECHO -ne "$3" > expected
89 $ECHO -ne "$4" > input
[3232]90 [ -z "$VERBOSE" ] || echo ======================
[2725]91 [ -z "$VERBOSE" ] || echo "echo -ne '$4' >input"
92 [ -z "$VERBOSE" ] || echo "echo -ne '$5' | $2"
93 $ECHO -ne "$5" | eval "$2" > actual
[821]94 RETVAL=$?
95
[2725]96 if cmp expected actual >/dev/null 2>/dev/null
[821]97 then
[2725]98 echo "PASS: $NAME"
99 else
100 FAILCOUNT=$(($FAILCOUNT + 1))
[821]101 echo "FAIL: $NAME"
[2725]102 [ -z "$VERBOSE" ] || diff -u expected actual
[821]103 fi
104 rm -f input expected actual
105
[2725]106 [ -z "$DEBUG" ] || set +x
[821]107
108 return $RETVAL
109}
110
111# Recursively grab an executable and all the libraries needed to run it.
112# Source paths beginning with / will be copied into destpath, otherwise
113# the file is assumed to already be there and only its library dependencies
114# are copied.
115
[1765]116mkchroot()
[821]117{
118 [ $# -lt 2 ] && return
119
[2725]120 $ECHO -n .
[821]121
122 dest=$1
123 shift
124 for i in "$@"
125 do
[2725]126 #bashism: [ "${i:0:1}" == "/" ] || i=$(which $i)
127 i=$(which $i) # no-op for /bin/prog
[821]128 [ -f "$dest/$i" ] && continue
129 if [ -e "$i" ]
130 then
131 d=`echo "$i" | grep -o '.*/'` &&
132 mkdir -p "$dest/$d" &&
133 cat "$i" > "$dest/$i" &&
134 chmod +x "$dest/$i"
135 else
136 echo "Not found: $i"
137 fi
138 mkchroot "$dest" $(ldd "$i" | egrep -o '/.* ')
139 done
140}
141
142# Set up a chroot environment and run commands within it.
143# Needed commands listed on command line
144# Script fed to stdin.
145
[1765]146dochroot()
[821]147{
148 mkdir tmpdir4chroot
149 mount -t ramfs tmpdir4chroot tmpdir4chroot
150 mkdir -p tmpdir4chroot/{etc,sys,proc,tmp,dev}
151 cp -L testing.sh tmpdir4chroot
152
153 # Copy utilities from command line arguments
154
[2725]155 $ECHO -n "Setup chroot"
[821]156 mkchroot tmpdir4chroot $*
157 echo
158
159 mknod tmpdir4chroot/dev/tty c 5 0
160 mknod tmpdir4chroot/dev/null c 1 3
161 mknod tmpdir4chroot/dev/zero c 1 5
162
163 # Copy script from stdin
164
165 cat > tmpdir4chroot/test.sh
166 chmod +x tmpdir4chroot/test.sh
167 chroot tmpdir4chroot /test.sh
168 umount -l tmpdir4chroot
169 rmdir tmpdir4chroot
170}
Note: See TracBrowser for help on using the repository browser.