def test_get_sriov_port_params(self, mock_port_bindings, mock_open,
                                mock_os, mock_re, mock_glob):
     sriov_port = copy.deepcopy(FAKE_SRIOV_PORT)
     fake_profile = mock_port_bindings.PROFILE = 'binding:profile'
     mock_port_bindings.VIF_DETAILS = 'binding:vif_details'
     sriov_port[fake_profile]['pci_slot'] = 3
     mock_glob.iglob.return_value = ['file1']
     mock_os.readlink.return_value = 12
     mock_re.compile().search.return_value = re.match(r"(\d+)", "89")
     mock_os.listdir.return_value = [
         'net_enp0s2_52_54_00_12_35_02', 'net_enp0s3_52_54_00_12_35_02'
     ]
     mock_os.path.join.return_value = 'random'
     fake_file_handle = ["52:54:00:12:35:02"]
     fake_file_iter = fake_file_handle.__iter__()
     mock_open.return_value.__enter__.return_value = fake_file_iter
     expected_output = {
         'mac': '52:54:00:12:35:02',
         'pci_slot': 3,
         'vf_index': '89',
         'pf_device': 'net_enp0s3_52_54_00_12_35_02',
         'src_vlans': 20
     }
     self.assertEqual(
         expected_output,
         sriov_nic_utils.SriovNicUtils().get_sriov_port_params(sriov_port))
 def test_execute_sysfs_command_both_rem(self, mock_os, mock_neutron_utils):
     sriov_nic_utils.SriovNicUtils().execute_sysfs_command(
         'rem', {
             'pf_device': 'p2p1',
             'vf_index': '9'
         }, {'vf_index': '18'}, "4,11-13", True, "BOTH")
     self.assertEqual(2, mock_neutron_utils.execute.call_count)
 def test_get_vf_num_by_pci_address(self, mock_os, mock_re, mock_glob):
     mock_glob.iglob.return_value = ['file1']
     mock_os.readlink.return_value = 12
     mock_re.compile().search.return_value = re.match(r"(\d+)", "89")
     self.assertEqual(
         '89',
         sriov_nic_utils.SriovNicUtils().get_vf_num_by_pci_address(12))
 def test_get_mac_by_pci_address_no_content(self, mock_open, mock_os):
     mock_os.listdir.return_value = ['random1', 'random2']
     mock_os.path.join.return_value = 'random'
     fake_file_handle = []
     fake_file_iter = fake_file_handle.__iter__()
     mock_open.return_value.__enter__.return_value = fake_file_iter
     self.assertRaises(
         taas_exc.PciDeviceNotFoundById,
         sriov_nic_utils.SriovNicUtils().get_mac_by_pci_address, 12, False)
 def test_get_mac_by_pci_address(self, mock_open, mock_os):
     mock_os.listdir.return_value = ['random1', 'random2']
     mock_os.path.join.return_value = 'random'
     fake_file_handle = ["52:54:00:12:35:02"]
     fake_file_iter = fake_file_handle.__iter__()
     mock_open.return_value.__enter__.return_value = fake_file_iter
     self.assertEqual(
         "52:54:00:12:35:02",
         sriov_nic_utils.SriovNicUtils().get_mac_by_pci_address(12, False))
 def test_get_net_name_by_vf_pci_address(self, mock_open, mock_os):
     mock_os.listdir.return_value = ['enp0s3', 'enp0s2']
     mock_os.path.join.return_value = 'random'
     fake_file_handle = ["52:54:00:12:35:02"]
     fake_file_iter = fake_file_handle.__iter__()
     mock_open.return_value.__enter__.return_value = fake_file_iter
     self.assertEqual(
         'net_enp0s3_52_54_00_12_35_02',
         sriov_nic_utils.SriovNicUtils().get_net_name_by_vf_pci_address(12))
 def test_execute_sysfs_command_not_both_vf_to_vf_all_vlans_False(
         self, mock_os, mock_neutron_utils):
     cmd = [
         'i40e_sysfs_command', 'p2p1', '9', 'vlan_mirror', 'rem', '4,11-13'
     ]
     sriov_nic_utils.SriovNicUtils().execute_sysfs_command(
         'rem', {
             'pf_device': 'p2p1',
             'vf_index': '9'
         }, {'vf_index': '18'}, "4,11-13", False, "FAKE")
     mock_neutron_utils.execute.assert_called_once_with(cmd,
                                                        run_as_root=True)
 def test_execute_sysfs_command_ingress_rem(self, mock_os,
                                            mock_neutron_utils):
     sriov_nic_utils.SriovNicUtils().execute_sysfs_command(
         'rem', {
             'pf_device': 'p2p1',
             'vf_index': '9'
         }, {'vf_index': '18'}, "4,11-13", True, "IN")
     ingress_cmd = [
         'i40e_sysfs_command', 'p2p1', '18', 'ingress_mirror', 'rem', '9'
     ]
     mock_neutron_utils.execute.assert_called_once_with(ingress_cmd,
                                                        run_as_root=True)
 def test_get_sriov_port_params_no_pci_slot(self, mock_port_bindings,
                                            mock_open, mock_os, mock_re,
                                            mock_glob):
     sriov_port = copy.deepcopy(FAKE_SRIOV_PORT)
     mock_port_bindings.PROFILE = 'binding:profile'
     mock_port_bindings.VIF_DETAILS = 'binding:vif_details'
     mock_glob.iglob.return_value = ['file1']
     mock_os.readlink.return_value = 12
     mock_re.compile().search.return_value = re.match(r"(\d+)", "89")
     mock_os.listdir.return_value = ['enp0s3', 'enp0s2']
     mock_os.path.join.return_value = 'random'
     fake_file_handle = ["52:54:00:12:35:02"]
     fake_file_iter = fake_file_handle.__iter__()
     mock_open.return_value.__enter__.return_value = fake_file_iter
     self.assertIsNone(
         sriov_nic_utils.SriovNicUtils().get_sriov_port_params(sriov_port))
 def initialize(self):
     LOG.debug("Initialize routine called for Taas SRIOV NIC Switch Driver")
     self.sriov_utils = sriov_utils.SriovNicUtils()
     return
 def test_get_mac_by_pci_address_wrong_dev_path(self, mock_os):
     mock_os.listdir.return_value = ['random1', 'random2']
     mock_os.path.join.return_value = 'random'
     self.assertRaises(
         taas_exc.PciDeviceNotFoundById,
         sriov_nic_utils.SriovNicUtils().get_mac_by_pci_address, 12, False)
 def test_get_ifname_by_pci_address_no_dev_info(self, mock_os):
     mock_os.listdir.return_value = list()
     self.assertRaises(
         taas_exc.PciDeviceNotFoundById,
         sriov_nic_utils.SriovNicUtils().get_ifname_by_pci_address, 12, 9)
 def test_get_ifname_by_pci_address(self, mock_os):
     mock_os.listdir.return_value = ['random1', 'random2']
     self.assertEqual(
         sriov_nic_utils.SriovNicUtils().get_ifname_by_pci_address(
             12, False), 'random2')
 def test_get_sysfs_netdev_path_without_pf_interface(self):
     self.assertEqual(
         "/sys/bus/pci/devices/12/net",
         sriov_nic_utils.SriovNicUtils()._get_sysfs_netdev_path(12, False))
 def test_get_vf_num_by_pci_address_neg(self):
     self.assertRaises(
         taas_exc.PciDeviceNotFoundById,
         sriov_nic_utils.SriovNicUtils().get_vf_num_by_pci_address, 12)