From d3e3dbffb8d61b0f34b6f3aadfe3d5210211d7f8 Mon Sep 17 00:00:00 2001 From: Liam Li Date: Feb 26 2010 07:32:45 +0000 Subject: add some functions,such as:imitate DVD install to boot and install,add dogtail support, add script running environment check --- diff --git a/tests/anaconda/dvd_install.py b/tests/anaconda/dvd_install.py index f283092..b47ac07 100755 --- a/tests/anaconda/dvd_install.py +++ b/tests/anaconda/dvd_install.py @@ -1,10 +1,11 @@ #!/usr/bin/python # Anaconda Test Suite - install testing # -# basic usage: ./dvd_install.py -n VMname -k KickstartFile -a Arch -d InstallTreeDirecotry +# basic usage: ./dvd_install.py -n VMname -k KickstartFile -a Arch -i DVD_image # some example of tests: -# mount -o loop DVDImage.ISO /InstallTree -#./dvd_install.py -k http://server/ks.cfg -a i386 -d /InstallTree |tee test.log +#./dvd_install.py -k http://server/ks.cfg -a i386 -i DVD_image.iso |tee test.log +#./dvd_install.py -k /path/to/ks.cfg -a i386 -i DVD_image.iso |tee test.log +#./dvd_install.py -n VMname -k http://server/ks.cfg -a x86_64 -i DVD_image.iso |tee test.log # Copyright 2009, Red Hat, Inc. # # This program is free software; you can redistribute it and/or modify @@ -25,12 +26,13 @@ import os import sys +import rpm import time +import gconf import select import commands import optparse import ConfigParser -import guestfs import curses from subprocess import * from SimpleXMLRPCServer import SimpleXMLRPCServer @@ -41,10 +43,10 @@ data = '' def parse_args(): archlist = ['i386', 'x86_64'] - parse = optparse.OptionParser(usage="dvd_install.py -n VMname -k KickstartFile -a Arch -d InstallTreeDirecotry",version="%prog 1.0") + parse = optparse.OptionParser(usage="dvd_install.py -n VMname -k KickstartFile -a Arch -i DVD_image",version="%prog 1.0") parse.add_option("-a","--arch",action="store",type="choice",choices=archlist,default=get_basearch(),dest="arch") parse.add_option("-k","--kickstart",action="store",dest="kickstart") - parse.add_option("-d","--directory",action="store",dest="directory") + parse.add_option("-i","--image",action="store",dest="image") parse.add_option("-n","--name",action="store",dest="name") (opt,args) = parse.parse_args() return opt @@ -61,6 +63,41 @@ def get_basearch(): print "Auto install only support i386 and x86_64" return arch +def user_env_check(): + login_user = os.environ['LOGNAME'] + print "log in user is: %s" % login_user + if login_user != "root" : + print "ERROR: Can't be run as a normal user!!" + print "You may have to run 'su -' or log in gnome as root!!" + sys.exit(1) + else : + gconf_val = gconf.client_get_default().get_bool('/desktop/gnome/interface/accessibility') + print "accessibility support is: %s" % gconf_val + if gconf_val != True: + print "Setting accessibility to True..." + gconf.client_get_default().set_bool('/desktop/gnome/interface/accessibility', True) + +def package_check(package_name): + rpm_state = False + rpm_ts = rpm.TransactionSet() + rpm_query = rpm_ts.dbMatch('name', package_name) + for i in rpm_query: + print "%s-%s-%s was found in system." % (i['name'],i['version'],i['release']) + rpm_state = True + if not rpm_state: + print "Package %s was not installed in system,please install it first!!" % package_name + return rpm_state + +def input_text(str): + import dogtail.procedural + for s in str: + if ord(s) >=65 and ord(s) <= 90: + dogtail.procedural.keyCombo("%s" % chr(ord(s)+32)) + elif ord(s) == 58: + dogtail.procedural.keyCombo("colon") + else: + dogtail.procedural.type(s) + def get_kickstart_file(ksfile): if os.path.exists(ksfile): ksfile = os.path.split(ksfile)[1] @@ -75,7 +112,7 @@ def set_disk(name, ksfile, kstype): if kstype == 'local': disk = [ '--disk', 'path=/var/lib/libvirt/images/%s.img,size=8' % name, - '--disk', 'path=/var/lib/libvirt/images/test.img' ] + '--disk', 'path=/var/lib/libvirt/images/ks.img' ] kickstart = 'hd:/dev/vdb1:/%s' % ksfile else : disk = [ @@ -84,14 +121,17 @@ def set_disk(name, ksfile, kstype): return (disk, kickstart) def put_local_ks(ksfile): + import guestfs g = guestfs.GuestFS() - f = open("/var/lib/libvirt/images/test.img", "w") + f = open("/var/lib/libvirt/images/ks.img", "w") f.truncate (500 * 1024 * 1024) f.close() - g.add_drive("/var/lib/libvirt/images/test.img") + g.add_drive("/var/lib/libvirt/images/ks.img") g.launch() g.sfdiskM("/dev/sda",["4",]) + print "disk was formatted\n" g.mkfs("ext3", "/dev/sda1") + print "file system was created\n" g.mount("/dev/sda1", "/") g.upload(opt.kickstart, "/%s" % ksfile) g.sync() @@ -127,21 +167,19 @@ def read_until(condition, readtimeout=60,max_wait=10800): if condition(data): return data os.system('clear') - print_to_screen('error!,did not get the data\n') + print_to_screen('error!,did not get output data\n') -def create_guest(img_location, name, kickstart, disk): +def create_guest(dvd_image, name, kickstart, disk): "Create a new virt guest" virt_args = [ '--name', name, - '--ram' , '512', + '--ram' , '768', '--vcpus', '1', '--os-type', 'linux', '--os-variant', 'fedora13', - '--location', img_location, - '--extra-args','serial console=ttyS0 ks=%s' % kickstart, + '--cdrom', dvd_image, '--accelerate', - '--nographics', '--noautoconsole' ] create_command = ['virt-install'] + virt_args + disk print "create command is: %s" % create_command @@ -149,6 +187,7 @@ def create_guest(img_location, name, kickstart, disk): if retval !=0: delete(name) print "virt-install failed: %i" % retval + sys.exit(1) return name def delete(name): @@ -156,16 +195,47 @@ def delete(name): if vmstate == 'running' or vmstate == 'paused' or vmstate == 'idle' : call(["virsh", "destroy", name]) call(["virsh", "undefine", name]) + elif vmstate == None : + print "vm %s was not found,nothing to do." % name else : call(["virsh","undefine", name]) def clean_guest(name): - print "Cleaning up guest\n" + print "Cleaning up guest %s ...\n" % name delete(name) +def pass_boot_args(vmname, ks): + import dogtail.procedural + print "Begin to start virt-viewer\n" + dogtail.procedural.run("virt-viewer","%s" % vmname) + print "mouse focus on the viewer\n" + time.sleep(2) + dogtail.procedural.focus.application('virt-viewer') + dogtail.procedural.focus.frame('%s - Virt Viewer' % vmname) + dogtail.procedural.click('', roleName='unknown', raw=True) + dogtail.procedural.keyCombo("Tab") + dogtail.procedural.keyCombo("Tab") + time.sleep(2) + if len(ks) >1: + input_text(" serial console=ttyS0 ks=%s" % ks ) + else: + print "kickstart args error!\n" + sys.exit(1) + time.sleep(2) + dogtail.procedural.keyCombo("Return") + dogtail.procedural.keyCombo("") + +def close_win(name): + import dogtail.procedural + print "begin to close window" + dogtail.procedural.focus.frame('%s - Virt Viewer' % name) + dogtail.procedural.click('File', roleName='menu item') + dogtail.procedural.click('Quit', roleName='menu item') + def get_vmstate(name): if name : status,line = commands.getstatusoutput('virsh list --all |grep %s' % name) + time.sleep(1) if line : vmstate = line.split()[2] return vmstate @@ -175,9 +245,10 @@ def opentty(name): (output, dummy) = p.communicate() tty = output.strip() assert os.path.exists(tty) - tty = tty + print "tty is %s" % tty global console console = open(tty) + print "console is : %s" % console pollobj.register(console, select.POLLIN) return (tty, console) @@ -186,6 +257,7 @@ def closetty(console): console.close() def kernel_boot_test(name): + print "start to open tty\n" (tty, console) = opentty(name) print "Serial console at %s" % tty print "Begin to poll and read....\n" @@ -211,12 +283,13 @@ def loader_stage2_test(name, readtimeout=300): condition = lambda data: "Running anaconda" in data data = read_until(condition, readtimeout) if data: + time.sleep(1) line = data[data.rfind("Running anaconda"):] version = line.split()[2] version = version.strip(",") time.sleep(1) - print "anaconda %s stage2 has started" % version - print "loader stage2 was successfully completed.\n" + print "\nanaconda %s stage2 has started" % version + print "\nloader stage2 was successfully completed." return version print "Test failed,loader stage2 timeout reached.\n" error_complete() @@ -242,26 +315,30 @@ def error_complete(): print "The error was detected above,test failed!" if __name__ == '__main__': + user_env_check() + if not package_check('qemu-kvm'): + print "KVM was not installed in system, you have to install virtualization first" + print "more information,please see at:http://fedoraproject.org/wiki/Virtualization_Quick_Start" + sys.exit(1) + if not package_check('python-libguestfs'): + sys.exit(1) + if not package_check('dogtail'): + sys.exit(1) opt = parse_args() sys_arch = get_basearch() if not opt.arch == sys_arch: print "The current system does not support your test image arch" sys.exit(1) - if opt.directory is None and opt.kickstart is None: - print "usage: ./dvd_install.py -n VMname -k KickstartFile -a Arch -d InstallTreeDirecotry" + if opt.image is None and opt.kickstart is None: + print "usage: ./dvd_install.py -n VMname -k KickstartFile -a Arch -i DVD_image" print "For example:" - print "mount -o loop DVDImage.iso /InstallTree" - print "./dvd_install.py -k http://server/ks.cfg -a i386 -d /InstallTree |tee test.log\n" - sys.exit(1) - if not os.getuid() == 0: - print "ERROR: Can't be run as a normal user!!" + print "./dvd_install.py -k http://server/ks.cfg -a i386 -i DVD_image |tee test.log\n" sys.exit(1) if opt.name == None: vmname = 'fedoratest' else: vmname = opt.name if not get_vmstate(vmname) : - print "vmstate is: %s, will delelte VM" % get_vmstate delete(vmname) (kickstart, kstype) = get_kickstart_file(opt.kickstart) if kstype == 'local': @@ -270,11 +347,16 @@ if __name__ == '__main__': try: print "Start to create virtual machine: %s" % vmname - name = create_guest(opt.directory, vmname, kickstart, disk) + name = create_guest(opt.image, vmname, kickstart, disk) + print "\n== start to pass kernel boot arguments ==\n" + pass_boot_args(name, kickstart) + print "\n== kernel boot test ==\n" kernel_boot_test(name) + close_win(name) + print "\n== anaconda loading stage2 test ==\n" loader_stage2_test(name)