From: Lucas Meneghel Rodrigues Subject: Re: [Autotest][PATCH] KVM Test: Add check_image script as post_command script of qcow2 variant.-V2 Date: Tue, 23 Feb 2010 22:04:22 -0300 Message-ID: <6ac58f4f1002231704t7ec8ac9ewe6c8300ce1cba0e@mail.gmail.com> References: <1266831044-15454-1-git-send-email-fyang@redhat.com> Mime-Version: 1.0 Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: QUOTED-PRINTABLE Cc: autotest@test.kernel.org, kvm@vger.kernel.org To: Feng Yang Return-path: Received: from mail-ww0-f46.google.com ([74.125.82.46]:52095 "EHLO mail-ww0-f46.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1754583Ab0BXBEY convert rfc822-to-8bit (ORCPT ); Tue, 23 Feb 2010 20:04:24 -0500 Received: by wwf26 with SMTP id 26so1045909wwf.19 for ; Tue, 23 Feb 2010 17:04:23 -0800 (PST) In-Reply-To: <1266831044-15454-1-git-send-email-fyang@redhat.com> Sender: kvm-owner@vger.kernel.org List-ID: Hi Feng, I've tested your patch, and have some additional comments to m= ake: On Mon, Feb 22, 2010 at 6:30 AM, Feng Yang wrote: > Signed-off-by: Feng Yang > --- > =A0client/tests/kvm/scripts/check_image.py | =A0 85 +++++++++++++++++= ++++++++++++++ > =A0client/tests/kvm/tests_base.cfg.sample =A0| =A0 =A04 ++ > =A02 files changed, 89 insertions(+), 0 deletions(-) > =A0create mode 100755 client/tests/kvm/scripts/check_image.py > > diff --git a/client/tests/kvm/scripts/check_image.py b/client/tests/k= vm/scripts/check_image.py > new file mode 100755 > index 0000000..97f5136 > --- /dev/null > +++ b/client/tests/kvm/scripts/check_image.py > @@ -0,0 +1,85 @@ > +import os, sys, commands > + > + > +class ImageCheckError(Exception): > + =A0 =A0""" > + =A0 =A0Simple wrapper for the builtin Exception class. > + =A0 =A0""" > + =A0 =A0pass > + > + > +class ImageCheck(object): > + =A0 =A0""" > + =A0 =A0Check qcow2 image by qemu-img info/check command. > + =A0 =A0""" > + =A0 =A0def __init__(self): > + =A0 =A0 =A0 =A0""" > + =A0 =A0 =A0 =A0Gets params from environment variables and sets clas= s attributes. > + =A0 =A0 =A0 =A0""" > + =A0 =A0 =A0 =A0self.image_path_list =3D [] > + =A0 =A0 =A0 =A0client_dir =3D =A0os.environ['AUTODIR'] > + =A0 =A0 =A0 =A0self.kvm_dir =3D os.path.join(client_dir, 'tests/kvm= ') > + =A0 =A0 =A0 =A0img_to_check =3D os.environ['KVM_TEST_images'].split= () > + > + =A0 =A0 =A0 =A0for img in img_to_check: > + =A0 =A0 =A0 =A0 =A0 =A0img_name_str =3D "KVM_TEST_image_name_%s" % = img > + =A0 =A0 =A0 =A0 =A0 =A0if not os.environ.has_key(img_name_str): > + =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0img_name_str =3D "KVM_TEST_image_nam= e" > + =A0 =A0 =A0 =A0 =A0 =A0img_format_str =3D "KVM_TEST_image_format_%s= " % img > + =A0 =A0 =A0 =A0 =A0 =A0if os.environ.has_key(img_format_str): > + =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0image_format =3D os.environ[img_form= at_str] > + =A0 =A0 =A0 =A0 =A0 =A0else: > + =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0image_format =3D os.environ['KVM_TES= T_image_format'] > + =A0 =A0 =A0 =A0 =A0 =A0if image_format !=3D "qcow2": > + =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0continue > + =A0 =A0 =A0 =A0 =A0 =A0image_name =3D os.environ[img_name_str] > + =A0 =A0 =A0 =A0 =A0 =A0image_filename =3D "%s.%s" % (image_name, im= age_format) > + =A0 =A0 =A0 =A0 =A0 =A0image_filename =3D os.path.join(self.kvm_dir= , image_filename) > + =A0 =A0 =A0 =A0 =A0 =A0self.image_path_list.append(image_filename) > + =A0 =A0 =A0 =A0if os.environ.has_key('KVM_TEST_qemu_img_binary'): > + =A0 =A0 =A0 =A0 =A0 =A0self.qemu_img_path =3D os.environ['KVM_TEST_= qemu_img_binary'] > + =A0 =A0 =A0 =A0else: > + =A0 =A0 =A0 =A0 =A0 =A0self.qemu_img_path =3D os.path.join(self.kvm= _dir, 'qemu-img') > + > + > + =A0 =A0def exec_img_cmd(self, cmd_type, image_path): > + =A0 =A0 =A0 =A0""" > + =A0 =A0 =A0 =A0Run qemu-img info/check on given image. > + > + =A0 =A0 =A0 =A0@param cmd_type: Sub command used together with qemu= =2E > + =A0 =A0 =A0 =A0@param image_path: Real path of the image. > + =A0 =A0 =A0 =A0""" > + =A0 =A0 =A0 =A0cmd =3D ' '.join([self.qemu_img_path, cmd_type, imag= e_path]) > + =A0 =A0 =A0 =A0print "checking with %s" % cmd > + =A0 =A0 =A0 =A0(status, output) =3D commands.getstatusoutput(cmd) Here you could print the output of the commands (as opposed to only printing it if things fail), it's not a lot of output and it's nice to see what the programs are printing to the user. > + =A0 =A0 =A0 =A0if status or (cmd_type =3D=3D "check" and not "No er= rors" in output): > + =A0 =A0 =A0 =A0 =A0 =A0msg =3D "Command %s failed" % cmd > + =A0 =A0 =A0 =A0 =A0 =A0print output > + =A0 =A0 =A0 =A0 =A0 =A0return False, msg > + =A0 =A0 =A0 =A0else: > + =A0 =A0 =A0 =A0 =A0 =A0return True, '' > + > + > + =A0 =A0def check_image(self): > + =A0 =A0 =A0 =A0""" > + =A0 =A0 =A0 =A0Run qemu-img info/check to check the image in list. > + > + =A0 =A0 =A0 =A0If the image checking is failed, raise an exception. > + =A0 =A0 =A0 =A0""" > + =A0 =A0 =A0 =A0# Check all the image in list. > + =A0 =A0 =A0 =A0errmsg =3D [] > + =A0 =A0 =A0 =A0for image_path in self.image_path_list: > + =A0 =A0 =A0 =A0 =A0 =A0s, o =3D self.exec_img_cmd('info', image_pat= h) > + =A0 =A0 =A0 =A0 =A0 =A0if not s: > + =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0errmsg.append(o) > + =A0 =A0 =A0 =A0 =A0 =A0s, o =3D self.exec_img_cmd('check', image_pa= th) We have one problem: qemu-img check is not available, let's say, on RHEL 5.X (the feature was added at a later point). So I suggest a qemu-img capability check first, while initializing the checker class, and simply finish the script cleanly if we have no 'check' command implemented (with the appropriate messages put on the logs). > + =A0 =A0 =A0 =A0 =A0 =A0if not s: > + =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0errmsg.append(o) > + > + =A0 =A0 =A0 =A0if len(errmsg) > 0: > + =A0 =A0 =A0 =A0 =A0 =A0raise ImageCheckError('Errors are found and = please check log!') > + > + > +if __name__ =3D=3D "__main__": > + =A0 =A0image_check =3D ImageCheck() > + =A0 =A0image_check.check_image() > diff --git a/client/tests/kvm/tests_base.cfg.sample b/client/tests/kv= m/tests_base.cfg.sample > index e9fdd05..159c1be 100644 > --- a/client/tests/kvm/tests_base.cfg.sample > +++ b/client/tests/kvm/tests_base.cfg.sample > @@ -1019,6 +1019,10 @@ virtio|virtio_blk|e1000: > =A0variants: > =A0 =A0 - @qcow2: > =A0 =A0 =A0 =A0 image_format =3D qcow2 > + =A0 =A0 =A0 =A0post_command =3D python scripts/check_image.py It is a good practice to append commands to post_command, not defining them exclusively, because in the fututre we might have more than one post command being run at a single test. In practice, it looks like this: post_command =3D " python scripts/check_image.py;" > + =A0 =A0 =A0 =A0remove_image =3D no > + =A0 =A0 =A0 =A0post_command_timeout =3D 600 > + > =A0 =A0 - vmdk: > =A0 =A0 =A0 =A0 only Fedora Ubuntu Windows > =A0 =A0 =A0 =A0 only smp2 Thanks for your work on this test!