def help_check(test_target): """ Check all virsh commands or groups's help information :param test_target: Test target,all virsh or all virsh groups :return: True if check successfully """ help_list = [] if test_target == "all_command": help_list = virsh.help_command_only("", False, ignore_status=True) elif test_target == "all_group": help_list = virsh.help_command_group("", False, ignore_status=True) if len(help_list) == 0: raise error.TestError("Cannot get any virsh command/group!") fail_list = [] # If any command or group's check failed, the test failed check_result = True for virsh_cmd_group in help_list: logging.info("Test command or group: '%s'", virsh_cmd_group) result = virsh.help(virsh_cmd_group, ignore_status=True) status = result.exit_status output = result.stdout.strip() if status != 0: fail_list.append(virsh_cmd_group) # No need to check output continue if not output: fail_list.append(virsh_cmd_group) # List all failed commands or groups if len(fail_list) > 0: check_result = False logging.info("These commands or groups' check failed!!!") for fail_cmd in fail_list: logging.info("%s", fail_cmd) return check_result
def run(test, params, env): """ Test command: virsh help. 1.Get all parameters from configuration. 2.Perform virsh help operation. 3.Check help information valid or not. 4.Check result. """ extra = params.get("help_extra", "") cmd = params.get("help_command", "") test_target = params.get("help_target", "") status_error = params.get("status_error", "no") def help_check(test_target): """ Check all virsh commands or groups's help information :param test_target: Test target,all virsh or all virsh groups :return: True if check successfully """ help_list = [] if test_target == "all_command": help_list = virsh.help_command_only("", False, ignore_status=True) elif test_target == "all_group": help_list = virsh.help_command_group("", False, ignore_status=True) if len(help_list) == 0: raise error.TestError("Cannot get any virsh command/group!") fail_list = [] # If any command or group's check failed, the test failed check_result = True for virsh_cmd_group in help_list: logging.info("Test command or group: '%s'", virsh_cmd_group) result = virsh.help(virsh_cmd_group, ignore_status=True) status = result.exit_status output = result.stdout.strip() if status != 0: fail_list.append(virsh_cmd_group) # No need to check output continue if not output: fail_list.append(virsh_cmd_group) # List all failed commands or groups if len(fail_list) > 0: check_result = False logging.info("These commands or groups' check failed!!!") for fail_cmd in fail_list: logging.info("%s", fail_cmd) return check_result if test_target == "": cmd = "%s %s" % (cmd, extra) result = virsh.help(cmd, ignore_status=True) else: check_result = help_check(test_target) if test_target == "": status = result.exit_status output = result.stdout.strip() # Check status_error if status_error == "yes": if test_target == "": if status == 0: raise error.TestFail("Run successfully with wrong command!") elif status_error == "no": if test_target == "": if status != 0: raise error.TestFail("Run failed with right command") if output == "": raise error.TestFail("Cannot see help information") else: if not check_result: raise error.TestFail( "virsh help command or groups test failed")
def run(test, params, env): """ Test command: virsh qemu-monitor-command. """ vm_name = params.get("main_vm") vm = env.get_vm(vm_name) vm_ref = params.get("vm_ref", "domname") vm_state = params.get("vm_state", "running") cmd = params.get("qemu_cmd", "") options = params.get("options", "") status_error = "yes" == params.get("status_error", "no") domuuid = vm.get_uuid() domid = "" libvirtd_inst = utils_libvirtd.Libvirtd() help_info = virsh.help("qemu-monitor-command").stdout.strip() if "--pretty" in options: if "--pretty" not in help_info: raise error.TestNAError("--pretty option is not supported in" " current version") try: # Prepare vm state for test if vm_state != "shutoff": vm.start() vm.wait_for_login() domid = vm.get_id() if vm_state == "paused": vm.pause() if vm_ref == "domname": vm_ref = vm_name elif vm_ref == "domid": vm_ref = domid elif vm_ref == "domuuid": vm_ref = domuuid elif domid and vm_ref == "hex_id": vm_ref = hex(int(domid)) # Run virsh command cmd_result = virsh.qemu_monitor_command(vm_ref, cmd, options, ignore_status=True, debug=True) output = cmd_result.stdout.strip() status = cmd_result.exit_status # Check result if not libvirtd_inst.is_running(): raise error.TestFail("Libvirtd is not running after run command.") if status_error: if not status: # Return status is 0 with unknown command if "unknown command:" in output: logging.debug("Command failed: %s" % output) else: raise error.TestFail("Expect fail, but run successfully.") else: logging.debug("Command failed as expected.") else: if status: raise error.TestFail("Expect succeed, but run fail.") finally: # Cleanup if not libvirtd_inst.is_running(): libvirtd_inst.restart()
def run(test, params, env): """ Test command: virsh list. 1) Filt parameters according libvirtd's version 2) Prepare domain's exist state:transient,managed-save. 3) Prepare libvirt's status. 4) Execute list command. 5) Result check. """ def list_local_domains_on_remote(options_ref, remote_ip, remote_passwd, local_ip): """ Create a virsh list command and execute it on remote host. It will list local domains on remote host. :param options_ref:options in virsh list command. :param remote_ip:remote host's ip. :param remote_passwd:remote host's password. :param local_ip:local ip, to create uri in virsh list. :return:return status and output of the virsh list command. """ complete_uri = libvirt_vm.complete_uri(local_ip) command_on_remote = ("virsh -c %s list %s" % (complete_uri, options_ref)) session = remote.remote_login( "ssh", remote_ip, "22", "root", remote_passwd, "#") time.sleep(5) status, output = session.cmd_status_output( command_on_remote, internal_timeout=5) time.sleep(5) session.close() return int(status), output vm_name = params.get("main_vm", "virt-tests-vm1") options_ref = params.get("list_options_ref", "") list_ref = params.get("list_type_ref", "") vm_ref = params.get("vm_ref", "") status_error = params.get("status_error", "no") addition_status_error = params.get("addition_status_error", "no") desc = params.get("list_desc", "") libvirtd = params.get("libvirtd", "on") remote_ref = params.get("remote_ref", "") remote_ip = params.get("remote_ip", "REMOTE.EXAMPLE.COM") remote_pwd = params.get("remote_pwd", None) local_ip = params.get("local_ip", "LOCAL.EXAMPLE.COM") vm = env.get_vm(vm_name) domuuid = vm.get_uuid() domid = vm.get_id() # Some parameters are not supported on old libvirt, skip them. help_info = virsh.help("list").stdout.strip() if vm_ref and not re.search(vm_ref, help_info): raise error.TestNAError("This version do not support vm type:%s" % vm_ref) if list_ref and not re.search(list_ref, help_info): raise error.TestNAError("This version do not support list type:%s" % list_ref) # If a transient domain is destroyed, it will disappear. if vm_ref == "transient" and options_ref == "inactive": logging.info("Set addition_status_error to yes") logging.info( "because transient domain will disappear after destroyed.") addition_status_error = "yes" if vm_ref == "transient": vm_backup = vm_xml.VMXML.new_from_inactive_dumpxml(vm_name) vm.undefine() elif vm_ref == "managed-save": virsh.managedsave(vm_name, ignore_status=True, print_info=True) # run test case if list_ref == "--uuid": result_expected = domuuid logging.info("%s's uuid is: %s", vm_name, domuuid) elif list_ref == "--title": vm_backup = vm_xml.VMXML.new_from_inactive_dumpxml(vm_name) if options_ref == "inactive": virsh.desc(vm_name, "--config --title", desc) else: virsh.desc(vm_name, "--live --title", desc) result_expected = desc logging.info("%s's title is: %s", vm_name, desc) else: result_expected = vm_name logging.info("domain's name is: %s", vm_name) if options_ref == "vm_id": logging.info("%s's running-id is: %s", vm_name, domid) options_ref = "%s %s" % (domid, list_ref) elif options_ref == "vm_uuid": logging.info("%s's uuid is: %s", vm_name, domuuid) options_ref = "%s %s" % (domuuid, list_ref) elif options_ref == "inactive": vm.destroy() options_ref = "--inactive %s" % list_ref elif options_ref == "vm_name": options_ref = "%s %s" % (vm_name, list_ref) elif options_ref == "all": options_ref = "--all %s" % list_ref elif options_ref == "": options_ref = "%s" % list_ref # Prepare libvirtd status if libvirtd == "off": utils_libvirtd.libvirtd_stop() if remote_ref == "remote": if remote_ip.count("EXAMPLE.COM") or local_ip.count("EXAMPLE.COM"): raise error.TestNAError( "Remote test parameters unchanged from default") logging.info("Execute virsh command on remote host %s.", remote_ip) status, output = list_local_domains_on_remote( options_ref, remote_ip, remote_pwd, local_ip) logging.info("Status:%s", status) logging.info("Output:\n%s", output) else: if vm_ref: options_ref = "%s --%s" % (options_ref, vm_ref) result = virsh.dom_list( options_ref, ignore_status=True, print_info=True) status = result.exit_status output = result.stdout.strip() # Recover libvirtd service status if libvirtd == "off": utils_libvirtd.libvirtd_start() # Recover of domain if vm_ref == "transient" or list_ref == "--title": vm_backup.sync() elif vm_ref == "managed-save": # Recover saved guest. virsh.managedsave_remove(vm_name, ignore_status=True, print_info=True) # Check result status_error = (status_error == "no") and (addition_status_error == "no") if vm_ref == "managed-save": saved_output = re.search(vm_name + "\s+saved", output) if saved_output: output = saved_output.group(0) else: output = "" if not status_error: if not status and re.search(result_expected, output): raise error.TestFail("Run successful with wrong command!") else: if status: raise error.TestFail("Run failed with right command.") if not re.search(result_expected, output): raise error.TestFail("Run successful but result is not expected.")
def run(test, params, env): """ Test command: virsh list. 1) Filt parameters according libvirtd's version 2) Prepare domain's exist state:transient,managed-save. 3) Prepare libvirt's status. 4) Execute list command. 5) Result check. """ def list_local_domains_on_remote(options_ref, remote_ip, remote_passwd, local_ip): """ Create a virsh list command and execute it on remote host. It will list local domains on remote host. :param options_ref:options in virsh list command. :param remote_ip:remote host's ip. :param remote_passwd:remote host's password. :param local_ip:local ip, to create uri in virsh list. :return:return status and output of the virsh list command. """ complete_uri = libvirt_vm.complete_uri(local_ip) command_on_remote = ("virsh -c %s list %s" % (complete_uri, options_ref)) session = remote.remote_login( "ssh", remote_ip, "22", "root", remote_passwd, "#") time.sleep(5) status, output = session.cmd_status_output( command_on_remote, internal_timeout=5) time.sleep(5) session.close() return int(status), output vm_name = params.get("main_vm", "avocado-vt-vm1") options_ref = params.get("list_options_ref", "") list_ref = params.get("list_type_ref", "") vm_ref = params.get("vm_ref", "") status_error = params.get("status_error", "no") addition_status_error = params.get("addition_status_error", "no") desc = params.get("list_desc", "") libvirtd = params.get("libvirtd", "on") remote_ref = params.get("remote_ref", "") remote_ip = params.get("remote_ip", "REMOTE.EXAMPLE.COM") remote_pwd = params.get("remote_pwd", None) local_ip = params.get("local_ip", "LOCAL.EXAMPLE.COM") vm = env.get_vm(vm_name) domuuid = vm.get_uuid() domid = vm.get_id() # Some parameters are not supported on old libvirt, skip them. help_info = virsh.help("list").stdout.strip() if vm_ref and not re.search(vm_ref, help_info): raise error.TestNAError("This version do not support vm type:%s" % vm_ref) if list_ref and not re.search(list_ref, help_info): raise error.TestNAError("This version do not support list type:%s" % list_ref) # If a transient domain is destroyed, it will disappear. if vm_ref == "transient" and options_ref == "inactive": logging.info("Set addition_status_error to yes") logging.info( "because transient domain will disappear after destroyed.") addition_status_error = "yes" if vm_ref == "transient": vm_backup = vm_xml.VMXML.new_from_inactive_dumpxml(vm_name) vm.undefine() elif vm_ref == "managed-save": virsh.managedsave(vm_name, ignore_status=True, print_info=True) # run test case if list_ref == "--uuid": result_expected = domuuid logging.info("%s's uuid is: %s", vm_name, domuuid) elif list_ref == "--title": vm_backup = vm_xml.VMXML.new_from_inactive_dumpxml(vm_name) if options_ref == "inactive": virsh.desc(vm_name, "--config --title", desc) else: virsh.desc(vm_name, "--live --title", desc) result_expected = desc logging.info("%s's title is: %s", vm_name, desc) else: result_expected = vm_name logging.info("domain's name is: %s", vm_name) if options_ref == "vm_id": logging.info("%s's running-id is: %s", vm_name, domid) options_ref = "%s %s" % (domid, list_ref) elif options_ref == "vm_uuid": logging.info("%s's uuid is: %s", vm_name, domuuid) options_ref = "%s %s" % (domuuid, list_ref) elif options_ref == "inactive": vm.destroy() options_ref = "--inactive %s" % list_ref elif options_ref == "vm_name": options_ref = "%s %s" % (vm_name, list_ref) elif options_ref == "all": options_ref = "--all %s" % list_ref elif options_ref == "": options_ref = "%s" % list_ref # Prepare libvirtd status if libvirtd == "off": utils_libvirtd.libvirtd_stop() if remote_ref == "remote": if remote_ip.count("EXAMPLE.COM") or local_ip.count("EXAMPLE.COM"): raise error.TestNAError( "Remote test parameters unchanged from default") logging.info("Execute virsh command on remote host %s.", remote_ip) status, output = list_local_domains_on_remote( options_ref, remote_ip, remote_pwd, local_ip) logging.info("Status:%s", status) logging.info("Output:\n%s", output) else: if vm_ref: options_ref = "%s --%s" % (options_ref, vm_ref) result = virsh.dom_list( options_ref, ignore_status=True, print_info=True) status = result.exit_status output = result.stdout.strip() # Recover libvirtd service status if libvirtd == "off": utils_libvirtd.libvirtd_start() # Recover of domain if vm_ref == "transient" or list_ref == "--title": vm_backup.sync() elif vm_ref == "managed-save": # Recover saved guest. virsh.managedsave_remove(vm_name, ignore_status=True, print_info=True) # Check result status_error = (status_error == "no") and (addition_status_error == "no") if vm_ref == "managed-save": saved_output = re.search(vm_name + "\s+saved", output) if saved_output: output = saved_output.group(0) else: output = "" if not status_error: if not status and re.search(result_expected, output): raise error.TestFail("Run successful with wrong command!") else: if status: raise error.TestFail("Run failed with right command.") if not re.search(result_expected, output): raise error.TestFail("Run successful but result is not expected.")
desc = params.get("list_desc", "") libvirtd = params.get("libvirtd", "on") remote_ref = params.get("remote_ref", "") remote_ip = params.get("remote_ip", "REMOTE.EXAMPLE.COM") remote_pwd = params.get("remote_pwd", None) local_ip = params.get("local_ip", "LOCAL.EXAMPLE.COM") remote_user = params.get("remote_user", "root") local_user = params.get("username", "root") local_pwd = params.get("local_pwd", "password") vm = env.get_vm(vm_name) domuuid = vm.get_uuid() domid = vm.get_id() # Some parameters are not supported on old libvirt, skip them. help_info = virsh.help("list").stdout.strip() if vm_ref and not re.search(vm_ref, help_info): raise exceptions.TestSkipError( "This version do not support vm type:%s" % vm_ref) if list_ref and not re.search(list_ref, help_info): raise exceptions.TestSkipError("This version do not support list" " type:%s" % list_ref) # If a transient domain is destroyed, it will disappear. if vm_ref == "transient" and options_ref == "inactive": logging.info("Set addition_status_error to yes") logging.info( "because transient domain will disappear after destroyed.") addition_status_error = "yes" if vm_ref == "transient":
remote_ip = params.get("remote_ip") remote_pwd = params.get("remote_pwd", None) local_ip = params.get("local_ip") remote_user = params.get("remote_user", "root") local_user = params.get("username", "root") local_pwd = params.get("local_pwd", None) if "EXAMPLE" in remote_ip or "EXAMPLE" in local_ip: test.cancel("Please set real value for remote_ip or local_ip") vm = env.get_vm(vm_name) domuuid = vm.get_uuid() domid = vm.get_id() # Some parameters are not supported on old libvirt, skip them. help_info = virsh.help("list").stdout.strip() if vm_ref and not re.search(vm_ref, help_info): raise exceptions.TestSkipError("This version do not support vm type:%s" % vm_ref) if list_ref and not re.search(list_ref, help_info): raise exceptions.TestSkipError("This version do not support list" " type:%s" % list_ref) # If a transient domain is destroyed, it will disappear. if vm_ref == "transient" and options_ref == "inactive": logging.info("Set addition_status_error to yes") logging.info( "because transient domain will disappear after destroyed.") addition_status_error = "yes" if vm_ref == "transient":