]> git.proxmox.com Git - qemu.git/blob - tests/qemu-iotests/common.rc
cdefafc62fa5ed26b5e866a2192661731c17cdc6
[qemu.git] / tests / qemu-iotests / common.rc
1 #!/bin/bash
2 #
3 # Copyright (C) 2009 Red Hat, Inc.
4 # Copyright (c) 2000-2006 Silicon Graphics, Inc. All Rights Reserved.
5 #
6 # This program is free software; you can redistribute it and/or modify
7 # it under the terms of the GNU General Public License as published by
8 # the Free Software Foundation; either version 2 of the License, or
9 # (at your option) any later version.
10 #
11 # This program is distributed in the hope that it will be useful,
12 # but WITHOUT ANY WARRANTY; without even the implied warranty of
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 # GNU General Public License for more details.
15 #
16 # You should have received a copy of the GNU General Public License
17 # along with this program. If not, see <http://www.gnu.org/licenses/>.
18 #
19
20 dd()
21 {
22 if [ "$HOSTOS" == "Linux" ]
23 then
24 command dd --help | grep noxfer > /dev/null 2>&1
25
26 if [ "$?" -eq 0 ]
27 then
28 command dd status=noxfer $@
29 else
30 command dd $@
31 fi
32 else
33 command dd $@
34 fi
35 }
36
37 # we need common.config
38 if [ "$iam" != "check" ]
39 then
40 if ! . ./common.config
41 then
42 echo "$iam: failed to source common.config"
43 exit 1
44 fi
45 fi
46
47 # make sure we have a standard umask
48 umask 022
49
50 if [ "$IMGPROTO" = "file" ]; then
51 TEST_IMG=$TEST_DIR/t.$IMGFMT
52 else
53 TEST_IMG=$IMGPROTO:$TEST_DIR/t.$IMGFMT
54 fi
55
56 _make_test_img()
57 {
58 # extra qemu-img options can be added by tests
59 # at least one argument (the image size) needs to be added
60 local extra_img_options=$*
61
62 if [ \( "$IMGFMT" = "qcow2" -o "$IMGFMT" = "qed" \) -a -n "$CLUSTER_SIZE" ]; then
63 extra_img_options="-o cluster_size=$CLUSTER_SIZE $extra_img_options"
64 fi
65
66 # XXX(hch): have global image options?
67 $QEMU_IMG create -f $IMGFMT $TEST_IMG $extra_img_options | \
68 sed -e "s#$IMGPROTO:$TEST_DIR#TEST_DIR#g" | \
69 sed -e "s#$TEST_DIR#TEST_DIR#g" | \
70 sed -e "s#$IMGFMT#IMGFMT#g" | \
71 sed -e "s# encryption=off##g" | \
72 sed -e "s# cluster_size=0##g" | \
73 sed -e "s# table_size=0##g" | \
74 sed -e "s# compat6=off##g" | \
75 sed -e "s# static=off##g"
76 }
77
78 _cleanup_test_img()
79 {
80 case "$IMGPROTO" in
81
82 file)
83 rm -f $TEST_DIR/t.$IMGFMT
84 rm -f $TEST_DIR/t.$IMGFMT.orig
85 rm -f $TEST_DIR/t.$IMGFMT.base
86 ;;
87
88 rbd)
89 rbd rm $TEST_DIR/t.$IMGFMT > /dev/null
90 ;;
91
92 sheepdog)
93 collie vdi delete $TEST_DIR/t.$IMGFMT
94 ;;
95
96 esac
97 }
98
99 _check_test_img()
100 {
101 $QEMU_IMG check -f $IMGFMT $TEST_IMG 2>&1 | \
102 sed -e 's/qemu-img\: This image format does not support checks/No errors were found on the image./'
103 }
104
105 _get_pids_by_name()
106 {
107 if [ $# -ne 1 ]
108 then
109 echo "Usage: _get_pids_by_name process-name" 1>&2
110 exit 1
111 fi
112
113 # Algorithm ... all ps(1) variants have a time of the form MM:SS or
114 # HH:MM:SS before the psargs field, use this as the search anchor.
115 #
116 # Matches with $1 (process-name) occur if the first psarg is $1
117 # or ends in /$1 ... the matching uses sed's regular expressions,
118 # so passing a regex into $1 will work.
119
120 ps $PS_ALL_FLAGS \
121 | sed -n \
122 -e 's/$/ /' \
123 -e 's/[ ][ ]*/ /g' \
124 -e 's/^ //' \
125 -e 's/^[^ ]* //' \
126 -e "/[0-9]:[0-9][0-9] *[^ ]*\/$1 /s/ .*//p" \
127 -e "/[0-9]:[0-9][0-9] *$1 /s/ .*//p"
128 }
129
130 # fqdn for localhost
131 #
132 _get_fqdn()
133 {
134 host=`hostname`
135 $NSLOOKUP_PROG $host | $AWK_PROG '{ if ($1 == "Name:") print $2 }'
136 }
137
138 # check if run as root
139 #
140 _need_to_be_root()
141 {
142 id=`id | $SED_PROG -e 's/(.*//' -e 's/.*=//'`
143 if [ "$id" -ne 0 ]
144 then
145 echo "Arrgh ... you need to be root (not uid=$id) to run this test"
146 exit 1
147 fi
148 }
149
150
151 # Do a command, log it to $seq.full, optionally test return status
152 # and die if command fails. If called with one argument _do executes the
153 # command, logs it, and returns its exit status. With two arguments _do
154 # first prints the message passed in the first argument, and then "done"
155 # or "fail" depending on the return status of the command passed in the
156 # second argument. If the command fails and the variable _do_die_on_error
157 # is set to "always" or the two argument form is used and _do_die_on_error
158 # is set to "message_only" _do will print an error message to
159 # $seq.out and exit.
160
161 _do()
162 {
163 if [ $# -eq 1 ]; then
164 _cmd=$1
165 elif [ $# -eq 2 ]; then
166 _note=$1
167 _cmd=$2
168 echo -n "$_note... "
169 else
170 echo "Usage: _do [note] cmd" 1>&2
171 status=1; exit
172 fi
173
174 (eval "echo '---' \"$_cmd\"") >>$here/$seq.full
175 (eval "$_cmd") >$tmp._out 2>&1; ret=$?
176 cat $tmp._out >>$here/$seq.full
177 if [ $# -eq 2 ]; then
178 if [ $ret -eq 0 ]; then
179 echo "done"
180 else
181 echo "fail"
182 fi
183 fi
184 if [ $ret -ne 0 ] \
185 && [ "$_do_die_on_error" = "always" \
186 -o \( $# -eq 2 -a "$_do_die_on_error" = "message_only" \) ]
187 then
188 [ $# -ne 2 ] && echo
189 eval "echo \"$_cmd\" failed \(returned $ret\): see $seq.full"
190 status=1; exit
191 fi
192
193 return $ret
194 }
195
196 # bail out, setting up .notrun file
197 #
198 _notrun()
199 {
200 echo "$*" >$seq.notrun
201 echo "$seq not run: $*"
202 status=0
203 exit
204 }
205
206 # just plain bail out
207 #
208 _fail()
209 {
210 echo "$*" | tee -a $here/$seq.full
211 echo "(see $seq.full for details)"
212 status=1
213 exit 1
214 }
215
216 # tests whether $IMGFMT is one of the supported image formats for a test
217 #
218 _supported_fmt()
219 {
220 for f; do
221 if [ "$f" = "$IMGFMT" -o "$f" = "generic" ]; then
222 return
223 fi
224 done
225
226 _notrun "not suitable for this image format: $IMGFMT"
227 }
228
229 # tests whether $IMGPROTO is one of the supported image protocols for a test
230 #
231 _supported_proto()
232 {
233 for f; do
234 if [ "$f" = "$IMGPROTO" -o "$f" = "generic" ]; then
235 return
236 fi
237 done
238
239 _notrun "not suitable for this image protocol: $IMGPROTO"
240 }
241
242 # tests whether the host OS is one of the supported OSes for a test
243 #
244 _supported_os()
245 {
246 for h
247 do
248 if [ "$h" = "$HOSTOS" ]
249 then
250 return
251 fi
252 done
253
254 _notrun "not suitable for this OS: $HOSTOS"
255 }
256
257 # this test requires that a specified command (executable) exists
258 #
259 _require_command()
260 {
261 [ -x "$1" ] || _notrun "$1 utility required, skipped this test"
262 }
263
264 _full_imgfmt_details()
265 {
266 echo "$IMGFMT"
267 }
268
269 _full_imgproto_details()
270 {
271 echo "$IMGPROTO"
272 }
273
274 _full_platform_details()
275 {
276 os=`uname -s`
277 host=`hostname -s`
278 kernel=`uname -r`
279 platform=`uname -m`
280 echo "$os/$platform $host $kernel"
281 }
282
283 _link_out_file()
284 {
285 if [ -z "$1" ]; then
286 echo Error must pass \$seq.
287 exit
288 fi
289 rm -f $1
290 if [ "`uname`" == "IRIX64" ] || [ "`uname`" == "IRIX" ]; then
291 ln -s $1.irix $1
292 elif [ "`uname`" == "Linux" ]; then
293 ln -s $1.linux $1
294 else
295 echo Error test $seq does not run on the operating system: `uname`
296 exit
297 fi
298 }
299
300 _die()
301 {
302 echo $@
303 exit 1
304 }
305
306 # make sure this script returns success
307 /bin/true