Skip to content

Commit cd8c4a7

Browse files
authored
Fix AttributeError triggered by use of non-existent log_critical method in pcied (#651)
* Fix AttributeError triggered by use of non-existent log_critical method in pcied * Adding unit test for redis DB connection failure path and fix incorrect table variable initialization
1 parent 7b347c6 commit cd8c4a7

File tree

3 files changed

+30
-7
lines changed

3 files changed

+30
-7
lines changed

sonic-pcied/scripts/pcied

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ def load_platform_pcieutil():
5858
except ImportError as e:
5959
log.log_error("Failed to load default PcieUtil module. Error : {}".format(str(e)), True)
6060
if _platform_pcieutil is None:
61-
log.log_critical("Failed to load any PCIe utility module. Exiting...", True)
61+
log.log_error("Failed to load any PCIe utility module. Exiting...", True)
6262
raise RuntimeError("Unable to load PCIe utility module.")
6363
return _platform_pcieutil
6464

@@ -84,7 +84,7 @@ class DaemonPcied(daemon_base.DaemonBase):
8484
self.stop_event = threading.Event()
8585
self.state_db = None
8686
self.device_table = None
87-
self.table = None
87+
self.status_table = None
8888
self.resultInfo = []
8989
self.device_name = None
9090
self.aer_stats = {}
@@ -102,7 +102,7 @@ class DaemonPcied(daemon_base.DaemonBase):
102102
self.status_table = swsscommon.Table(self.state_db, PCIE_STATUS_TABLE_NAME)
103103
self.detach_info = swsscommon.Table(self.state_db, PCIE_DETACH_INFO_TABLE)
104104
except Exception as e:
105-
log.log_critical("Failed to connect to STATE_DB or create table. Error: {}".format(str(e)), True)
105+
log.log_error("Failed to connect to STATE_DB or create table. Error: {}".format(str(e)), True)
106106
sys.exit(PCIEUTIL_CONF_FILE_ERROR)
107107

108108
def __del__(self):

sonic-pcied/tests/test_DaemonPcied.py

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -397,3 +397,29 @@ def test_update_aer_to_statedb(self):
397397
daemon_pcied.log_error.assert_called_once_with(
398398
"Exception while updating AER attributes to STATE_DB for PCIe Device 1: Test Exception"
399399
)
400+
401+
402+
@mock.patch('pcied.load_platform_pcieutil', mock.MagicMock())
403+
@mock.patch('pcied.daemon_base.db_connect', mock.MagicMock())
404+
@mock.patch('pcied.sys.exit')
405+
@mock.patch('pcied.log')
406+
def test_init_db_connection_failure(self, mock_log, mock_exit):
407+
# Case 1 : Normal Execution path; Verify error was not logged and exit was not called
408+
pcied.DaemonPcied(SYSLOG_IDENTIFIER)
409+
mock_log.log_error.assert_not_called()
410+
mock_exit.assert_not_called()
411+
412+
# Reset mock objects
413+
mock_log.reset_mock()
414+
mock_exit.reset_mock()
415+
416+
# Case 2 : Test exception during Redis connection or table creation error and verify error was logged and exit was called with correct error code
417+
with mock.patch('pcied.swsscommon.Table', side_effect=Exception('Test Redis DB Exception')):
418+
pcied.DaemonPcied(SYSLOG_IDENTIFIER)
419+
420+
mock_log.log_error.assert_called_once_with(
421+
'Failed to connect to STATE_DB or create table. Error: Test Redis DB Exception',
422+
True
423+
)
424+
425+
mock_exit.assert_called_once_with(pcied.PCIEUTIL_CONF_FILE_ERROR)

sonic-pcied/tests/test_pcied.py

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,6 @@ def test_load_platform_pcieutil():
6464
assert result == instance
6565
mock_log.log_notice.assert_not_called()
6666
mock_log.log_error.assert_not_called()
67-
mock_log.log_critical.assert_not_called()
6867

6968
# Case 2: Fallback to sonic_platform_base.sonic_pcie.pcie_common.PcieUtil
7069
with patch('sonic_platform.pcie.Pcie', side_effect=ImportError("No module named 'sonic_platform.pcie'")), \
@@ -76,7 +75,6 @@ def test_load_platform_pcieutil():
7675
assert result == instance
7776
mock_log.log_notice.assert_called_once()
7877
mock_log.log_error.assert_not_called()
79-
mock_log.log_critical.assert_not_called()
8078
mock_log.reset_mock()
8179

8280
# Case 3: Failure to import both modules
@@ -86,5 +84,4 @@ def test_load_platform_pcieutil():
8684
pcied.load_platform_pcieutil()
8785

8886
mock_log.log_notice.assert_called_once()
89-
mock_log.log_error.assert_called_once()
90-
mock_log.log_critical.assert_called_once()
87+
assert mock_log.log_error.call_count == 2

0 commit comments

Comments
 (0)