From e0f97ef0be18cabbd937570ef12c7a7966bf1932 Mon Sep 17 00:00:00 2001 From: Abdul Haleem Date: Wed, 23 May 2018 12:06:03 +0530 Subject: [PATCH] get_slot_from_sysfs: accomodate more type of slot names in pci utils This patche takes care of Slot format for ibm and openpower machines * S0001 * Slot1 * SLOT1 * Backplane USB * U78CB.001.WZS07CU-P1-C9-T1 (should return U78CB.001.WZS07CU-P1-C9) * Onboard USB Respective unit test has also been added test_utils_pci.py Signed-off-by: Abdul Haleem Signed-off-by: Narasimhan V --- avocado/utils/pci.py | 8 ++++++-- selftests/unit/test_utils_pci.py | 25 +++++++++++++++++++++++++ 2 files changed, 31 insertions(+), 2 deletions(-) create mode 100644 selftests/unit/test_utils_pci.py diff --git a/avocado/utils/pci.py b/avocado/utils/pci.py index cefc1efa..e0729b8a 100644 --- a/avocado/utils/pci.py +++ b/avocado/utils/pci.py @@ -182,8 +182,12 @@ def get_slot_from_sysfs(full_pci_address): if not os.path.isfile("/proc/device-tree/%s/ibm,loc-code" % devspec): return slot = genio.read_file("/proc/device-tree/%s/ibm,loc-code" % devspec) - slot = re.match(r'((\w+)[.])+(\w+)-P(\d+)-C(\d+)|Slot(\d+)', slot).group() - return slot + slot_ibm = re.match(r'((\w+)[.])+(\w+)-[P(\d+)-]*C(\d+)', slot) + slot_openpower = re.match(r'(\w+)[\s]*(\w+)(\d*)', slot) + if slot_ibm: + return slot_ibm.group() + else: + return slot_openpower.group() def get_slot_list(): diff --git a/selftests/unit/test_utils_pci.py b/selftests/unit/test_utils_pci.py new file mode 100644 index 00000000..a5ce55b7 --- /dev/null +++ b/selftests/unit/test_utils_pci.py @@ -0,0 +1,25 @@ +import unittest + +try: + from unittest import mock +except ImportError: + import mock + +from avocado.utils import pci + + +class UtilsPciTest(unittest.TestCase): + + def test_get_slot_from_sysfs(self): + pcid = '0002:01:00.1' + file_values = ['S0001', 'S0001[', 'Slot2', 'SLOT1', 'Backplane USB', 'U78CB.001.WZS07CU-P1-C9-T1', 'PLX Slot1', 'Onboard USB', 'U78D5.001.CSS130E-P1-P2-P2-C1-T1'] + expected_values = ['S0001', 'S0001', 'Slot2', 'SLOT1', 'Backplane USB', 'U78CB.001.WZS07CU-P1-C9', 'PLX Slot1', 'Onboard USB', 'U78D5.001.CSS130E-P1-P2-P2-C1'] + for value, exp in zip(file_values, expected_values): + with mock.patch('os.path.isfile', return_value=True): + with mock.patch('avocado.utils.genio.read_file', + return_value=value): + self.assertEqual(pci.get_slot_from_sysfs(pcid), exp) + + +if __name__ == '__main__': + unittest.main() -- GitLab