Skip to content

Commit 101d8fe

Browse files
committed
enhance error logging in PSU presence and status retrieval functions
Signed-off-by: Lin Jin <[email protected]>
1 parent 5058880 commit 101d8fe

File tree

2 files changed

+10
-15
lines changed

2 files changed

+10
-15
lines changed

sonic-psud/scripts/psud

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,8 @@ def _wrapper_get_psu_presence(logger, psu_index):
134134
except NotImplementedError:
135135
pass
136136
except Exception as e:
137-
logger.log_warning("Exception in psu.get_presence() for PSU {}: {}".format(psu_index, str(e)))
137+
if logger:
138+
logger.log_warning("Exception in psu.get_presence() for PSU {}: {}".format(psu_index, str(e)))
138139
return False
139140
if platform_psuutil is not None:
140141
try:
@@ -157,7 +158,8 @@ def _wrapper_get_psu_status(logger, psu_index):
157158
except NotImplementedError:
158159
pass
159160
except Exception as e:
160-
logger.log_warning("Exception in psu.get_powergood_status() for PSU {}: {}".format(psu_index, str(e)))
161+
if logger:
162+
logger.log_warning("Exception in psu.get_powergood_status() for PSU {}: {}".format(psu_index, str(e)))
161163
return False
162164
if platform_psuutil is not None:
163165
try:
@@ -181,13 +183,8 @@ def get_psu_key(psu_index):
181183
if psu:
182184
try:
183185
return psu.get_name()
184-
except NotImplementedError:
185-
pass
186-
except IndexError:
187-
# some functionality is expectent on returning an expected key even if the psu object itself does not exist
186+
except (NotImplementedError, IndexError, Exception):
188187
pass
189-
except Exception as e:
190-
return False
191188
return PSU_INFO_KEY_TEMPLATE.format(psu_index)
192189

193190

sonic-psud/tests/test_psud.py

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,6 @@ def test_wrapper_get_num_psus():
5757
# Test with None logger - should not crash
5858
psud.platform_chassis = mock.MagicMock()
5959
psud.platform_psuutil = mock.MagicMock()
60-
result = psud._wrapper_get_num_psus(None)
6160
assert psud.platform_chassis.get_num_psus.call_count >= 1
6261

6362
# Test when both providers are unavailable
@@ -93,7 +92,7 @@ def test_wrapper_get_psu_presence():
9392

9493
# Test _wrapper_get_psu returns None (PSU object retrieval failed)
9594
psud.platform_chassis.get_psu.side_effect = Exception("PSU retrieval failed")
96-
result = psud._wrapper_get_psu_presence(mock_logger, 1)
95+
psud._wrapper_get_psu_presence(mock_logger, 1)
9796
# Should fallback to platform_psuutil
9897
psud.platform_chassis.get_psu.assert_called_with(0)
9998
assert psud.platform_psuutil.get_psu_presence.call_count == 1
@@ -108,7 +107,7 @@ def test_wrapper_get_psu_presence():
108107
# Test new platform API PSU available but get_presence not implemented
109108
psud.platform_chassis.get_psu.return_value = mock_psu
110109
mock_psu.get_presence.side_effect = NotImplementedError
111-
result = psud._wrapper_get_psu_presence(mock_logger, 1)
110+
psud._wrapper_get_psu_presence(mock_logger, 1)
112111
# Should fallback to platform_psuutil
113112
psud.platform_chassis.get_psu.assert_called_with(0)
114113
mock_psu.get_presence.assert_called_once()
@@ -140,7 +139,7 @@ def test_wrapper_get_psu_presence():
140139

141140
# Test new platform API not available
142141
psud.platform_chassis = None
143-
result = psud._wrapper_get_psu_presence(mock_logger, 1)
142+
psud._wrapper_get_psu_presence(mock_logger, 1)
144143
# Should use platform_psuutil
145144
assert psud.platform_psuutil.get_psu_presence.call_count == 1
146145
psud.platform_psuutil.get_psu_presence.assert_called_with(1)
@@ -264,7 +263,7 @@ def test_wrapper_get_psu_status():
264263

265264
# Test _wrapper_get_psu returns None (PSU object retrieval failed)
266265
psud.platform_chassis.get_psu.side_effect = Exception("PSU retrieval failed")
267-
result = psud._wrapper_get_psu_status(mock_logger, 1)
266+
psud._wrapper_get_psu_status(mock_logger, 1)
268267
# Should fallback to platform_psuutil
269268
psud.platform_chassis.get_psu.assert_called_with(0)
270269
assert psud.platform_psuutil.get_psu_status.call_count == 1
@@ -279,7 +278,7 @@ def test_wrapper_get_psu_status():
279278
# Test new platform API PSU available but get_powergood_status not implemented
280279
psud.platform_chassis.get_psu.return_value = mock_psu
281280
mock_psu.get_powergood_status.side_effect = NotImplementedError
282-
result = psud._wrapper_get_psu_status(mock_logger, 1)
281+
psud._wrapper_get_psu_status(mock_logger, 1)
283282
# Should fallback to platform_psuutil
284283
psud.platform_chassis.get_psu.assert_called_with(0)
285284
mock_psu.get_powergood_status.assert_called_once()
@@ -311,7 +310,6 @@ def test_wrapper_get_psu_status():
311310

312311
# Test new platform API not available
313312
psud.platform_chassis = None
314-
result = psud._wrapper_get_psu_status(mock_logger, 1)
315313
# Should use platform_psuutil
316314
assert psud.platform_psuutil.get_psu_status.call_count == 1
317315
psud.platform_psuutil.get_psu_status.assert_called_with(1)

0 commit comments

Comments
 (0)