def run(test, params, env): """ Test virsh nwfilter-binding-create 1)start a vm with interface 2)perpare the building xml 3)create binding 4)check ebtables rule is added 5)restart libvirtd and check the filter still there """ vm_name = params.get("main_vm") vm = env.get_vm(vm_name) check_cmd = params.get("check_cmd") expected_match = params.get("expected_match") status_error = "yes" == params.get("status_error") filter_name = params.get("filter_name", "clean-traffic") wait_time = params.get("wait_time", 1) # back up for recovery vmxml_backup = libvirt_xml.vm_xml.VMXML.new_from_inactive_dumpxml(vm_name) # prepare filter parameters dict filter_param_dict = [] param_dict = {} logging.debug("wait_time is : %s" % wait_time) wait_time = float(wait_time) libvirtd = utils_libvirtd.Libvirtd() def prepare_env(): vmxml = libvirt_xml.VMXML.new_from_inactive_dumpxml(vm_name) iface_xml = vmxml.get_devices('interface')[0] vmxml.del_device(iface_xml) new_iface = interface.Interface('network') new_iface.xml = iface_xml.xml new_iface.type_name = "network" new_iface.source = {'network': "default", 'bridge': "virbr0"} alias_dict = {'name': "net0"} new_iface.alias = alias_dict target_dict = {'dev': "vnet0"} new_iface.target = target_dict logging.debug("new interface xml is : %s" % new_iface) vmxml.add_device(new_iface) vmxml.sync() return new_iface def create_binding_file(new_iface): vmxml = libvirt_xml.VMXML.new_from_inactive_dumpxml(vm_name) binding = nwfilter_binding.NwfilterBinding() binding.owner = binding.new_owner(vm_name, vmxml.uuid) binding.mac_address = new_iface.mac_address portdev = "vnet0" binding.portdev = portdev param_dict['name'] = "MAC" param_dict['value'] = new_iface.mac_address filter_param_dict.append(param_dict) filterrefs_dict = {} filterrefs_dict['name'] = filter_name filterrefs_dict['parameters'] = filter_param_dict binding.filterref = binding.new_filterref(**filterrefs_dict) logging.debug("filter binding xml is: %s" % binding) return binding try: new_iface = prepare_env() binding = create_binding_file(new_iface) # binding xml vm.start() vmxml = libvirt_xml.VMXML.new_from_dumpxml(vm_name) iface_xml = vmxml.get_devices('interface')[0] iface_target = iface_xml.target['dev'] logging.debug("iface target dev name is %s" % iface_target) virsh.nwfilter_binding_create(binding.xml, debug=True) # check ebtables rule is add # wait_for nwfilter-binding-create command exec finish time.sleep(wait_time) utlv.check_cmd_output(check_cmd, expected_match, True) if not libvirtd.restart(): virsh.nwfilter_binding_list(debug=True) test.fail("fail to restart libvirtd") ret = virsh.nwfilter_binding_list(debug=True) utlv.check_exit_status(ret, status_error) ret = virsh.nwfilter_binding_dumpxml("vnet0", debug=True) utlv.check_exit_status(ret, status_error) finally: if vm.is_alive(): vm.destroy(gracefully=False) vmxml_backup.sync()
def run(test, params, env): """ 1. prepare env 2. check if nwfilter binding 3. run test 4. destroy vm and restore the status """ vm_name = params.get("main_vm") vm = env.get_vm(vm_name) status_error = "yes" == params.get("status_error") vmxml_backup = libvirt_xml.vm_xml.VMXML.new_from_inactive_dumpxml(vm_name) filter_name = params.get("filter_name") is_nwfilter_define = "yes" == params.get("is_nwfilter_define") vnet0_binding = os.path.join(data_dir.get_tmp_dir(), "vnet0_binding.xml") filter_binding_name = params.get("filter_binding_name") failed_msg = params.get("expected_failed") target_dev = params.get("target_dev") source_network = params.get("source_network") source_bridge = params.get("source_bridge") alias_name = params.get("alias_name") def set_env(): """ prepare the vm interface xml this xml can both use in two senario. but little different for two senario """ vmxml = libvirt_xml.VMXML.new_from_inactive_dumpxml(vm_name) iface_xml = vmxml.get_devices('interface')[0] vmxml.del_device(iface_xml) new_iface = interface.Interface('network') new_iface.xml = iface_xml.xml new_iface.type_name = "network" iface_target = {'dev': target_dev} new_iface.target = iface_target source = {'network': source_network, 'bridge': source_bridge} new_iface.source = source filterrefs_dict = {} filterrefs_dict['name'] = filter_name filterrefs_dict['parameters'] = [] new_filterref = new_iface.new_filterref(**filterrefs_dict) new_iface.filterref = new_filterref alias_dict = {'name': alias_name} new_iface.alias = alias_dict vmxml.add_device(new_iface) logging.debug("new interface xml is: %s" % new_iface) vmxml.sync() return new_iface def check_binding_port(cmd_res, match, is_match=True): """ check the list binding ports """ list_res = cmd_res.stdout_text.strip() if list_res and re.search(match, list_res): if not is_match: test.fail("expected not match %s" % match) elif is_match: test.fail("expected match %s but not match" % match) try: # set new interface env new_iface = set_env() # create binding dump file virsh.start(vm_name, debug=True) ret = virsh.nwfilter_binding_dumpxml(new_iface.target['dev'], to_file=vnet0_binding, debug=True) utlv.check_exit_status(ret, status_error) binding = nwfilter_binding.NwfilterBinding() binding.xml = vnet0_binding filterrefs_dict = {} filterrefs_dict['name'] = filter_binding_name filterrefs_dict['parameters'] = [{ 'name': "MAC", 'value': new_iface.mac_address }] binding.filterref = binding.new_filterref(**filterrefs_dict) logging.debug("binding is %s" % binding) # list filter if not is_nwfilter_define: virsh.nwfilter_binding_delete(new_iface.target['dev'], debug=True) if is_nwfilter_define: ret = virsh.nwfilter_binding_list(debug=True) utlv.check_exit_status(ret, status_error) check_binding_port(ret, filter_name, is_match=True) ret_create = virsh.nwfilter_binding_create(binding.xml, debug=True) # two Senario if is_nwfilter_define: utlv.check_result(ret_create, failed_msg) elif not is_nwfilter_define: # get params for senario2 check_cmd = params.get("check_cmd") expected_match = params.get("expected_match") filter_binding_copy = params.get("filter_binding_copy") ret = virsh.nwfilter_binding_list(debug=True) check_binding_port(ret, filter_binding_name, is_match=True) utlv.check_cmd_expected(check_cmd, expected_match, True) utils_libvirtd.libvirtd_restart() ret = virsh.nwfilter_binding_list(debug=True) check_binding_port(ret, filter_binding_name, is_match=True) # use check command to check result utlv.check_cmd_expected(check_cmd, expected_match, True) new_binding = nwfilter_binding.NwfilterBinding() new_binding.xml = binding.xml filterrefs_dict = {} filterrefs_dict['name'] = filter_binding_copy filterrefs_dict['parameters'] = [{ 'name': "MAC", 'value': new_iface.mac_address }] binding.filterref = binding.new_filterref(**filterrefs_dict) logging.debug("binding is %s" % new_binding) ret_create = virsh.nwfilter_binding_create(new_binding.xml, debug=True) utlv.check_result(ret_create, failed_msg) finally: if vm.is_alive(): vm.destroy(gracefully=False) vmxml_backup.sync()
def run(test, params, env): """ Test virsh nwfilter-binding-list 1)Prepare parameters 2)Run nwfilter_binding_list command 3)check result 4)Clean env """ vm_name = params.get("main_vm") vm = env.get_vm(vm_name) new_filter_1 = params.get("newfilter_1") new_filter_2 = params.get("newfilter_2") vmxml_backup = libvirt_xml.vm_xml.VMXML.new_from_inactive_dumpxml(vm_name) new_net0_xml = os.path.join(data_dir.get_tmp_dir(), "new_net0.xml") new_net1_xml = os.path.join(data_dir.get_tmp_dir(), "new_net1.xml") option = params.get("option") status_error = "yes" == params.get("status_error") alias_name = params.get("alias_name") new_filter_name = params.get("new_filter_name") source_network = params.get("source_network") # prepare vm filterrfer parameters dict list filter_param_list_1 = [] params_key_1 = [] filter_param_list_2 = [] params_key_2 = [] for i in params.keys(): if 'parameters_name_' in i: params_key_1.append(i) params_key_1.sort() for i in range(len(params_key_1)): params_dict = {} params_dict['name'] = params[params_key_1[i]] params_dict['value'] = params['parameters_value_%s' % i] filter_param_list_1.append(params_dict) filterref_dict_1 = {} filterref_dict_1['name'] = new_filter_1 filterref_dict_1['parameters'] = filter_param_list_1 for i in params.keys(): if 'parameters_dhcp_' in i: params_key_2.append(i) params_key_2.sort() for i in range(len(params_key_2)): params_dict = {} params_dict['name'] = params[params_key_2[i]] params_dict['value'] = params['dhcp_value_%s' % i] filter_param_list_2.append(params_dict) filterref_dict_2 = {} filterref_dict_2['name'] = new_filter_2 filterref_dict_2['parameters'] = filter_param_list_2 def set_env(): """ set two interface with different network filter and change interface type """ # Add enough PCI to attach interface libvirt_pcicontr.reset_pci_num(vm_name) virsh.attach_interface(vm_name, option, debug=True) vmxml = libvirt_xml.VMXML.new_from_dumpxml(vm_name) logging.debug("Guest xml is {}".format(vmxml)) devices = vmxml.get_devices('interface') iface_xml = devices[0] iface_xml_2 = devices[1] vmxml.del_device(iface_xml) vmxml.del_device(iface_xml_2) new_iface_1 = interface.Interface('network') new_iface_2 = interface.Interface('network') new_iface_1.xml = iface_xml.xml new_iface_2.xml = iface_xml_2.xml new_iface_1.type_name = "network" new_iface_2.type_name = "network" new_iface_1.source = {'network': source_network} new_iface_2.source = {'network': source_network} new_iface_1.target = {'dev': 'new_net0'} new_iface_2.target = {'dev': 'new_net1'} new_filterref = new_iface_1.new_filterref(**filterref_dict_1) new_iface_1.filterref = new_filterref new_filterref = new_iface_2.new_filterref(**filterref_dict_2) new_iface_2.filterref = new_filterref logging.debug("new interface xml is: %s \n %s" % (new_iface_1, new_iface_2)) vmxml.add_device(new_iface_1) vmxml.add_device(new_iface_2) vmxml.sync() return new_iface_1, new_iface_2 try: new_iface_1, new_iface_2 = set_env() # start vm virsh.start(vm_name, debug=True) # list binding port dev ret = virsh.nwfilter_binding_list(debug=True) utlv.check_exit_status(ret, status_error) virsh.nwfilter_binding_dumpxml(new_iface_1.target['dev'], to_file=new_net0_xml, debug=True) virsh.nwfilter_binding_dumpxml(new_iface_2.target['dev'], to_file=new_net1_xml, debug=True) # check dump filterbinding can pass xml validate new_net0_cmd = "virt-xml-validate %s" % new_net0_xml new_net1_cmd = "virt-xml-validate %s" % new_net1_xml valid_0 = process.run(new_net0_cmd, ignore_status=True, shell=True).exit_status valid_1 = process.run(new_net1_cmd, ignore_status=True, shell=True).exit_status if valid_0 or valid_1: test.fail("the xml can not validate successfully") # create new xml and update device newnet_iface = interface.Interface('network') newnet_iface.xml = new_iface_1.xml filterref_list = [] filterref_dict = {} filterref_dict['name'] = new_filter_name filterref_dict['parameters'] = filterref_list newnet_iface.alias = {'name': alias_name} newnet_iface.filterref = newnet_iface.new_filterref(**filterref_dict) ret = virsh.update_device(domainarg=vm_name, filearg=newnet_iface.xml, debug=True) utlv.check_exit_status(ret, status_error) ret_list = virsh.nwfilter_binding_list(debug=True) utlv.check_result(ret_list, expected_match="new_net1") ret_dump = virsh.nwfilter_binding_dumpxml('new_net0', debug=True) utlv.check_result(ret_dump, expected_match=new_filter_name) finally: if vm.is_alive(): vm.destroy(gracefully=False) vmxml_backup.sync()