blob: 08be6fa0b7df90eb8eff3551621d240febb41092 [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 Landley5f1d7e22007-02-18 14:23:10 -050040optional()
41{
42 option=`echo "$OPTIONFLAGS" | egrep "(^|:)$1(:|\$)"`
43 # Not set?
44 if [ -z "$1" ] || [ -z "$OPTIONFLAGS" ] || [ ${#option} -ne 0 ]
45 then
46 SKIP=""
47 return
48 fi
49 SKIP=1
50}
51
52# The testing function
53
Rob Landley387edf52014-09-20 13:09:14 -050054testing()
Rob Landley5f1d7e22007-02-18 14:23:10 -050055{
56 NAME="$1"
57 [ -z "$1" ] && NAME=$2
58
59 if [ $# -ne 5 ]
60 then
61 echo "Test $NAME has the wrong number of arguments ($# $*)" >&2
62 exit
63 fi
64
65 [ -n "$DEBUG" ] && set -x
66
67 if [ -n "$SKIP" ]
68 then
Rob Landley7bc357d2008-06-22 00:57:44 -050069 [ ! -z "$VERBOSE" ] && echo "SKIPPED: $NAME"
Rob Landley5f1d7e22007-02-18 14:23:10 -050070 return 0
71 fi
72
73 echo -ne "$3" > expected
74 echo -ne "$4" > input
Rob Landley5f1d7e22007-02-18 14:23:10 -050075 echo -ne "$5" | eval "$2" > actual
76 RETVAL=$?
77
Rob Landley41ed9792013-01-05 00:44:24 -060078 cmp expected actual > /dev/null 2>&1
Rob Landley5f1d7e22007-02-18 14:23:10 -050079 if [ $? -ne 0 ]
80 then
81 FAILCOUNT=$[$FAILCOUNT+1]
82 echo "FAIL: $NAME"
Rob Landley273f2782007-12-16 17:56:31 -060083 if [ -n "$VERBOSE" ]
84 then
85 echo "echo '$5' | $2"
86 diff -u expected actual
Rob Landley50b82972014-08-30 13:03:40 -050087 [ "$VERBOSE" == fail ] && exit 1
Rob Landley273f2782007-12-16 17:56:31 -060088 fi
Rob Landley5f1d7e22007-02-18 14:23:10 -050089 else
90 echo "PASS: $NAME"
91 fi
92 rm -f input expected actual
93
94 [ -n "$DEBUG" ] && set +x
95
96 return $RETVAL
97}
98
99# Recursively grab an executable and all the libraries needed to run it.
100# Source paths beginning with / will be copied into destpath, otherwise
101# the file is assumed to already be there and only its library dependencies
102# are copied.
103
Rob Landley387edf52014-09-20 13:09:14 -0500104mkchroot()
Rob Landley5f1d7e22007-02-18 14:23:10 -0500105{
106 [ $# -lt 2 ] && return
107
108 echo -n .
109
110 dest=$1
111 shift
112 for i in "$@"
113 do
114 [ "${i:0:1}" == "/" ] || i=$(which $i)
115 [ -f "$dest/$i" ] && continue
116 if [ -e "$i" ]
117 then
118 d=`echo "$i" | grep -o '.*/'` &&
119 mkdir -p "$dest/$d" &&
120 cat "$i" > "$dest/$i" &&
121 chmod +x "$dest/$i"
122 else
123 echo "Not found: $i"
124 fi
125 mkchroot "$dest" $(ldd "$i" | egrep -o '/.* ')
126 done
127}
128
129# Set up a chroot environment and run commands within it.
130# Needed commands listed on command line
131# Script fed to stdin.
132
Rob Landley387edf52014-09-20 13:09:14 -0500133dochroot()
Rob Landley5f1d7e22007-02-18 14:23:10 -0500134{
135 mkdir tmpdir4chroot
136 mount -t ramfs tmpdir4chroot tmpdir4chroot
137 mkdir -p tmpdir4chroot/{etc,sys,proc,tmp,dev}
138 cp -L testing.sh tmpdir4chroot
139
140 # Copy utilities from command line arguments
141
142 echo -n "Setup chroot"
143 mkchroot tmpdir4chroot $*
144 echo
145
146 mknod tmpdir4chroot/dev/tty c 5 0
147 mknod tmpdir4chroot/dev/null c 1 3
148 mknod tmpdir4chroot/dev/zero c 1 5
149
150 # Copy script from stdin
151
152 cat > tmpdir4chroot/test.sh
153 chmod +x tmpdir4chroot/test.sh
154 chroot tmpdir4chroot /test.sh
155 umount -l tmpdir4chroot
156 rmdir tmpdir4chroot
157}
158