def run_virsh_domname(test, params, env): """ Test command: virsh domname <id/uuid>. 1) Prepare libvirtd status and test environment. 2) Try to get domname through valid and invalid command. 3) Recover libvirtd service and test environment. 4) Check result. """ vm_name = params.get("main_vm", "vm1") vm = env.get_vm(params["main_vm"]) domid = vm.get_id() domuuid = vm.get_uuid() connect_uri = vm.connect_uri #run test case options_ref = params.get("options_ref", "id") addition_status_error = params.get("addition_status_error", "no") status_error = params.get("status_error", "no") options = params.get("options", "%s") options_suffix = params.get("options_suffix", "") if options_ref == "id": options_ref = domid if options_ref == "-": options = "%s" else: options_ref = int(domid) elif options_ref == "uuid": options_ref = domuuid # UUID can get domain name in any state. logging.warning("Reset addition_status_error to NO for uuid test!") addition_status_error = "no" elif options_ref == "name": options_ref = vm_name if options: options = (options % options_ref) if options_suffix: options = options + " " + options_suffix #Prepare libvirtd status libvirtd = params.get("libvirtd", "on") if libvirtd == "off": libvirt_vm.service_libvirtd_control("stop") result = virsh.domname(options, ignore_status=True, debug=True, uri=connect_uri) #Recover libvirtd service to start if libvirtd == "off": libvirt_vm.service_libvirtd_control("start") addition_status_error = "yes" #check status_error status_error = (status_error == "no") and (addition_status_error == "no") if status_error: if result.exit_status != 0 or result.stdout.strip() != vm_name: raise error.TestFail("Run failed because unexpected result.") else: if result.exit_status == 0 and result.stdout.strip() != vm_name: raise error.TestFail("Run passed but result is unexpected.")
def run(test, params, env): """ Test command: virsh domname <id/uuid>. 1) Prepare libvirtd status and test environment. 2) Try to get domname through valid and invalid command. 3) Recover libvirtd service and test environment. 4) Check result. """ vm_name = params.get("main_vm") vm = env.get_vm(params["main_vm"]) domid = vm.get_id() domuuid = vm.get_uuid() connect_uri = vm.connect_uri # run test case options_ref = params.get("domname_options_ref", "id") addition_status_error = params.get("addition_status_error", "no") status_error = params.get("status_error", "no") options = params.get("domname_options", "%s") options_suffix = params.get("domname_options_suffix", "") if options_ref == "id": options_ref = domid if options_ref == "-": options = "%s" else: options_ref = int(domid) elif options_ref == "uuid": options_ref = domuuid # UUID can get domain name in any state. logging.warning("Reset addition_status_error to NO for uuid test!") addition_status_error = "no" elif options_ref == "name": options_ref = vm_name if options: options = (options % options_ref) if options_suffix: options = options + " " + options_suffix # Prepare libvirtd status libvirtd = params.get("libvirtd", "on") if libvirtd == "off": utils_libvirtd.libvirtd_stop() result = virsh.domname(options, ignore_status=True, debug=True, uri=connect_uri) # Recover libvirtd service to start if libvirtd == "off": utils_libvirtd.libvirtd_start() addition_status_error = "yes" # check status_error status_error = (status_error == "no") and (addition_status_error == "no") if status_error: if result.exit_status != 0 or result.stdout.strip() != vm_name: test.fail("Run failed because unexpected result.") else: if result.exit_status == 0 and result.stdout.strip() != vm_name: test.fail("Run passed but result is unexpected.")
def run(test, params, env): """ Test command: virsh rename. The command can rename a domain. 1.Prepare test environment. 2.Perform virsh rename operation. 3.Recover test environment. 4.Confirm the test result. """ # Get specific parameter value vm_name = params.get("main_vm") vm = env.get_vm(vm_name) domuuid = vm.get_uuid() vm_ref = params.get("domrename_vm_ref", "name") status_error = "yes" == params.get("status_error", "no") new_name = params.get("vm_new_name", "new") pre_vm_state = params.get("domrename_vm_state", "shutoff") domain_option = params.get("dom_opt", "") new_name_option = params.get("newname_opt", "") add_vm = "yes" == params.get("add_vm", "no") # Replace the varaiables if vm_ref == "name": vm_ref = vm_name elif vm_ref == "uuid": vm_ref = domuuid if new_name == "vm2_name": vm2_name = ("%s" % vm_name[:-1])+"2" new_name = vm2_name # Build input params dom_param = ' '.join([domain_option, vm_ref]) new_name_param = ' '.join([new_name_option, new_name]) # Backup for recovery. vmxml_backup = libvirt_xml.vm_xml.VMXML.new_from_inactive_dumpxml(vm_name) logging.debug("vm xml is %s", vmxml_backup) # Clone additional vms if needed if add_vm: try: utils_path.find_command("virt-clone") except utils_path.CmdNotFoundError: if not utils_package.package_install(["virt-install"]): test.cancel("Failed to install virt-install on host") ret_clone = utils_libguestfs.virt_clone_cmd(vm_name, vm2_name, True, timeout=360) if ret_clone.exit_status: test.fail("Error occured when clone a second vm!") vm2 = libvirt_vm.VM(vm2_name, vm.params, vm.root_dir, vm.address_cache) virsh.dom_list("--name --all", debug=True) # Create object instance for renamed domain new_vm = libvirt_vm.VM(new_name, vm.params, vm.root_dir, vm.address_cache) # Prepare vm state if pre_vm_state != "shutoff": vm.start() if pre_vm_state == "paused": vm.pause() logging.debug("Domain state is now: %s", vm.state()) elif pre_vm_state == "managed_saved": vm.managedsave() elif pre_vm_state == "with_snapshot": virsh.snapshot_create_as(vm_name, "snap1 --disk-only", debug=True) vm.destroy(gracefully=False) try: result = virsh.domrename(dom_param, new_name_param, ignore_status=True, debug=True) # Raise unexpected pass or fail libvirt.check_exit_status(result, status_error) # Return expected failure for negative tests if status_error: logging.debug("Expected failure: %s", result.stderr) return # Checkpoints after domrename succeed else: list_ret = virsh.dom_list("--name --all", debug=True).stdout domname_ret = virsh.domname(domuuid, debug=True).stdout.strip() if new_name not in list_ret or vm_name in list_ret: test.fail("New name does not affect in virsh list") if domname_ret != new_name: test.fail("New domain name does not affect in virsh domname uuid") # Try to start vm with the new name new_vm.start() finally: # Remove additional vms if add_vm and vm2.exists() and result.exit_status: virsh.remove_domain(vm2_name, "--remove-all-storage") # Undefine newly renamed domain if new_vm.exists(): if new_vm.is_alive(): new_vm.destroy(gracefully=False) new_vm.undefine() # Recover domain state if pre_vm_state != "shutoff": if pre_vm_state == "with_snapshot": libvirt.clean_up_snapshots(vm_name) else: if pre_vm_state == "managed_saved": vm.start() vm.destroy(gracefully=False) # Restore VM vmxml_backup.sync()
def run(test, params, env): """ Test command: virsh rename. The command can rename a domain. 1.Prepare test environment. 2.Perform virsh rename operation. 3.Recover test environment. 4.Confirm the test result. """ # Get specific parameter value vm_name = params.get("main_vm") vm = env.get_vm(vm_name) domuuid = vm.get_uuid() vm_ref = params.get("domrename_vm_ref", "name") status_error = "yes" == params.get("status_error", "no") new_name = params.get("vm_new_name", "new") pre_vm_state = params.get("domrename_vm_state", "shutoff") domain_option = params.get("dom_opt", "") new_name_option = params.get("newname_opt", "") add_vm = "yes" == params.get("add_vm", "no") # Replace the varaiables if vm_ref == "name": vm_ref = vm_name elif vm_ref == "uuid": vm_ref = domuuid if new_name == "vm2_name": vm2_name = ("%s" % vm_name[:-1]) + "2" new_name = vm2_name # Build input params dom_param = ' '.join([domain_option, vm_ref]) new_name_param = ' '.join([new_name_option, new_name]) if vm.is_alive(): vm.destroy() # Backup for recovery. vmxml_backup = libvirt_xml.vm_xml.VMXML.new_from_inactive_dumpxml(vm_name) logging.debug("vm xml is %s", vmxml_backup) # Clone additional vms if needed if add_vm: try: utils_path.find_command("virt-clone") except utils_path.CmdNotFoundError: if not utils_package.package_install(["virt-install"]): test.cancel("Failed to install virt-install on host") ret_clone = utils_libguestfs.virt_clone_cmd(vm_name, vm2_name, True, timeout=360) if ret_clone.exit_status: test.fail("Error occured when clone a second vm!") vm2 = libvirt_vm.VM(vm2_name, vm.params, vm.root_dir, vm.address_cache) virsh.dom_list("--name --all", debug=True) # Create object instance for renamed domain new_vm = libvirt_vm.VM(new_name, vm.params, vm.root_dir, vm.address_cache) # Prepare vm state if pre_vm_state not in ["shutoff", "autostart"]: vm.start() if pre_vm_state == "paused": vm.pause() logging.debug("Domain state is now: %s", vm.state()) elif pre_vm_state == "managed_saved": vm.managedsave() elif pre_vm_state == "with_snapshot": virsh.snapshot_create_as(vm_name, "snap1 --disk-only", debug=True) vm.destroy(gracefully=False) try: if pre_vm_state == "autostart": virsh.autostart(dom_param, "", debug=True) virsh.dom_list("--all --autostart", debug=True) logging.debug("files under '/etc/libvirt/qemu/autostart/' are %s", os.listdir('/etc/libvirt/qemu/autostart/')) result = virsh.domrename(dom_param, new_name_param, ignore_status=True, debug=True) # Raise unexpected pass or fail libvirt.check_exit_status(result, status_error) # Return expected failure for negative tests if status_error: logging.debug("Expected failure: %s", result.stderr) return # Checkpoints after domrename succeed else: list_ret = virsh.dom_list("--name --all", debug=True).stdout.strip().splitlines() domname_ret = virsh.domname(domuuid, debug=True).stdout.strip() if new_name not in list_ret or vm_name in list_ret: test.fail("New name does not affect in virsh list") if domname_ret != new_name: test.fail( "New domain name does not affect in virsh domname uuid") if pre_vm_state != "autostart": # Try to start vm with the new name new_vm.start() else: utils_libvirtd.libvirtd_restart() list_autostart = virsh.dom_list("--autostart", debug=True).stdout logging.debug( "files under '/etc/libvirt/qemu/autostart/' are %s", os.listdir('/etc/libvirt/qemu/autostart/')) process.run("file /etc/libvirt/qemu/autostart/%s.xml" % vm_name, verbose=True) if new_name not in list_autostart: test.fail( "Domain isn't autostarted after restart libvirtd," "or becomes a never 'autostart' one.") finally: # Remove additional vms if add_vm and vm2.exists() and result.exit_status: virsh.remove_domain(vm2_name, "--remove-all-storage") # Undefine newly renamed domain if new_vm.exists(): if new_vm.is_alive(): new_vm.destroy(gracefully=False) new_vm.undefine() # Recover domain state if pre_vm_state != "shutoff": if pre_vm_state == "with_snapshot": libvirt.clean_up_snapshots(vm_name) else: if pre_vm_state == "managed_saved": vm.start() vm.destroy(gracefully=False) # Restore VM vmxml_backup.sync()
def run(test, params, env): """ Test command: virsh rename. The command can rename a domain. 1.Prepare test environment. 2.Perform virsh rename operation. 3.Recover test environment. 4.Confirm the test result. """ # Get specific parameter value vm_name = params.get("main_vm") vm = env.get_vm(vm_name) domuuid = vm.get_uuid() vm_ref = params.get("domrename_vm_ref", "name") status_error = "yes" == params.get("status_error", "no") new_name = params.get("vm_new_name", "new") vm_state = params.get("domrename_vm_state", "shutoff") domain_option = params.get("dom_opt", "") new_name_option = params.get("newname_opt", "") # Create object instance for renamed domain new_vm = libvirt_vm.VM(new_name, vm.params, vm.root_dir, vm.address_cache) # Replace the varaiables if vm_ref == "name": vm_ref = vm_name else: vm_ref = domuuid # Build input params dom_param = ' '.join([domain_option, vm_ref]) new_name_param = ' '.join([new_name_option, new_name]) # Backup for recovery. vmxml_backup = libvirt_xml.vm_xml.VMXML.new_from_inactive_dumpxml(vm_name) try: result = virsh.domrename(dom_param, new_name_param, ignore_status=True, debug=True) # Raise unexpected pass or fail libvirt.check_exit_status(result, status_error) # Return expected failure for negative tests if status_error: logging.debug("Expected failure: %s", result.stderr) return # Checkpoints after domrename succeed else: list_ret = virsh.dom_list("--name --all", debug=True).stdout.strip() domname_ret = virsh.domname(domuuid, debug=True).stdout.strip() if list_ret != new_name: test.fail("New name does not affect in virsh list") if domname_ret != new_name: test.fail("New domain name does not affect in virsh domname uuid") # Try to start vm with the new name new_vm.start() finally: # Restore VM if new_vm.exists(): if new_vm.is_alive(): new_vm.destroy(gracefully=False) new_vm.undefine() vmxml_backup.sync()