def run_cpu_compare(params, libvirtd, vm): """ Comprare a cpu without model property. """ cpu_xml = VMCPUXML() cpu_xml.topology = {"sockets": 1, "cores": 1, "threads": 1} res = virsh.cpu_compare(cpu_xml.xml) logging.debug(res)
def run(test, params, env): """ Test command: virsh cpu-compare. Compare host CPU with a CPU described by an XML file. 1.Get all parameters from configuration. 2.Prepare temp file saves of CPU information. 3.Perform virsh cpu-compare operation. 4.Confirm the result. """ def get_cpu_xml(target, mode): """ Get CPU information and put it into a file. :param target: Test target, host or guest's cpu description. :param mode: Test mode, decides file's detail. """ libvirtxml = vm_xml.VMCPUXML() if target == "host": cpu_feature_list = host_cpu_xml.get_feature_list() if cpu_match: libvirtxml['match'] = cpu_match libvirtxml['vendor'] = host_cpu_xml['vendor'] libvirtxml['model'] = host_cpu_xml['model'] for cpu_feature in cpu_feature_list: feature_name = cpu_feature.get('name') libvirtxml.add_feature(feature_name, "require") else: try: libvirtxml = vmxml['cpu'] except LibvirtXMLNotFoundError: test.cancel("No <cpu> element in domain XML") if mode == "modify": if modify_target == "mode": libvirtxml['mode'] = modify_value # Power CPU model names are in lowercases for compatibility mode if "ppc" in platform.machine( ) and modify_value == "host-model": libvirtxml['model'] = libvirtxml['model'].lower() elif modify_target == "model": libvirtxml['model'] = modify_value elif modify_target == "vendor": libvirtxml['vendor'] = modify_value elif modify_target == "feature_name": if modify_value == "REPEAT": feature_name = libvirtxml.get_feature_name(feature_num) feature_policy = libvirtxml.get_feature_policy(0) libvirtxml.add_feature(feature_name, feature_policy) else: libvirtxml.set_feature(feature_num, name=modify_value) elif modify_target == "feature_policy": libvirtxml.set_feature(feature_num, policy=modify_value) elif mode == "delete": libvirtxml.remove_feature(feature_num) else: pass return libvirtxml # Get all parameters. ref = params.get("cpu_compare_ref") mode = params.get("cpu_compare_mode", "") modify_target = params.get("cpu_compare_modify_target", "") modify_value = params.get("cpu_compare_modify_value", "") feature_num = int(params.get("cpu_compare_feature_num", -1)) target = params.get("cpu_compare_target", "host") extra = params.get("cpu_compare_extra", "") file_name = params.get("cpu_compare_file_name", "cpu.xml") cpu_match = params.get("cpu_compare_cpu_match", "") modify_invalid = "yes" == params.get("cpu_compare_modify_invalid", "no") check_vm_ps = "yes" == params.get("check_vm_ps", "no") check_vm_ps_value = params.get("check_vm_ps_value") tmp_file = os.path.join(test.tmpdir, file_name) vm_name = params.get("main_vm") vm = env.get_vm(vm_name) if vm.is_alive(): vm.destroy() vmxml = vm_xml.VMXML.new_from_dumpxml(vm_name) vmxml_backup = vmxml.copy() host_cpu_xml = capability_xml.CapabilityXML() try: # Add cpu element if it not present in VM XML if not vmxml.get('cpu'): new_cpu = vm_xml.VMCPUXML() new_cpu['model'] = host_cpu_xml['model'] vmxml['cpu'] = new_cpu # Add cpu model element if it not present in VM XML if not vmxml['cpu'].get('model'): vmxml['cpu']['model'] = host_cpu_xml['model'] # Prepare VM cpu feature if necessary if modify_target in ['feature_name', 'feature_policy', 'delete']: if len(vmxml['cpu'].get_feature_list() ) == 0 and host_cpu_xml.get_feature_list(): # Add a host feature to VM for testing vmxml_cpu = vmxml['cpu'].copy() vmxml_cpu.add_feature(host_cpu_xml.get_feature_name('-1')) vmxml['cpu'] = vmxml_cpu vmxml.sync() else: test.cancel("No features present in host " "capability XML") # Prepare temp compare file. cpu_compare_xml = get_cpu_xml(target, mode) with open(tmp_file, 'w+b') as cpu_compare_xml_f: if mode == "clear": cpu_compare_xml_f.truncate(0) else: cpu_compare_xml.xmltreefile.write(cpu_compare_xml_f) cpu_compare_xml_f.seek(0) logging.debug("CPU description XML:\n%s", cpu_compare_xml_f.read()) # Expected possible result msg patterns and exit status msg_patterns = "" if not mode: if target == "host": msg_patterns = "identical" else: # As we don't know the <cpu> element in domain, # so just check command exit status pass elif mode == "delete": if cpu_match == "strict": msg_patterns = "incompatible" else: msg_patterns = "superset" elif mode == "modify": if modify_target == "mode": if modify_invalid: msg_patterns = "Invalid mode" elif modify_target == "model": if modify_invalid: msg_patterns = "Unknown CPU model" elif modify_target == "vendor": if modify_invalid: msg_patterns = "incompatible" elif modify_target == "feature_name": if modify_value == "REPEAT": msg_patterns = "more than once" elif modify_value == "ia64": msg_patterns = "incompatible" elif modify_invalid: msg_patterns = "Unknown" elif modify_target == "feature_policy": if modify_value == "forbid": msg_patterns = "incompatible" else: msg_patterns = "identical" else: test.cancel("Unsupport modify target %s in this " "test" % mode) elif mode == "clear": msg_patterns = "empty" elif mode == "invalid_test": msg_patterns = "" else: test.cancel("Unsupport modify mode %s in this " "test" % mode) status_error = params.get("status_error", "") if status_error == "yes": expected_status = 1 elif status_error == "no": expected_status = 0 else: # If exit status is not specified in cfg, using msg_patterns # to get expect exit status if msg_patterns in ['identical', 'superset']: expected_status = 0 else: expected_status = 1 # Default guest cpu compare should pass if not mode and target == "guest": expected_status = 0 if ref == "file": ref = tmp_file ref = "%s %s" % (ref, extra) # Perform virsh cpu-compare operation. result = virsh.cpu_compare(ref, ignore_status=True, debug=True) if os.path.exists(tmp_file): os.remove(tmp_file) # Check result logging.debug("Expect command exit status: %s", expected_status) if result.exit_status != expected_status: test.fail("Exit status %s is not expected" % result.exit_status) if msg_patterns: logging.debug("Expect key word in comand output: %s", msg_patterns) if result.stdout.strip(): output = result.stdout.strip() else: output = result.stderr.strip() if not output.count(msg_patterns): test.fail("Not find expect key word in command output") # Check VM for cpu 'mode' related cases if check_vm_ps: vmxml['cpu'] = cpu_compare_xml vmxml.sync() virsh.start(vm_name, ignore_status=True, debug=True) vm_pid = vm.get_pid() if vm_pid is None: test.error("Could not get VM pid") with open("/proc/%d/cmdline" % vm_pid) as vm_cmdline_file: vm_cmdline = vm_cmdline_file.read() vm_features_str = "" # cmdline file is always separated by NUL characters('\x00') for item in vm_cmdline.split('\x00-'): if item.count('cpu'): vm_features_str = item logging.debug("VM cpu device: %s", vm_features_str) vm_features = [] for f in vm_features_str.split(','): if f.startswith('+'): vm_features.append(f[1:]) host_features = [] if check_vm_ps_value == 'CAPABILITY': for feature in host_cpu_xml.get_feature_list(): host_features.append(feature.get('name')) else: host_features.append(check_vm_ps_value) for feature in vm_features: if feature not in host_features: test.fail("Not find %s in host capability" % feature) finally: vmxml_backup.sync()
def run(test, params, env): """ Test command: virsh cpu-compare. Compare host CPU with a CPU described by an XML file. 1.Get all parameters from configuration. 2.Prepare temp file saves of CPU information. 3.Perform virsh cpu-compare operation. 4.Confirm the result. """ def get_cpu_xml(target, mode): """ Get CPU information and put it into a file. :param target: Test target, host or guest's cpu description. :param mode: Test mode, decides file's detail. """ libvirtxml = vm_xml.VMCPUXML() if target == "host": cpu_feature_list = host_cpu_xml.get_feature_list() if cpu_match: libvirtxml['match'] = cpu_match libvirtxml['vendor'] = host_cpu_xml['vendor'] libvirtxml['model'] = host_cpu_xml['model'] for cpu_feature in cpu_feature_list: feature_name = cpu_feature.get('name') libvirtxml.add_feature(feature_name, "require") else: try: libvirtxml = vmxml['cpu'] except LibvirtXMLNotFoundError: raise error.TestNAError("No <cpu> element in domain XML") if mode == "modify": if modify_target == "mode": libvirtxml['mode'] = modify_value elif modify_target == "model": libvirtxml['model'] = modify_value elif modify_target == "vendor": libvirtxml['vendor'] = modify_value elif modify_target == "feature_name": if modify_value == "REPEAT": feature_name = libvirtxml.get_feature_name(feature_num) feature_policy = libvirtxml.get_feature_policy(0) libvirtxml.add_feature(feature_name, feature_policy) else: libvirtxml.set_feature(feature_num, name=modify_value) elif modify_target == "feature_policy": libvirtxml.set_feature(feature_num, policy=modify_value) elif mode == "delete": libvirtxml.remove_feature(feature_num) else: pass return libvirtxml # Get all parameters. ref = params.get("cpu_compare_ref") mode = params.get("cpu_compare_mode", "") modify_target = params.get("cpu_compare_modify_target", "") modify_value = params.get("cpu_compare_modify_value", "") feature_num = int(params.get("cpu_compare_feature_num", -1)) target = params.get("cpu_compare_target", "host") extra = params.get("cpu_compare_extra", "") file_name = params.get("cpu_compare_file_name", "cpu.xml") cpu_match = params.get("cpu_compare_cpu_match", "") modify_invalid = "yes" == params.get("cpu_compare_modify_invalid", "no") check_vm_ps = "yes" == params.get("check_vm_ps", "no") check_vm_ps_value = params.get("check_vm_ps_value") tmp_file = os.path.join(test.tmpdir, file_name) vm_name = params.get("main_vm") vm = env.get_vm(vm_name) if vm.is_alive(): vm.destroy() vmxml = vm_xml.VMXML.new_from_dumpxml(vm_name) vmxml_backup = vmxml.copy() host_cpu_xml = capability_xml.CapabilityXML() try: # Add cpu element if it not present in VM XML if not vmxml.get('cpu'): new_cpu = vm_xml.VMCPUXML() new_cpu['model'] = host_cpu_xml['model'] vmxml['cpu'] = new_cpu # Add cpu model element if it not present in VM XML if not vmxml['cpu'].get('model'): vmxml['cpu']['model'] = host_cpu_xml['model'] # Prepare VM cpu feature if necessary if modify_target in ['feature_name', 'feature_policy', 'delete']: if len(vmxml['cpu'].get_feature_list()) == 0: # Add a host feature to VM for testing vmxml_cpu = vmxml['cpu'].copy() vmxml_cpu.add_feature(host_cpu_xml.get_feature_name('-1')) vmxml['cpu'] = vmxml_cpu vmxml.sync() # Prepare temp compare file. cpu_compare_xml = get_cpu_xml(target, mode) cpu_compare_xml_f = open(tmp_file, 'w+b') if mode == "clear": cpu_compare_xml_f.truncate(0) else: cpu_compare_xml.xmltreefile.write(cpu_compare_xml_f) cpu_compare_xml_f.seek(0) logging.debug("CPU description XML:\n%s", cpu_compare_xml_f.read()) cpu_compare_xml_f.close() # Expected possible result msg patterns and exit status msg_patterns = "" if not mode: if target == "host": msg_patterns = "identical" else: # As we don't know the <cpu> element in domain, # so just check command exit status pass elif mode == "delete": if cpu_match == "strict": msg_patterns = "incompatible" else: msg_patterns = "superset" elif mode == "modify": if modify_target == "mode": if modify_invalid: msg_patterns = "Invalid mode" elif modify_target == "model": if modify_invalid: msg_patterns = "Unknown CPU model" elif modify_target == "vendor": if modify_invalid: msg_patterns = "incompatible" elif modify_target == "feature_name": if modify_value == "REPEAT": msg_patterns = "more than once" elif modify_value == "ia64": msg_patterns = "incompatible" elif modify_invalid: msg_patterns = "Unknown" elif modify_target == "feature_policy": if modify_value == "forbid": msg_patterns = "incompatible" else: msg_patterns = "identical" else: raise error.TestNAError("Unsupport modify target %s in this " "test" % mode) elif mode == "clear": msg_patterns = "empty" elif mode == "invalid_test": msg_patterns = "" else: raise error.TestNAError("Unsupport modify mode %s in this " "test" % mode) status_error = params.get("status_error", "") if status_error == "yes": expected_status = 1 elif status_error == "no": expected_status = 0 else: # If exit status is not specified in cfg, using msg_patterns # to get expect exit status if msg_patterns in ['identical', 'superset']: expected_status = 0 else: expected_status = 1 # Default guest cpu compare should pass if not mode and target == "guest": expected_status = 0 if ref == "file": ref = tmp_file ref = "%s %s" % (ref, extra) # Perform virsh cpu-compare operation. result = virsh.cpu_compare(ref, ignore_status=True, debug=True) if os.path.exists(tmp_file): os.remove(tmp_file) # Check result logging.debug("Expect command exit status: %s", expected_status) if result.exit_status != expected_status: raise error.TestFail("Exit status %s is not expected" % result.exit_status) if msg_patterns: logging.debug("Expect key word in comand output: %s", msg_patterns) if result.stdout.strip(): output = result.stdout.strip() else: output = result.stderr.strip() if not output.count(msg_patterns): raise error.TestFail("Not find expect key word in command output") # Check VM for cpu 'mode' related cases if check_vm_ps: vmxml['cpu'] = cpu_compare_xml vmxml.sync() virsh.start(vm_name, ignore_status=True, debug=True) vm_pid = vm.get_pid() if vm_pid is None: raise error.TestError("Could not get VM pid") with open("/proc/%d/cmdline" % vm_pid) as vm_cmdline_file: vm_cmdline = vm_cmdline_file.read() vm_features_str = "" # cmdline file is always separated by NUL characters('\x00') for item in vm_cmdline.split('\x00-'): if item.count('cpu'): vm_features_str = item logging.debug("VM cpu device: %s", vm_features_str) vm_features = [] for f in vm_features_str.split(','): if f.startswith('+'): vm_features.append(f[1:]) host_features = [] if check_vm_ps_value == 'CAPABILITY': for feature in host_cpu_xml.get_feature_list(): host_features.append(feature.get('name')) else: host_features.append(check_vm_ps_value) for feature in vm_features: if feature not in host_features: raise error.TestFail("Not find %s in host capability" % feature) finally: vmxml_backup.sync()
def run_virsh_cpu_compare(test, params, env): """ Test command: virsh cpu-compare. Compare host CPU with a CPU described by an XML file. 1.Get all parameters from configuration. 2.Prepare temp file saves of CPU infomation. 3.Perform virsh cpu-compare operation. 4.Confirm the result. """ def get_cpu_xml(target, mode, tmp_file, cpu_mode=""): """ Get CPU infomation and put it into a file. @param: target: Test target, host or guest's cpu description. @param: mode: Test mode, decides file's detail. @param: tmp_file: File saves CPU infomation. """ try: cpu_xml_file = open(tmp_file, 'wb') if target == "host": libvirtxml = capability_xml.CapabilityXML() else: libvirtxml = vm_xml.VMCPUXML(vm_name=vm_name, mode=cpu_mode) if mode == "modify": if modify_target == "vendor": libvirtxml['vendor'] = test_vendor else: # Choose the last feature to test if feature_action == "remove": libvirtxml.remove_feature(feature_num) elif feature_action == "repeat": name = libvirtxml.get_feature_name(feature_num) libvirtxml.add_feature(name) else: libvirtxml.set_feature(feature_num, feature_name) libvirtxml.dict_get('xml').write(cpu_xml_file) elif mode == "clear": # Clear up file detail cpu_xml_file.truncate(0) else: libvirtxml.dict_get('xml').write(cpu_xml_file) cpu_xml_file.close() except (IndexError, AttributeError): if target == "guest": vmxml.undefine() vmxml.define() raise error.TestError("Get CPU infomation failed!") # Get all parameters. ref = params.get("cpu_compare_ref") mode = params.get("cpu_compare_mode", "") modify_target = params.get("cpu_compare_modify_target", "vendor") feature_num = int(params.get("cpu_compare_feature_num", -1)) feature_action = params.get("cpu_compare_feature_action", "modify") feature_name = params.get("cpu_compare_feature", "") test_vendor = params.get("cpu_compare_vendor", "") target = params.get("cpu_compare_target", "host") status_error = params.get("status_error", "no") extra = params.get("cpu_compare_extra", "") file_name = params.get("cpu_compare_file_name", "cpu.xml") cpu_mode = params.get("cpu_compare_cpu_mode", "") tmp_file = os.path.join(test.tmpdir, file_name) if target == "guest": vm_name = params.get("main_vm") vm = env.get_vm(vm_name) if vm.is_alive(): vm.destroy() # Backup the VM's xml. vmxml = vm_xml.VMXML.new_from_dumpxml(vm_name) # Prepare temp file. get_cpu_xml(target, mode, tmp_file, cpu_mode) if ref == "file": ref = tmp_file ref = "%s %s" % (ref, extra) # Perform virsh cpu-compare operation. status = virsh.cpu_compare(ref, ignore_status=True, debug=True).exit_status # Recover VM. if target == "guest": vmxml.undefine() vmxml.define() # 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_cpu_compare(test, params, env): """ Test command: virsh cpu-compare. Compare host CPU with a CPU described by an XML file. 1.Get all parameters from configuration. 2.Prepare temp file saves of CPU infomation. 3.Perform virsh cpu-compare operation. 4.Confirm the result. """ def get_cpu_xml(mode, tmp_file): """ Get CPU infomation and put it into a file. @param: mode: Test mode, decides file's detail. @param: tmp_file: File saves CPU infomation. """ cpu_xml_file = open(tmp_file, 'wb') domxml = virsh.capabilities() dom = parseString(domxml) cpu_node = dom.getElementsByTagName('cpu')[0] if mode == "modify": vendor = cpu_node.getElementsByTagName('vendor')[0] for node in vendor.childNodes: if node.nodeType == node.TEXT_NODE: vendor.removeChild(node) break text_node = dom.createTextNode('test_vendor') vendor.appendChild(text_node) cpu_node.writexml(cpu_xml_file) elif mode == "clear": # Clear up file detail cpu_xml_file.truncate(0) else: cpu_node.writexml(cpu_xml_file) cpu_xml_file.close() dom.unlink() # Get all parameters. ref = params["cpu_compare_ref"] mode = params.get("cpu_compare_mode", "") status_error = params.get("status_error", "no") extra = params.get("cpu_compare_extra", "") file_name = params.get("cpu_compare_file_name", "cpu.xml") tmp_file = os.path.join(test.tmpdir, file_name) # Prepare temp file. get_cpu_xml(mode, tmp_file) if ref == "file": ref = tmp_file ref = "%s %s" % (ref, extra) # Perform virsh cpu-compare operation. status = virsh.cpu_compare(ref, ignore_status=True, debug=True).exit_status # 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(test, params, env): """ Test command: virsh cpu-compare with domain XML. Compare host CPU with a CPU described by an XML file. 1.Get all parameters from configuration. 2.Prepare temp file saves of CPU information. 3.Perform virsh cpu-compare operation. 4.Confirm the result. """ # Get all parameters. file_type = params.get("compare_file_type", "domxml") is_extracted = params.get("extract_mode", False) cpu_mode = params.get("cpu_mode", "custom") cpu_compare_mode = params.get("cpu_compare_mode") status_error = ("yes" == params.get("status_error", "no")) tmp_file = "" vm_name = params.get("main_vm") vm = env.get_vm(vm_name) if not vm.is_alive(): vm.start() if not libvirt_version.version_compare(4, 4, 0): test.cancel("CPU compare new update cases does not support" " in this libvirt version") try: # Prepare temp compare file. cpu_compare_xml = "" if file_type not in ("domxml", "domcapa_xml", "capa_xml"): test.cancel("Unsupported xml type: %s" % file_type) if file_type == "domxml": cpu_compare_xml = get_domxml(cpu_mode, vm_name, is_extracted) if file_type == "domcapa_xml": cpu_compare_xml = get_domcapa_xml(is_extracted) if file_type == "capa_xml": cpu_compare_xml = get_capa_xml(cpu_compare_mode, is_extracted) if cpu_compare_mode == "invalid_test": cpu_compare_xml = get_invalid_xml(cpu_compare_xml) cpu_compare_xml.xmltreefile.write() tmp_file = cpu_compare_xml.xml with open(tmp_file) as tmp_file_f: logging.debug("CPU description XML:\n%s", tmp_file_f.read()) # Perform virsh cpu-compare operation. result = virsh.cpu_compare(xml_file=tmp_file, ignore_status=True, debug=True) msg_pattern = params.get("msg_pattern") # Check result if status_error: if not result.exit_status: test.fail("Expect should fail but got:\n%s" % result.stdout) else: if result.exit_status: test.fail("Expect success but got:\n%s" % result.stderr) if msg_pattern: logging.debug("Expect key word in comand output: %s", msg_pattern) output = result.stdout.strip() if not output: output = result.stderr.strip() if not output.count(msg_pattern): test.fail("Not find expect key word in command output") finally: if os.path.exists(tmp_file): os.remove(tmp_file)