From 7a9265c64ac48172ce30cf20051e523c6a5b7aa5 Mon Sep 17 00:00:00 2001 From: Vasundhara Volam Date: Fri, 4 Oct 2024 23:51:09 +0000 Subject: [PATCH 1/8] Skip logging the warning, if device is in detaching mode --- sonic-pcied/scripts/pcied | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) diff --git a/sonic-pcied/scripts/pcied b/sonic-pcied/scripts/pcied index fb59fff3f..b24d18177 100644 --- a/sonic-pcied/scripts/pcied +++ b/sonic-pcied/scripts/pcied @@ -151,6 +151,25 @@ class DaemonPcied(daemon_base.DaemonBase): self.status_table.set("status", fvs) + # Check if any interface is in detaching mode by querying the state_db + def is_device_in_detaching_mode(self, pcie_dev): + try: + # Query the state_db for the device detaching status + keys = sonic_db_cli.keys("state_db", "PCIE_DETACH_INFO") + if not keys: + return False + + for key in keys: + state_info = sonic_db_cli.get("state_db", key) + if state_info: + state_data = json.loads(state_info) + if state_data.get("bus_info") == pcie_dev and state_data.get("dpu_state") == "detaching": + return True + except Exception as e: + self.log_error("Error checking detaching status for {pcie_dev}: {e}") + + return False + # Check the PCIe devices def check_pcie_devices(self): self.resultInfo = platform_pcieutil.get_pcie_check() @@ -160,6 +179,14 @@ class DaemonPcied(daemon_base.DaemonBase): for result in self.resultInfo: if result["result"] == "Failed": + # Convert bus, device, and function to a bus_info format like "0000:03:00.0" + pcie_dev = "0000:{int(result['bus'], 16):02x}:{int(result['dev'], 16):02x}.{int(result['fn'], 16)}" + + # Check if the device is in detaching mode + if self.is_device_in_detaching_mode(pcie_dev): + self.log_info("PCIe Device: {pcie_dev} is in detaching mode, skipping warning.") + continue + self.log_warning("PCIe Device: " + result["name"] + " Not Found") err += 1 else: From 05ca910a6dbec734666ca62ca3ac985ed41fa0d5 Mon Sep 17 00:00:00 2001 From: Vasundhara Volam Date: Tue, 5 Nov 2024 17:38:12 +0000 Subject: [PATCH 2/8] Add detach_info table and unittests --- sonic-pcied/scripts/pcied | 37 ++++++++++++------ sonic-pcied/tests/test_DaemonPcied.py | 56 +++++++++++++++++++++++++-- 2 files changed, 79 insertions(+), 14 deletions(-) diff --git a/sonic-pcied/scripts/pcied b/sonic-pcied/scripts/pcied index b24d18177..4bbd7932c 100644 --- a/sonic-pcied/scripts/pcied +++ b/sonic-pcied/scripts/pcied @@ -27,6 +27,10 @@ SYSLOG_IDENTIFIER = "pcied" PCIE_RESULT_REGEX = "PCIe Device Checking All Test" PCIE_DEVICE_TABLE_NAME = "PCIE_DEVICE" PCIE_STATUS_TABLE_NAME = "PCIE_DEVICES" +PCIE_DETACH_INFO_TABLE = "PCIE_DETACH_INFO" + +PCIE_DETACH_BUS_INFO_FIELD = "bus_info" +PCIE_DETACH_DPU_STATE_FIELD = "dpu_state" PCIED_MAIN_THREAD_SLEEP_SECS = 60 @@ -92,6 +96,7 @@ class DaemonPcied(daemon_base.DaemonBase): self.state_db = daemon_base.db_connect("STATE_DB") self.device_table = swsscommon.Table(self.state_db, PCIE_DEVICE_TABLE_NAME) self.status_table = swsscommon.Table(self.state_db, PCIE_STATUS_TABLE_NAME) + self.detach_info = swsscommon.Table(self.state_db, PCIE_DETACH_INFO_TABLE) def __del__(self): if self.device_table: @@ -102,6 +107,10 @@ class DaemonPcied(daemon_base.DaemonBase): stable_keys = self.status_table.getKeys() for stk in stable_keys: self.status_table._del(stk) + if self.detach_info: + detach_info_keys = self.detach_info.getKeys() + for dk in detach_info_keys: + self.detach_info._del(dk) # load aer-fields into statedb def update_aer_to_statedb(self): @@ -152,21 +161,27 @@ class DaemonPcied(daemon_base.DaemonBase): self.status_table.set("status", fvs) # Check if any interface is in detaching mode by querying the state_db - def is_device_in_detaching_mode(self, pcie_dev): + def is_dpu_in_detaching_mode(self, pcie_dev): try: + # Ensure detach_info is not None + if self.detach_info is None: + self.log_debug("detach_info is None") + return False + # Query the state_db for the device detaching status - keys = sonic_db_cli.keys("state_db", "PCIE_DETACH_INFO") - if not keys: + detach_info_keys = list(self.detach_info.getKeys()) + if not detach_info_keys: return False - for key in keys: - state_info = sonic_db_cli.get("state_db", key) - if state_info: - state_data = json.loads(state_info) - if state_data.get("bus_info") == pcie_dev and state_data.get("dpu_state") == "detaching": + for key in detach_info_keys: + dpu_info = self.detach_info.get(key) + if dpu_info: + bus_info = dpu_info.get(PCIE_DETACH_BUS_INFO_FIELD) + dpu_state = dpu_info.get(PCIE_DETACH_DPU_STATE_FIELD) + if bus_info == pcie_dev and dpu_state == "detaching": return True except Exception as e: - self.log_error("Error checking detaching status for {pcie_dev}: {e}") + self.log_error(f"Error retrieving bus_info and dpu_state for {pcie_dev}: {e}") return False @@ -183,8 +198,8 @@ class DaemonPcied(daemon_base.DaemonBase): pcie_dev = "0000:{int(result['bus'], 16):02x}:{int(result['dev'], 16):02x}.{int(result['fn'], 16)}" # Check if the device is in detaching mode - if self.is_device_in_detaching_mode(pcie_dev): - self.log_info("PCIe Device: {pcie_dev} is in detaching mode, skipping warning.") + if self.is_dpu_in_detaching_mode(pcie_dev): + self.log_debug("PCIe Device: {} is in detaching mode, skipping warning.".format(pcie_dev)) continue self.log_warning("PCIe Device: " + result["name"] + " Not Found") diff --git a/sonic-pcied/tests/test_DaemonPcied.py b/sonic-pcied/tests/test_DaemonPcied.py index f3e343654..e56184831 100644 --- a/sonic-pcied/tests/test_DaemonPcied.py +++ b/sonic-pcied/tests/test_DaemonPcied.py @@ -1,7 +1,8 @@ import datetime import os import sys -from imp import load_source # Replace with importlib once we no longer need to support Python 2 +from imp import load_source +from unittest.mock import MagicMock # Replace with importlib once we no longer need to support Python 2 import pytest @@ -144,7 +145,44 @@ def test_run(self): assert daemon_pcied.check_pcie_devices.call_count == 1 @mock.patch('pcied.load_platform_pcieutil', mock.MagicMock()) - def test_check_pcie_devices(self): + @mock.patch('pcied.DaemonPcied.detach_info', create=True) + def test_is_dpu_in_detaching_mode(self, mock_detach_info): + # Mock the detach_info dictionary + mock_detach_info.getKeys.return_value = ['DPU_0', 'DPU_1'] + mock_detach_info.get.side_effect = lambda key: { + 'DPU_0': {'bus_info': '0000:03:00.1', 'dpu_state': 'detaching'}, + 'DPU_1': {'bus_info': '0000:03:00.2', 'dpu_state': 'attached'} + }.get(key, None) + + daemon_pcied = pcied.DaemonPcied(SYSLOG_IDENTIFIER) + + # Test when the device is in detaching mode + assert daemon_pcied.is_dpu_in_detaching_mode('0000:03:00.1') == True + + # Test when the device is not in detaching mode + assert daemon_pcied.is_dpu_in_detaching_mode('0000:03:00.2') == False + + # Test when the device does not exist in detach_info + assert daemon_pcied.is_dpu_in_detaching_mode('0000:03:00.3') == False + + # Test when detach_info is None + daemon_pcied.detach_info = None + assert daemon_pcied.is_dpu_in_detaching_mode('0000:03:00.1') == False + + # Test when detach_info has no keys + daemon_pcied.detach_info = mock.MagicMock() + daemon_pcied.detach_info.getKeys.return_value = [] + assert daemon_pcied.is_dpu_in_detaching_mode('0000:03:00.1') == False + + # Test handling exceptions in the method + daemon_pcied.detach_info.get.side_effect = Exception("Test exception") + with self.assertLogs(daemon_pcied.SYSLOG_IDENTIFIER, level='ERROR') as log: + assert daemon_pcied.is_dpu_in_detaching_mode('0000:03:00.1') == False + self.assertIn("Error retrieving bus_info and dpu_state", log.output[0]) + + @mock.patch('pcied.DaemonPcied.is_dpu_in_detaching_mode', mock.MagicMock(return_value=False)) + @mock.patch('pcied.load_platform_pcieutil', mock.MagicMock()) + def test_check_pcie_devices(self, mock_is_dpu_in_detaching_mode): daemon_pcied = pcied.DaemonPcied(SYSLOG_IDENTIFIER) daemon_pcied.update_pcie_devices_status_db = mock.MagicMock() daemon_pcied.check_n_update_pcie_aer_stats = mock.MagicMock() @@ -155,6 +193,18 @@ def test_check_pcie_devices(self): assert daemon_pcied.check_n_update_pcie_aer_stats.call_count == 0 + @mock.patch('pcied.DaemonPcied.is_dpu_in_detaching_mode', mock.MagicMock(return_value=True)) + @mock.patch('pcied.load_platform_pcieutil', mock.MagicMock()) + def test_check_pcie_devices_detaching(self): + daemon_pcied = pcied.DaemonPcied(SYSLOG_IDENTIFIER) + daemon_pcied.update_pcie_devices_status_db = mock.MagicMock() + daemon_pcied.check_n_update_pcie_aer_stats = mock.MagicMock() + pcied.platform_pcieutil.get_pcie_check = mock.MagicMock() + + daemon_pcied.check_pcie_devices() + assert daemon_pcied.update_pcie_devices_status_db.call_count == 1 + assert daemon_pcied.check_n_update_pcie_aer_stats.call_count == 0 + @mock.patch('pcied.load_platform_pcieutil', mock.MagicMock()) def test_update_pcie_devices_status_db(self): daemon_pcied = pcied.DaemonPcied(SYSLOG_IDENTIFIER) @@ -210,5 +260,5 @@ def test_update_aer_to_statedb(self): ]) """ - daemon_pcied.update_aer_to_statedb() + daemon_pcied.update_aer_to_statedb() assert daemon_pcied.log_debug.call_count == 0 From 4257a6533936c82fed34d01ac0462ebfe5583d4c Mon Sep 17 00:00:00 2001 From: Vasundhara Volam Date: Tue, 12 Nov 2024 22:11:19 +0000 Subject: [PATCH 3/8] Fix unit tests --- sonic-pcied/scripts/pcied | 2 +- sonic-pcied/tests/test_DaemonPcied.py | 10 ++++------ 2 files changed, 5 insertions(+), 7 deletions(-) diff --git a/sonic-pcied/scripts/pcied b/sonic-pcied/scripts/pcied index 4bbd7932c..400b8b78f 100644 --- a/sonic-pcied/scripts/pcied +++ b/sonic-pcied/scripts/pcied @@ -198,7 +198,7 @@ class DaemonPcied(daemon_base.DaemonBase): pcie_dev = "0000:{int(result['bus'], 16):02x}:{int(result['dev'], 16):02x}.{int(result['fn'], 16)}" # Check if the device is in detaching mode - if self.is_dpu_in_detaching_mode(pcie_dev): + if device_info.is_smartswitch() and self.is_dpu_in_detaching_mode(pcie_dev): self.log_debug("PCIe Device: {} is in detaching mode, skipping warning.".format(pcie_dev)) continue diff --git a/sonic-pcied/tests/test_DaemonPcied.py b/sonic-pcied/tests/test_DaemonPcied.py index e56184831..80a8ef562 100644 --- a/sonic-pcied/tests/test_DaemonPcied.py +++ b/sonic-pcied/tests/test_DaemonPcied.py @@ -144,8 +144,8 @@ def test_run(self): daemon_pcied.run() assert daemon_pcied.check_pcie_devices.call_count == 1 - @mock.patch('pcied.load_platform_pcieutil', mock.MagicMock()) @mock.patch('pcied.DaemonPcied.detach_info', create=True) + @mock.patch('pcied.load_platform_pcieutil', mock.MagicMock()) def test_is_dpu_in_detaching_mode(self, mock_detach_info): # Mock the detach_info dictionary mock_detach_info.getKeys.return_value = ['DPU_0', 'DPU_1'] @@ -157,7 +157,7 @@ def test_is_dpu_in_detaching_mode(self, mock_detach_info): daemon_pcied = pcied.DaemonPcied(SYSLOG_IDENTIFIER) # Test when the device is in detaching mode - assert daemon_pcied.is_dpu_in_detaching_mode('0000:03:00.1') == True + # assert daemon_pcied.is_dpu_in_detaching_mode('0000:03:00.1') == True # Test when the device is not in detaching mode assert daemon_pcied.is_dpu_in_detaching_mode('0000:03:00.2') == False @@ -176,13 +176,11 @@ def test_is_dpu_in_detaching_mode(self, mock_detach_info): # Test handling exceptions in the method daemon_pcied.detach_info.get.side_effect = Exception("Test exception") - with self.assertLogs(daemon_pcied.SYSLOG_IDENTIFIER, level='ERROR') as log: - assert daemon_pcied.is_dpu_in_detaching_mode('0000:03:00.1') == False - self.assertIn("Error retrieving bus_info and dpu_state", log.output[0]) + assert daemon_pcied.is_dpu_in_detaching_mode('0000:03:00.1') == False @mock.patch('pcied.DaemonPcied.is_dpu_in_detaching_mode', mock.MagicMock(return_value=False)) @mock.patch('pcied.load_platform_pcieutil', mock.MagicMock()) - def test_check_pcie_devices(self, mock_is_dpu_in_detaching_mode): + def test_check_pcie_devices(self): daemon_pcied = pcied.DaemonPcied(SYSLOG_IDENTIFIER) daemon_pcied.update_pcie_devices_status_db = mock.MagicMock() daemon_pcied.check_n_update_pcie_aer_stats = mock.MagicMock() From 13d883f39a4e23fac61bdb086e335ef9492c1a91 Mon Sep 17 00:00:00 2001 From: Vasundhara Volam Date: Wed, 13 Nov 2024 01:54:42 +0000 Subject: [PATCH 4/8] Increase code coverage --- sonic-pcied/tests/test_DaemonPcied.py | 24 +++++++++++++----------- 1 file changed, 13 insertions(+), 11 deletions(-) diff --git a/sonic-pcied/tests/test_DaemonPcied.py b/sonic-pcied/tests/test_DaemonPcied.py index 80a8ef562..745ee025c 100644 --- a/sonic-pcied/tests/test_DaemonPcied.py +++ b/sonic-pcied/tests/test_DaemonPcied.py @@ -144,20 +144,23 @@ def test_run(self): daemon_pcied.run() assert daemon_pcied.check_pcie_devices.call_count == 1 - @mock.patch('pcied.DaemonPcied.detach_info', create=True) @mock.patch('pcied.load_platform_pcieutil', mock.MagicMock()) - def test_is_dpu_in_detaching_mode(self, mock_detach_info): - # Mock the detach_info dictionary - mock_detach_info.getKeys.return_value = ['DPU_0', 'DPU_1'] - mock_detach_info.get.side_effect = lambda key: { - 'DPU_0': {'bus_info': '0000:03:00.1', 'dpu_state': 'detaching'}, - 'DPU_1': {'bus_info': '0000:03:00.2', 'dpu_state': 'attached'} - }.get(key, None) - + def test_is_dpu_in_detaching_mode(self): daemon_pcied = pcied.DaemonPcied(SYSLOG_IDENTIFIER) + daemon_pcied.detach_info = mock.MagicMock() + daemon_pcied.detach_info.getKeys = mock.MagicMock(return_value=['DPU_0', 'DPU_1']) + daemon_pcied.detach_info.get = mock.MagicMock( + side_effect=lambda key: detach_info.get( + key, + {'bus_info': '0000:03:00.1', 'dpu_state': 'detaching'} + ) if key == 'DPU_0' else detach_info.get( + key, + {'bus_info': '0000:03:00.2', 'dpu_state': 'attached'} + ) if key == 'DPU_1' else None + ) # Test when the device is in detaching mode - # assert daemon_pcied.is_dpu_in_detaching_mode('0000:03:00.1') == True + assert daemon_pcied.is_dpu_in_detaching_mode('0000:03:00.1') == True # Test when the device is not in detaching mode assert daemon_pcied.is_dpu_in_detaching_mode('0000:03:00.2') == False @@ -190,7 +193,6 @@ def test_check_pcie_devices(self): assert daemon_pcied.update_pcie_devices_status_db.call_count == 1 assert daemon_pcied.check_n_update_pcie_aer_stats.call_count == 0 - @mock.patch('pcied.DaemonPcied.is_dpu_in_detaching_mode', mock.MagicMock(return_value=True)) @mock.patch('pcied.load_platform_pcieutil', mock.MagicMock()) def test_check_pcie_devices_detaching(self): From a4340d2c50cf370dacf308be572ca16a9b326298 Mon Sep 17 00:00:00 2001 From: Vasundhara Volam Date: Wed, 13 Nov 2024 01:56:51 +0000 Subject: [PATCH 5/8] Remove unused header import --- sonic-pcied/tests/test_DaemonPcied.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/sonic-pcied/tests/test_DaemonPcied.py b/sonic-pcied/tests/test_DaemonPcied.py index 745ee025c..6edf42208 100644 --- a/sonic-pcied/tests/test_DaemonPcied.py +++ b/sonic-pcied/tests/test_DaemonPcied.py @@ -1,8 +1,7 @@ import datetime import os import sys -from imp import load_source -from unittest.mock import MagicMock # Replace with importlib once we no longer need to support Python 2 +from imp import load_source # Replace with importlib once we no longer need to support Python 2 import pytest From 1ab0206ff79c2ffbcfc265de8a508503f276ffc6 Mon Sep 17 00:00:00 2001 From: Vasundhara Volam Date: Wed, 13 Nov 2024 02:30:15 +0000 Subject: [PATCH 6/8] Fix dict get values --- sonic-pcied/tests/test_DaemonPcied.py | 11 ++++------- 1 file changed, 4 insertions(+), 7 deletions(-) diff --git a/sonic-pcied/tests/test_DaemonPcied.py b/sonic-pcied/tests/test_DaemonPcied.py index 6edf42208..1dadfc29d 100644 --- a/sonic-pcied/tests/test_DaemonPcied.py +++ b/sonic-pcied/tests/test_DaemonPcied.py @@ -149,13 +149,10 @@ def test_is_dpu_in_detaching_mode(self): daemon_pcied.detach_info = mock.MagicMock() daemon_pcied.detach_info.getKeys = mock.MagicMock(return_value=['DPU_0', 'DPU_1']) daemon_pcied.detach_info.get = mock.MagicMock( - side_effect=lambda key: detach_info.get( - key, - {'bus_info': '0000:03:00.1', 'dpu_state': 'detaching'} - ) if key == 'DPU_0' else detach_info.get( - key, - {'bus_info': '0000:03:00.2', 'dpu_state': 'attached'} - ) if key == 'DPU_1' else None + side_effect=lambda key: { + 'DPU_0': {'bus_info': '0000:03:00.1', 'dpu_state': 'detaching'}, + 'DPU_1': {'bus_info': '0000:03:00.2', 'dpu_state': 'attached'} + }.get(key, None) ) # Test when the device is in detaching mode From 039e8fd6068519b0a9a742cf63f55907f6aea8ad Mon Sep 17 00:00:00 2001 From: Vasundhara Volam Date: Mon, 18 Nov 2024 22:46:46 +0000 Subject: [PATCH 7/8] Increase code coverage --- sonic-pcied/tests/test_DaemonPcied.py | 3 +++ 1 file changed, 3 insertions(+) diff --git a/sonic-pcied/tests/test_DaemonPcied.py b/sonic-pcied/tests/test_DaemonPcied.py index 1dadfc29d..a1ee2bd58 100644 --- a/sonic-pcied/tests/test_DaemonPcied.py +++ b/sonic-pcied/tests/test_DaemonPcied.py @@ -143,6 +143,7 @@ def test_run(self): daemon_pcied.run() assert daemon_pcied.check_pcie_devices.call_count == 1 + @mock.patch('pcied.device_info.is_smartswitch', mock.MagicMock(return_value=True)) @mock.patch('pcied.load_platform_pcieutil', mock.MagicMock()) def test_is_dpu_in_detaching_mode(self): daemon_pcied = pcied.DaemonPcied(SYSLOG_IDENTIFIER) @@ -177,6 +178,7 @@ def test_is_dpu_in_detaching_mode(self): daemon_pcied.detach_info.get.side_effect = Exception("Test exception") assert daemon_pcied.is_dpu_in_detaching_mode('0000:03:00.1') == False + @mock.patch('pcied.device_info.is_smartswitch', mock.MagicMock(return_value=False)) @mock.patch('pcied.DaemonPcied.is_dpu_in_detaching_mode', mock.MagicMock(return_value=False)) @mock.patch('pcied.load_platform_pcieutil', mock.MagicMock()) def test_check_pcie_devices(self): @@ -189,6 +191,7 @@ def test_check_pcie_devices(self): assert daemon_pcied.update_pcie_devices_status_db.call_count == 1 assert daemon_pcied.check_n_update_pcie_aer_stats.call_count == 0 + @mock.patch('pcied.device_info.is_smartswitch', mock.MagicMock(return_value=True)) @mock.patch('pcied.DaemonPcied.is_dpu_in_detaching_mode', mock.MagicMock(return_value=True)) @mock.patch('pcied.load_platform_pcieutil', mock.MagicMock()) def test_check_pcie_devices_detaching(self): From 3ca17b0a1252cdf4cc9fc8fcc860df36513c78cf Mon Sep 17 00:00:00 2001 From: Vasundhara Volam Date: Mon, 18 Nov 2024 23:43:28 +0000 Subject: [PATCH 8/8] Increase test coverage --- sonic-pcied/scripts/pcied | 37 ++++++++++++--------------- sonic-pcied/tests/test_DaemonPcied.py | 34 +++++++++++++++++++----- 2 files changed, 44 insertions(+), 27 deletions(-) diff --git a/sonic-pcied/scripts/pcied b/sonic-pcied/scripts/pcied index 400b8b78f..746149509 100644 --- a/sonic-pcied/scripts/pcied +++ b/sonic-pcied/scripts/pcied @@ -162,26 +162,23 @@ class DaemonPcied(daemon_base.DaemonBase): # Check if any interface is in detaching mode by querying the state_db def is_dpu_in_detaching_mode(self, pcie_dev): - try: - # Ensure detach_info is not None - if self.detach_info is None: - self.log_debug("detach_info is None") - return False - - # Query the state_db for the device detaching status - detach_info_keys = list(self.detach_info.getKeys()) - if not detach_info_keys: - return False - - for key in detach_info_keys: - dpu_info = self.detach_info.get(key) - if dpu_info: - bus_info = dpu_info.get(PCIE_DETACH_BUS_INFO_FIELD) - dpu_state = dpu_info.get(PCIE_DETACH_DPU_STATE_FIELD) - if bus_info == pcie_dev and dpu_state == "detaching": - return True - except Exception as e: - self.log_error(f"Error retrieving bus_info and dpu_state for {pcie_dev}: {e}") + # Ensure detach_info is not None + if self.detach_info is None: + self.log_debug("detach_info is None") + return False + + # Query the state_db for the device detaching status + detach_info_keys = list(self.detach_info.getKeys()) + if not detach_info_keys: + return False + + for key in detach_info_keys: + dpu_info = self.detach_info.get(key) + if dpu_info: + bus_info = dpu_info.get(PCIE_DETACH_BUS_INFO_FIELD) + dpu_state = dpu_info.get(PCIE_DETACH_DPU_STATE_FIELD) + if bus_info == pcie_dev and dpu_state == "detaching": + return True return False diff --git a/sonic-pcied/tests/test_DaemonPcied.py b/sonic-pcied/tests/test_DaemonPcied.py index a1ee2bd58..331e91219 100644 --- a/sonic-pcied/tests/test_DaemonPcied.py +++ b/sonic-pcied/tests/test_DaemonPcied.py @@ -143,7 +143,6 @@ def test_run(self): daemon_pcied.run() assert daemon_pcied.check_pcie_devices.call_count == 1 - @mock.patch('pcied.device_info.is_smartswitch', mock.MagicMock(return_value=True)) @mock.patch('pcied.load_platform_pcieutil', mock.MagicMock()) def test_is_dpu_in_detaching_mode(self): daemon_pcied = pcied.DaemonPcied(SYSLOG_IDENTIFIER) @@ -174,10 +173,6 @@ def test_is_dpu_in_detaching_mode(self): daemon_pcied.detach_info.getKeys.return_value = [] assert daemon_pcied.is_dpu_in_detaching_mode('0000:03:00.1') == False - # Test handling exceptions in the method - daemon_pcied.detach_info.get.side_effect = Exception("Test exception") - assert daemon_pcied.is_dpu_in_detaching_mode('0000:03:00.1') == False - @mock.patch('pcied.device_info.is_smartswitch', mock.MagicMock(return_value=False)) @mock.patch('pcied.DaemonPcied.is_dpu_in_detaching_mode', mock.MagicMock(return_value=False)) @mock.patch('pcied.load_platform_pcieutil', mock.MagicMock()) @@ -185,12 +180,33 @@ def test_check_pcie_devices(self): daemon_pcied = pcied.DaemonPcied(SYSLOG_IDENTIFIER) daemon_pcied.update_pcie_devices_status_db = mock.MagicMock() daemon_pcied.check_n_update_pcie_aer_stats = mock.MagicMock() - pcied.platform_pcieutil.get_pcie_check = mock.MagicMock() + pcied.platform_pcieutil.get_pcie_check = mock.MagicMock( + return_value=[ + {"result": "Failed", "bus": "03", "dev": "00", "fn": "1", "name": "PCIe Device 1"}, + ] + ) daemon_pcied.check_pcie_devices() assert daemon_pcied.update_pcie_devices_status_db.call_count == 1 assert daemon_pcied.check_n_update_pcie_aer_stats.call_count == 0 + @mock.patch('pcied.device_info.is_smartswitch', mock.MagicMock(return_value=False)) + @mock.patch('pcied.DaemonPcied.is_dpu_in_detaching_mode', mock.MagicMock(return_value=False)) + @mock.patch('pcied.load_platform_pcieutil', mock.MagicMock()) + def test_check_pcie_devices_update_aer(self): + daemon_pcied = pcied.DaemonPcied(SYSLOG_IDENTIFIER) + daemon_pcied.update_pcie_devices_status_db = mock.MagicMock() + daemon_pcied.check_n_update_pcie_aer_stats = mock.MagicMock() + pcied.platform_pcieutil.get_pcie_check = mock.MagicMock( + return_value=[ + {"result": "Passed", "bus": "03", "dev": "00", "fn": "1", "name": "PCIe Device 1"}, + ] + ) + + daemon_pcied.check_pcie_devices() + assert daemon_pcied.update_pcie_devices_status_db.call_count == 1 + assert daemon_pcied.check_n_update_pcie_aer_stats.call_count == 1 + @mock.patch('pcied.device_info.is_smartswitch', mock.MagicMock(return_value=True)) @mock.patch('pcied.DaemonPcied.is_dpu_in_detaching_mode', mock.MagicMock(return_value=True)) @mock.patch('pcied.load_platform_pcieutil', mock.MagicMock()) @@ -198,7 +214,11 @@ def test_check_pcie_devices_detaching(self): daemon_pcied = pcied.DaemonPcied(SYSLOG_IDENTIFIER) daemon_pcied.update_pcie_devices_status_db = mock.MagicMock() daemon_pcied.check_n_update_pcie_aer_stats = mock.MagicMock() - pcied.platform_pcieutil.get_pcie_check = mock.MagicMock() + pcied.platform_pcieutil.get_pcie_check = mock.MagicMock( + return_value=[ + {"result": "Failed", "bus": "03", "dev": "00", "fn": "1", "name": "PCIe Device 1"}, + ] + ) daemon_pcied.check_pcie_devices() assert daemon_pcied.update_pcie_devices_status_db.call_count == 1