forked from rhboot/grubby
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest-bls.sh
executable file
·317 lines (263 loc) · 9.83 KB
/
test-bls.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
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
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
#!/bin/bash
#
# test.sh -- grubby wrapper regression tests
#
# Copyright 2007-2018 Red Hat, Inc. All rights reserved.
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# 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/>.
#
#----------------------------------------------------------------------
# Global vars
#----------------------------------------------------------------------
cmd=${0##*/}
opt_bootloader=*
opt_verbose=false
read -d '' usage <<EOT
usage: test.sh [ -hv ]
-b B --bootloader=B Test bootloader B instead of all
-h --help Show this help message
-v --verbose Verbose output
EOT
declare -i pass=0 fail=0
testing=
BLSDIR=$(mktemp -d)
#----------------------------------------------------------------------
# Functions
#----------------------------------------------------------------------
empty_blsdir() {
rm -rf $BLSDIR/*
}
oneTest() {
typeset mode=$1 correct=test/bls/results/$2
shift 2
local ENV_FILE=""
if [ "$mode" == "--grub2" ]; then
ENV_FILE="test/bls/grub2-support_files/env_temp"
if [ ! -f $ENV_FILE ]; then
cp "test/bls/grub2-support_files/grubenv" "$ENV_FILE"
fi
ENV_FILE="--env=$ENV_FILE"
fi
local CFG_FILE=""
if [ "$mode" == "--zipl" ]; then
CFG_FILE=$BLSDIR/cfg_temp
if [ ! -f $CFG_FILE ]; then
cp "test/bls/zipl-support_files/zipl.conf" "$CFG_FILE"
fi
CFG_FILE="--config-file=$CFG_FILE"
fi
echo "$testing ... $mode $correct"
runme=( ./grubby-bls "$mode" --bad-image-okay $ENV_FILE $CFG_FILE -b $BLSDIR "$@" )
runtest=( ./grubby-bls "$mode" --info=ALL $ENV_FILE $CFG_FILE -b $BLSDIR)
if "${runme[@]}" && "${runtest[@]}" | cmp "$correct" > /dev/null; then
(( pass++ ))
if $opt_verbose; then
echo -------------------------------------------------------------
echo -n "PASS: "
printf "%q " "${runme[@]}"; echo
"${runtest[@]}" | diff -U30 "$correct" -
echo
fi
else
(( fail++ ))
echo -------------------------------------------------------------
echo -n "FAIL: "
printf "%q " "${runme[@]}"; echo
"${runtest[@]}" | diff -U30 "$correct" -
echo
fi
}
# Test feature that display some information, checking that output instead of
# the generated configuration file
oneDisplayTest() {
typeset mode=$1 correct=test/bls/results/$2
shift 2
local ENV_FILE=""
if [ "$mode" == "--grub2" ]; then
ENV_FILE="test/bls/grub2-support_files/env_temp"
if [ ! -f $ENV_FILE ]; then
cp "test/bls/grub2-support_files/grubenv" "$ENV_FILE"
fi
ENV_FILE="--env=$ENV_FILE"
fi
local CFG_FILE=""
if [ "$mode" == "--zipl" ]; then
CFG_FILE=$BLSDIR/cfg_temp
if [ ! -f $CFG_FILE ]; then
cp "test/bls/zipl-support_files/zipl.conf" "$CFG_FILE"
fi
CFG_FILE="--config-file=$CFG_FILE"
fi
local BIO="--bad-image-okay"
if [ "$1" == "--bad-image-bad" ]; then
BIO=""
shift
fi
echo "$testing ... $mode $correct"
runme=( ./grubby-bls "$mode" $BIO $ENV_FILE $CFG_FILE -b $BLSDIR "$@" )
if "${runme[@]}" 2>&1 | cmp "$correct" > /dev/null; then
(( pass++ ))
if $opt_verbose; then
echo -------------------------------------------------------------
echo -n "PASS: "
printf "%q " "${runme[@]}"; echo
"${runme[@]}" 2>&1 | diff -U30 "$correct" -
echo
fi
else
(( fail++ ))
echo -------------------------------------------------------------
echo -n "FAIL: "
printf "%q " "${runme[@]}"; echo
"${runme[@]}" 2>&1 | diff -U30 "$correct" -
echo
fi
}
commandTest() {
description=$1
cmd0=$2
text1=$3
shift 3
echo "$description"
output0=$(mktemp)
$cmd0 > $output0
if echo $text1 | cmp $output0 - >/dev/null; then
(( pass++))
if $opt_verbose; then
echo -------------------------------------------------------------
echo -n "PASS: "
printf "%q " "\"$cmd0\""; echo
echo $text1 | diff -U30 $output0 -
echo
fi
else
(( fail++ ))
echo -------------------------------------------------------------
echo -n "FAIL: "
printf "%q " "\"$cmd0\""; echo
echo $text1 | diff -U30 $output0 -
echo
fi
}
# generate convenience functions
for b in $(./grubby-bls --help | \
sed -n 's/^.*--\([^ ]*\) *configure \1 bootloader.*/\1/p'); do
eval "${b}Test() { [[ \"$b\" == \$opt_bootloader ]] && oneTest --$b \"\$@\"; }"
eval "${b}DisplayTest() { [[ \"$b\" == \$opt_bootloader ]] && oneDisplayTest --$b \"\$@\"; }"
done
#----------------------------------------------------------------------
# Main
#----------------------------------------------------------------------
# Use /usr/bin/getopt which supports GNU-style long options
args=$(getopt -o b:hv --long bootloader,help,verbose -n "$cmd" -- "$@") || exit
eval set -- "$args"
while true; do
case $1 in
-b|--bootloader) opt_bootloader=$2; shift 2 ;;
-h|--help) echo "$usage"; exit 0 ;;
-v|--verbose) opt_verbose=true; shift ;;
--) shift; break ;;
*) echo "failed to process cmdline args" >&2; exit 1 ;;
esac
done
empty_blsdir
testing="Z/IPL default directive"
ziplTest add/z1.1 --add-kernel /boot/new-kernel0 --title "title 0"
ziplTest add/z1.2 --add-kernel /boot/new-kernel1 --title "title 1" --make-default
testing="Z/IPL display default index"
ziplDisplayTest default/index0 --default-index
testing="Z/IPL default index directive"
ziplTest default/z2 --set-default-index=1
testing="Z/IPL display default index"
ziplDisplayTest default/index1 --default-index
testing="Z/IPL display default title"
ziplDisplayTest default/title0 --default-title
testing="Z/IPL display default kernel"
ziplDisplayTest default/kernel0 --default-kernel
testing="Z/IPL display entry information"
ziplDisplayTest display/info0 --info=1
testing="Z/IPL remove kernel"
ziplTest remove/z3.1 --remove-kernel=/boot/new-kernel1
ziplTest remove/z3.2 --remove-kernel=DEFAULT
testing="Z/IPL add kernel"
ziplTest add/z4.1 --add-kernel=/boot/new-kernel0 --title test --args="foo=bar"
ziplTest add/z4.2 --add-kernel=/boot/new-kernel1 --title test --copy-default --args="x=y"
empty_blsdir
testing="GRUB2 add kernel with title"
grub2Test add/g1 --add-kernel=/boot/new-kernel0 --title='title 0' \
--initrd=/boot/new-initrd0.img
testing="GRUB2 add kernel with initrd"
grub2Test add/g2 --add-kernel=/boot/new-kernel1 --initrd=/boot/new-initrd1.img \
--title="title 1" --make-default
testing="GRUB2 add kernel"
grub2Test add/g3 --add-kernel=/boot/new-kernel2 --title "title 2"
testing="GRUB2 remove kernel"
grub2Test remove/g4 --remove-kernel=/boot/new-kernel2
testing="GRUB2 add kernel with copy default"
grub2Test add/g5 --add-kernel=/boot/new-kernel3.img --title='title 3' \
--initrd=/boot/new-initrd3.img --copy-default
testing="GRUB2 add kernel with args and ignore remove kernel"
grub2Test add/g6 --add-kernel=/boot/vmlinuz-0-rescue-5a94251776a14678911d4ae0949500f5 \
--initrd /boot/initramfs-0-rescue-5a94251776a14678911d4ae0949500f5.img \
--copy-default --title "Fedora 21 Rescue" --args=root=/fooooo \
--remove-kernel=wtf
testing="GRUB2 add initrd"
grub2Test add/g7 --update-kernel=/boot/new-kernel0 --initrd=/boot/new-initrd
testing="GRUB2 display default index"
grub2DisplayTest default/index1 --default-index
testing="GRUB2 display default title"
grub2DisplayTest default/title1 --default-title
testing="GRUB2 remove kernel via index"
grub2Test remove/g8 --remove-kernel=1
testing="GRUB2 remove kernel via title"
grub2Test remove/g9 --remove-kernel="TITLE=title 3"
testing="GRUB2 default index directive"
grub2Test default/g10.1 --set-default-index=0
grub2DisplayTest default/index0 --default-index
grub2Test default/g10.2 --set-default-index=1
grub2DisplayTest default/index1 --default-index
grub2Test default/g10.3 --set-default-index=9
grub2DisplayTest default/index1 --default-index
testing="GRUB2 add kernel with default=saved_entry"
grub2Test add/g11 --add-kernel=/boot/new-kernel.img \
--title='title' --initrd=/boot/new-initrd \
--copy-default
commandTest "saved_default output" \
"grub2-editenv test/bls/grub2-support_files/env_temp list" \
"saved_entry=Fedora 21 Rescue"
testing="GRUB2 add kernel with default=saved_entry and a terrible title"
grub2Test add/g12 --add-kernel=/boot/new-kernel.img \
--title='Fedora (3.10.3-300.fc19.x86_64) 19 (Schrödinger’s Cat)' \
--initrd=/boot/new-initrd --copy-default
testing="GRUB2 set default with default=saved_entry and a terrible name"
grub2Test add/g13 --set-default-index=1
commandTest "saved_default output" \
"grub2-editenv test/bls/grub2-support_files/env_temp list" \
'saved_entry=Fedora (3.10.3-300.fc19.x86_64) 19 (Schrödinger’s Cat)'
testing="GRUB2 set default with default=saved_entry"
grub2Test add/g14 --set-default-index=0
commandTest "saved_default output" \
"grub2-editenv test/bls/grub2-support_files/env_temp list" \
"saved_entry=title 0"
testing="GRUB2 --default-index with default=saved_entry"
grub2DisplayTest default/index0 --default-index
testing="GRUB2 --default-title with default=saved_entry"
grub2DisplayTest default/title0 --default-title
testing="GRUB2 --default-index with default=saved_entry and empty grubenv"
grub2DisplayTest default/index0 --env /dev/null --default-index
printf "\n%d (%d%%) tests passed, %d (%d%%) tests failed\n" \
$pass $(((100*pass)/(pass+fail))) \
$fail $(((100*fail)/(pass+fail)))
rm -rf $BLSDIR
exit $(( !!fail ))