On 29/03/13 14:40, liyang wrote:
On 2013-3-29 14:19, whuang wrote:Yes, I know..So I suggest use test.tmpdir instead of test.virtdir...test.tmpdir is created and deleted by autotest...On 29/03/13 14:11, liyang wrote:sorry I confuse you , the cdrom_dir = os.path.join(test.virtdir, "tmp/") is created by me , so I need cleanup by myself :POn 2013-3-29 13:52, whuang wrote:On 27/03/13 17:07, liyang wrote:On 2013-3-27 13:53, [email protected] wrote:I think you used the old autotest version..Now libvirt_xml is not a module any more, it's a package.From: whuang<[email protected]> Signed-off-by: whuang<[email protected]> ---libvirt/tests/virsh_change_media.py | 186 ++++++++++++++++++++++++++++++++++++1 file changed, 186 insertions(+) create mode 100755 libvirt/tests/virsh_change_media.pydiff --git a/libvirt/tests/virsh_change_media.py b/libvirt/tests/virsh_change_media.pynew file mode 100755 index 0000000..96156e4 --- /dev/null +++ b/libvirt/tests/virsh_change_media.py @@ -0,0 +1,186 @@ +import logging, os +from autotest.client.shared import error, utils +from virttest import libvirt_vm, virsh, libvirt_xmlIn the patch sets I sent, about "libvirtd test" is enough, you can ignore such test case..:-)+ +def run_virsh_change_media(test, params, env): + """ + Test command: virsh change-media. + + The command Change media of CD or floppy drive. + 1. Prepare test environment. + 2. When the libvirtd == "off", stop the libvirtd service.+ 3. Perform virsh change-media operation. + 4. Recover test environment. + 5. Confirm the test result. + """ + + def env_pre(old_iso, new_iso): + """ + Prepare ISO image for test + """ ++ utils.run("dd if=/dev/urandom of=%s/old bs=10M count=1" % cdrom_dir) + utils.run("dd if=/dev/urandom of=%s/new bs=10M count=1" % cdrom_dir)+ utils.run("mkisofs -o %s %s/old" % (old_iso, cdrom_dir)) + utils.run("mkisofs -o %s %s/new" % (new_iso, cdrom_dir)) + + def check_media(session, target_file, action): + """ + Check guest cdrom files + 1. guest session + 2. the expected files + 3. test case action + """ + + if action != "--eject ": + #sometimes device is busy so need mount twice + cmd = "mount /dev/cdrom /media -oloop"\ + "|| mount /dev/cdrom /media -oloop" + status, output = session.cmd_status_output(cmd) + logging.debug("Mount CDrom :%s %s" % (status, output))+ status, output = session.cmd_status_output("ls /media/%s" \+ % target_file) + if status == 0 :+ logging.debug("Check %s file then umount CDrom device"\+ % output) + session.cmd("umount -l /dev/cdrom") + else: + logging.error("Ls file :%s" % output)Maybe here needs to close the session before raise a error.+ raise error.TestFail("Can not find target file") + + else:+ if session.cmd_status("mount /dev/cdrom /media -oloop") == 32:+ logging.info("Eject succeed!") + return 0 + + def add_cdrom_device(vm_name, init_cdrom): + """ + Add cdrom device for test vm + """ ++ #options = init_cdrom + " hdc --type cdrom --sourcetype file --config"+ if vm.is_alive(): + virsh.destroy(vm_name) + virsh.attach_disk(vm_name, init_cdrom, \+ " hdc", " --type cdrom --sourcetype file --config",) +# cmd = "attach-disk --domain %s --source %s --target %s %s"\+# % (name, source, target, extra)Err..I think you forgot to delete this..+ + def update_cdrom(vm_name, init_iso, options, start_vm ): + """ + Update cdrom iso file for test case + """ + + cmd = """cat<< EOF> %s +<disk type='file' device='cdrom'> +<driver name='qemu' type='raw'/> +<source file='%s'/> +<target dev='hdc' bus='ide'/> +<readonly/> +</disk> +EOF""" % (update_iso_xml, init_iso) + cmd_options = "--force " + if os.system(cmd): + logging.error("Create update_iso_xml failed!") + + if options == "--config" or start_vm == "no": + cmd_options += " --config" + + #give domain the ISO image file + virsh.update_device(domainarg=vm_name, \+ filearg=update_iso_xml, flagstr=cmd_options)+ + vm_name = params.get("main_vm") + vm = env.get_vm(vm_name) + vm_ref = params.get("vm_ref") + action = params.get("action") + start_vm = params.get("start_vm") + options = params.get("options") + cdrom_dir = os.path.join(test.virtdir, "tmp/")small issue../tmp/?Err...my mistake..libvirt ignore --source when action = --eject , so I do not think it is a libvirt's bug :>+ if not os.path.exists(cdrom_dir): + os.mkdir(cdrom_dir) + + old_iso_name = params.get("old_iso") + new_iso_name = params.get("new_iso") + old_iso = cdrom_dir + old_iso_name + new_iso = cdrom_dir + new_iso_name + init_cdrom = params.get("init_cdrom") + update_iso_xml_name = params.get("update_iso_xml") + update_iso_xml = cdrom_dir + update_iso_xml_name + init_iso_name = params.get("init_iso") + if not init_iso_name : + init_iso = "" + + else: + init_iso = cdrom_dir + init_iso_name + + if vm_ref == "name": + vm_ref = vm_name + + env_pre(old_iso, new_iso) + #check domain's disk device + disk_blk = libvirt_xml.VMXML.get_disk_blk(vm_name)Here..Use libvirt_xml package's vm_xml module...According to your configuration..when option is "--eject", source_name will be null..So the source is just a path(test.virtdir)... And I know it won't effect the "virsh change-media" command's executing...I just want to know if it's a bug..> Chris+ logging.info("disk_blk %s" % disk_blk) + if "hdc" not in disk_blk: + logging.info("Need add CDROM device") + add_cdrom_device(vm_name, init_cdrom) + + if vm.is_alive() and start_vm == "no": + vm.destroy() + logging.info("Destroy guest !") + + elif vm.is_dead() and start_vm == "yes": + vm.start() + logging.info("Start guest !") + + update_cdrom(vm_name, init_iso, options, start_vm) + disk_device = params.get("disk_device") + libvirtd = params.get("libvirtd", "on") + + if libvirtd == "off": + libvirt_vm.libvirtd_stop() + + source_name = params.get("change_media_source") + source = cdrom_dir + source_namethanks liyang I will update my patch as your suggestionYou want to remove this directory? or just want to remove the files you created in the directory ... I think test.virtdir is belong to the autotest..it's not good to remove it..+ all_options = action + options + result = virsh.change_media(vm_ref, disk_device, \+ source, all_options, ignore_status=True)+ status = result.exit_status + status_error = params.get("status_error", "no") + + if status_error == "no": + if options == "--config" and vm.is_alive(): + vm.destroy() + + vm.start() + session = vm.wait_for_login() + check_file = params.get("check_file") + check_media(session, check_file, action) + session.close() + + #recover libvirtd service start + if libvirtd == "off": + libvirt_vm.libvirtd_start() + + #clean the cdrom dir and clean the cdrom device + update_cdrom(vm_name, "", options, start_vm) + utils.run("rm -rf %s/*" % cdrom_dir)cdrom_dir = os.path.join(test.virtdir, "tmp/")this is a temp dir create by autotest it can be deleted after testing .Sorry.. I made a mistake..:-( BTW, I used test.tmpdir like this: tmp_file = os.path.join(test.tmpdir,"tmp.xml")test.tmpdir is a temp dir created by autotest as you said, and it will be deleted automatically after testing. So you don't need to add some codes to clean up..hope helpful for you..:-)autotest do not cleanup the test.virtdir .Just a suggestion, you can ignore this..:-D
Oh, got it , I will use test.tmpdir ,thanks
+ + # Check status_error + + if status_error == "yes": + if status: + logging.info("It's an expected error") + + else: + raise error.TestFail("%d not a expected command " + "return value", status) + elif status_error == "no": + if status: + raise error.TestFail(result.stderr) + + else: + logging.info(result.stdout) +Alex suggested me to add: else: raise error.TestFail("The status_error must be 'yes' or 'no'!") I think it's useful, and I suggest you to do this too..I will add this+ +
-- Best Regards! Wenlong Huang IRC Account: wenlong Phone: 62608117 / 15011214521 _______________________________________________ Virt-test-devel mailing list [email protected] https://www.redhat.com/mailman/listinfo/virt-test-devel
