Skip to content

Commit c4127c2

Browse files
[psud] Fix PSU log issue (#235)
Description Set PSU status default to True Add PSU index to PsuStatus to help print the log with PSU id Motivation and Context Issue flow: 1. PSU is in bad state (e.g. PSU is not present, PSU is powered off, PSU overheat and so on) psud startup 2. There is no warning logs in syslog 3. This PR is aimed to fix the issue How Has This Been Tested? 1. Manual test 2. Adjusted unit test
1 parent 07542cb commit c4127c2

File tree

3 files changed

+51
-65
lines changed

3 files changed

+51
-65
lines changed

sonic-psud/scripts/psud

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -272,13 +272,13 @@ class PsuChassisInfo(logger.Logger):
272272

273273

274274
class PsuStatus(object):
275-
def __init__(self, logger, psu):
276-
275+
def __init__(self, logger, psu, psu_index):
277276
self.psu = psu
278-
self.presence = False
279-
self.power_good = False
280-
self.voltage_good = False
281-
self.temperature_good = False
277+
self.psu_index = psu_index
278+
self.presence = True
279+
self.power_good = True
280+
self.voltage_good = True
281+
self.temperature_good = True
282282
self.logger = logger
283283

284284
def set_presence(self, presence):
@@ -308,8 +308,8 @@ class PsuStatus(object):
308308
def set_voltage(self, voltage, high_threshold, low_threshold):
309309
if voltage == NOT_AVAILABLE or high_threshold == NOT_AVAILABLE or low_threshold == NOT_AVAILABLE:
310310
if self.voltage_good is not True:
311-
self.logger.log_warning('PSU voltage or high_threshold or low_threshold become unavailable, '
312-
'voltage={}, high_threshold={}, low_threshold={}'.format(voltage, high_threshold, low_threshold))
311+
self.logger.log_warning('PSU {} voltage or high_threshold or low_threshold become unavailable, '
312+
'voltage={}, high_threshold={}, low_threshold={}'.format(self.psu_index, voltage, high_threshold, low_threshold))
313313
self.voltage_good = True
314314
return False
315315

@@ -323,8 +323,8 @@ class PsuStatus(object):
323323
def set_temperature(self, temperature, high_threshold):
324324
if temperature == NOT_AVAILABLE or high_threshold == NOT_AVAILABLE:
325325
if self.temperature_good is not True:
326-
self.logger.log_warning('PSU temperature or high_threshold become unavailable, '
327-
'temperature={}, high_threshold={}'.format(temperature, high_threshold))
326+
self.logger.log_warning('PSU {} temperature or high_threshold become unavailable, '
327+
'temperature={}, high_threshold={}'.format(self.psu_index, temperature, high_threshold))
328328
self.temperature_good = True
329329
return False
330330

@@ -465,16 +465,19 @@ class DaemonPsud(daemon_base.DaemonBase):
465465
power = try_get(psu.get_power, NOT_AVAILABLE)
466466

467467
if index not in self.psu_status_dict:
468-
self.psu_status_dict[index] = PsuStatus(self, psu)
468+
self.psu_status_dict[index] = PsuStatus(self, psu, index)
469469

470470
psu_status = self.psu_status_dict[index]
471471
set_led = self.first_run
472-
if psu_status.set_presence(presence):
472+
presence_changed = psu_status.set_presence(presence)
473+
if presence_changed:
473474
set_led = True
474475
log_on_status_changed(self, psu_status.presence,
475476
'PSU absence warning cleared: {} is inserted back.'.format(name),
476477
'PSU absence warning: {} is not present.'.format(name)
477478
)
479+
480+
if presence_changed or self.first_run:
478481
# Have to update PSU fan data here because PSU presence status changed. If we don't
479482
# update PSU fan data here, there might be an inconsistent output between "show platform psustatus"
480483
# and "show platform fan". For example, say PSU 1 is removed, and psud query PSU status every 3 seconds,

sonic-psud/tests/test_DaemonPsud.py

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -175,14 +175,10 @@ def test_update_single_psu_data(self):
175175
def test_set_psu_led(self):
176176
mock_logger = mock.MagicMock()
177177
mock_psu = MockPsu("PSU 1", 0, True, True)
178-
psu_status = psud.PsuStatus(mock_logger, mock_psu)
178+
psu_status = psud.PsuStatus(mock_logger, mock_psu, 1)
179179

180180
daemon_psud = psud.DaemonPsud(SYSLOG_IDENTIFIER)
181181

182-
psu_status.presence = True
183-
psu_status.power_good = True
184-
psu_status.voltage_good = True
185-
psu_status.temperature_good = True
186182
daemon_psud._set_psu_led(mock_psu, psu_status)
187183
assert mock_psu.get_status_led() == mock_psu.STATUS_LED_COLOR_GREEN
188184

@@ -228,7 +224,7 @@ def test_set_psu_led(self):
228224
def test_update_led_color(self):
229225
mock_psu = MockPsu("PSU 1", 0, True, True)
230226
mock_logger = mock.MagicMock()
231-
psu_status = psud.PsuStatus(mock_logger, mock_psu)
227+
psu_status = psud.PsuStatus(mock_logger, mock_psu, 1)
232228

233229
daemon_psud = psud.DaemonPsud(SYSLOG_IDENTIFIER)
234230
daemon_psud.psu_tbl = mock.MagicMock()

sonic-psud/tests/test_PsuStatus.py

Lines changed: 34 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -33,29 +33,39 @@ def test_set_presence(self):
3333
mock_logger = mock.MagicMock()
3434
mock_psu = MockPsu("PSU 1", 0, True, True)
3535

36-
psu_status = psud.PsuStatus(mock_logger, mock_psu)
36+
psu_status = psud.PsuStatus(mock_logger, mock_psu, 1)
37+
assert psu_status.presence is True
38+
39+
# Test toggling presence to False
40+
ret = psu_status.set_presence(False)
41+
assert ret == True
3742
assert psu_status.presence == False
3843

3944
# Test toggling presence to True
4045
ret = psu_status.set_presence(True)
4146
assert ret == True
4247
assert psu_status.presence == True
4348

44-
# Test toggling presence to False
45-
ret = psu_status.set_presence(False)
46-
assert ret == True
47-
assert psu_status.presence == False
48-
4949
# Test attempting to set presence to the same as the current value
50-
ret = psu_status.set_presence(False)
50+
ret = psu_status.set_presence(True)
5151
assert ret == False
52-
assert psu_status.presence == False
52+
assert psu_status.presence == True
5353

5454
def test_set_power_good(self):
5555
mock_logger = mock.MagicMock()
5656
mock_psu = MockPsu("PSU 1", 0, True, True)
5757

58-
psu_status = psud.PsuStatus(mock_logger, mock_psu)
58+
psu_status = psud.PsuStatus(mock_logger, mock_psu, 1)
59+
assert psu_status.power_good is True
60+
61+
# Test toggling power_good to False
62+
ret = psu_status.set_power_good(False)
63+
assert ret == True
64+
assert psu_status.power_good == False
65+
66+
# Test attempting to set power_good to the same as the current value (return value should be False)
67+
ret = psu_status.set_power_good(False)
68+
assert ret == False
5969
assert psu_status.power_good == False
6070

6171
# Test toggling power_good to True
@@ -68,32 +78,13 @@ def test_set_power_good(self):
6878
assert ret == False
6979
assert psu_status.power_good == True
7080

71-
# Test toggling power_good to False
72-
ret = psu_status.set_power_good(False)
73-
assert ret == True
74-
assert psu_status.power_good == False
75-
76-
# Test attempting to set power_good to the same as the current value (return value should be False)
77-
ret = psu_status.set_power_good(False)
78-
assert ret == False
79-
assert psu_status.power_good == False
8081

8182
def test_set_voltage(self):
8283
mock_logger = mock.MagicMock()
8384
mock_psu = MockPsu("PSU 1", 0, True, True)
8485

85-
psu_status = psud.PsuStatus(mock_logger, mock_psu)
86-
assert psu_status.voltage_good == False
87-
88-
# Pass in a good voltage
89-
ret = psu_status.set_voltage(12.0, 12.5, 11.5)
90-
assert ret == True
91-
assert psu_status.voltage_good == True
92-
93-
# Pass in a another good voltage successively (return value should be False)
94-
ret = psu_status.set_voltage(11.9, 12.5, 11.5)
95-
assert ret == False
96-
assert psu_status.voltage_good == True
86+
psu_status = psud.PsuStatus(mock_logger, mock_psu, 1)
87+
assert psu_status.voltage_good is True
9788

9889
# Pass in a high voltage
9990
ret = psu_status.set_voltage(12.6, 12.5, 11.5)
@@ -110,6 +101,11 @@ def test_set_voltage(self):
110101
assert ret == True
111102
assert psu_status.voltage_good == True
112103

104+
# Pass in a another good voltage successively (return value should be False)
105+
ret = psu_status.set_voltage(11.9, 12.5, 11.5)
106+
assert ret == False
107+
assert psu_status.voltage_good == True
108+
113109
# Pass in a low voltage
114110
ret = psu_status.set_voltage(11.4, 12.5, 11.5)
115111
assert ret == True
@@ -149,18 +145,8 @@ def test_set_temperature(self):
149145
mock_logger = mock.MagicMock()
150146
mock_psu = MockPsu("PSU 1", 0, True, True)
151147

152-
psu_status = psud.PsuStatus(mock_logger, mock_psu)
153-
assert psu_status.temperature_good == False
154-
155-
# Pass in a good temperature
156-
ret = psu_status.set_temperature(20.123, 50.0)
157-
assert ret == True
158-
assert psu_status.temperature_good == True
159-
160-
# Pass in a another good temperature successively (return value should be False)
161-
ret = psu_status.set_temperature(31.456, 50.0)
162-
assert ret == False
163-
assert psu_status.temperature_good == True
148+
psu_status = psud.PsuStatus(mock_logger, mock_psu, 1)
149+
assert psu_status.temperature_good is True
164150

165151
# Pass in a high temperature
166152
ret = psu_status.set_temperature(50.001, 50.0)
@@ -177,6 +163,11 @@ def test_set_temperature(self):
177163
assert ret == True
178164
assert psu_status.temperature_good == True
179165

166+
# Pass in a another good temperature successively (return value should be False)
167+
ret = psu_status.set_temperature(31.456, 50.0)
168+
assert ret == False
169+
assert psu_status.temperature_good == True
170+
180171
# Test passing parameters as None when temperature_good == True
181172
ret = psu_status.set_temperature(psud.NOT_AVAILABLE, 50.0)
182173
assert ret == False
@@ -199,11 +190,7 @@ def test_is_ok(self):
199190
mock_logger = mock.MagicMock()
200191
mock_psu = MockPsu("PSU 1", 0, True, True)
201192

202-
psu_status = psud.PsuStatus(mock_logger, mock_psu)
203-
psu_status.presence = True
204-
psu_status.power_good = True
205-
psu_status.voltage_good = True
206-
psu_status.temperature_good = True
193+
psu_status = psud.PsuStatus(mock_logger, mock_psu, 1)
207194
ret = psu_status.is_ok()
208195
assert ret == True
209196

0 commit comments

Comments
 (0)