forked from sonic-net/sonic-buildimage
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[psud] Increase unit test coverage (sonic-net#140)
- Add 100% unit test coverage of `PsuStatus` class in psud. - Add skeleton of class to test `DaemonPsud` class - Add test case for `get_psu_key()` and `try_get()` helper functions - Add checks to import 'mock' from the 'unittest' package if running with Python 3 Overall psud unit test coverage increases from 39% to 51%. Previous unit test coverage: ``` ----------- coverage: platform linux, python 3.7.3-final-0 ----------- Name Stmts Miss Cover ---------------------------------- scripts/psud 381 233 39% Coverage HTML written to dir htmlcov Coverage XML written to file coverage.xml ``` Unit test coverage with this patch: ``` ----------- coverage: platform linux, python 3.7.3-final-0 ----------- Name Stmts Miss Cover ---------------------------------- scripts/psud 381 185 51% Coverage HTML written to dir htmlcov Coverage XML written to file coverage.xml ```
- Loading branch information
Showing
4 changed files
with
334 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,236 @@ | ||
import os | ||
import sys | ||
from imp import load_source | ||
|
||
# TODO: Clean this up once we no longer need to support Python 2 | ||
if sys.version_info.major == 3: | ||
from unittest.mock import MagicMock | ||
else: | ||
from mock import MagicMock | ||
|
||
from .mock_platform import MockPsu | ||
|
||
test_path = os.path.dirname(os.path.abspath(__file__)) | ||
modules_path = os.path.dirname(test_path) | ||
scripts_path = os.path.join(modules_path, "scripts") | ||
sys.path.insert(0, modules_path) | ||
|
||
os.environ["PSUD_UNIT_TESTING"] = "1" | ||
load_source('psud', scripts_path + '/psud') | ||
from psud import * | ||
|
||
|
||
class TestPsuStatus(object): | ||
""" | ||
Test cases to cover functionality of PsuStatus class | ||
""" | ||
|
||
def test_set_presence(self): | ||
mock_logger = MagicMock() | ||
mock_psu = MockPsu(True, True, "PSU 1") | ||
|
||
psu_status = PsuStatus(mock_logger, mock_psu) | ||
assert psu_status.presence == False | ||
|
||
# Test toggling presence to True | ||
ret = psu_status.set_presence(True) | ||
assert ret == True | ||
assert psu_status.presence == True | ||
|
||
# Test toggling presence to False | ||
ret = psu_status.set_presence(False) | ||
assert ret == True | ||
assert psu_status.presence == False | ||
|
||
# Test attempting to set presence to the same as the current value | ||
ret = psu_status.set_presence(False) | ||
assert ret == False | ||
assert psu_status.presence == False | ||
|
||
def test_set_power_good(self): | ||
mock_logger = MagicMock() | ||
mock_psu = MockPsu(True, True, "PSU 1") | ||
|
||
psu_status = PsuStatus(mock_logger, mock_psu) | ||
assert psu_status.power_good == False | ||
|
||
# Test toggling power_good to True | ||
ret = psu_status.set_power_good(True) | ||
assert ret == True | ||
assert psu_status.power_good == True | ||
|
||
# Test attempting to set power_good to the same as the current value (return value should be False) | ||
ret = psu_status.set_power_good(True) | ||
assert ret == False | ||
assert psu_status.power_good == True | ||
|
||
# Test toggling power_good to False | ||
ret = psu_status.set_power_good(False) | ||
assert ret == True | ||
assert psu_status.power_good == False | ||
|
||
# Test attempting to set power_good to the same as the current value (return value should be False) | ||
ret = psu_status.set_power_good(False) | ||
assert ret == False | ||
assert psu_status.power_good == False | ||
|
||
def test_set_voltage(self): | ||
mock_logger = MagicMock() | ||
mock_psu = MockPsu(True, True, "PSU 1") | ||
|
||
psu_status = PsuStatus(mock_logger, mock_psu) | ||
assert psu_status.voltage_good == False | ||
|
||
# Pass in a good voltage | ||
ret = psu_status.set_voltage(12.0, 12.5, 11.5) | ||
assert ret == True | ||
assert psu_status.voltage_good == True | ||
|
||
# Pass in a another good voltage successively (return value should be False) | ||
ret = psu_status.set_voltage(11.9, 12.5, 11.5) | ||
assert ret == False | ||
assert psu_status.voltage_good == True | ||
|
||
# Pass in a high voltage | ||
ret = psu_status.set_voltage(12.6, 12.5, 11.5) | ||
assert ret == True | ||
assert psu_status.voltage_good == False | ||
|
||
# Pass in a another bad voltage successively (return value should be False) | ||
ret = psu_status.set_voltage(12.7, 12.5, 11.5) | ||
assert ret == False | ||
assert psu_status.voltage_good == False | ||
|
||
# Pass in a good (high edge case) voltage | ||
ret = psu_status.set_voltage(12.5, 12.5, 11.5) | ||
assert ret == True | ||
assert psu_status.voltage_good == True | ||
|
||
# Pass in a low voltage | ||
ret = psu_status.set_voltage(11.4, 12.5, 11.5) | ||
assert ret == True | ||
assert psu_status.voltage_good == False | ||
|
||
# Pass in a good (low edge case) voltage | ||
ret = psu_status.set_voltage(11.5, 12.5, 11.5) | ||
assert ret == True | ||
assert psu_status.voltage_good == True | ||
|
||
# Test passing parameters as None when voltage_good == True | ||
ret = psu_status.set_voltage(None, 12.5, 11.5) | ||
assert ret == False | ||
assert psu_status.voltage_good == True | ||
ret = psu_status.set_voltage(11.5, None, 11.5) | ||
assert ret == False | ||
assert psu_status.voltage_good == True | ||
ret = psu_status.set_voltage(11.5, 12.5, None) | ||
assert ret == False | ||
assert psu_status.voltage_good == True | ||
|
||
# Test passing parameters as None when voltage_good == False | ||
psu_status.voltage_good = False | ||
ret = psu_status.set_voltage(None, 12.5, 11.5) | ||
assert ret == False | ||
assert psu_status.voltage_good == True | ||
psu_status.voltage_good = False | ||
ret = psu_status.set_voltage(11.5, None, 11.5) | ||
assert ret == False | ||
assert psu_status.voltage_good == True | ||
psu_status.voltage_good = False | ||
ret = psu_status.set_voltage(11.5, 12.5, None) | ||
assert ret == False | ||
assert psu_status.voltage_good == True | ||
|
||
def test_set_temperature(self): | ||
mock_logger = MagicMock() | ||
mock_psu = MockPsu(True, True, "PSU 1") | ||
|
||
psu_status = PsuStatus(mock_logger, mock_psu) | ||
assert psu_status.temperature_good == False | ||
|
||
# Pass in a good temperature | ||
ret = psu_status.set_temperature(20.123, 50.0) | ||
assert ret == True | ||
assert psu_status.temperature_good == True | ||
|
||
# Pass in a another good temperature successively (return value should be False) | ||
ret = psu_status.set_temperature(31.456, 50.0) | ||
assert ret == False | ||
assert psu_status.temperature_good == True | ||
|
||
# Pass in a high temperature | ||
ret = psu_status.set_temperature(50.001, 50.0) | ||
assert ret == True | ||
assert psu_status.temperature_good == False | ||
|
||
# Pass in a another bad temperature successively (return value should be False) | ||
ret = psu_status.set_temperature(50.0, 50.0) | ||
assert ret == False | ||
assert psu_status.temperature_good == False | ||
|
||
# Pass in a good (high edge case) temperature | ||
ret = psu_status.set_temperature(49.999, 50.0) | ||
assert ret == True | ||
assert psu_status.temperature_good == True | ||
|
||
# Test passing parameters as None when temperature_good == True | ||
ret = psu_status.set_temperature(None, 50.0) | ||
assert ret == False | ||
assert psu_status.temperature_good == True | ||
ret = psu_status.set_temperature(20.123, None) | ||
assert ret == False | ||
assert psu_status.temperature_good == True | ||
|
||
# Test passing parameters as None when temperature_good == False | ||
psu_status.temperature_good = False | ||
ret = psu_status.set_temperature(None, 50.0) | ||
assert ret == False | ||
assert psu_status.temperature_good == True | ||
psu_status.temperature_good = False | ||
ret = psu_status.set_temperature(20.123, None) | ||
assert ret == False | ||
assert psu_status.temperature_good == True | ||
|
||
def test_is_ok(self): | ||
mock_logger = MagicMock() | ||
mock_psu = MockPsu(True, True, "PSU 1") | ||
|
||
psu_status = PsuStatus(mock_logger, mock_psu) | ||
psu_status.presence = True | ||
psu_status.power_good = True | ||
psu_status.voltage_good = True | ||
psu_status.temperature_good = True | ||
ret = psu_status.is_ok() | ||
assert ret == True | ||
|
||
psu_status.presence = False | ||
ret = psu_status.is_ok() | ||
assert ret == False | ||
|
||
psu_status.presence = True | ||
ret = psu_status.is_ok() | ||
assert ret == True | ||
|
||
psu_status.power_good = False | ||
ret = psu_status.is_ok() | ||
assert ret == False | ||
|
||
psu_status.power_good = True | ||
ret = psu_status.is_ok() | ||
assert ret == True | ||
|
||
psu_status.voltage_good = False | ||
ret = psu_status.is_ok() | ||
assert ret == False | ||
|
||
psu_status.voltage_good = True | ||
ret = psu_status.is_ok() | ||
assert ret == True | ||
|
||
psu_status.temperature_good = False | ||
ret = psu_status.is_ok() | ||
assert ret == False | ||
|
||
psu_status.temperature_good = True | ||
ret = psu_status.is_ok() | ||
assert ret == True |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters