Skip to content

Commit

Permalink
Import mock psu object for testing LED (#119)
Browse files Browse the repository at this point in the history
Updating for completeness on how mock objects need to be imported

```
mprabhu@565bc0455e84:/sonic/src/sonic-platform-daemons/sonic-psud$ python2 setup.py test
running pytest
running egg_info
writing sonic_psud.egg-info/PKG-INFO
writing top-level names to sonic_psud.egg-info/top_level.txt
writing dependency_links to sonic_psud.egg-info/dependency_links.txt
reading manifest file 'sonic_psud.egg-info/SOURCES.txt'
writing manifest file 'sonic_psud.egg-info/SOURCES.txt'
running build_ext
==================================================================================== test session starts =====================================================================================
platform linux2 -- Python 2.7.16, pytest-3.10.1, py-1.7.0, pluggy-0.8.0
rootdir: /sonic/src/sonic-platform-daemons/sonic-psud, inifile: pytest.ini
plugins: cov-2.6.0
collected 3 items

tests/test_psud.py ...                                                                                                                                                                 [100%]

---------- coverage: platform linux2, python 2.7.16-final-0 ----------
Name           Stmts   Miss  Cover
----------------------------------
scripts/psud     355    216    39%
Coverage HTML written to dir htmlcov
Coverage XML written to file coverage.xml


================================================================================== 3 passed in 0.16 seconds ==================================================================================
```
  • Loading branch information
mprabhu-nokia authored Dec 8, 2020
1 parent 73e6ddd commit 4d619b9
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 22 deletions.
11 changes: 6 additions & 5 deletions sonic-psud/scripts/psud
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,8 @@ try:
# If unit testing is occurring, mock swsscommon and module_base
if os.getenv("PSUD_UNIT_TESTING") == "1":
from tests import mock_swsscommon as swsscommon
from tests.mock_device_base import DeviceBase
else:
from swsscommon import swsscommon
from sonic_platform_base.device_base import DeviceBase
except ImportError as e:
raise ImportError (str(e) + " - required module not found")

Expand Down Expand Up @@ -256,11 +254,14 @@ class PsuChassisInfo(logger.Logger):
def _set_psu_master_led(self, master_status):
try:
try:
from sonic_platform.psu import Psu
if os.getenv("PSUD_UNIT_TESTING") == "1":
from tests.mock_platform import MockPsu as Psu
else:
from sonic_platform.psu import Psu
except ImportError as e:
from tests.mock_platform import MockPsu as Psu
raise ImportError (str(e) + " - required module not found")

color = DeviceBase.STATUS_LED_COLOR_GREEN if master_status else DeviceBase.STATUS_LED_COLOR_RED
color = Psu.STATUS_LED_COLOR_GREEN if master_status else Psu.STATUS_LED_COLOR_RED
Psu.set_status_master_led(color)
except NotImplementedError as e:
pass
Expand Down
11 changes: 0 additions & 11 deletions sonic-psud/tests/mock_device_base.py

This file was deleted.

9 changes: 6 additions & 3 deletions sonic-psud/tests/mock_platform.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
from .mock_device_base import DeviceBase

class MockDevice:
STATUS_LED_COLOR_GREEN = "green"
STATUS_LED_COLOR_AMBER = "amber"
STATUS_LED_COLOR_RED = "red"
STATUS_LED_COLOR_OFF = "off"

def __init__(self):
self.name = None
self.presence = True
Expand All @@ -24,7 +27,7 @@ def get_serial(self):

class MockPsu(MockDevice):

psu_master_led_color = DeviceBase.STATUS_LED_COLOR_OFF
psu_master_led_color = MockDevice.STATUS_LED_COLOR_OFF

def __init__(self, psu_presence, psu_status, psu_name):
self.name = psu_name
Expand Down
5 changes: 2 additions & 3 deletions sonic-psud/tests/test_psud.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
from sonic_py_common import daemon_base

from .mock_platform import MockChassis, MockPsu, MockFanDrawer, MockModule
from .mock_device_base import DeviceBase

SYSLOG_IDENTIFIER = 'psud_test'
NOT_AVAILABLE = 'N/A'
Expand Down Expand Up @@ -148,7 +147,7 @@ def test_psuchassis_check_power_budget():

assert float(fvs[CHASSIS_INFO_TOTAL_POWER_SUPPLIED_FIELD]) < float(fvs[CHASSIS_INFO_TOTAL_POWER_CONSUMED_FIELD])
assert chassis_info.master_status_good == False
assert MockPsu.get_status_master_led() == DeviceBase.STATUS_LED_COLOR_RED
assert MockPsu.get_status_master_led() == MockPsu.STATUS_LED_COLOR_RED

#Add a PSU
psu = MockPsu(True, True, "PSU 2")
Expand All @@ -164,4 +163,4 @@ def test_psuchassis_check_power_budget():

assert float(fvs[CHASSIS_INFO_TOTAL_POWER_SUPPLIED_FIELD]) > float(fvs[CHASSIS_INFO_TOTAL_POWER_CONSUMED_FIELD])
assert chassis_info.master_status_good == True
assert MockPsu.get_status_master_led() == DeviceBase.STATUS_LED_COLOR_GREEN
assert MockPsu.get_status_master_led() == MockPsu.STATUS_LED_COLOR_GREEN

0 comments on commit 4d619b9

Please sign in to comment.