From 3957a469594eeaaaa65a12e1b694c516e6269542 Mon Sep 17 00:00:00 2001 From: Sahid Orentino Ferdjaoui Date: Thu, 28 Feb 2019 08:51:14 +0000 Subject: [PATCH] pci: fix missing update attributes We should update state of PCIDevDevice after to have updated its numvfs. Related-to: #1817079 Change-Id: I2c07c4376cca5afe9fe8e636a1be051c5f63367a Signed-off-by: Sahid Orentino Ferdjaoui --- hooks/pci.py | 3 +++ unit_tests/test_pci.py | 34 ++++++++++++++++++++++++++++++++++ 2 files changed, 37 insertions(+) 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):