test qemu-img rebase
[qemu-iotests/stefanha.git] / common.rc
blob0a7ba006e5e9fa0661950e92bd9ca5f51890906e
1 #!/bin/sh
3 # Copyright (C) 2009 Red Hat, Inc.
4 # Copyright (c) 2000-2006 Silicon Graphics, Inc. All Rights Reserved.
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.
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.
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/>.
20 dd()
22 if [ "$HOSTOS" == "Linux" ]
23 then
24 command dd --help | grep noxfer > /dev/null 2>&1
26 if [ "$?" -eq 0 ]
27 then
28 command dd status=noxfer $@
29 else
30 command dd $@
32 else
33 command dd $@
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
47 # make sure we have a standard umask
48 umask 022
50 TEST_IMG=$TEST_DIR/t.$IMGFMT
52 _make_test_img()
54 # extra qemu-img options can be added by tests
55 # at least one argument (the image size) needs to be added
56 local extra_img_options=$*
58 if [ "$IMGFMT" = "qcow2" -a -n "$CLUSTER_SIZE" ]; then
59 extra_img_options="-o cluster_size=$CLUSTER_SIZE $extra_img_options"
62 # XXX(hch): have global image options?
63 $QEMU_IMG create -f $IMGFMT $TEST_IMG $extra_img_options | \
64 sed -e "s#$TEST_DIR#TEST_DIR#g" | \
65 sed -e "s#$IMGFMT#IMGFMT#g" | \
66 sed -e "s# encryption=off##g" | \
67 sed -e "s# cluster_size=0##g" | \
68 sed -e "s# compat6=off##g" | \
69 sed -e "s# static=off##g"
72 _cleanup_test_img()
74 rm -f $TEST_DIR/t.$IMGFMT
75 rm -f $TEST_DIR/t.$IMGFMT.orig
76 rm -f $TEST_DIR/t.$IMGFMT.base
79 _check_test_img()
81 $QEMU_IMG check -f $IMGFMT $TEST_IMG 2>&1 | \
82 sed -e 's/qemu-img\: This image format does not support checks/No errors were found on the image./'
85 _get_pids_by_name()
87 if [ $# -ne 1 ]
88 then
89 echo "Usage: _get_pids_by_name process-name" 1>&2
90 exit 1
93 # Algorithm ... all ps(1) variants have a time of the form MM:SS or
94 # HH:MM:SS before the psargs field, use this as the search anchor.
96 # Matches with $1 (process-name) occur if the first psarg is $1
97 # or ends in /$1 ... the matching uses sed's regular expressions,
98 # so passing a regex into $1 will work.
100 ps $PS_ALL_FLAGS \
101 | sed -n \
102 -e 's/$/ /' \
103 -e 's/[ ][ ]*/ /g' \
104 -e 's/^ //' \
105 -e 's/^[^ ]* //' \
106 -e "/[0-9]:[0-9][0-9] *[^ ]*\/$1 /s/ .*//p" \
107 -e "/[0-9]:[0-9][0-9] *$1 /s/ .*//p"
110 # fqdn for localhost
112 _get_fqdn()
114 host=`hostname`
115 $NSLOOKUP_PROG $host | $AWK_PROG '{ if ($1 == "Name:") print $2 }'
118 # check if run as root
120 _need_to_be_root()
122 id=`id | $SED_PROG -e 's/(.*//' -e 's/.*=//'`
123 if [ "$id" -ne 0 ]
124 then
125 echo "Arrgh ... you need to be root (not uid=$id) to run this test"
126 exit 1
131 # Do a command, log it to $seq.full, optionally test return status
132 # and die if command fails. If called with one argument _do executes the
133 # command, logs it, and returns its exit status. With two arguments _do
134 # first prints the message passed in the first argument, and then "done"
135 # or "fail" depending on the return status of the command passed in the
136 # second argument. If the command fails and the variable _do_die_on_error
137 # is set to "always" or the two argument form is used and _do_die_on_error
138 # is set to "message_only" _do will print an error message to
139 # $seq.out and exit.
141 _do()
143 if [ $# -eq 1 ]; then
144 _cmd=$1
145 elif [ $# -eq 2 ]; then
146 _note=$1
147 _cmd=$2
148 echo -n "$_note... "
149 else
150 echo "Usage: _do [note] cmd" 1>&2
151 status=1; exit
154 (eval "echo '---' \"$_cmd\"") >>$here/$seq.full
155 (eval "$_cmd") >$tmp._out 2>&1; ret=$?
156 cat $tmp._out >>$here/$seq.full
157 if [ $# -eq 2 ]; then
158 if [ $ret -eq 0 ]; then
159 echo "done"
160 else
161 echo "fail"
164 if [ $ret -ne 0 ] \
165 && [ "$_do_die_on_error" = "always" \
166 -o \( $# -eq 2 -a "$_do_die_on_error" = "message_only" \) ]
167 then
168 [ $# -ne 2 ] && echo
169 eval "echo \"$_cmd\" failed \(returned $ret\): see $seq.full"
170 status=1; exit
173 return $ret
176 # bail out, setting up .notrun file
178 _notrun()
180 echo "$*" >$seq.notrun
181 echo "$seq not run: $*"
182 status=0
183 exit
186 # just plain bail out
188 _fail()
190 echo "$*" | tee -a $here/$seq.full
191 echo "(see $seq.full for details)"
192 status=1
193 exit 1
196 # tests whether $IMGFMT is one of the supported image formats for a test
198 _supported_fmt()
200 for f; do
201 if [ "$f" = "$IMGFMT" -o "$f" = "generic" ]; then
202 return
204 done
206 _notrun "not suitable for this image format: $IMGFMT"
209 # tests whether the host OS is one of the supported OSes for a test
211 _supported_os()
213 for h
215 if [ "$h" = "$HOSTOS" ]
216 then
217 return
219 done
221 _notrun "not suitable for this OS: $HOSTOS"
224 # this test requires that a specified command (executable) exists
226 _require_command()
228 [ -x "$1" ] || _notrun "$1 utility required, skipped this test"
231 _full_imgfmt_details()
233 echo "$IMGFMT"
236 _full_platform_details()
238 os=`uname -s`
239 host=`hostname -s`
240 kernel=`uname -r`
241 platform=`uname -m`
242 echo "$os/$platform $host $kernel"
245 _link_out_file()
247 if [ -z "$1" ]; then
248 echo Error must pass \$seq.
249 exit
251 rm -f $1
252 if [ "`uname`" == "IRIX64" ] || [ "`uname`" == "IRIX" ]; then
253 ln -s $1.irix $1
254 elif [ "`uname`" == "Linux" ]; then
255 ln -s $1.linux $1
256 else
257 echo Error test $seq does not run on the operating system: `uname`
258 exit
262 _die()
264 echo $@
265 exit 1
268 # make sure this script returns success
269 /bin/true