blob: 8da1089e7ce702f74dd798c71dc0d11184c48227 [file] [log] [blame]
Rob Landley2c226852007-11-15 18:30:30 -06001# Simple test harness infrastructure
Rob Landley5f1d7e22007-02-18 14:23:10 -05002#
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.
Rob Landley50b82972014-08-30 13:03:40 -050010# If equal to "fail", stop after first failed test.
Rob Landley5f1d7e22007-02-18 14:23:10 -050011# SKIP - do not perform this test (this is set by "optionflag")
12#
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
19#
20# The exit value of testing is the exit value of the command it ran.
21#
22# The environment variable "FAILCOUNT" contains a cumulative total of the
23# number of failed tests.
24
25# The "optional" function is used to skip certain tests, ala:
26# optionflag CFG_THINGY
27#
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).
32
33export FAILCOUNT=0
34export SKIP=
35
36# Helper functions
37
Rob Landley9201a012012-02-02 07:29:09 -060038# Check config to see if option is enabled, set SKIP if not.
39
Rob Landley9dbcee42014-11-14 16:44:00 -060040SHOWPASS=PASS
41SHOWFAIL=FAIL
42SHOWSKIP=SKIP
43
44if tty -s <&1
45then
46 SHOWPASS="$(echo -e "\033[1m\033[32m${SHOWPASS}\033[0m")"
47 SHOWFAIL="$(echo -e "\033[1m\033[31m${SHOWFAIL}\033[0m")"
48 SHOWSKIP="$(echo -e "\033[1m\033[33m${SHOWSKIP}\033[0m")"
49fi
50
Rob Landley5f1d7e22007-02-18 14:23:10 -050051optional()
52{
53 option=`echo "$OPTIONFLAGS" | egrep "(^|:)$1(:|\$)"`
54 # Not set?
55 if [ -z "$1" ] || [ -z "$OPTIONFLAGS" ] || [ ${#option} -ne 0 ]
56 then
57 SKIP=""
58 return
59 fi
60 SKIP=1
61}
62
63# The testing function
64
Rob Landley387edf52014-09-20 13:09:14 -050065testing()
Rob Landley5f1d7e22007-02-18 14:23:10 -050066{
67 NAME="$1"
68 [ -z "$1" ] && NAME=$2
69
70 if [ $# -ne 5 ]
71 then
72 echo "Test $NAME has the wrong number of arguments ($# $*)" >&2
73 exit
74 fi
75
76 [ -n "$DEBUG" ] && set -x
77
Rob Landley9dbcee42014-11-14 16:44:00 -060078 if [ -n "$SKIP" ] || ( [ -n "$SKIP_HOST" ] && [ -n "$TEST_HOST" ])
Rob Landley5f1d7e22007-02-18 14:23:10 -050079 then
Rob Landley9dbcee42014-11-14 16:44:00 -060080 [ ! -z "$VERBOSE" ] && echo "$SHOWSKIP: $NAME"
Rob Landley5f1d7e22007-02-18 14:23:10 -050081 return 0
82 fi
83
84 echo -ne "$3" > expected
85 echo -ne "$4" > input
Rob Landley5f1d7e22007-02-18 14:23:10 -050086 echo -ne "$5" | eval "$2" > actual
87 RETVAL=$?
88
Rob Landley41ed9792013-01-05 00:44:24 -060089 cmp expected actual > /dev/null 2>&1
Rob Landley5f1d7e22007-02-18 14:23:10 -050090 if [ $? -ne 0 ]
91 then
92 FAILCOUNT=$[$FAILCOUNT+1]
Rob Landley9dbcee42014-11-14 16:44:00 -060093 echo "$SHOWFAIL: $NAME"
Rob Landley273f2782007-12-16 17:56:31 -060094 if [ -n "$VERBOSE" ]
95 then
Rob Landley9dbcee42014-11-14 16:44:00 -060096 [ ! -z "$4" ] && echo "echo -ne \"$4\" > input"
97 echo "echo -ne '$5' | $2"
Rob Landleyfd493752014-11-28 16:52:06 -060098 diff -au expected actual
Rob Landley50b82972014-08-30 13:03:40 -050099 [ "$VERBOSE" == fail ] && exit 1
Rob Landley273f2782007-12-16 17:56:31 -0600100 fi
Rob Landley5f1d7e22007-02-18 14:23:10 -0500101 else
Rob Landley9dbcee42014-11-14 16:44:00 -0600102 echo "$SHOWPASS: $NAME"
Rob Landley5f1d7e22007-02-18 14:23:10 -0500103 fi
104 rm -f input expected actual
105
106 [ -n "$DEBUG" ] && set +x
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
Rob Landley387edf52014-09-20 13:09:14 -0500116mkchroot()
Rob Landley5f1d7e22007-02-18 14:23:10 -0500117{
118 [ $# -lt 2 ] && return
119
120 echo -n .
121
122 dest=$1
123 shift
124 for i in "$@"
125 do
126 [ "${i:0:1}" == "/" ] || i=$(which $i)
127 [ -f "$dest/$i" ] && continue
128 if [ -e "$i" ]
129 then
130 d=`echo "$i" | grep -o '.*/'` &&
131 mkdir -p "$dest/$d" &&
132 cat "$i" > "$dest/$i" &&
133 chmod +x "$dest/$i"
134 else
135 echo "Not found: $i"
136 fi
137 mkchroot "$dest" $(ldd "$i" | egrep -o '/.* ')
138 done
139}
140
141# Set up a chroot environment and run commands within it.
142# Needed commands listed on command line
143# Script fed to stdin.
144
Rob Landley387edf52014-09-20 13:09:14 -0500145dochroot()
Rob Landley5f1d7e22007-02-18 14:23:10 -0500146{
147 mkdir tmpdir4chroot
148 mount -t ramfs tmpdir4chroot tmpdir4chroot
149 mkdir -p tmpdir4chroot/{etc,sys,proc,tmp,dev}
150 cp -L testing.sh tmpdir4chroot
151
152 # Copy utilities from command line arguments
153
154 echo -n "Setup chroot"
155 mkchroot tmpdir4chroot $*
156 echo
157
158 mknod tmpdir4chroot/dev/tty c 5 0
159 mknod tmpdir4chroot/dev/null c 1 3
160 mknod tmpdir4chroot/dev/zero c 1 5
161
162 # Copy script from stdin
163
164 cat > tmpdir4chroot/test.sh
165 chmod +x tmpdir4chroot/test.sh
166 chroot tmpdir4chroot /test.sh
167 umount -l tmpdir4chroot
168 rmdir tmpdir4chroot
169}
170