def run_virsh_hostname(test, params, env): """ Test the command virsh hostname (1) Call virsh hostname (2) Call virsh hostname with an unexpected option (3) Call virsh hostname with libvirtd service stop """ hostname_result = utils.run("hostname", ignore_status=True) hostname = hostname_result.stdout.strip() # Prepare libvirtd service check_libvirtd = params.has_key("libvirtd") if check_libvirtd: libvirtd = params.get("libvirtd") if libvirtd == "off": libvirt_vm.libvirtd_stop() # Run test case option = params.get("virsh_hostname_options") try: hostname_test = virsh.hostname(option, ignore_status=False, debug=True) status = 0 # good except error.CmdError: status = 1 # bad hostname_test = None # Recover libvirtd service start if libvirtd == "off": libvirt_vm.libvirtd_start() # Check status_error status_error = params.get("status_error") if status_error == "yes": if status == 0: raise error.TestFail("Command 'virsh hostname %s' succeeded " "(incorrect command)" % option) elif status_error == "no": if cmp(hostname, hostname_test) != 0: raise error.TestFail("Virsh cmd gives hostname %s != %s." % (hostname_test, hostname)) if status != 0: raise error.TestFail("Command 'virsh hostname %s' failed " "(correct command)" % option)
def run_virsh_version(test, params, env): """ Test the command virsh version (1) Call virsh version (2) Call virsh version with an unexpected option (3) Call virsh version with libvirtd service stop """ connect_uri = libvirt_vm.normalize_connect_uri( params.get("connect_uri", "default")) # Prepare libvirtd service check_libvirtd = params.has_key("libvirtd") if check_libvirtd: libvirtd = params.get("libvirtd") if libvirtd == "off": libvirt_vm.libvirtd_stop() # Run test case option = params.get("virsh_version_options") try: output = virsh.version(option, uri=connect_uri, ignore_status=False, debug=True) status = 0 #good except error.CmdError: status = 1 #bad # Recover libvirtd service start if libvirtd == "off": libvirt_vm.libvirtd_start() # Check status_error status_error = params.get("status_error") if status_error == "yes": if status == 0: raise error.TestFail("Command 'virsh version %s' succeeded " "(incorrect command)" % option) elif status_error == "no": if status != 0: raise error.TestFail("Command 'virsh version %s' failed " "(correct command)" % option)
def run_virsh_domifstat(test, params, env): """ Test command: virsh domifstat. The command can get network interface stats for a running domain. 1.Prepare test environment. 2.When the libvirtd == "off", stop the libvirtd service. 3.Perform virsh domifstat operation. 4.Recover test environment. 5.Confirm the test result. """ vm_name = params.get("main_vm") vm = env.get_vm(vm_name) def get_interface(guest_name): """ Get interface device of VM. @param guest_name: VM's name. @return: interface device of VM. """ interface = "" domxml = utils.system_output("virsh dumpxml %s" % guest_name) dom = parseString(domxml) root = dom.documentElement array = root.getElementsByTagName("interface") for element in array: if element.getAttribute("type") == "bridge" or \ element.getAttribute("type") == "network": interface = "vnet0" nodelist = element.childNodes for node in nodelist: if node.nodeName == "target": interface = node.getAttribute("dev") break return interface domid = vm.get_id() domuuid = vm.get_uuid() vm_ref = params.get("domifstat_vm_ref", vm_name) nic_ref = params.get("domifstat_nic_ref", "") libvirtd = params.get("libvirtd", "on") status_error = params.get("status_error", "no") if vm_ref == "id": vm_ref = domid elif vm_ref == "hex_id": vm_ref = hex(int(domid)) elif vm_ref.find("invalid") != -1: vm_ref = params.get(vm_ref) elif vm_ref == "uuid": vm_ref = domuuid elif vm_ref == "name": vm_ref = vm_name interface = get_interface(vm_name) if nic_ref == "": interface = "" elif nic_ref == "error_interface": interface = params.get(nic_ref) interface = "%s %s" % (interface, params.get("domifstat_extra")) if libvirtd == "off": libvirt_vm.libvirtd_stop() status = virsh.domifstat(vm_ref, interface, ignore_status=True).exit_status #recover libvirtd service start if libvirtd == "off": libvirt_vm.libvirtd_start() #check status_error if status_error == "yes": if status == 0: raise error.TestFail("Run successfully with wrong command!") elif status_error == "no": if status != 0: raise error.TestFail("Run failed with right command")
session.close() # FIXME: Catch specific exception except Exception, detail: logging.error("Exception: %s", str(detail)) status = -1 if vm_ref != "remote_name": vm_ref = "%s %s" % (vm_ref, extra) cmdresult = virsh.reboot(vm_ref, ignore_status=True, debug=True) status = cmdresult.exit_status if status: logging.debug("Error status, command output: %s", cmdresult.stdout) if not virsh.has_command_help_match('reboot', '\s+--mode\s+'): # old libvirt doesn't support reboot status = -2 output = virsh.dom_list(ignore_status=True).stdout.strip() #recover libvirtd service start if libvirtd == "off": libvirt_vm.libvirtd_start() #check status_error if status_error == "yes": if status == 0: raise error.TestFail("Run successfully with wrong command!") elif status_error == "no": if status != 0 or (not re.search(vm_name, output)): if status == -2: raise error.TestNAError( "Reboot command doesn't work on older libvirt versions") raise error.TestFail("Run failed with right command")
def run_virsh_start(test, params, env): """ Test command: virsh start. 1) Get the params from params. 2) Prepare libvirtd's status. 3) Do the start operation. 4) Result check. 5) clean up. """ #get the params from params vm_name = params.get("vm_name", "vm1") backup_name = vm_name if vm_name is not "": vm = env.get_vm(vm_name) vmxml = libvirt_xml.VMXML() libvirtd_state = params.get("libvirtd", "on") pre_operation = params.get("vs_pre_operation", "") status_error = params.get("status_error", "no") #get the params for remote test remote_ip = params.get("remote_ip", "ENTER.YOUR.REMOTE.IP") remote_password = params.get("remote_password", "ENTER.YOUR.REMOTE.PASSWORD") local_ip = params.get("local_ip", "ENTER.YOUR.LOCAL.IP") if pre_operation == "remote" and ( remote_ip.count("ENTER.YOUR.") or local_ip.count("ENTER.YOUR.")): raise error.TestNAError("Remote test parameters not configured") try: #prepare before start vm if libvirtd_state == "on": libvirt_vm.libvirtd_start() elif libvirtd_state == "off": libvirt_vm.libvirtd_stop() if pre_operation == "rename": new_vm_name = params.get("vs_new_vm_name", "virsh_start_vm1") vm = libvirt_xml.VMXML.vm_rename(vm, new_vm_name) vm_name = new_vm_name elif pre_operation == "undefine": vmxml = vmxml.new_from_dumpxml(vm_name) vmxml.undefine() #do the start operation try: if pre_operation == "remote": #get remote session session = remote.wait_for_login("ssh", remote_ip, "22", "root", remote_password, "#") #get uri of local uri = libvirt_vm.complete_uri(local_ip) cmd = "virsh -c %s start %s" % (uri, vm_name) status, output = session.cmd_status_output(cmd) if status: raise StartError(vm_name, output) else: do_virsh_start(vm_name) #start vm successfully if status_error == "yes": raise error.TestFail("Run successfully with wrong command!") except StartError, excpt: #start vm failed if status_error == "no": raise error.TestFail("Run failed with right command: %s", str(excpt)) finally: #clean up if libvirtd_state == "off": libvirt_vm.libvirtd_start() if (pre_operation == "undefine") and (not vmxml.xml == None): vmxml.define() elif pre_operation == "rename": libvirt_xml.VMXML.vm_rename(vm, backup_name)
def run_virsh_domxml_to_native(test, params, env): """ Test command: virsh domxml-to-native. Convert domain XML config to a native guest configuration format. 1.Prepare test environment. 2.When the libvirtd == "off", stop the libvirtd service. 3.Perform virsh domxml-from-native operation. 4.Recover test environment. 5.Confirm the test result. """ vm_name = params.get("main_vm") vm = env.get_vm(params["main_vm"]) vm.verify_alive() def compare(conv_arg): """ Compare converted infomation with vm's infomation. @param: conv_arg : Converted infomation. @return: True if converted infomation has no diffrent from vm's infomation. """ pid = vm.get_pid() cmdline_tmp = utils.system_output("cat -v /proc/%d/cmdline" % pid) cmdline = re.sub(r'\^@', ' ', cmdline_tmp) tmp = re.search('LC_ALL.[^\s]\s', conv_arg).group(0) +\ re.search('PATH.[^\s]+\s', conv_arg).group(0) +\ re.search('QEMU_AUDIO_DRV.[^\s]+\s', conv_arg).group(0) qemu_arg = tmp + cmdline conv_arg_lines = conv_arg.split('\x20') qemu_arg_lines = qemu_arg.split('\x20') i = 0 result = True for arg in conv_arg_lines: print arg print qemu_arg_lines[i] if re.search("mode=readline", arg): i += 1 continue elif re.search("mac=00:00:00:00:00:00", arg): i += 1 continue elif re.search("127.0.0.1:0", arg): i += 1 continue elif re.search("tap", arg): i += 1 continue if arg != qemu_arg_lines[i]: result = False i += 1 return result #run test case dtn_format = params.get("dtn_format") file_xml = params.get("dtn_file_xml") extra_param = params.get("dtn_extra_param") libvirtd = params.get("libvirtd") status_error = params.get("status_error") virsh.dumpxml(vm_name, extra="", to_file=file_xml) if libvirtd == "off": libvirt_vm.libvirtd_stop() ret = virsh.domxml_to_native(dtn_format, file_xml, extra_param, ignore_status = True) status = ret.exit_status conv_arg = ret.stdout.strip() #recover libvirtd service start if libvirtd == "off": libvirt_vm.libvirtd_start() #clean up if os.path.exists(file_xml): os.remove(file_xml) #check status_error if status_error == "yes": if status == 0: raise error.TestFail("Run successfully with wrong command!") elif status_error == "no": if status != 0: raise error.TestFail("Run failed with right command") if compare(conv_arg) != True: raise error.TestFail("Test failed!")
def run_virsh_domjobinfo(test, params, env): """ Test command: virsh domjobinfo. The command returns information about jobs running on a domain. 1.Prepare test environment. 2.When the libvirtd == "off", stop the libvirtd service. 3.Perform virsh domjobinfo operation. 4.Recover test environment. 5.Confirm the test result. """ vm_name = params.get("main_vm") vm = env.get_vm(vm_name) domid = vm.get_id() domuuid = vm.get_uuid() pre_vm_state = params.get("domjobinfo_pre_vm_state", "null") vm_ref = params.get("domjobinfo_vm_ref") status_error = params.get("status_error", "no") libvirtd = params.get("libvirtd", "on") tmp_file = os.path.join(test.tmpdir, '%s.tmp' % vm_name ) #prepare the state of vm if pre_vm_state == "dump": virsh.dump(vm_name, tmp_file) elif pre_vm_state == "save": virsh.save(vm_name, tmp_file) elif pre_vm_state == "restore": virsh.save(vm_name, tmp_file) virsh.restore(tmp_file) elif pre_vm_state == "managedsave": virsh.managedsave(vm_name) #run test case if vm_ref == "id": vm_ref = domid elif vm_ref == "hex_id": vm_ref = hex(int(domid)) elif vm_ref == "name": vm_ref = "%s %s" % (vm_name, params.get("domjobinfo_extra")) elif vm_ref == "uuid": vm_ref = domuuid elif vm_ref.find("invalid") != -1: vm_ref = params.get(vm_ref) if libvirtd == "off": libvirt_vm.libvirtd_stop() status = virsh.domjobinfo(vm_ref, ignore_status=True).exit_status #recover libvirtd service start if libvirtd == "off": libvirt_vm.libvirtd_start() #check status_error if status_error == "yes": if status == 0: raise error.TestFail("Run successfully with wrong command!") elif status_error == "no": if status != 0: raise error.TestFail("Run failed with right command")
def run_virsh_restore(test, params, env): """ Test command: virsh restore. Restore a domain from a saved state in a file 1.Prepare test environment. 2.When the libvirtd == "off", stop the libvirtd service. 3.Run virsh restore command with assigned option. 4.Recover test environment. 5.Confirm the test result. """ vm_name = params.get("main_vm", "vm1") vm = env.get_vm(params["main_vm"]) session = vm.wait_for_login() os_type = params.get("os_type") status_error = params.get("restore_status_error") libvirtd = params.get("restore_libvirtd") extra_param = params.get("restore_extra_param") pre_status = params.get("restore_pre_status") vm_ref = params.get("restore_vm_ref") #run test if vm_ref == "" or vm_ref == "xyz": status = virsh.restore(vm_ref, ignore_status=True).exit_status else: if os_type == "linux": cmd = "cat /proc/cpuinfo" status, output = session.cmd_status_output(cmd, internal_timeout=10) session.close() if not re.search("processor", output): raise error.TestFail("Unable to read /proc/cpuinfo") tmp_file = vm_ref if vm_ref == "/tmp/save.file": virsh.save(vm_name, vm_ref) vm_ref = "%s %s" % (vm_ref, extra_param) elif vm_ref == "/tmp/new.file": open(vm_ref, 'w').close() if vm.is_alive(): vm.destroy() if pre_status == "start": virsh.start(vm_name) if libvirtd == "off": libvirt_vm.libvirtd_stop() status = virsh.restore(vm_ref, ignore_status=True).exit_status os.unlink(tmp_file) if status_error == "no": list_output = virsh.dom_list().stdout.strip() session.close() #recover libvirtd service start if libvirtd == "off": libvirt_vm.libvirtd_start() if vm.is_alive(): vm.destroy() if status_error == "yes": if status == 0: raise error.TestFail("Run successfully with wrong command!") elif status_error == "no": if status != 0: raise error.TestFail("Run failed with right command") else: if not re.search(vm_name, list_output): raise error.TestFail("Run failed with right command")
def run_virsh_domblkstat(test, params, env): """ Test command: virsh domblkstat. The command get device block stats for a running domain. 1.Prepare test environment. 2.When the libvirtd == "off", stop the libvirtd service. 3.Perform virsh domblkstat operation. 4.Recover test environment. 5.Confirm the test result. """ vm_name = params.get("main_vm") vm = env.get_vm(vm_name) domid = vm.get_id() domuuid = vm.get_uuid() blklist = libvirt_xml.VMXML.get_disk_blk(vm_name) if blklist == None: raise error.TestFail("Cannot find disk in %s" % vm_name) #Select a block device from disks blk = blklist[0] libvirtd = params.get("libvirtd", "on") vm_ref = params.get("domblkstat_vm_ref") options = params.get("domblkstat_option", "") status_error = params.get("status_error", "no") if params.get("domblkinfo_dev") == "no": blk = "" if vm_ref == "id": vm_ref = domid elif vm_ref == "uuid": vm_ref = domuuid elif vm_ref == "hex_id": vm_ref = hex(int(domid)) elif vm_ref.find("invalid") != -1: vm_ref = params.get(vm_ref) elif vm_ref == "name": vm_ref = "%s %s" % (vm_name, params.get("domblkstat_extra")) option_list = options.split(" ") for option in option_list: if virsh.has_command_help_match("domblkstat", option) == None: status_error = "yes" break if libvirtd == "off": libvirt_vm.libvirtd_stop() result = virsh.domblkstat(vm_ref, blk, options, ignore_status=True) status = result.exit_status output = result.stdout.strip() err = result.stderr.strip() #recover libvirtd service start if libvirtd == "off": libvirt_vm.libvirtd_start() #check status_error if status_error == "yes": if status == 0 or err == "": raise error.TestFail("Run successfully with wrong command!") elif status_error == "no": if status != 0 or output == "": raise error.TestFail("Run failed with right command")
def run_virsh_managedsave(test, params, env): """ Test command: virsh managedsave. This command can save and destroy a running domain, so it can be restarted from the same state at a later time. """ vm_name = params.get("main_vm") vm = env.get_vm(params["main_vm"]) #define function def vm_recover_check(guest_name): """ Check if the vm can be recovered correctly. @param: guest_name : Checked vm's name. """ ret = virsh.dom_list() #This time vm should not be in the list if re.search(guest_name, ret.stdout): raise error.TestFail("virsh list output invalid") virsh.start(guest_name) if params.get("paused_after_start_vm") == "yes": virsh.resume(guest_name) #This time vm should be in the list ret = virsh.dom_list() if not re.search(guest_name, ret.stdout): raise error.TestFail("virsh list output invalid") domid = vm.get_id() domuuid = vm.get_uuid() libvirtd = params.get("managedsave_libvirtd","on") #run test case vm_ref = params.get("managedsave_vm_ref") if vm_ref == "id": vm_ref = domid elif vm_ref == "uuid": vm_ref = domuuid elif vm_ref == "hex_id": vm_ref = hex(int(domid)) elif vm_ref == "managedsave_invalid_id" or\ vm_ref == "managedsave_invalid_uuid": vm_ref = params.get(vm_ref) elif vm_ref == "name" or vm_ref == "extra_parame": vm_ref = "%s %s" % (vm_name, params.get("managedsave_extra_parame")) #stop the libvirtd service if libvirtd == "off": libvirt_vm.libvirtd_stop() #Ignore exception with "ignore_status=True" ret = virsh.managedsave(vm_ref, ignore_status=True) status = ret.exit_status #recover libvirtd service start if libvirtd == "off": libvirt_vm.libvirtd_start() #check status_error status_error = params.get("status_error") if status_error == "yes": if status == 0: if not virsh.has_command_help_match('managedsave', r'\s+--running\s+'): # Older libvirt does not have --running parameter raise error.TestNAError("Older libvirt does not handle arguments consistently") else: raise error.TestFail("Run successfully with wrong command!") elif status_error == "no": if status != 0: raise error.TestFail("Run failed with right command") vm_recover_check(vm_name)
def run_virsh_save(test, params, env): """ Test command: virsh save. The command can save the RAM state of a running domain. 1.Prepare test environment. 2.When the libvirtd == "off", stop the libvirtd service. 3.Run virsh save command with assigned options. 4.Recover test environment.(If the libvirtd service is stopped ,start the libvirtd service.) 5.Confirm the test result. """ vm_name = params.get("main_vm") vm = env.get_vm(params["main_vm"]) vm.verify_alive() domid = vm.get_id().strip() domuuid = vm.get_uuid().strip() savefile = params.get("save_file", "save.file") # If savefile is not an abs path, join it to test.tmpdir if os.path.dirname(savefile) is "": savefile = os.path.join(test.tmpdir, savefile) pre_vm_state = params.get("save_pre_vm_state", "null") libvirtd = params.get("save_libvirtd") extra_param = params.get("save_extra_param") vm_ref = params.get("save_vm_ref") # prepare the environment if vm_ref == "name" and pre_vm_state == "paused": virsh.suspend(vm_name) elif vm_ref == "name" and pre_vm_state == "shut off": virsh.destroy(vm_name) # set the option if vm_ref == "id": vm_ref = domid elif vm_ref == "hex_id": vm_ref = hex(int(domid)) elif vm_ref == "uuid": vm_ref = domuuid elif vm_ref == "save_invalid_id" or vm_ref == "save_invalid_uuid": vm_ref = params.get(vm_ref) elif vm_ref.find("name") != -1 or vm_ref == "extra_param": savefile = "%s %s" % (savefile, extra_param) if vm_ref == "only_name": savefile = " " vm_ref = vm_name if libvirtd == "off": libvirt_vm.libvirtd_stop() status = virsh.save(vm_ref, savefile, ignore_status=True).exit_status # recover libvirtd service start if libvirtd == "off": libvirt_vm.libvirtd_start() # cleanup if os.path.exists(savefile): virsh.restore(savefile) os.remove(savefile) # check status_error status_error = params.get("save_status_error") if status_error == "yes": if status == 0: raise error.TestFail("Run successfully with wrong command!") elif status_error == "no": if status != 0: raise error.TestFail("Run failed with right command")
def run_virsh_managedsave(test, params, env): """ Test command: virsh managedsave. This command can save and destroy a running domain, so it can be restarted from the same state at a later time. """ vm_name = params.get("main_vm", "vm1") vm = env.get_vm(params["main_vm"]) #define function def vm_recover_check(guest_name): """ Check if the vm can be recovered correctly. @param: guest_name : Checked vm's name. """ ret = virsh.dom_list() #This time vm should not be in the list if re.search(guest_name, ret.stdout): raise error.TestFail("virsh list output invalid") virsh.start(guest_name) if params.get("paused_after_start_vm") == "yes": virsh.resume(guest_name) #This time vm should be in the list ret = virsh.dom_list() if not re.search(guest_name, ret.stdout): raise error.TestFail("virsh list output invalid") domid = virsh.domid(vm_name).strip() domuuid = virsh.domuuid(vm_name).strip() libvirtd = params.get("managedsave_libvirtd", "on") #run test case vm_ref = params.get("managedsave_vm_ref") if vm_ref == "id": vm_ref = domid elif vm_ref == "uuid": vm_ref = domuuid elif vm_ref == "hex_id": vm_ref = hex(int(domid)) elif vm_ref == "managedsave_invalid_id" or\ vm_ref == "managedsave_invalid_uuid": vm_ref = params.get(vm_ref) elif vm_ref == "name" or vm_ref == "extra_parame": vm_ref = "%s %s" % (vm_name, params.get("managedsave_extra_parame")) #stop the libvirtd service if libvirtd == "off": libvirt_vm.libvirtd_stop() #Ignore exception with "ignore_status=True" ret = virsh.managedsave(vm_ref, ignore_status=True) status = ret.exit_status #recover libvirtd service start if libvirtd == "off": libvirt_vm.libvirtd_start() #check status_error status_error = params.get("status_error") if status_error == "yes": if status == 0: if not virsh.has_command_help_match('managedsave', r'\s+--running\s+'): # Older libvirt does not have --running parameter raise error.TestNAError( "Older libvirt does not handle arguments consistently") else: raise error.TestFail("Run successfully with wrong command!") elif status_error == "no": if status != 0: raise error.TestFail("Run failed with right command") vm_recover_check(vm_name)
def run_virsh_vcpuinfo(test, params, env): """ Test command: virsh vcpuinfo. The command can get domain vcpu information 1.Prepare test environment. 2.When the ibvirtd == "off", stop the libvirtd service. 3.Perform virsh vcpuinfo operation. 4.Recover test environment. 5.Confirm the test result. """ vm_name = params.get("main_vm") vm = env.get_vm(vm_name) status_error = params.get("status_error", "no") libvirtd = params.get("libvirtd", "on") if libvirtd == "off": libvirt_vm.libvirtd_stop() #run test case vm_ref = params.get("vcpuinfo_vm_ref") domid = vm.get_id() domuuid = vm.get_uuid() def remote_case(params, vm_name): """ Test remote case. """ remote_ip = params.get("remote_ip", "REMOTE.EXAMPLE.COM") remote_pwd = params.get("remote_pwd", None) # Verify vm.connect_uri was modified from the default for this test. if not virsh.VirshConnectBack.kosher_args(remote_ip, vm.connect_uri): raise error.TestNAError("The connect_uri parameter '%s' does " "not point at fully-qualified host " "from perspective of remote support " "system at '%s'." % (vm.connect_uri, remote_ip)) status = 0 output = "" err = "" try: vcback = virsh.VirshConnectBack(remote_ip=remote_ip, remote_pwd=remote_pwd, uri=vm.connect_uri, debug=True, ignore_status=True) cmdresult = vcback.vcpuinfo(vm_name) status = cmdresult.exit_status output = cmdresult.stdout vcback.close_session() if status != 0: err = output except error.CmdError: status = 1 output = "" err = "remote test failed" # Maintain result format conformance with local test return status, output, err if vm_ref == "id": vm_ref = domid elif vm_ref == "hex_id": vm_ref = hex(int(domid)) elif vm_ref.find("invalid") != -1: vm_ref = params.get(vm_ref) elif vm_ref == "uuid": vm_ref = domuuid elif vm_ref == "name": vm_ref = "%s %s" % (vm_name, params.get("vcpuinfo_extra")) if vm_ref == "remote": # Keep status_error check conditions (below) simple status, output, err = remote_case(params, vm_name) else: result = virsh.vcpuinfo(vm_ref) status = result.exit_status output = result.stdout.strip() err = result.stderr.strip() #recover libvirtd service start if libvirtd == "off": libvirt_vm.libvirtd_start() #check status_error if status_error == "yes": if status == 0 or err == "": raise error.TestFail("Run successfully with wrong command!") elif status_error == "no": if status != 0 or output == "": raise error.TestFail("Run failed with right command")
def run_virsh_vncdisplay(test, params, env): """ Test command: virsh vncdisplay. The command can output the IP address and port number for the VNC display. 1.Prepare test environment. 2.When the ibvirtd == "off", stop the libvirtd service. 3.Perform virsh vncdisplay operation. 4.Recover test environment. 5.Confirm the test result. """ vm_name = params.get("main_vm", "vm1") vm = env.get_vm(vm_name) libvirtd = params.get("libvirtd", "on") vm_ref = params.get("vncdisplay_vm_ref") status_error = params.get("status_error", "no") extra = params.get("vncdisplay_extra", "") domid = vm.get_id() domuuid = vm.get_uuid() def remote_case(params, vm_name): """ Test remote case. """ remote_ip = params.get("remote_ip", "REMOTE.EXAMPLE.COM") local_ip = params.get("local_ip", "LOCAL.EXAMPLE.COM") remote_pwd = params.get("remote_pwd", "") status = 0 output = "" try: if remote_ip.count("EXAMPLE.COM") or local_ip.count("EXAMPLE.COM"): raise error.TestNAError("remote_ip and/or local_ip parameters " "not changed from default values.") uri = libvirt_vm.complete_uri(local_ip) session = remote.remote_login("ssh", remote_ip, "22", "root", remote_pwd, "#") session.cmd_output('LANG=C') command = "virsh -c %s vncdisplay %s" % (uri, vm_name) status, output = session.cmd_status_output(command, internal_timeout=5) session.close() except error.CmdError: status = 1 output = "remote test failed" return status, output if vm_ref == "id": vm_ref = domid elif vm_ref == "hex_id": vm_ref = hex(int(domid)) elif vm_ref.find("invalid") != -1: vm_ref = params.get(vm_ref) elif vm_ref == "name": vm_ref = "%s %s" % (vm_name, extra) elif vm_ref == "uuid": vm_ref = domuuid if libvirtd == "off": libvirt_vm.libvirtd_stop() if vm_ref == "remote": status, output = remote_case(params, vm_name) else: result = virsh.vncdisplay(vm_ref, ignore_status=True) status = result.exit_status output = result.stdout.strip() if libvirtd == "off": libvirt_vm.libvirtd_start() #check status_error if status_error == "yes": if status == 0: raise error.TestFail("Run successfully with wrong command!") elif status_error == "no": if status != 0 or output == "": raise error.TestFail("Run failed with right command")
def run_virsh_destroy(test, params, env): """ Test command: virsh destroy. The command can destroy (stop) a domain. 1.Prepare test environment. 2.When the ibvirtd == "off", stop the libvirtd service. 3.Perform virsh destroy operation. 4.Recover test environment. 5.Confirm the test result. """ vm_name = params.get("main_vm") vm = env.get_vm(vm_name) domid = vm.get_id() domuuid = vm.get_uuid() vm_ref = params.get("destroy_vm_ref") status_error = params.get("status_error", "no") libvirtd = params.get("libvirtd", "on") 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") if vm_ref == "remote" and (remote_ip.count("EXAMPLE.COM") or local_ip.count("EXAMPLE.COM")): raise error.TestNAError("Remote test parameters unchanged from default") if vm_ref == "id": vm_ref = domid elif vm_ref == "hex_id": vm_ref = hex(int(domid)) elif vm_ref.find("invalid") != -1: vm_ref = params.get(vm_ref) elif vm_ref == "name": vm_ref = "%s %s" % (vm_name, params.get("destroy_extra")) elif vm_ref == "uuid": vm_ref = domuuid if libvirtd == "off": libvirt_vm.libvirtd_stop() if vm_ref != "remote": status = virsh.destroy(vm_ref, ignore_status=True).exit_status else: status = 0 try: remote_uri = libvirt_vm.complete_uri(local_ip) session = remote.remote_login("ssh", remote_ip, "22", "root", remote_pwd, "#") session.cmd_output('LANG=C') command = "virsh -c %s destroy %s" % (remote_uri, vm_name) status, output = session.cmd_status_output(command, internal_timeout=5) session.close() except error.CmdError: status = 1 if libvirtd == "off": libvirt_vm.libvirtd_start() #check status_error if status_error == "yes": if status == 0: raise error.TestFail("Run successfully with wrong command! " "Output:\n%s" % output) elif status_error == "no": if status != 0: raise error.TestFail("Run failed with right command! Output:\n%s" % output)
def run_virsh_change_media(test, params, env): """ Test command: virsh change-media. The command changes the media used by CD or floppy drives. Test steps: 1. Prepare test environment. 2. Perform virsh change-media operation. 3. Recover test environment. 4. Confirm the test result. """ @error.context_aware def env_pre(old_iso, new_iso): """ Prepare ISO image for test """ error.context("Preparing ISO images") 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)) @error.context_aware 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 ": error.context("Checking guest cdrom files") session.cmd("mount /dev/cdrom /media || mount /dev/cdrom /media") session.cmd("test -f /media/%s" % target_file) session.cmd("umount /dev/cdrom") else: error.context("Ejecting guest cdrom files") if session.cmd_status("mount /dev/cdrom /media -o loop") == 32: logging.info("Eject succeeded") def add_cdrom_device(vm_name, init_cdrom): """ Add cdrom device for test vm """ if vm.is_alive(): virsh.destroy(vm_name) virsh.attach_disk(vm_name, init_cdrom, " hdc", " --type cdrom --sourcetype file --config", debug=True) def update_cdrom(vm_name, init_iso, options, start_vm ): """ Update cdrom iso file for test case """ snippet = """ <disk type='file' device='cdrom'> <driver name='qemu' type='raw'/> <source file='%s'/> <target dev='hdc' bus='ide'/> <readonly/> </disk> """ % (init_iso) update_iso_file = open(update_iso_xml, "w") update_iso_file.write(snippet) update_iso_file.close() cmd_options = "--force " 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, debug=True) 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.tmpdir, "tmp") 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 = vm_xml.VMXML.get_disk_blk(vm_name) logging.info("disk_blk %s" % disk_blk) if "hdc" not in disk_blk: logging.info("Adding cdrom device") add_cdrom_device(vm_name, init_cdrom) if vm.is_alive() and start_vm == "no": logging.info("Destroying guest...") vm.destroy() elif vm.is_dead() and start_vm == "yes": logging.info("Starting guest...") vm.start() 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") # Libvirt will ignore --source when action is eject if action == "--eject ": source = "" else: source = cdrom_dir + source_name all_options = action + options + " " + source result = virsh.change_media(vm_ref, disk_device, all_options, ignore_status=True, debug=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) shutil.rmtree(cdrom_dir) # Check status_error # Negative testing if status_error == "yes": if status: logging.info("Expected error (negative testing). Output: %s", result.stderr.strip()) else: raise error.TestFail("Unexpected return code %d " "(negative testing)" % status) # Positive testing elif status_error == "no": if status: raise error.TestFail("Unexpected error (positive testing). " "Output: %s" % result.stderr.strip()) else: logging.info("Expected success. Output: %s", result.stdout.strip()) else: raise error.TestError("Invalid value for status_error '%s' " "(must be 'yes' or 'no')" % status_error)
def run_virsh_domstate(test, params, env): """ Test command: virsh domstate. 1.Prepare test environment. 2.When the libvirtd == "off", stop the libvirtd service. 3.Perform virsh domstate operation. 4.Recover test environment. 5.Confirm the test result. """ vm_name = params.get("main_vm") vm = env.get_vm(vm_name) libvirtd = params.get("libvirtd", "on") vm_ref = params.get("domstate_vm_ref") status_error = params.get("status_error", "no") domid = vm.get_id() domuuid = vm.get_uuid() if vm_ref == "id": vm_ref = domid elif vm_ref == "hex_id": vm_ref = hex(int(domid)) elif vm_ref.find("invalid") != -1: vm_ref = params.get(vm_ref) elif vm_ref == "name": vm_ref = "%s %s" % (vm_name, params.get("domstate_extra")) elif vm_ref == "uuid": vm_ref = domuuid if libvirtd == "off": libvirt_vm.libvirtd_stop() if vm_ref == "remote": remote_ip = params.get("remote_ip", "REMOTE.EXAMPLE.COM") local_ip = params.get("local_ip", "LOCAL.EXAMPLE.COM") remote_pwd = params.get("remote_pwd", None) if remote_ip.count("EXAMPLE.COM") or local_ip.count("EXAMPLE.COM"): raise error.TestNAError("Test 'remote' parameters not setup") status = 0 try: remote_uri = libvirt_vm.complete_uri(local_ip) session = remote.remote_login("ssh", remote_ip, "22", "root", remote_pwd, "#") session.cmd_output('LANG=C') command = "virsh -c %s domstate %s" % (remote_uri, vm_name) status, output = session.cmd_status_output(command, internal_timeout=5) session.close() except error.CmdError: status = 1 else: result = virsh.domstate(vm_ref, ignore_status=True) status = result.exit_status output = result.stdout #recover libvirtd service start if libvirtd == "off": libvirt_vm.libvirtd_start() #check status_error if status_error == "yes": if status == 0: raise error.TestFail("Run successfully with wrong command!") elif status_error == "no": if status != 0 or output == "": raise error.TestFail("Run failed with right command") if vm_ref == "remote": if not (re.match("running", output) or re.match("blocked", output) or re.match("idle", output)): raise error.TestFail("Run failed with right command")
def run_virsh_shutdown(test, params, env): """ Test command: virsh shutdown. The conmand can gracefully shutdown a domain. 1.Prepare test environment. 2.When the libvirtd == "off", stop the libvirtd service. 3.Perform virsh setvcpus operation. 4.Recover test environment. 5.Confirm the test result. """ vm_name = params.get("main_vm") vm = env.get_vm(vm_name) domid = vm.get_id() domuuid = vm.get_uuid() vm_ref = params.get("shutdown_vm_ref") libvirtd = params.get("libvirtd", "on") #run test case if vm_ref == "id": vm_ref = domid elif vm_ref == "hex_id": vm_ref = hex(int(domid)) elif vm_ref.find("invalid") != -1: vm_ref = params.get(vm_ref) elif vm_ref == "name": vm_ref = "%s %s" % (vm_name, params.get("shutdown_extra")) elif vm_ref == "uuid": vm_ref = domuuid if libvirtd == "off": libvirt_vm.libvirtd_stop() if vm_ref != "remote": status = virsh.shutdown(vm_ref, ignore_status=True).exit_status else: remote_ip = params.get("remote_ip", None) remote_pwd = params.get("remote_pwd", None) local_ip = params.get("local_ip", None) status = 0 try: remote_uri = libvirt_vm.complete_uri(local_ip) session = remote.remote_login("ssh", remote_ip, "22", "root", remote_pwd, "#") session.cmd_output('LANG=C') command = "virsh -c %s shutdown %s" % (remote_uri, vm_name) status = session.cmd_status(command, internal_timeout=5) session.close() except error.CmdError: status = 1 #recover libvirtd service start if libvirtd == "off": libvirt_vm.libvirtd_start() #check status_error status_error = params.get("status_error") if status_error == "yes": if status == 0: raise error.TestFail("Run successfully with wrong command!") elif status_error == "no": if status != 0: raise error.TestFail("Run failed with right command")
def run_virsh_save(test, params, env): """ Test command: virsh save. The command can save the RAM state of a running domain. 1.Prepare test environment. 2.When the libvirtd == "off", stop the libvirtd service. 3.Run virsh save command with assigned options. 4.Recover test environment.(If the libvirtd service is stopped ,start the libvirtd service.) 5.Confirm the test result. """ vm_name = params.get("main_vm", "vm1") vm = env.get_vm(params["main_vm"]) vm.verify_alive() domid = virsh.domid(vm_name).strip() domuuid = virsh.domuuid(vm_name).strip() savefile = params.get("save_file") pre_vm_state = params.get("save_pre_vm_state", "null") libvirtd = params.get("save_libvirtd") extra_param = params.get("save_extra_param") vm_ref = params.get("save_vm_ref") # prepare the environment if vm_ref == "name" and pre_vm_state == "paused": virsh.suspend(vm_name) elif vm_ref == "name" and pre_vm_state == "shut off": virsh.destroy(vm_name) # set the option if vm_ref == "id": vm_ref = domid elif vm_ref == "hex_id": vm_ref = hex(int(domid)) elif vm_ref == "uuid": vm_ref = domuuid elif vm_ref == "save_invalid_id" or vm_ref == "save_invalid_uuid": vm_ref = params.get(vm_ref) elif vm_ref.find("name") != -1 or vm_ref == "extra_param": savefile = "%s %s" % (savefile, extra_param) if vm_ref == "only_name": savefile = " " vm_ref = vm_name if libvirtd == "off": libvirt_vm.libvirtd_stop() status = virsh.save(vm_ref, savefile, ignore_status=True).exit_status # recover libvirtd service start if libvirtd == "off": libvirt_vm.libvirtd_start() # cleanup if os.path.exists(savefile): virsh.restore(savefile) os.remove(savefile) # check status_error status_error = params.get("save_status_error") if status_error == "yes": if status == 0: raise error.TestFail("Run successfully with wrong command!") elif status_error == "no": if status != 0: raise error.TestFail("Run failed with right command")
def run_virsh_domid(test, params, env): """ Test command: virsh domid. The command returns basic information about the domain. 1.Prepare test environment. 2.When the libvirtd == "off", stop the libvirtd service. 3.Perform virsh domid operation. 4.Recover test environment. 5.Confirm the test result. """ vm_name = params.get("main_vm", "vm1") vm = env.get_vm(vm_name) if vm.is_alive() and params.get("start_vm") == "no": vm.destroy() domid = vm.get_id() domuuid = vm.get_uuid() vm_ref = params.get("domid_vm_ref") extra = params.get("domid_extra", "") status_error = params.get("status_error", "no") libvirtd = params.get("libvirtd", "on") def remote_test(params, vm_name): """ Test remote case. """ remote_ip = params.get("remote_ip", "REMOTE.EXAMPLE.COM") local_ip = params.get("local_ip", "LOCAL.EXAMPLE.COM") remote_pwd = params.get("remote_pwd", "") status = 0 output = "" err = "" try: if remote_ip.count("EXAMPLE.COM") or local_ip.count("EXAMPLE.COM"): raise error.TestNAError("remote_ip and/or local_ip parameters " "not changed from default values.") uri = libvirt_vm.complete_uri(local_ip) session = remote.remote_login("ssh", remote_ip, "22", "root", remote_pwd, "#") session.cmd_output('LANG=C') command = "virsh -c %s domid %s" % (uri, vm_name) status, output = session.cmd_status_output(command, internal_timeout=5) if status != 0: err = output session.close() except error.CmdError: status = 1 output = "" err = "remote test failed" return status, output, err #run test case if vm_ref == "id": vm_ref = domid elif vm_ref.find("invalid") != -1: vm_ref = params.get(vm_ref) elif vm_ref == "name": vm_ref = "%s %s" % (vm_name, extra) elif vm_ref == "uuid": vm_ref = domuuid if libvirtd == "off": libvirt_vm.libvirtd_stop() if vm_ref != "remote": result = virsh.domid(vm_ref, ignore_status=True) status = result.exit_status output = result.stdout.strip() err = result.stderr.strip() else: status, output, err = remote_test(params, vm_name) #recover libvirtd service start if libvirtd == "off": libvirt_vm.libvirtd_start() #check status_error if status_error == "yes": if status == 0 or err == "": raise error.TestFail("Run successfully with wrong command!") elif status_error == "no": if status != 0 or output == "": raise error.TestFail("Run failed with right command")
def run_virt_edit(test, params, env): """ Test of virt-edit. 1) Get and init parameters for test. 2) Prepare environment. 3) Run virt-edit command and get result. 5) Recover environment. 6) Check result. """ vm_name = params.get("main_vm") vm = env.get_vm(vm_name) uri = libvirt_vm.normalize_connect_uri(params.get("connect_uri", "default")) start_vm = params.get("start_vm", "no") vm_ref = params.get("virt_edit_vm_ref", vm_name) file_ref = params.get("virt_edit_file_ref", "/etc/hosts") created_img = params.get("virt_edit_created_img", "/tmp/foo.img") foo_line = params.get("foo_line", "") options = params.get("virt_edit_options") options_suffix = params.get("virt_edit_options_suffix") status_error = params.get("status_error", "no") # virt-edit should not be used when vm is running. # (for normal test) if vm.is_alive() and start_vm == "no": vm.destroy(gracefully=True) dom_disk_dict = vm.get_disk_devices() # TODO dom_uuid = vm.get_uuid() if vm_ref == "domdisk": if len(dom_disk_dict) != 1: raise error.TestError("Only one disk device should exist on " "%s:\n%s." % (vm_name, dom_disk_dict)) disk_detail = dom_disk_dict.values()[0] vm_ref = disk_detail['source'] logging.info("disk to be edit:%s", vm_ref) elif vm_ref == "domname": vm_ref = vm_name elif vm_ref == "domuuid": vm_ref = dom_uuid elif vm_ref == "createdimg": vm_ref = created_img utils.run("dd if=/dev/zero of=%s bs=256M count=1" % created_img) # Decide whether pass a exprt for virt-edit command. if foo_line != "": expr = "s/$/%s/" % foo_line else: expr = "" # Stop libvirtd if test need. libvirtd = params.get("libvirtd", "on") if libvirtd == "off": libvirt_vm.libvirtd_stop() # Run test virsh_dargs = {'ignore_status': True, 'debug': True, 'uri': uri} result = lgf.virt_edit_cmd(vm_ref, file_ref, options, options_suffix, expr, **virsh_dargs) status = result.exit_status # Recover libvirtd. if libvirtd == "off": libvirt_vm.libvirtd_start() utils.run("rm -f %s" % created_img) status_error = (status_error == "yes") if status != 0: if not status_error: raise error.TestFail("Command executed failed.") else: if (expr != "" and (not login_to_check_foo_line(vm, file_ref, foo_line))): raise error.TestFail("Virt-edit to add %s in %s failed." "Test failed." % (foo_line, file_ref))
def run_virsh_undefine(test, params, env): """ Test virsh undefine command. Undefine an inactive domain, or convert persistent to transient. 1.Prepare test environment. 2.Backup the VM's infomation to a xml file. 3.When the libvirtd == "off", stop the libvirtd service. 4.Perform virsh undefine operation. 5.Recover test environment.(libvirts service,VM) 6.Confirm the test result. """ vm_ref = params.get("undefine_vm_ref", "vm_name") extra = params.get("undefine_extra", "") libvirtd_state = params.get("libvirtd", "on") status_error = params.get("status_error") undefine_twice = params.get("undefine_twice", 'no') local_ip = params.get("local_ip", "LOCAL.EXAMPLE.COM") remote_ip = params.get("remote_ip", "REMOTE.EXAMPLE.COM") xml_file = os.path.join(test.tmpdir, 'tmp.xml') remote_user = params.get("remote_user", "user") remote_password = params.get("remote_password", "password") remote_prompt = params.get("remote_prompt", "#") vm_name = params.get("main_vm") vm = env.get_vm(vm_name) vm_id = vm.get_id() vm_uuid = vm.get_uuid() # Back up xml file.Xen host has no guest xml file to define a guset. virsh.dumpxml(vm_name, xml_file) # Confirm how to reference a VM. if vm_ref == "vm_name": vm_ref = vm_name elif vm_ref == "id": vm_ref = vm_id elif vm_ref == "hex_vm_id": vm_ref = hex(int(vm_id)) elif vm_ref == "uuid": vm_ref = vm_uuid elif vm_ref.find("invalid") != -1: vm_ref = params.get(vm_ref) # Turn libvirtd into certain state. if libvirtd_state == "off": libvirt_vm.libvirtd_stop() # Test virsh undefine command. status = 0 try: uri = libvirt_vm.complete_uri(local_ip) except error.CmdError: status = 1 uri = None if vm_ref != "remote": vm_ref = "%s %s" % (vm_ref, extra) cmdresult = virsh.undefine(vm_ref, uri=uri, ignore_status=True, debug=True) status = cmdresult.exit_status if status: logging.debug("Error status, command output: %s", cmdresult.stdout) if undefine_twice == "yes": status2 = virsh.undefine(vm_ref, uri=uri, ignore_status=True).exit_status else: if remote_ip.count("EXAMPLE.COM") or local_ip.count("EXAMPLE.COM"): raise error.TestNAError("remote_ip and/or local_ip parameters not" " changed from default values") session = remote.remote_login("ssh", remote_ip, "22", remote_user, remote_password, remote_prompt) cmd_undefine = "virsh -c %s undefine %s" % (uri, vm_name) status, output = session.cmd_status_output(cmd_undefine) logging.info("Undefine output: %s", output) # Recover libvirtd state. if libvirtd_state == "off": libvirt_vm.libvirtd_start() # Shutdown VM. if virsh.domain_exists(vm.name, uri=uri): try: if vm.is_alive(): vm.destroy() except error.CmdError, detail: logging.error("Detail: %s", detail)
def run_virsh_destroy(test, params, env): """ Test command: virsh destroy. The command can destroy (stop) a domain. 1.Prepare test environment. 2.When the ibvirtd == "off", stop the libvirtd service. 3.Perform virsh destroy operation. 4.Recover test environment. 5.Confirm the test result. """ vm_name = params.get("main_vm") vm = env.get_vm(vm_name) domid = vm.get_id() domuuid = vm.get_uuid() vm_ref = params.get("destroy_vm_ref") status_error = params.get("status_error", "no") libvirtd = params.get("libvirtd", "on") 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") if vm_ref == "remote" and (remote_ip.count("EXAMPLE.COM") or local_ip.count("EXAMPLE.COM")): raise error.TestNAError( "Remote test parameters unchanged from default") if vm_ref == "id": vm_ref = domid elif vm_ref == "hex_id": vm_ref = hex(int(domid)) elif vm_ref.find("invalid") != -1: vm_ref = params.get(vm_ref) elif vm_ref == "name": vm_ref = "%s %s" % (vm_name, params.get("destroy_extra")) elif vm_ref == "uuid": vm_ref = domuuid if libvirtd == "off": libvirt_vm.libvirtd_stop() if vm_ref != "remote": status = virsh.destroy(vm_ref, ignore_status=True).exit_status else: status = 0 try: remote_uri = libvirt_vm.complete_uri(local_ip) session = remote.remote_login("ssh", remote_ip, "22", "root", remote_pwd, "#") session.cmd_output('LANG=C') command = "virsh -c %s destroy %s" % (remote_uri, vm_name) status, output = session.cmd_status_output(command, internal_timeout=5) session.close() except error.CmdError: status = 1 if libvirtd == "off": libvirt_vm.libvirtd_start() #check status_error if status_error == "yes": if status == 0: raise error.TestFail("Run successfully with wrong command! " "Output:\n%s" % output) elif status_error == "no": if status != 0: raise error.TestFail("Run failed with right command! Output:\n%s" % output)
def run_virsh_start(test, params, env): """ Test command: virsh start. 1) Get the params from params. 2) Prepare libvirtd's status. 3) Do the start operation. 4) Result check. 5) clean up. """ #get the params from params vm_name = params.get("vm_name", "vm1") backup_name = vm_name if vm_name is not "": vm = env.get_vm(vm_name) vmxml = libvirt_xml.VMXML() libvirtd_state = params.get("libvirtd", "on") pre_operation = params.get("vs_pre_operation", "") status_error = params.get("status_error", "no") #get the params for remote test remote_ip = params.get("remote_ip", "ENTER.YOUR.REMOTE.IP") remote_password = params.get("remote_password", "ENTER.YOUR.REMOTE.PASSWORD") local_ip = params.get("local_ip", "ENTER.YOUR.LOCAL.IP") if pre_operation == "remote" and (remote_ip.count("ENTER.YOUR.") or local_ip.count("ENTER.YOUR.")): raise error.TestNAError("Remote test parameters not configured") try: #prepare before start vm if libvirtd_state == "on": libvirt_vm.libvirtd_start() elif libvirtd_state == "off": libvirt_vm.libvirtd_stop() if pre_operation == "rename": new_vm_name = params.get("vs_new_vm_name", "virsh_start_vm1") vm = libvirt_xml.VMXML.vm_rename(vm, new_vm_name) vm_name = new_vm_name elif pre_operation == "undefine": vmxml = vmxml.new_from_dumpxml(vm_name) vmxml.undefine() #do the start operation try: if pre_operation == "remote": #get remote session session = remote.wait_for_login("ssh", remote_ip, "22", "root", remote_password, "#") #get uri of local uri = libvirt_vm.complete_uri(local_ip) cmd = "virsh -c %s start %s" % (uri, vm_name) status, output = session.cmd_status_output(cmd) if status: raise StartError(vm_name, output) else: do_virsh_start(vm_name) #start vm successfully if status_error == "yes": raise error.TestFail("Run successfully with wrong command!") except StartError, excpt: #start vm failed if status_error == "no": raise error.TestFail("Run failed with right command: %s", str(excpt)) finally: #clean up if libvirtd_state == "off": libvirt_vm.libvirtd_start() if (pre_operation == "undefine") and (not vmxml.xml == None): vmxml.define() elif pre_operation == "rename": libvirt_xml.VMXML.vm_rename(vm, backup_name)
def run_virsh_domxml_from_native(test, params, env): """ Test command: virsh domxml-from-native. Convert native guest configuration format to domain XML format. 1.Prepare test environment. 2.When the libvirtd == "off", stop the libvirtd service. 3.Perform virsh domxml-from-native operation. 4.Recover test environment.(If the libvirtd service is stopped ,start the libvirtd service.) 5.Confirm the test result. """ vm_name = params.get("main_vm") vm = env.get_vm(vm_name) vm.verify_alive() def virsh_convxml(guest_args): """ Put dumpxml vm'infomation to a file @param: guest_args : File which will save config infomation. """ pid = vm.get_pid() cmdline = utils.system_output("cat -v /proc/%d/cmdline" % pid) cmdline = re.sub(r'\^@', ' ', cmdline) cmdline_tmp = re.sub(r'\s-drive\s[^\s]+', '\s', cmdline) guest_file = file(guest_args,'w') guest_file.write(cmdline_tmp) guest_file.close() libvirtd = params.get("libvirtd") dfn_format = params.get("dfn_format") guest_args = params.get("dfn_guest_args") invalid_guest_args = params.get("dfn_invalid_guest_args") status_error = params.get("status_error") #put vm's infomation to a file if guest_args != "": virsh_convxml(guest_args) #libvirtd off if libvirtd == "off": libvirt_vm.libvirtd_stop() #Ignore exception with ignore_status=True. ret = virsh.domxml_from_native(dfn_format, guest_args, invalid_guest_args, ignore_status=True) status = ret.exit_status #recover libvirtd service start if libvirtd == "off": libvirt_vm.libvirtd_start() #clean up if os.path.exists(guest_args): os.remove(guest_args) #check status_error if status_error == "yes": if status == 0: raise error.TestFail("Run successfully with wrong command!") elif status_error == "no": if status != 0: raise error.TestFail("Run failed with right command")
def run_virsh_dommemstat(test, params, env): """ Test command: virsh dommemstat. The command gets memory statistics for a domain 1.Prepare test environment. 2.When the ibvirtd == "off", stop the libvirtd service. 3.Perform virsh dommemstat operation. 4.Recover test environment. 5.Confirm the test result. """ vm_name = params.get("main_vm") vm = env.get_vm(params["main_vm"]) domid = vm.get_id() domuuid = vm.get_uuid() status_error = params.get("status_error", "no") vm_ref = params.get("dommemstat_vm_ref", "name") libvirtd = params.get("libvirtd", "on") extra = params.get("dommemstat_extra", "") if libvirtd == "off": libvirt_vm.libvirtd_stop() #run test case if vm_ref == "id": vm_ref = domid elif vm_ref == "hex_id": vm_ref = hex(int(domid)) elif vm_ref == "uuid": vm_ref = domuuid elif vm_ref.count("_invalid_"): # vm_ref names parameter to fetch vm_ref = params.get(vm_ref) elif vm_ref == "name": vm_ref = "%s" % vm_name if libvirtd == "off": libvirt_vm.libvirtd_stop() if vm_ref != "remote": status = virsh.dommemstat(vm_ref, extra, ignore_status=True, debug=True).exit_status else: 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") if remote_ip.count("EXAMPLE.COM") or local_ip.count("EXAMPLE.COM"): raise error.TestNAError("local/remote ip parameters not set.") status = 0 try: remote_uri = libvirt_vm.complete_uri(local_ip) session = remote.remote_login("ssh", remote_ip, "22", "root", remote_pwd, "#") session.cmd_output('LANG=C') command = "virsh -c %s dommemstat %s %s" % (remote_uri, vm_name, extra) status = session.cmd_status(command, internal_timeout=5) session.close() except error.CmdError: status = 1 #recover libvirtd service start if libvirtd == "off": libvirt_vm.libvirtd_start() #check status_error if status_error == "yes": if status == 0: raise error.TestFail("Run successfully with wrong command!") elif status_error == "no": if status != 0: raise error.TestFail("Run failed with right command")
def run_virsh_edit(test, params, env): """ Test command: virsh edit. The command can edit XML configuration for a domain 1.Prepare test environment,destroy or suspend a VM. 2.When the libvirtd == "off", stop the libvirtd service. 3.Perform virsh edit operation. 4.Recover test environment. 5.Confirm the test result. """ vm_name = params.get("main_vm") vm = env.get_vm(vm_name) domid = vm.get_id() domuuid = vm.get_uuid() libvirtd = params.get("libvirtd", "on") vm_ref = params.get("edit_vm_ref") status_error = params.get("status_error") def modify_vcpu(source, edit_cmd): """ Modify vm's cpu infomation. @param: source : virsh edit's option. @param: dic_mode : a edit commad line . @return: True if edit successed,False if edit failed. """ session = aexpect.ShellSession("sudo -s") try: session.sendline("export EDITOR=vi") session.sendline("virsh edit %s" % source) session.sendline(edit_cmd) session.send('\x1b') session.send('ZZ') # use sleep(1) to make sure the modify has been completed. time.sleep(1) session.close() return True except: return False def edit_vcpu(source, guest_name): """ Modify vm's cpu infomation by virsh edit command. @param: source : virsh edit's option. @param: guest_name : vm's name. @return: True if edit successed,False if edit failed. """ dic_mode = { "edit": ":%s /1<\/vcpu>/2<\/vcpu>", "recover": ":%s /2<\/vcpu>/1<\/vcpu>" } status = modify_vcpu(source, dic_mode["edit"]) if not status: return status if params.get("paused_after_start_vm") == "yes": virsh.resume(guest_name, ignore_status=True) virsh.destroy(guest_name) elif params.get("start_vm") == "yes": virsh.destroy(guest_name) vcpus = vm.dominfo()["CPU(s)"] #Recover cpuinfo status = modify_vcpu(source, dic_mode["recover"]) if status and vcpus != '2': return False return status #run test case xml_file = os.path.join(test.tmpdir, 'tmp.xml') virsh.dumpxml(vm_name, xml_file) if libvirtd == "off": libvirt_vm.libvirtd_stop() try: if vm_ref == "id": status = edit_vcpu(domid, vm_name) elif vm_ref == "uuid": status = edit_vcpu(domuuid, vm_name) elif vm_ref == "name" and status_error == "no": status = edit_vcpu(vm_name, vm_name) else: status = False if vm_ref.find("invalid") != -1: vm_ref = params.get(vm_ref) elif vm_ref == "name": vm_ref = "%s %s" % (vm_name, params.get("edit_extra_param")) edit_status = virsh.edit(vm_ref).exit_status if edit_status == 0: status = True except: status = False #recover libvirtd service start if libvirtd == "off": libvirt_vm.libvirtd_start() #Recover VM if vm.is_alive(): vm.destroy() virsh.undefine(vm_name) virsh.define(xml_file) #check status_error if status_error == "yes": if status: raise error.TestFail("Run successfully with wrong command!") elif status_error == "no": if not status: raise error.TestFail("Run failed with right command")
def run_virt_edit(test, params, env): """ Test of virt-edit. 1) Get and init parameters for test. 2) Prepare environment. 3) Run virt-edit command and get result. 5) Recover environment. 6) Check result. """ vm_name = params.get("main_vm", "vm1") vm = env.get_vm(vm_name) uri = libvirt_vm.normalize_connect_uri( params.get("connect_uri", "default")) start_vm = params.get("start_vm", "no") vm_ref = params.get("virt_edit_vm_ref", vm_name) file_ref = params.get("virt_edit_file_ref", "/etc/hosts") created_img = params.get("virt_edit_created_img", "/tmp/foo.img") foo_line = params.get("foo_line", "") options = params.get("virt_edit_options") options_suffix = params.get("virt_edit_options_suffix") status_error = params.get("status_error", "no") # virt-edit should not be used when vm is running. # (for normal test) if vm.is_alive() and start_vm == "no": vm.destroy(gracefully=True) dom_disk_dict = vm.get_disk_devices() # TODO dom_uuid = vm.get_uuid() if vm_ref == "domdisk": if len(dom_disk_dict) != 1: raise error.TestError("Only one disk device should exist on " "%s:\n%s." % (vm_name, dom_disk_dict)) disk_detail = dom_disk_dict.values()[0] vm_ref = disk_detail['source'] logging.info("disk to be edit:%s", vm_ref) elif vm_ref == "domname": vm_ref = vm_name elif vm_ref == "domuuid": vm_ref = dom_uuid elif vm_ref == "createdimg": vm_ref = created_img utils.run("dd if=/dev/zero of=%s bs=256M count=1" % created_img) # Decide whether pass a exprt for virt-edit command. if foo_line != "": expr = "s/$/%s/" % foo_line else: expr = "" # Stop libvirtd if test need. libvirtd = params.get("libvirtd", "on") if libvirtd == "off": libvirt_vm.libvirtd_stop() # Run test virsh_dargs = {'ignore_status': True, 'debug': True, 'uri': uri} result = lgf.virt_edit_cmd(vm_ref, file_ref, options, options_suffix, expr, **virsh_dargs) status = result.exit_status # Recover libvirtd. if libvirtd == "off": libvirt_vm.libvirtd_start() utils.run("rm -f %s" % created_img) status_error = (status_error == "yes") if status != 0: if not status_error: raise error.TestFail("Command executed failed.") else: if (expr != "" and (not login_to_check_foo_line(vm, file_ref, foo_line))): raise error.TestFail("Virt-edit to add %s in %s failed." "Test failed." % (foo_line, file_ref))
def run_virsh_edit(test, params, env): """ Test command: virsh edit. The command can edit XML configuration for a domain 1.Prepare test environment,destroy or suspend a VM. 2.When the libvirtd == "off", stop the libvirtd service. 3.Perform virsh edit operation. 4.Recover test environment. 5.Confirm the test result. """ vm_name = params.get("main_vm") vm = env.get_vm(vm_name) domid = vm.get_id() domuuid = vm.get_uuid() libvirtd = params.get("libvirtd", "on") vm_ref = params.get("edit_vm_ref") status_error = params.get("status_error") def modify_vcpu(source, edit_cmd): """ Modify vm's cpu infomation. @param: source : virsh edit's option. @param: dic_mode : a edit commad line . @return: True if edit successed,False if edit failed. """ session = aexpect.ShellSession("sudo -s") try: session.sendline("export EDITOR=vi") session.sendline("virsh edit %s" % source) session.sendline(edit_cmd) session.send("\x1b") session.send("ZZ") # use sleep(1) to make sure the modify has been completed. time.sleep(1) session.close() return True except: return False def edit_vcpu(source, guest_name): """ Modify vm's cpu infomation by virsh edit command. @param: source : virsh edit's option. @param: guest_name : vm's name. @return: True if edit successed,False if edit failed. """ dic_mode = {"edit": ":%s /1<\/vcpu>/2<\/vcpu>", "recover": ":%s /2<\/vcpu>/1<\/vcpu>"} status = modify_vcpu(source, dic_mode["edit"]) if not status: return status if params.get("paused_after_start_vm") == "yes": virsh.resume(guest_name, ignore_status=True) virsh.destroy(guest_name) elif params.get("start_vm") == "yes": virsh.destroy(guest_name) vcpus = vm.dominfo()["CPU(s)"] # Recover cpuinfo status = modify_vcpu(source, dic_mode["recover"]) if status and vcpus != "2": return False return status # run test case xml_file = os.path.join(test.tmpdir, "tmp.xml") virsh.dumpxml(vm_name, xml_file) if libvirtd == "off": libvirt_vm.libvirtd_stop() try: if vm_ref == "id": status = edit_vcpu(domid, vm_name) elif vm_ref == "uuid": status = edit_vcpu(domuuid, vm_name) elif vm_ref == "name" and status_error == "no": status = edit_vcpu(vm_name, vm_name) else: status = False if vm_ref.find("invalid") != -1: vm_ref = params.get(vm_ref) elif vm_ref == "name": vm_ref = "%s %s" % (vm_name, params.get("edit_extra_param")) edit_status = virsh.edit(vm_ref).exit_status if edit_status == 0: status = True except: status = False # recover libvirtd service start if libvirtd == "off": libvirt_vm.libvirtd_start() # Recover VM if vm.is_alive(): vm.destroy() virsh.undefine(vm_name) virsh.define(xml_file) # check status_error if status_error == "yes": if status: raise error.TestFail("Run successfully with wrong command!") elif status_error == "no": if not status: raise error.TestFail("Run failed with right command")
def run_virsh_shutdown(test, params, env): """ Test command: virsh shutdown. The conmand can gracefully shutdown a domain. 1.Prepare test environment. 2.When the libvirtd == "off", stop the libvirtd service. 3.Perform virsh setvcpus operation. 4.Recover test environment. 5.Confirm the test result. """ vm_name = params.get("main_vm", "vm1") vm = env.get_vm(vm_name) domid = vm.get_id() domuuid = vm.get_uuid() vm_ref = params.get("shutdown_vm_ref") libvirtd = params.get("libvirtd", "on") #run test case if vm_ref == "id": vm_ref = domid elif vm_ref == "hex_id": vm_ref = hex(int(domid)) elif vm_ref.find("invalid") != -1: vm_ref = params.get(vm_ref) elif vm_ref == "name": vm_ref = "%s %s" % (vm_name, params.get("shutdown_extra")) elif vm_ref == "uuid": vm_ref = domuuid if libvirtd == "off": libvirt_vm.libvirtd_stop() if vm_ref != "remote": status = virsh.shutdown(vm_ref, ignore_status = True).exit_status else: remote_ip = params.get("remote_ip", None) remote_pwd = params.get("remote_pwd", None) local_ip = params.get("local_ip", None) status = 0 try: remote_uri = libvirt_vm.complete_uri(local_ip) session = remote.remote_login("ssh", remote_ip, "22", "root", remote_pwd, "#") session.cmd_output('LANG=C') command = "virsh -c %s shutdown %s" % (remote_uri, vm_name) status = session.cmd_status(command, internal_timeout=5) session.close() except error.CmdError: status = 1 #recover libvirtd service start if libvirtd == "off": libvirt_vm.libvirtd_start() #check status_error status_error = params.get("status_error") if status_error == "yes": if status == 0: raise error.TestFail("Run successfully with wrong command!") elif status_error == "no": if status != 0: raise error.TestFail("Run failed with right command")
def run_virsh_vcpuinfo(test, params, env): """ Test command: virsh vcpuinfo. The command can get domain vcpu information 1.Prepare test environment. 2.When the ibvirtd == "off", stop the libvirtd service. 3.Perform virsh vcpuinfo operation. 4.Recover test environment. 5.Confirm the test result. """ vm_name = params.get("main_vm") vm = env.get_vm(vm_name) status_error = params.get("status_error", "no") libvirtd = params.get("libvirtd", "on") if libvirtd == "off": libvirt_vm.libvirtd_stop() #run test case vm_ref = params.get("vcpuinfo_vm_ref") domid = vm.get_id() domuuid = vm.get_uuid() def remote_case(params, vm_name): """ Test remote case. """ 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") if remote_ip.count("EXAMPLE.COM") or local_ip.count("EXAMPLE.COM"): raise error.TestNAError("Remote test defaults not changed") status = 0 output = "" err = "" try: remote_uri = libvirt_vm.complete_uri(local_ip) session = remote.remote_login("ssh", remote_ip, "22", "root", remote_pwd, "#") session.cmd_output('LANG=C') command = "virsh -c %s vcpuinfo %s" % (remote_uri, vm_name) status, output = session.cmd_status_output(command, internal_timeout=5) session.close() if status != 0: err = output except error.CmdError: status = 1 output = "" err = "remote test failed" return status, output, err if vm_ref == "id": vm_ref = domid elif vm_ref == "hex_id": vm_ref = hex(int(domid)) elif vm_ref.find("invalid") != -1: vm_ref = params.get(vm_ref) elif vm_ref == "uuid": vm_ref = domuuid elif vm_ref == "name": vm_ref = "%s %s" % (vm_name, params.get("vcpuinfo_extra")) if vm_ref == "remote": status, output, err = remote_case(params, vm_name) else: result = virsh.vcpuinfo(vm_ref) status = result.exit_status output = result.stdout.strip() err = result.stderr.strip() #recover libvirtd service start if libvirtd == "off": libvirt_vm.libvirtd_start() #check status_error if status_error == "yes": if status == 0 or err == "": raise error.TestFail("Run successfully with wrong command!") elif status_error == "no": if status != 0 or output == "": raise error.TestFail("Run failed with right command")
def run_virsh_undefine(test, params, env): """ Test virsh undefine command. Undefine an inactive domain, or convert persistent to transient. 1.Prepare test environment. 2.Backup the VM's infomation to a xml file. 3.When the libvirtd == "off", stop the libvirtd service. 4.Perform virsh undefine operation. 5.Recover test environment.(libvirts service,VM) 6.Confirm the test result. """ vm_ref = params.get("undefine_vm_ref", "vm_name") extra = params.get("undefine_extra", "") libvirtd_state = params.get("libvirtd", "on") status_error = params.get("status_error") undefine_twice = params.get("undefine_twice", 'no') local_ip = params.get("local_ip", "LOCAL.EXAMPLE.COM") remote_ip = params.get("remote_ip", "REMOTE.EXAMPLE.COM") xml_file = os.path.join(test.tmpdir, 'tmp.xml') remote_user = params.get("remote_user", "user") remote_password = params.get("remote_password", "password") remote_prompt = params.get("remote_prompt", "#") vm_name = params.get("main_vm", "vm1") vm = env.get_vm(vm_name) vm_id = vm.get_id() vm_uuid = vm.get_uuid() # Back up xml file.Xen host has no guest xml file to define a guset. virsh.dumpxml(vm_name, xml_file) # Confirm how to reference a VM. if vm_ref == "vm_name": vm_ref = vm_name elif vm_ref == "id": vm_ref = vm_id elif vm_ref == "hex_vm_id": vm_ref = hex(int(vm_id)) elif vm_ref == "uuid": vm_ref = vm_uuid elif vm_ref.find("invalid") != -1: vm_ref = params.get(vm_ref) # Turn libvirtd into certain state. if libvirtd_state == "off": libvirt_vm.libvirtd_stop() # Test virsh undefine command. status = 0 try: uri = libvirt_vm.complete_uri(local_ip) except error.CmdError: status = 1 uri = None if vm_ref != "remote": vm_ref = "%s %s"% (vm_ref, extra) cmdresult = virsh.undefine(vm_ref, uri=uri, ignore_status=True, debug=True) status = cmdresult.exit_status if status: logging.debug("Error status, command output: %s", cmdresult.stdout) if undefine_twice == "yes": status2 = virsh.undefine(vm_ref, uri=uri, ignore_status=True).exit_status else: if remote_ip.count("EXAMPLE.COM") or local_ip.count("EXAMPLE.COM"): raise error.TestNAError("remote_ip and/or local_ip parameters not" " changed from default values") session = remote.remote_login("ssh", remote_ip, "22", remote_user, remote_password, remote_prompt) cmd_undefine = "virsh -c %s undefine %s" % (uri, vm_name) status, output = session.cmd_status_output(cmd_undefine) logging.info("Undefine output: %s", output) # Recover libvirtd state. if libvirtd_state == "off": libvirt_vm.libvirtd_start() # Shutdown VM. if virsh.domain_exists(vm.name, uri=uri): try: if vm.is_alive(): vm.destroy() except error.CmdError, detail: logging.error("Detail: %s", detail)
def run_virsh_domxml_to_native(test, params, env): """ Test command: virsh domxml-to-native. Convert domain XML config to a native guest configuration format. 1.Prepare test environment. 2.When the libvirtd == "off", stop the libvirtd service. 3.Perform virsh domxml-from-native operation. 4.Recover test environment. 5.Confirm the test result. """ vm_name = params.get("main_vm", "vm1") vm = env.get_vm(params["main_vm"]) vm.verify_alive() def compare(conv_arg): """ Compare converted infomation with vm's infomation. @param: conv_arg : Converted infomation. @return: True if converted infomation has no diffrent from vm's infomation. """ pid = vm.get_pid() cmdline_tmp = utils.system_output("cat -v /proc/%d/cmdline" % pid) cmdline = re.sub(r'\^@', ' ', cmdline_tmp) tmp = re.search('LC_ALL.[^\s]\s', conv_arg).group(0) +\ re.search('PATH.[^\s]+\s', conv_arg).group(0) +\ re.search('QEMU_AUDIO_DRV.[^\s]+\s', conv_arg).group(0) qemu_arg = tmp + cmdline conv_arg_lines = conv_arg.split('\x20') qemu_arg_lines = qemu_arg.split('\x20') i = 0 result = True for arg in conv_arg_lines: print arg print qemu_arg_lines[i] if re.search("mode=readline", arg): i += 1 continue elif re.search("mac=00:00:00:00:00:00", arg): i += 1 continue elif re.search("127.0.0.1:0", arg): i += 1 continue elif re.search("tap", arg): i += 1 continue if arg != qemu_arg_lines[i]: result = False i += 1 return result #run test case dtn_format = params.get("dtn_format") file_xml = params.get("dtn_file_xml") extra_param = params.get("dtn_extra_param") libvirtd = params.get("libvirtd") status_error = params.get("status_error") virsh.dumpxml(vm_name, file_xml) if libvirtd == "off": libvirt_vm.libvirtd_stop() ret = virsh.domxml_to_native(dtn_format, file_xml, extra_param, ignore_status=True) status = ret.exit_status conv_arg = ret.stdout.strip() #recover libvirtd service start if libvirtd == "off": libvirt_vm.libvirtd_start() #clean up if os.path.exists(file_xml): os.remove(file_xml) #check status_error if status_error == "yes": if status == 0: raise error.TestFail("Run successfully with wrong command!") elif status_error == "no": if status != 0: raise error.TestFail("Run failed with right command") if compare(conv_arg) != True: raise error.TestFail("Test failed!")
def run_virsh_domxml_from_native(test, params, env): """ Test command: virsh domxml-from-native. Convert native guest configuration format to domain XML format. 1.Prepare test environment. 2.When the libvirtd == "off", stop the libvirtd service. 3.Perform virsh domxml-from-native operation. 4.Recover test environment.(If the libvirtd service is stopped ,start the libvirtd service.) 5.Confirm the test result. """ vm_name = params.get("main_vm", "vm1") vm = env.get_vm(vm_name) vm.verify_alive() def virsh_convxml(guest_args): """ Put dumpxml vm'infomation to a file @param: guest_args : File which will save config infomation. """ pid = vm.get_pid() cmdline = utils.system_output("cat -v /proc/%d/cmdline" % pid) cmdline = re.sub(r'\^@', ' ', cmdline) cmdline_tmp = re.sub(r'\s-drive\s[^\s]+', '\s', cmdline) guest_file = file(guest_args,'w') guest_file.write(cmdline_tmp) guest_file.close() libvirtd = params.get("libvirtd") dfn_format = params.get("dfn_format") guest_args = params.get("dfn_guest_args") invalid_guest_args = params.get("dfn_invalid_guest_args") status_error = params.get("status_error") #put vm's infomation to a file if guest_args != "": virsh_convxml(guest_args) #libvirtd off if libvirtd == "off": libvirt_vm.libvirtd_stop() #Ignore exception with ignore_status=True. ret = virsh.domxml_from_native(dfn_format, guest_args, invalid_guest_args, ignore_status=True) status = ret.exit_status #recover libvirtd service start if libvirtd == "off": libvirt_vm.libvirtd_start() #clean up if os.path.exists(guest_args): os.remove(guest_args) #check status_error if status_error == "yes": if status == 0: raise error.TestFail("Run successfully with wrong command!") elif status_error == "no": if status != 0: raise error.TestFail("Run failed with right command")
def run_virsh_setvcpus(test, params, env): """ Test command: virsh setvcpus. The conmand can change the number of virtual CPUs in the guest domain. 1.Prepare test environment,destroy or suspend a VM. 2.When the ibvirtd == "off", stop the libvirtd service. 3.Perform virsh setvcpus operation. 4.Recover test environment. 5.Confirm the test result. """ vm_name = params.get("main_vm", "vm1") vm = env.get_vm(vm_name) xml_file = params.get("setvcpus_xml_file", "vm.xml") virsh.dumpxml(vm_name, xml_file) tmp_file = params.get("setvcpus_tmp_file", "tmp.xml") libvirtd = params.get("libvirtd") pre_vm_state = params.get("setvcpus_pre_vm_state") command = params.get("setvcpus_command", "setvcpus") options = params.get("setvcpus_options") domain = params.get("setvcpus_domain") count = params.get("setvcpus_count") extra_param = params.get("setvcpus_extra_param") count_option = "%s %s" % (count, extra_param) status_error = params.get("status_error") def get_current_vcpus(): """ Get current vcpu number. """ vcpus_set = "" virsh.dumpxml(vm_name, tmp_file) dom = parse(tmp_file) root = dom.documentElement vcpus_2 = root.getElementsByTagName("vcpu") for n in vcpus_2: vcpus_set += n.getAttribute("current") vcpus_set = int(vcpus_set) dom.unlink() return vcpus_set if vm.is_alive(): vm.destroy() vm_xml = libvirt_xml.VMXML() vm_xml.set_vm_vcpus(vm_name, 2) vm.start() vm.wait_for_login() if status_error == "no": vcpus_new = len(vm.vcpuinfo()) domid = vm.get_id() domuuid = vm.get_uuid() if pre_vm_state == "paused": vm.pause() elif pre_vm_state == "shut off": vm.destroy() if libvirtd == "off": libvirt_vm.libvirtd_stop() if domain == "remote_name": remote_ssh_addr = params.get("remote_ip", None) remote_addr = params.get("local_ip", None) remote_password = params.get("remote_password", None) host_type = virsh.driver() if host_type == "qemu": remote_string = "qemu+ssh://%s/system" % remote_addr elif host_type == "xen": remote_string = "xen+ssh://%s" % remote_addr command = "virsh -c %s setvcpus %s 1 --live" % (remote_string, vm_name) if virsh.has_command_help_match(command, "--live") == None: status_error = "yes" session = remote.remote_login("ssh", remote_ssh_addr, "22", "root", remote_password, "#") session.cmd_output('LANG=C') status, output = session.cmd_status_output(command, internal_timeout=5) session.close() vcpus_current = len(vm.vcpuinfo()) else: if domain == "name": dom_option = vm_name elif domain == "id": dom_option = domid if params.get("setvcpus_hex_id") != None: dom_option = hex(int(domid)) elif params.get("setvcpus_invalid_id") != None: dom_option = params.get("setvcpus_invalid_id") elif domain == "uuid": dom_option = domuuid if params.get("setvcpus_invalid_uuid") != None: dom_option = params.get("setvcpus_invalid_uuid") else: dom_option = domain option_list = options.split(" ") for item in option_list: if virsh.has_command_help_match(command, item) == None: status_error = "yes" break status = virsh.setvcpus(dom_option, count_option, options, ignore_status=True).exit_status if pre_vm_state == "paused": virsh.resume(vm_name) if status_error == "no": if status == 0: if pre_vm_state == "shut off": if options == "--config": vcpus_set = len(vm.vcpuinfo()) elif options == "--current": vcpus_set = get_current_vcpus() elif options == "--maximum --config": vcpus_set = "" dom = parse("/etc/libvirt/qemu/%s.xml" % vm_name) vcpus_set = dom.getElementsByTagName("vcpu")[0].firstChild.data vcpus_set = int(vcpus_set) dom.unlink() else: vcpus_set = len(vm.vcpuinfo()) if domain == "id": cmd_chk = "cat /etc/libvirt/qemu/%s.xml" % vm_name output1 = commands.getoutput(cmd_chk) logging.info("guest-info:\n%s" % output1) #recover libvirtd service start if libvirtd == "off": libvirt_vm.libvirtd_start() virsh.destroy(vm_name) virsh.undefine(vm_name) virsh.define(xml_file) if os.path.exists(xml_file): os.remove(xml_file) if os.path.exists(tmp_file): os.remove(tmp_file) #check status_error if status_error == "yes": if status == 0: raise error.TestFail("Run successfully with wrong command!") else: if status != 0: raise error.TestFail("Run failed with right command") else: if options == "--maximum --config": if vcpus_set != 4: raise error.TestFail("Run failed with right command1") elif domain == "id": if options == "--config": if vcpus_set != vcpus_new or not re.search('<vcpu current=\'1\'>%s</vcpu>' % vcpus_new, output1): raise error.TestFail("Run failed with right command2") elif options == "--config --live": if vcpus_set != 1 or not re.search('<vcpu current=\'1\'>%s</vcpu>' % vcpus_new, output1): raise error.TestFail("Run failed with right command3") else: if vcpus_set != 1 or re.search('<vcpu current=\'1\'>%s</vcpu>' % vcpus_new, output1): raise error.TestFail("Run failed with right command4") else: if vcpus_set != 1: raise error.TestFail("Run failed with right command5")
def run_virsh_domid(test, params, env): """ Test command: virsh domid. The command returns basic information about the domain. 1.Prepare test environment. 2.When the libvirtd == "off", stop the libvirtd service. 3.Perform virsh domid operation. 4.Recover test environment. 5.Confirm the test result. """ vm_name = params.get("main_vm", "vm1") vm = env.get_vm(vm_name) if vm.is_alive() and params.get("start_vm") == "no": vm.destroy() domid = vm.get_id() domuuid = vm.get_uuid() vm_ref = params.get("domid_vm_ref") extra = params.get("domid_extra", "") status_error = params.get("status_error", "no") libvirtd = params.get("libvirtd", "on") def remote_test(params, vm_name): """ Test remote case. """ remote_ip = params.get("remote_ip", "REMOTE.EXAMPLE.COM") local_ip = params.get("local_ip", "LOCAL.EXAMPLE.COM") remote_pwd = params.get("remote_pwd", "") status = 0 output = "" err = "" try: if remote_ip.count("EXAMPLE.COM") or local_ip.count("EXAMPLE.COM"): raise error.TestNAError("remote_ip and/or local_ip parameters " "not changed from default values.") uri = libvirt_vm.complete_uri(local_ip) session = remote.remote_login("ssh", remote_ip, "22", "root", remote_pwd, "#") session.cmd_output("LANG=C") command = "virsh -c %s domid %s" % (uri, vm_name) status, output = session.cmd_status_output(command, internal_timeout=5) if status != 0: err = output session.close() except error.CmdError: status = 1 output = "" err = "remote test failed" return status, output, err # run test case if vm_ref == "id": vm_ref = domid elif vm_ref.find("invalid") != -1: vm_ref = params.get(vm_ref) elif vm_ref == "name": vm_ref = "%s %s" % (vm_name, extra) elif vm_ref == "uuid": vm_ref = domuuid if libvirtd == "off": libvirt_vm.libvirtd_stop() if vm_ref != "remote": result = virsh.domid(vm_ref, ignore_status=True) status = result.exit_status output = result.stdout.strip() err = result.stderr.strip() else: status, output, err = remote_test(params, vm_name) # recover libvirtd service start if libvirtd == "off": libvirt_vm.libvirtd_start() # check status_error if status_error == "yes": if status == 0 or err == "": raise error.TestFail("Run successfully with wrong command!") elif status_error == "no": if status != 0 or output == "": raise error.TestFail("Run failed with right command")
def run_virsh_restore(test, params, env): """ Test command: virsh restore. Restore a domain from a saved state in a file 1.Prepare test environment. 2.When the libvirtd == "off", stop the libvirtd service. 3.Run virsh restore command with assigned option. 4.Recover test environment. 5.Confirm the test result. """ vm_name = params.get("main_vm") vm = env.get_vm(params["main_vm"]) session = vm.wait_for_login() os_type = params.get("os_type") status_error = params.get("restore_status_error") libvirtd = params.get("restore_libvirtd") extra_param = params.get("restore_extra_param") pre_status = params.get("restore_pre_status") vm_ref = params.get("restore_vm_ref") #run test if vm_ref =="" or vm_ref == "xyz": status = virsh.restore(vm_ref, ignore_status=True).exit_status else: if os_type == "linux": cmd = "cat /proc/cpuinfo" status, output = session.cmd_status_output(cmd, internal_timeout = 10) session.close() if not re.search("processor", output): raise error.TestFail("Unable to read /proc/cpuinfo") tmp_file = vm_ref if vm_ref == "/tmp/save.file": virsh.save(vm_name, vm_ref) vm_ref = "%s %s" % (vm_ref, extra_param) elif vm_ref == "/tmp/new.file": open(vm_ref, 'w').close() if vm.is_alive(): vm.destroy() if pre_status == "start": virsh.start(vm_name) if libvirtd == "off": libvirt_vm.libvirtd_stop() status = virsh.restore(vm_ref, ignore_status=True).exit_status os.unlink(tmp_file) if status_error == "no": list_output = virsh.dom_list().stdout.strip() session.close() #recover libvirtd service start if libvirtd == "off": libvirt_vm.libvirtd_start() if vm.is_alive(): vm.destroy() if status_error == "yes": if status == 0: raise error.TestFail("Run successfully with wrong command!") elif status_error == "no": if status != 0: raise error.TestFail("Run failed with right command") else: if not re.search(vm_name, list_output): raise error.TestFail("Run failed with right command")
def run_virsh_change_media(test, params, env): """ Test command: virsh change-media. The command changes the media used by CD or floppy drives. Test steps: 1. Prepare test environment. 2. Perform virsh change-media operation. 3. Recover test environment. 4. Confirm the test result. """ @error.context_aware def env_pre(old_iso, new_iso): """ Prepare ISO image for test """ error.context("Preparing ISO images") 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)) @error.context_aware 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 ": error.context("Checking guest cdrom files") session.cmd("mount /dev/cdrom /media || mount /dev/cdrom /media") session.cmd("test -f /media/%s" % target_file) session.cmd("umount /dev/cdrom") else: error.context("Ejecting guest cdrom files") if session.cmd_status("mount /dev/cdrom /media -o loop") == 32: logging.info("Eject succeeded") def add_cdrom_device(vm_name, init_cdrom): """ Add cdrom device for test vm """ if vm.is_alive(): virsh.destroy(vm_name) virsh.attach_disk(vm_name, init_cdrom, " hdc", " --type cdrom --sourcetype file --config", debug=True) def update_cdrom(vm_name, init_iso, options, start_vm): """ Update cdrom iso file for test case """ snippet = """ <disk type='file' device='cdrom'> <driver name='qemu' type='raw'/> <source file='%s'/> <target dev='hdc' bus='ide'/> <readonly/> </disk> """ % (init_iso) update_iso_file = open(update_iso_xml, "w") update_iso_file.write(snippet) update_iso_file.close() cmd_options = "--force " 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, debug=True) 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.tmpdir, "tmp") 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 = vm_xml.VMXML.get_disk_blk(vm_name) logging.info("disk_blk %s" % disk_blk) if "hdc" not in disk_blk: logging.info("Adding cdrom device") add_cdrom_device(vm_name, init_cdrom) if vm.is_alive() and start_vm == "no": logging.info("Destroying guest...") vm.destroy() elif vm.is_dead() and start_vm == "yes": logging.info("Starting guest...") vm.start() 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") # Libvirt will ignore --source when action is eject if action == "--eject ": source = "" else: source = cdrom_dir + source_name all_options = action + options + " " + source result = virsh.change_media(vm_ref, disk_device, all_options, ignore_status=True, debug=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) shutil.rmtree(cdrom_dir) # Check status_error # Negative testing if status_error == "yes": if status: logging.info("Expected error (negative testing). Output: %s", result.stderr.strip()) else: raise error.TestFail("Unexpected return code %d " "(negative testing)" % status) # Positive testing elif status_error == "no": if status: raise error.TestFail("Unexpected error (positive testing). " "Output: %s" % result.stderr.strip()) else: logging.info("Expected success. Output: %s", result.stdout.strip()) else: raise error.TestError("Invalid value for status_error '%s' " "(must be 'yes' or 'no')" % status_error)
logging.debug("Turning libvirtd into certain status.") if libvirtd_state == "off": libvirt_vm.libvirtd_stop() # Test uni-direction migration. logging.debug("Doing migration test.") if vm_ref != vm_name: vm.name = vm_ref # For vm name error testing. ret_migrate = do_migration(delay, vm, dest_uri, options, extra) if vm_ref != vm_name: vm.name = vm_name # Recover libvirtd state. logging.debug("Recovering libvirtd status.") if libvirtd_state == "off": libvirt_vm.libvirtd_start() # Check vm state on destination. logging.debug("Checking %s state on %s." % (vm.name, vm.connect_uri)) if options.count("dname") or extra.count("dname"): vm.name = extra.split()[1].strip() check_dest_state = True dest_state = params.get("virsh_migrate_dest_state", "running") check_dest_state = check_vm_state(vm, dest_state) logging.info("Supposed state: %s" % dest_state) logging.info("Actual state: %s" % vm.state()) # Recover VM state. logging.debug("Recovering %s state." % vm.name) if src_state == "paused": vm.resume()
def run_virsh_domjobinfo(test, params, env): """ Test command: virsh domjobinfo. The command returns information about jobs running on a domain. 1.Prepare test environment. 2.When the libvirtd == "off", stop the libvirtd service. 3.Perform virsh domjobinfo operation. 4.Recover test environment. 5.Confirm the test result. """ vm_name = params.get("main_vm") vm = env.get_vm(vm_name) domid = vm.get_id() domuuid = vm.get_uuid() pre_vm_state = params.get("domjobinfo_pre_vm_state", "null") vm_ref = params.get("domjobinfo_vm_ref") status_error = params.get("status_error", "no") libvirtd = params.get("libvirtd", "on") tmp_file = os.path.join(test.tmpdir, '%s.tmp' % vm_name) #prepare the state of vm if pre_vm_state == "dump": virsh.dump(vm_name, tmp_file) elif pre_vm_state == "save": virsh.save(vm_name, tmp_file) elif pre_vm_state == "restore": virsh.save(vm_name, tmp_file) virsh.restore(tmp_file) elif pre_vm_state == "managedsave": virsh.managedsave(vm_name) #run test case if vm_ref == "id": vm_ref = domid elif vm_ref == "hex_id": vm_ref = hex(int(domid)) elif vm_ref == "name": vm_ref = "%s %s" % (vm_name, params.get("domjobinfo_extra")) elif vm_ref == "uuid": vm_ref = domuuid elif vm_ref.find("invalid") != -1: vm_ref = params.get(vm_ref) if libvirtd == "off": libvirt_vm.libvirtd_stop() status = virsh.domjobinfo(vm_ref, ignore_status=True).exit_status #recover libvirtd service start if libvirtd == "off": libvirt_vm.libvirtd_start() #check status_error if status_error == "yes": if status == 0: raise error.TestFail("Run successfully with wrong command!") elif status_error == "no": if status != 0: raise error.TestFail("Run failed with right command")