make a few more tests generic
[qemu-iotests/stefanha.git] / common.rc
bloba05a250d74effc83b40beae39d46bbded79d389c
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, write to the Free Software
18 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
19 # USA
22 dd()
24 if [ "$HOSTOS" == "Linux" ]
25 then
26 command dd --help | grep noxfer > /dev/null 2>&1
28 if [ "$?" -eq 0 ]
29 then
30 command dd status=noxfer $@
31 else
32 command dd $@
34 else
35 command dd $@
39 # we need common.config
40 if [ "$iam" != "check" ]
41 then
42 if ! . ./common.config
43 then
44 echo "$iam: failed to source common.config"
45 exit 1
49 # make sure we have a standard umask
50 umask 022
52 TEST_IMG=$TEST_DIR/t.$IMGFMT
54 _make_test_img()
56 # extra qemu-img options can be added by tests
57 # at least one argument (the image size) needs to be added
58 local extra_img_options=$*
60 # XXX(hch): have global image options?
61 $QEMU_IMG create -f $IMGFMT $TEST_IMG $extra_img_options | \
62 sed -e "s#$TEST_DIR#TEST_DIR#g" | \
63 sed -e "s#$IMGFMT#IMGFMT#g" | \
64 sed -e "s# encryption=off##g" | \
65 sed -e "s# cluster_size=0##g" | \
66 sed -e "s# compat6=off##g"
70 _cleanup_test_img()
72 rm -f $TEST_DIR/t.$IMGFMT
73 rm -f $TEST_DIR/t.$IMGFMT.orig
76 _check_test_img()
78 $QEMU_IMG check -f $IMGFMT $TEST_IMG 2>&1 | \
79 sed -e 's/qemu-img\: This image format does not support checks/No errors were found on the image./'
82 _get_pids_by_name()
84 if [ $# -ne 1 ]
85 then
86 echo "Usage: _get_pids_by_name process-name" 1>&2
87 exit 1
90 # Algorithm ... all ps(1) variants have a time of the form MM:SS or
91 # HH:MM:SS before the psargs field, use this as the search anchor.
93 # Matches with $1 (process-name) occur if the first psarg is $1
94 # or ends in /$1 ... the matching uses sed's regular expressions,
95 # so passing a regex into $1 will work.
97 ps $PS_ALL_FLAGS \
98 | sed -n \
99 -e 's/$/ /' \
100 -e 's/[ ][ ]*/ /g' \
101 -e 's/^ //' \
102 -e 's/^[^ ]* //' \
103 -e "/[0-9]:[0-9][0-9] *[^ ]*\/$1 /s/ .*//p" \
104 -e "/[0-9]:[0-9][0-9] *$1 /s/ .*//p"
107 # fqdn for localhost
109 _get_fqdn()
111 host=`hostname`
112 $NSLOOKUP_PROG $host | $AWK_PROG '{ if ($1 == "Name:") print $2 }'
115 # check if run as root
117 _need_to_be_root()
119 id=`id | $SED_PROG -e 's/(.*//' -e 's/.*=//'`
120 if [ "$id" -ne 0 ]
121 then
122 echo "Arrgh ... you need to be root (not uid=$id) to run this test"
123 exit 1
128 # Do a command, log it to $seq.full, optionally test return status
129 # and die if command fails. If called with one argument _do executes the
130 # command, logs it, and returns its exit status. With two arguments _do
131 # first prints the message passed in the first argument, and then "done"
132 # or "fail" depending on the return status of the command passed in the
133 # second argument. If the command fails and the variable _do_die_on_error
134 # is set to "always" or the two argument form is used and _do_die_on_error
135 # is set to "message_only" _do will print an error message to
136 # $seq.out and exit.
138 _do()
140 if [ $# -eq 1 ]; then
141 _cmd=$1
142 elif [ $# -eq 2 ]; then
143 _note=$1
144 _cmd=$2
145 echo -n "$_note... "
146 else
147 echo "Usage: _do [note] cmd" 1>&2
148 status=1; exit
151 (eval "echo '---' \"$_cmd\"") >>$here/$seq.full
152 (eval "$_cmd") >$tmp._out 2>&1; ret=$?
153 cat $tmp._out >>$here/$seq.full
154 if [ $# -eq 2 ]; then
155 if [ $ret -eq 0 ]; then
156 echo "done"
157 else
158 echo "fail"
161 if [ $ret -ne 0 ] \
162 && [ "$_do_die_on_error" = "always" \
163 -o \( $# -eq 2 -a "$_do_die_on_error" = "message_only" \) ]
164 then
165 [ $# -ne 2 ] && echo
166 eval "echo \"$_cmd\" failed \(returned $ret\): see $seq.full"
167 status=1; exit
170 return $ret
173 # bail out, setting up .notrun file
175 _notrun()
177 echo "$*" >$seq.notrun
178 echo "$seq not run: $*"
179 status=0
180 exit
183 # just plain bail out
185 _fail()
187 echo "$*" | tee -a $here/$seq.full
188 echo "(see $seq.full for details)"
189 status=1
190 exit 1
193 # tests whether $IMGFMT is one of the supported image formats for a test
195 _supported_fmt()
197 for f; do
198 if [ "$f" = "$IMGFMT" -o "$f" = "generic" ]; then
199 return
201 done
203 _notrun "not suitable for this image format: $IMGFMT"
206 # tests whether the host OS is one of the supported OSes for a test
208 _supported_os()
210 for h
212 if [ "$h" = "$HOSTOS" ]
213 then
214 return
216 done
218 _notrun "not suitable for this OS: $HOSTOS"
221 # this test requires that a specified command (executable) exists
223 _require_command()
225 [ -x "$1" ] || _notrun "$1 utility required, skipped this test"
228 _full_imgfmt_details()
230 echo "$IMGFMT"
233 _full_platform_details()
235 os=`uname -s`
236 host=`hostname -s`
237 kernel=`uname -r`
238 platform=`uname -m`
239 echo "$os/$platform $host $kernel"
242 _link_out_file()
244 if [ -z "$1" ]; then
245 echo Error must pass \$seq.
246 exit
248 rm -f $1
249 if [ "`uname`" == "IRIX64" ] || [ "`uname`" == "IRIX" ]; then
250 ln -s $1.irix $1
251 elif [ "`uname`" == "Linux" ]; then
252 ln -s $1.linux $1
253 else
254 echo Error test $seq does not run on the operating system: `uname`
255 exit
259 _die()
261 echo $@
262 exit 1
265 # make sure this script returns success
266 /bin/true