diff --git a/hooks/pci.py b/hooks/pci.py index 0f24df8a..d52e10fa 100644 --- a/hooks/pci.py +++ b/hooks/pci.py @@ -155,6 +155,8 @@ class PCINetDevice(object): self.mac_address = None self.state = None self.sriov = False + self.sriov_totalvfs = None + self.sriov_numvfs = None self.update_attributes() def update_attributes(self): @@ -186,6 +188,7 @@ class PCINetDevice(object): 'device', 'sriov_numvfs') with open(sdevice, 'w') as sh: sh.write(str(numvfs)) + self.update_attributes() class PCINetDevices(object): diff --git a/unit_tests/test_pci.py b/unit_tests/test_pci.py index a5043b35..e8615639 100644 --- a/unit_tests/test_pci.py +++ b/unit_tests/test_pci.py @@ -178,6 +178,40 @@ class PCINetDeviceTest(CharmTestCase): self.assertEqual( pci.get_sysnet_interface('/sys/class/net/eth3'), 'eth3') + @patch('pci.get_sysnet_interfaces_and_macs') + def test_set_sriov_numvfs(self, mock_sysnet_ints): + mock_sysnet_ints.side_effect = [{ + 'interface': 'eth2', + 'mac_address': 'a8:9d:21:cf:93:fc', + 'pci_address': '0000:10:00.0', + 'state': 'up', + 'sriov': True, + 'sriov_totalvfs': 7, + 'sriov_numvfs': 0 + }], [{ + 'interface': 'eth2', + 'mac_address': 'a8:9d:21:cf:93:fc', + 'pci_address': '0000:10:00.0', + 'state': 'up', + 'sriov': True, + 'sriov_totalvfs': 7, + 'sriov_numvfs': 4 + }] + dev = pci.PCINetDevice('0000:10:00.0') + self.assertEqual('eth2', dev.interface_name) + self.assertTrue(dev.sriov) + self.assertEqual(7, dev.sriov_totalvfs) + self.assertEqual(0, dev.sriov_numvfs) + + with patch_open() as (mock_open, mock_file): + dev.set_sriov_numvfs(4) + mock_open.assert_called_with( + '/sys/class/net/eth2/device/sriov_numvfs', 'w') + mock_file.write.assert_called_with("4") + self.assertTrue(dev.sriov) + self.assertEqual(7, dev.sriov_totalvfs) + self.assertEqual(4, dev.sriov_numvfs) + class PCINetDevicesTest(CharmTestCase):