On 03/12/2013 11:06 AM, [email protected] wrote:
From: whuang<[email protected]>
Signed-off-by: whuang<[email protected]>
---
libvirt/tests/virsh_change_media.py | 169 ++++++++++++++++++++++++++++++++++++
1 file changed, 169 insertions(+)
create mode 100755 libvirt/tests/virsh_change_media.py
diff --git a/libvirt/tests/virsh_change_media.py
b/libvirt/tests/virsh_change_media.py
new file mode 100755
index 0000000..a56dd39
--- /dev/null
+++ b/libvirt/tests/virsh_change_media.py
@@ -0,0 +1,169 @@
+import logging, os
+from autotest.client.shared import error, utils
+from virttest import libvirt_vm, virsh, libvirt_xml
+
+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(params):
+ """
+ Prepare ISO image for test
+ """
+
+ old_iso = params.get("old_iso")
+ new_iso = params.get("new_iso")
+ if not os.path.exists(cdrom_dir):
+ os.mkdir(cdrom_dir)
+
+ 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
+ status, output = session.cmd_status_output("mount /dev/cdrom /media
"\
+ "|| mount /dev/cdrom
/media")
+ 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)
+ raise error.TestFail("Can not find target file")
+
+ else:
+ if session.cmd_status("mount /dev/cdrom /media") == 32:
+ logging.info("Eject succeed!")
+ return 0
+
+ def add_cdrom_device(vm_name, params):
I advise you to get parameters of 'params' in main code, rather than
pass the whole 'params'.
One reason is that it is good for maintain. :D
Another reason is that if your function is general enough,
we can move it to a suitable module as a common API easily.
+ """
+ Add cdrom device for test vm
+ """
+
+ init_cdrom = params.get("init_cdrom")
+ options = init_cdrom + " hdc --type cdrom --sourcetype file --config"
+ if vm.is_alive():
+ virsh.destroy(vm_name)
+ virsh.attach_disk(vm_name, options)
+
+ 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(vm_name, update_iso_xml, cmd_options)
+
+ vm_name = params.get("main_vm")
+ vm = env.get_vm(vm_name)
+ vm_ref = params.get("vm_ref")
+ cdrom_dir = params.get("cdrom_dir")
+ action = params.get("action")
+ start_vm = params.get("start_vm")
+ update_iso_xml = params.get("update_iso_xml")
+ init_iso = params.get("init_iso")
+ options = params.get("options")
+
+ if vm_ref == "name":
+ vm_ref = vm_name
+
+ env_pre(params)
+ #check domain's disk device
+ disk_blk = libvirt_xml.VMXML.get_disk_blk(vm_name)
+ logging.info("disk_blk %s" % disk_blk)
+ if "hdc" not in disk_blk:
+ logging.info("Need add CDROM device")
+ add_cdrom_device(vm_name, params)
+
+ update_cdrom(vm_name, init_iso, options, start_vm)
+ if vm.is_alive() and start_vm == "no":
+ vm.destroy()
+ logging.info("Destoy guest !")
+
+ elif vm.is_dead() and start_vm == "yes":
+ vm.start()
+ logging.info("Start guest !")
+
+ disk_device = params.get("disk_device")
+ libvirtd = params.get("libvirtd", "on")
+
+ if libvirtd == "off":
+ libvirt_vm.libvirtd_stop()
+
+ options = params.get("options")
+ source = params.get("change_media_source")
+ all_options = action + options
+
+ result = virsh.change_media(vm_ref, disk_device, source, all_options)
You'd better adding 'ignore_status=True' in virsh.change_media.
Because when some exception happen, this testcase will end without
result check and cleanup.
+ status = result.exit_status
+ status_error = params.get("status_error", "no")
+
+ if status_error == "no":
+ if options == "--config" and vm.is_alive():
+ virsh.destroy(vm_name)
To avoid unexpected exception, I advise you to use vm.destroy().
+
+ virsh.start(vm_name)
Same as above.
+ 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()
+
+ # 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)
+
+ #clean the cdrom dir and clean the cdrom device
+ update_cdrom(vm_name, "", options, start_vm)
+ utils.run("rm -rf %s" % cdrom_dir)
Should we cleanup before result check?
I'm afraid that if negative test run pass, the environment will be not
cleaned. :(
+
Thanks~
--
Best Regards
Yu Mingfei
_______________________________________________
Virt-test-devel mailing list
[email protected]
https://www.redhat.com/mailman/listinfo/virt-test-devel