On 2013-3-29 13:52, whuang wrote:
On 27/03/13 17:07, liyang wrote:
On 2013-3-27 13:53, [email protected] wrote:
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.py
diff --git a/libvirt/tests/virsh_change_media.py
b/libvirt/tests/virsh_change_media.py
new 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_xml
I think you used the old autotest version..Now libvirt_xml is not a
module any more, it's a package.
+
+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.
In the patch sets I sent, about "libvirtd test" is enough, you can
ignore such test case..:-)
+ 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..
+ 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...
+ 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_name
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
libvirt ignore --source when action = --eject , so I do not think it
is a libvirt's bug :>
thanks liyang I will update my patch as your suggestion
+ 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)
You 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..
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..:-)
+
+ # 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
+
+
--
Regards,
--------------------------------------------------
Li Yang
No. 6 Wenzhu Road, Nanjing, 210012, China
TEL:+86+25-86630566-8526
FUJITSU INTERNAL:7998-8526
FAX:+86+25-83317685
EMail:[email protected]
--------------------------------------------------
This communication is for use by the intended recipient(s) only and may contain
information that is privileged, confidential and exempt from disclosure under
applicable law. If you are not an intended recipient of this communication, you
are hereby notified that any dissemination, distribution or copying hereof is
strictly prohibited. If you have received this communication in error, please
notify me by reply e-mail, permanently delete this communication from your
system, and destroy any hard copies you may have printed
_______________________________________________
Virt-test-devel mailing list
[email protected]
https://www.redhat.com/mailman/listinfo/virt-test-devel