def test_get_vpe_cli_out(self): self.patch_object(pci.PCINetDevice, 'update_attributes') self.patch_object(pci, 'subprocess') self.subprocess.check_output.side_effect = \ mocked_subprocess() dev = pci.PCINetDevice('0000:07:00.0') self.assertTrue('local0' in dev.get_vpe_cli_out())
def test_pci_rescan(self): self.patch_object(pci.PCINetDevice, 'update_attributes') self.patch_object(pci, 'subprocess') dev = pci.PCINetDevice('0000:07:00.0') with utils.patch_open() as (_open, _file): dev.pci_rescan() _open.assert_called_with('/sys/bus/pci/rescan', 'w') _file.write.assert_called_with('1')
def test_extract_pci_addr_from_vpe_interface(self): self.patch_object(pci.PCINetDevice, 'update_attributes') dev = pci.PCINetDevice('0000:07:00.0') self.assertEqual(dev.extract_pci_addr_from_vpe_interface( 'TenGigabitEthernet1/1/1'), '0000:01:01.1') self.assertEqual(dev.extract_pci_addr_from_vpe_interface( 'TenGigabitEtherneta/0/0'), '0000:0a:00.0') self.assertEqual(dev.extract_pci_addr_from_vpe_interface( 'GigabitEthernet0/2/0'), '0000:00:02.0')
def test_update_attributes(self): self.patch_object(pci.PCINetDevice, '__init__') self.patch_object(pci.PCINetDevice, 'loaded_kmod') self.patch_object(pci.PCINetDevice, 'update_modalias_kmod') self.patch_object(pci.PCINetDevice, 'update_interface_info') a = pci.PCINetDevice('pciaddr') a.update_attributes() self.update_modalias_kmod.assert_called_once_with() self.update_interface_info.assert_called_once_with()
def test_get_vpe_interfaces_and_macs_invalid_cli(self): self.patch_object(pci.PCINetDevice, 'get_vpe_cli_out') self.patch_object(pci.PCINetDevice, 'update_attributes') self.patch_object(pci, 'subprocess') self.subprocess.check_output.side_effect = \ mocked_subprocess() dev = pci.PCINetDevice('0000:07:00.0') self.get_vpe_cli_out.return_value = pci_responses.CONFD_CLI_NOLOCAL with self.assertRaises(pci.VPECLIException): dev.get_vpe_interfaces_and_macs()
def test_bind(self): self.patch_object(pci.PCINetDevice, 'pci_rescan') self.patch_object(pci.PCINetDevice, 'update_attributes') dev = pci.PCINetDevice('0000:07:00.0') with utils.patch_open() as (_open, _file): dev.bind('enic') _open.assert_called_with('/sys/bus/pci/drivers/enic/bind', 'w') _file.write.assert_called_with('0000:07:00.0') self.pci_rescan.assert_called_with() self.update_attributes.assert_called_with()
def test_get_sysnet_device_state(self): self.patch_object(pci.PCINetDevice, 'update_attributes') device = pci.PCINetDevice('0000:10:00.1') with utils.patch_open() as (_open, _file): super_fh = mocked_filehandle() _file.readlines = mock.MagicMock() _open.side_effect = super_fh._setfilename _file.read.side_effect = super_fh._getfilecontents_read state = device.get_sysnet_device_state('/sys/class/net/eth3') self.assertEqual(state, 'down')
def test_unbind(self): self.patch_object(pci.PCINetDevice, 'pci_rescan') self.patch_object(pci.PCINetDevice, 'update_attributes') self.patch_object(pci.PCINetDevice, 'loaded_kmod', new='igb_uio') dev = pci.PCINetDevice('0000:07:00.0') with utils.patch_open() as (_open, _file): dev.unbind() _open.assert_called_with('/sys/bus/pci/drivers/igb_uio/unbind', 'w') _file.write.assert_called_with('0000:07:00.0') self.pci_rescan.assert_called_with() self.update_attributes.assert_called_with()
def test_update_modalias_kmod(self): self.patch_object(pci.PCINetDevice, 'update_attributes') self.patch_object(pci, 'subprocess') device = pci.PCINetDevice('0000:07:00.0') self.subprocess.check_output.side_effect = mocked_subprocess() with utils.patch_open() as (_open, _file): super_fh = mocked_filehandle() _file.readlines = mock.MagicMock() _open.side_effect = super_fh._setfilename _file.read.side_effect = super_fh._getfilecontents_read _file.readlines.side_effect = super_fh._getfilecontents_readlines device.update_modalias_kmod() self.assertEqual(device.modalias_kmod, 'enic')
def test_update_interface_info_vpe_orphan(self): self.patch_object(pci.PCINetDevice, 'update_attributes') self.patch_object(pci.PCINetDevice, 'get_vpe_interfaces_and_macs') self.get_vpe_interfaces_and_macs.return_value = [ { 'interface': 'TenGigabitEthernet6/0/0', 'macAddress': '84:b8:02:2a:5f:c3', 'pci_address': '0000:06:00.0'}] dev = pci.PCINetDevice('0000:07:00.0') dev.update_interface_info_vpe() self.assertEqual(None, dev.interface_name) self.assertEqual(None, dev.mac_address) self.assertEqual(None, dev.state)
def test_update_interface_info_call_vpeinfo(self): self.patch_object(pci.PCINetDevice, 'update_interface_info_eth') self.patch_object(pci.PCINetDevice, 'update_interface_info_vpe') self.patch_object(pci.PCINetDevice, 'update_attributes') self.patch_object(pci.PCINetDevice, 'get_kernel_name') self.patch_object(pci.PCINetDevice, 'loaded_kmod', new='igb_uio') self.patch_object(pci, 'subprocess') self.get_kernel_name.return_value = '3.13.0-77-generic' self.subprocess.check_output.side_effect = \ mocked_subprocess() dev6 = pci.PCINetDevice('0000:06:00.0') dev6.update_interface_info() self.update_interface_info_vpe.assert_called_with() self.assertFalse(self.update_interface_info_eth.called)
def test_test_update_interface_info_orphan(self): self.patch_object(pci.PCINetDevice, 'update_interface_info_eth') self.patch_object(pci.PCINetDevice, 'update_interface_info_vpe') self.patch_object(pci.PCINetDevice, 'update_attributes') self.patch_object(pci.PCINetDevice, 'get_kernel_name') self.patch_object(pci, 'subprocess') self.subprocess.check_output.side_effect = \ mocked_subprocess( subproc_map=pci_responses.NET_SETUP_ORPHAN) dev = pci.PCINetDevice('0000:07:00.0') dev.update_interface_info() self.assertFalse(self.update_interface_info_vpe.called) self.assertFalse(self.update_interface_info_eth.called) self.assertEqual(dev.interface_name, None) self.assertEqual(dev.mac_address, None)
def test_get_vpe_interfaces_and_macs_invmac(self): self.patch_object(pci.PCINetDevice, 'get_vpe_cli_out') self.patch_object(pci.PCINetDevice, 'update_attributes') self.patch_object(pci, 'subprocess') self.subprocess.check_output.side_effect = \ mocked_subprocess() dev = pci.PCINetDevice('0000:07:00.0') self.get_vpe_cli_out.return_value = pci_responses.CONFD_CLI_INVMAC vpe_devs = dev.get_vpe_interfaces_and_macs() expect = [ { 'interface': 'TenGigabitEthernet7/0/0', 'macAddress': '84:b8:02:2a:5f:c4', 'pci_address': '0000:07:00.0' }, ] self.assertEqual(vpe_devs, expect)
def test_update_interface_info_eth(self): self.patch_object(pci.PCINetDevice, 'update_attributes') self.patch_object(pci.PCINetDevice, 'get_sysnet_interfaces_and_macs') dev = pci.PCINetDevice('0000:10:00.0') self.get_sysnet_interfaces_and_macs.return_value = [{ 'interface': 'eth2', 'macAddress': 'a8:9d:21:cf:93:fc', 'pci_address': '0000:10:00.0', 'state': 'up' }, { 'interface': 'eth3', 'macAddress': 'a8:9d:21:cf:93:fd', 'pci_address': '0000:10:00.1', 'state': 'down' }] dev.update_interface_info_eth() self.assertEqual(dev.interface_name, 'eth2')
def test_get_sysnet_interfaces_and_macs_virtio(self): self.patch_object(pci.glob, 'glob') self.patch_object(pci.os.path, 'islink') self.patch_object(pci.os.path, 'realpath') self.patch_object(pci.PCINetDevice, 'get_sysnet_device_state') self.patch_object(pci.PCINetDevice, 'get_sysnet_mac') self.patch_object(pci.PCINetDevice, 'get_sysnet_interface') self.patch_object(pci.PCINetDevice, 'update_attributes') dev = pci.PCINetDevice('0000:06:00.0') self.glob.return_value = ['/sys/class/net/eth2'] self.get_sysnet_interface.return_value = 'eth2' self.get_sysnet_mac.return_value = 'a8:9d:21:cf:93:fc' self.get_sysnet_device_state.return_value = 'up' self.realpath.return_value = ('/sys/devices/pci0000:00/0000:00:07.0/' 'virtio5') self.islink.return_value = True expect = { 'interface': 'eth2', 'macAddress': 'a8:9d:21:cf:93:fc', 'pci_address': '0000:00:07.0', 'state': 'up', } self.assertEqual(dev.get_sysnet_interfaces_and_macs(), [expect])
def test_loaded_kmod(self): self.patch_object(pci.PCINetDevice, 'update_attributes') self.patch_object(pci, 'subprocess') self.subprocess.check_output.side_effect = mocked_subprocess() device = pci.PCINetDevice('0000:06:00.0') self.assertEqual(device.loaded_kmod, 'igb_uio')
def test_get_kernel_name(self): self.patch_object(pci.PCINetDevice, 'update_attributes') self.patch_object(pci, 'subprocess') dev = pci.PCINetDevice('0000:07:00.0') self.subprocess.check_output.return_value = '3.13.0-55-generic' self.assertEqual(dev.get_kernel_name(), '3.13.0-55-generic')
def test_init(self): self.patch_object(pci.PCINetDevice, 'update_attributes') a = pci.PCINetDevice('pciaddr') self.update_attributes.assert_called_once_with() self.assertEqual(a.pci_address, 'pciaddr')
def test_get_sysnet_interface(self): self.patch_object(pci.PCINetDevice, 'update_attributes') device = pci.PCINetDevice('0000:10:00.1') self.assertEqual(device.get_sysnet_interface('/sys/class/net/eth3'), 'eth3')