-
Notifications
You must be signed in to change notification settings - Fork 19
/
Copy pathrun_tests
executable file
·300 lines (232 loc) · 5.41 KB
/
run_tests
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
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
#! /bin/bash
# set $WITH_DASH to 1 to run tests also with dash
with_dash=${WITH_DASH:-0}
test_dir="tests"
test_root="$test_dir/root"
test_data="$test_dir/data"
programs="\
/usr/bin/bash\
/usr/bin/ksh\
/usr/bin/busybox\
"
[ "$with_dash" = 1 ] && programs="$programs /usr/bin/dash"
busybox_programs="\
/usr/bin/basename\
/usr/bin/cat\
/usr/bin/chmod\
/usr/bin/cut\
/usr/bin/dirname\
/usr/bin/grep\
/usr/bin/ln\
/usr/bin/ls\
/usr/bin/mkdir\
/usr/bin/mktemp\
/usr/bin/readlink\
/usr/bin/rm\
/usr/bin/sed\
/usr/bin/sleep\
/usr/bin/tr\
"
dummy_programs="\
/usr/bin/kernel-install\
/usr/bin/sdbootutil\
/usr/sbin/grub2-install\
/usr/sbin/grub2-mkconfig\
/usr/sbin/grub2-set-default\
/usr/sbin/kexec\
/usr/sbin/shim-install\
"
# Notes
#
# /usr/bin/bootctl, /usr/bin/date, /usr/bin/df, /usr/sbin/efibootmgr, /usr/sbin/fdisk, and /usr/bin/uname are wrapper scripts
#
# - 'bootctl status' returns a sample status listing; other bootctl calls do nothing
# - 'date "+%F %T"' returns the fixed date "2023-11-01 23:59:58"
# - 'df --local --sync --output=source /foo' returns $TEST_DF_foo
# - 'efibootmgr' returns sample output.
# - 'fdisk -l -o 'Device,Type' /dev/sda returns a sample lsiting; other fdisk calls return an error
# - 'uname -m' returns $TEST_ARCH
#
# programs listed in dummy_programs are replaced by wrapper scripts that
# return 0 or (if set) the value of the environment variable $TEST_<NAME>_EXIT
# where <NAME> is the upper case program name, with any '-' replaced by '_'.
# For example, grub2-install exits with $TEST_GRUB2_INSTALL_EXIT.
opt_refs=
while true ; do
case $1 in
-r|--make-reference) opt_refs=".ref" ; shift ; continue ;;
esac
break
done
init_tests ()
{
rm -rf "$test_root"
mkdir -p "$test_root"/{tmp,boot/grub2,etc/sysconfig,dev,var/log,usr/bin/system,usr/lib/bootloader,usr/lib64,usr/sbin}
for i in bin sbin lib lib64 ; do
ln -s "usr/$i" "$test_root"
done
cp -r $test_data/* "$test_root"
touch "$test_root/var/log/pbl.log"
for p in $programs $busybox_programs ; do
cp --parents "$p" $(ldd "$p" | sed -e '/\//! d; s/^.*[ \t]\(\/[^ ]\+\) .*$/\1/') "$test_root"
done
for p in $busybox_programs ; do
mv "$test_root/$p" "$test_root/usr/bin/system"
done
for p in $dummy_programs ; do
cat >"$test_root/$p" <<'EOF'
#! /usr/bin/bash
p=${0##*/}
p=${p//-/_}
p=TEST_${p@U}_EXIT
eval "p_exit=\$$p"
[ -z "$p_exit" ] && p_exit=0
if [ "$p_exit" != 0 ] ; then
echo "$0 $*: test error $p_exit :-(" >&2
fi
exit $p_exit
EOF
chmod 755 "$test_root/$p"
done
make install DESTDIR="$test_root" INTERPRETER=sh
sed -i -e '1s/[^/]*sh/sh/' "$test_root/usr/sbin/pbl"
}
done_tests ()
{
rm -r "$test_root"
}
set_bash ()
{
ln -sf "bash" "$test_root/usr/bin/sh"
for p in $busybox_programs ; do
ln -sf "system/${p##*/}" "$test_root/$p"
done
shell=bash
}
set_dash ()
{
ln -sf "dash" "$test_root/usr/bin/sh"
for p in $busybox_programs ; do
ln -sf "system/${p##*/}" "$test_root/$p"
done
shell=dash
}
set_ksh ()
{
ln -sf "ksh" "$test_root/usr/bin/sh"
for p in $busybox_programs ; do
ln -sf "system/${p##*/}" "$test_root/$p"
done
shell=ksh
}
set_busybox ()
{
ln -sf "busybox" "$test_root/usr/bin/sh"
for p in $busybox_programs ; do
ln -sf "busybox" "$test_root/$p"
done
shell=busybox
}
get_testdata ()
{
t="$1"
root_dir="$2"
sed -E -e "s/ pbl-[0-9]+: / pbl-0: /" < "$root_dir/var/log/pbl.log" > "$test_dir/$t/pbl.log$suffix"
cp "$root_dir/etc/sysconfig/bootloader" "$test_dir/$t/bootloader$suffix"
}
do_test ()
{
t="$1"
real_root="$test_dir/real_root"
mkdir "$real_root" || exit 1
cp -r $test_root/* "$test_dir/real_root"
for data_dir in "$test_dir/$t"/data* ; do
if [ -d "$data_dir" ] ; then
cp -r --remove-destination "$data_dir"/* "$real_root"
fi
done
echo -n >"$real_root/var/log/pbl.log"
if [ -n "$opt_refs" ] ; then
suffix=".$shell$opt_refs"
else
suffix=".$shell"
fi
script=$(mktemp)
# add 'run' function for logging command exit codes
cat >"$script" <<'EOF'
run () {
"$@"
if [ "$?" = 0 ] ; then
echo "[✔] $*"
else
echo "[✘] $* = $?"
fi
}
EOF
cat "$test_dir/$t/script" >>"$script"
# mounting /proc or /dev needed?
unshare --pid --fork --user --map-root-user --mount-proc --root="$real_root" >"$test_dir/$t/output$suffix" 2>&1 <"$script"
get_testdata "$t" "$real_root"
rm -r "$real_root"
}
do_tests ()
{
for t in "$test_dir"/[0-9]???_* ; do
do_test "${t##*/}"
done
}
show_results ()
{
echo "== $shell =="
rm -f "$test_dir/testresults_$shell.diff"
for t in "$test_dir"/[0-9]???_* ; do
n=${t##*/}
printf "%-48s" " $n"
first=
for i in "$t"/*."$shell".ref ; do
[ -e "$i" ] || continue
i="${i%.ref}"
f=${i##*/}
echo -n "$first$f "
if diff -u "$i.ref" "$i" >> "$test_dir/testresults_$shell.diff" ; then
echo -en "\u2714"
else
test_error=1
echo -en "\u2718"
fi
first=", "
done
echo
done
}
init_tests
test_error=0
if [ -n "$opt_refs" ] ; then
set_bash
do_tests
set_ksh
do_tests
set_busybox
do_tests
if [ "$with_dash" = 1 ] ; then
set_dash
do_tests
fi
else
set_bash
do_tests
show_results
set_ksh
do_tests
show_results
set_busybox
do_tests
show_results
if [ "$with_dash" = 1 ] ; then
set_dash
do_tests
show_results
fi
fi
done_tests
exit "$test_error"