-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathrun_tests.sh
executable file
·257 lines (219 loc) · 7.04 KB
/
run_tests.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
#!/bin/sh
# Copyright 2008-2009 Canonical Ltd.
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License version 3,
# as published by the Free Software Foundation.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
export LANG=C
testdir="tests"
tests="installation bad bugs good util"
set -e
# Some systems may not have iptables in their PATH. Try to account for that.
if ! which iptables >/dev/null ; then
export PATH="/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin"
echo "INFO: 'iptables' not in PATH. Using:"
echo " $PATH"
if ! which iptables >/dev/null; then
echo "ERROR: could not find iptables. Aborting."
exit 1
fi
fi
set +e
ipt_major=`iptables --version | sed 's/.* v//' | cut -d '.' -f 1 | sed 's/\([0-9]\+\).*/\\1/'`
ipt_minor=`iptables --version | sed 's/.* v//' | cut -d '.' -f 2 | sed 's/\([0-9]\+\).*/\\1/'`
ipt_micro=`iptables --version | sed 's/.* v//' | cut -d '.' -f 3 | sed 's/\([0-9]\+\).*/\\1/'`
get_result_path() {
d="$1"
f="$d/result"
f_micro="$f.$ipt_major.$ipt_minor.$ipt_micro"
f_minor="$f.$ipt_major.$ipt_minor"
if [ -f "$f.$ipt_major.$ipt_minor.$ipt_micro" ]; then
f="$f_micro"
elif [ -f "$f.$ipt_major.$ipt_minor" ]; then
f="$f_minor"
fi
echo "$f"
}
CUR=`pwd`
export TESTPATH="$testdir/testarea"
export TESTTMP="$testdir/testarea/tmp"
export TESTSTATE="$TESTPATH/lib/ufw"
STOPONFAIL="no"
STOPONSKIP="no"
if [ "$1" = "-s" ]; then
shift
STOPONFAIL="yes"
elif [ "$1" = "-S" ]; then
shift
STOPONFAIL="yes"
STOPONSKIP="yes"
fi
interpreter=""
if [ "$1" = "-i" ]; then
shift
if [ -z "$1" ]; then
echo "Specified '-i' without an interpreter. Aborting" >&2
exit
fi
interpreter="$1"
shift
fi
if [ -z "$interpreter" ]; then
for exe in python python2.7 python2.6 python2.5; do
if which $exe >/dev/null 2>&1; then
interpreter="$exe"
break
fi
done
fi
# export the interpreter so the tests can use it too
export interpreter="$interpreter"
echo "Interpreter: $interpreter"
echo ""
if [ -e "/proc/sys/net/ipv6" ]; then
tests="$tests ipv6"
fi
subclass=""
if [ ! -z "$1" ]; then
tmp="$1"
if echo "$tmp" | egrep -q '/' ; then
subclass=`basename $tmp`
tests=`dirname $tmp`
else
tests="$tmp"
fi
fi
if [ ! -d "$testdir" ]; then
echo "Couldn't find '$testdir' directory"
exit 1
fi
if [ ! -e "./setup.py" ]; then
echo "Couldn't find setup.py"
exit 1
fi
skipped=0
errors=0
numtests=0
statsdir=`mktemp -d`
trap "rm -rf $statsdir" EXIT HUP INT QUIT TERM
export statsdir
echo "0" > $statsdir/individual
for class in $tests
do
for d in `ls -d -1 $testdir/$class/* 2>/dev/null`
do
if [ ! -z "$subclass" ]; then
if [ "$d" != "$testdir/$class/$subclass" ]; then
continue
fi
fi
if [ $skipped -gt 0 ]; then
if [ "$STOPONSKIP" = "yes" ]; then
echo ""
echo "STOPONSKIP set, exiting on skip"
exit 1
fi
fi
thistest=`basename $d`
echo ""
echo "Performing tests '$class/$thistest'"
if [ ! -x "$testdir/$class/$thistest/runtest.sh" ]; then
skipped=$(($skipped + 1))
echo " WARNING: couldn't find '$testdir/$class/$thistest/runtest.sh' (skipping)"
continue
fi
echo "- installing"
if [ -d "$TESTPATH" ]; then
rm -rf "$TESTPATH"
fi
tmpdir=`mktemp -d`
mv "$tmpdir" "$TESTPATH"
mkdir -p "$TESTPATH/usr/sbin" "$TESTPATH/etc" "$TESTPATH/tmp" || exit 1
install_dir="$TESTPATH"
setup_output=`$interpreter ./setup.py install --home="$install_dir" 2>&1`
if [ "$?" != "0" ]; then
echo "$setup_output"
exit 1
fi
# make the installed user rules files available to tests
find "$TESTPATH" -name "user*.rules" -exec cp {} {}.orig \;
# this is to allow root to run the tests without error. I don't
# like building things as root, but some people do...
sed -i 's/self.do_checks = True/self.do_checks = False/' "$TESTPATH/lib/python/ufw/backend.py"
cp -rL $testdir/$class/$thistest/orig/* "$TESTPATH/etc" || exit 1
cp -f $testdir/$class/$thistest/runtest.sh "$TESTPATH" || exit 1
# Explicitly disable IPv6 here, since some tests assume it is disabled.
# IPv6 will be re-enabled in the individual tests that require it.
sed -i 's/IPV6=.*/IPV6=no/' $TESTPATH/etc/default/ufw
echo "- result: "
numtests=$(($numtests + 1))
# now run the test
PYTHONPATH="$PYTHONPATH:$install_dir/lib/python" "$TESTPATH/runtest.sh"
if [ "$?" != "0" ];then
echo " ** FAIL **"
errors=$(($errors + 1))
else
if [ ! -f "$TESTTMP/result" ]; then
skipped=$(($skipped + 1))
echo " WARNING: couldn't find '$TESTTMP/result' (skipping)"
continue
else
# fix discrepencies between python versions
sed -i 's/^usage:/Usage:/' $TESTTMP/result
sed -i 's/^options:/Options:/' $TESTTMP/result
fi
if [ ! -f "$testdir/$class/$thistest/result" ]; then
skipped=$(($skipped + 1))
echo " WARNING: couldn't find '$testdir/$class/$thistest/result' (skipping)"
continue
fi
result_file=`get_result_path $testdir/$class/$thistest`
diffs=`diff -w $result_file $TESTTMP/result`
if [ -z "$diffs" ]; then
echo " PASS"
else
errors=$(($errors + 1))
echo " FAIL:"
echo "$diffs"
fi
fi
chmod 755 "$TESTPATH"
if [ $errors -gt 0 ]; then
if [ "$STOPONFAIL" = "yes" ]; then
echo ""
echo "FAILED $testdir/$class/$thistest -- result found in $TESTTMP/result"
echo "For more information, see:"
echo "diff -Naur $testdir/$class/$thistest/result $TESTTMP/result"
exit 1
fi
fi
done
done
if [ -d "$TESTPATH" ]; then
rm -rf "$TESTPATH"
fi
individual=$(cat $statsdir/individual)
echo ""
echo "-------"
echo "Results"
echo "-------"
echo "Attempted: $numtests ($individual individual tests)"
echo "Skipped: $skipped"
echo "Errors: $errors"
if [ "$errors" != "0" ]; then
exit 1
fi
# cleanup
rm -rf $statsdir
if [ "$skipped" != "0" ]; then
exit 2
fi
exit 0