Test cdrom about mount/format/copy/md5sum, change iso file by monitor command, create iso files by pre_command, clean temporary file by post_command. Signed-off-by: Amos Kong <akong@xxxxxxxxxx> --- client/tests/kvm/tests/cdrom.py | 131 ++++++++++++++++++++++++++++++++ client/tests/kvm/tests_base.cfg.sample | 11 +++ 2 files changed, 142 insertions(+), 0 deletions(-) create mode 100644 client/tests/kvm/tests/cdrom.py diff --git a/client/tests/kvm/tests/cdrom.py b/client/tests/kvm/tests/cdrom.py new file mode 100644 index 0000000..be5e4cd --- /dev/null +++ b/client/tests/kvm/tests/cdrom.py @@ -0,0 +1,131 @@ +import logging, re, time +from autotest_lib.client.common_lib import error +from autotest_lib.client.virt import virt_utils + +def run_cdrom(test, params, env): + """ + KVM cdrom test: + 1) Boot up a VM with one iso + 2) Check if VM identify iso file + 3) Eject cdrom and change with another iso for many times + 4) Try to fromat cdrom and check the return string + 5) Mount cdrom device to /mnt + 6) Copy file from cdrom and compare files by diff + 7) Umount and mount many times + + @param test: kvm test object + @param params: Dictionary with the test parameters + @param env: Dictionary with test environment. + """ + def get_cdrom_info(): + o = vm.monitor.info("block") + try: + device = re.findall("(ide\d+-cd\d+): .*", o)[0] + except IndexError: + device = None + try: + file = re.findall("ide\d+-cd\d+: .*file=(\S*) ", o)[0] + except IndexError: + file = None + logging.debug("Device name: %s, ISO: %s" % (device, file)) + return (device, file) + + def check_cdrom_locked(cdrom): + blocks_info = vm.monitor.info("block") + if isinstance(blocks_info, str): + lock_str = "locked=1" + for block in blocks_info.splitlines(): + if cdrom in block and lock_str in block: + return True + else: + for block in blocks_info: + if 'inserted' in block.keys() and\ + block['inserted']['file'] == cdrom: + return block['locked'] + return False + + + vm = env.get_vm(params["main_vm"]) + vm.verify_alive() + + session = vm.wait_for_login(timeout=int(params.get("login_timeout", 360))) + cdrom_orig = params.get("cdrom_cd1") + cdrom_new = params.get("new_iso") + cdrom = cdrom_orig + output = session.get_command_output("ls /dev/cdrom*") + cdrom_dev_list = re.findall("/dev/cdrom-\w+|/dev/cdrom\d*", output) + logging.debug("cdrom_dev_list: %s" % cdrom_dev_list) + + cdrom_dev = "" + test_cmd = "dd if=%s of=/dev/null bs=1 count=1" + for d in cdrom_dev_list: + s, o = session.cmd_status_output(test_cmd % d) + if s == 0: + cdrom_dev = d + break + else: + raise error.TestFail("Could not find a valid cdrom device." + " dd returns: %d, output:\n%s" % (s, o)) + + logging.info("Detecting the existence of cdrom") + (device, file) = get_cdrom_info() + if file != cdrom: + raise error.TestError("%s does not realized" % cdrom) + + session.get_command_output("umount %s" % cdrom_dev) + if not virt_utils.wait_for(lambda: not check_cdrom_locked(file), 300): + raise error.TestError("%s could not become unlocked" % device) + + max_times = int(params.get("max_times", 100)) + logging.info("Eject the cdrom for %s times" % max_times) + max = max_times + while max > 0: + vm.monitor.cmd("eject %s" % device) + (device, file) = get_cdrom_info() + if file is not None: + raise error.TestFail("%s is not ejected" % cdrom) + + cdrom = cdrom_new + if max % 2 == 0: + cdrom = cdrom_orig + vm.monitor.cmd("change %s %s" % (device, cdrom)) + time.sleep(10) + (device, file) = get_cdrom_info() + if file != cdrom: + raise error.TestError("%s is not changed" % cdrom) + max -= 1 + + logging.info("Check whether the cdrom is read-only") + filename = params.get("filename") + dest_dir = "/mnt" + s, o = session.get_command_status_output("echo y|mkfs %s" % cdrom_dev) + if not "Read-only" in o: + logging.debug("Format cdrom doesn't return Read-only error") + if s == 0: + raise error.TestFail("Format %s unexpected success" % cdrom_dev) + + if not virt_utils.wait_for(lambda: session.get_command_status("mount %s %s" + % (cdrom_dev, dest_dir)) == 0, 30): + logging.debug(session.get_command_output("cat /etc/mtab")) + raise error.TestFail("Could not mount %s" % cdrom_dev) + + logging.info("File copying test") + cmd = "/bin/cp -f %s/%s /tmp/" + if session.get_command_status(cmd % (dest_dir, filename)) != 0: + raise error.TestFail("Fail to copy file from %s" % dest_dir) + cmd = "diff %s/%s /tmp/%s" % (dest_dir, filename, filename) + if session.get_command_status(cmd) != 0: + raise error.TestFail("Two file is different, cmd: %s" % cmd) + + logging.info("Mounting/Unmounting test") + max = max_times + while max > 0: + if session.get_command_status("umount %s" % cdrom_dev) != 0: + raise error.TestFail("Could not umount %s" % cdrom_dev) + cmd = "mount %s %s" % (cdrom_dev, dest_dir) + if session.get_command_status(cmd) != 0: + logging.debug(session.get_command_output("cat /etc/mtab")) + raise error.TestFail("Could not mount %s" % cdrom_dev) + max -= 1 + + session.close() diff --git a/client/tests/kvm/tests_base.cfg.sample b/client/tests/kvm/tests_base.cfg.sample index 65880d8..5d6227b 100644 --- a/client/tests/kvm/tests_base.cfg.sample +++ b/client/tests/kvm/tests_base.cfg.sample @@ -1101,6 +1101,17 @@ variants: kill_vm_gracefully = no # Do not define test variants below shutdown + - cdrom_test: install setup image_copy unattended_install.cdrom + type = cdrom + pre_command = "dd if=/dev/urandom of=orig bs=10M count=1 && dd if=/dev/urandom of=new bs=10M count=1 && mkisofs -o /tmp/kvm_autotest_root/orig.iso orig && mkisofs -o /tmp/kvm_autotest_root/new.iso new" + new_iso = /tmp/kvm_autotest_root/new.iso + cdrom_cd1 = orig.iso + max_times = 20 + filename = new + kill_vm = yes + post_command = rm -rf /tmp/kvm_autotest_root/orig.iso /tmp/kvm_autotest_root/new.iso orig new + only Linux + # NICs variants: -- To unsubscribe from this list: send the line "unsubscribe kvm" in the body of a message to majordomo@xxxxxxxxxxxxxxx More majordomo info at http://vger.kernel.org/majordomo-info.html