-
Notifications
You must be signed in to change notification settings - Fork 171
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Default implementation of under/over speed checks #382
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
1c73c9c
Default implementation of under/over speed checks
spilkey-cisco 738ccc6
Update comment, remove requirement for float conversion
spilkey-cisco c0d4fcc
Merge remote-tracking branch 'origin/master' into spilkey/fan_tolerance
spilkey-cisco 093c69f
Merge branch 'master' into spilkey/fan_tolerance
spilkey-cisco fa986c5
Fan test coverage
spilkey-cisco File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,158 @@ | ||
''' | ||
Test FanBase module | ||
''' | ||
|
||
from unittest import mock | ||
from sonic_platform_base.fan_base import FanBase | ||
|
||
class TestFanBase: | ||
''' | ||
Collection of FanBase test methods | ||
''' | ||
|
||
@staticmethod | ||
def test_is_under_speed(): | ||
''' | ||
Test fan.is_under_speed default implementation | ||
''' | ||
fan = FanBase() | ||
fan.get_speed = mock.MagicMock(return_value=100) | ||
fan.get_target_speed = mock.MagicMock(return_value=50) | ||
fan.get_speed_tolerance = mock.MagicMock(return_value=10) | ||
|
||
for func in (fan.get_speed, fan.get_target_speed, fan.get_speed_tolerance): | ||
return_val = func() | ||
|
||
# Check type and bounds errors | ||
for value, exc_type in ((None, TypeError), (-1, ValueError), (101, ValueError)): | ||
func.return_value = value | ||
expected_exception = False | ||
try: | ||
fan.is_under_speed() | ||
except Exception as exc: | ||
expected_exception = isinstance(exc, exc_type) | ||
assert expected_exception | ||
|
||
# Reset function return value | ||
func.return_value = return_val | ||
|
||
# speed=100, minimum tolerated speed=45, not under speed | ||
assert not fan.is_under_speed() | ||
|
||
# speed=46, minimum tolerated speed=45, not under speed | ||
fan.get_speed.return_value = 46 | ||
assert not fan.is_under_speed() | ||
|
||
# speed=45, minimum tolerated speed=45, not under speed | ||
fan.get_speed.return_value = 45 | ||
assert not fan.is_under_speed() | ||
|
||
# speed=44, minimum tolerated speed=45, under speed | ||
fan.get_speed.return_value = 44 | ||
assert fan.is_under_speed() | ||
|
||
# speed=44, minimum tolerated speed=40, not under speed | ||
fan.get_speed_tolerance.return_value = 20 | ||
assert not fan.is_under_speed() | ||
|
||
# speed=41, minimum tolerated speed=40, not under speed | ||
fan.get_speed.return_value = 41 | ||
assert not fan.is_under_speed() | ||
|
||
# speed=40, minimum tolerated speed=40, not under speed | ||
fan.get_speed.return_value = 40 | ||
assert not fan.is_under_speed() | ||
|
||
# speed=39, minimum tolerated speed=40, under speed | ||
fan.get_speed.return_value = 39 | ||
assert fan.is_under_speed() | ||
|
||
# speed=1, minimum tolerated speed=40, under speed | ||
fan.get_speed.return_value = 1 | ||
assert fan.is_under_speed() | ||
|
||
@staticmethod | ||
def test_is_over_speed(): | ||
''' | ||
test fan.is_over_speed default implementation | ||
''' | ||
fan = FanBase() | ||
fan.get_speed = mock.MagicMock(return_value=1) | ||
fan.get_target_speed = mock.MagicMock(return_value=50) | ||
fan.get_speed_tolerance = mock.MagicMock(return_value=10) | ||
|
||
for func in (fan.get_speed, fan.get_target_speed, fan.get_speed_tolerance): | ||
return_val = func() | ||
|
||
# Check type and bounds errors | ||
for value, exc_type in ((None, TypeError), (-1, ValueError), (101, ValueError)): | ||
func.return_value = value | ||
expected_exception = False | ||
try: | ||
fan.is_under_speed() | ||
except Exception as exc: | ||
expected_exception = isinstance(exc, exc_type) | ||
assert expected_exception | ||
|
||
# Reset function return value | ||
func.return_value = return_val | ||
|
||
# speed=1, maximum tolerated speed=55, not over speed | ||
assert not fan.is_over_speed() | ||
|
||
# speed=54, maximum tolerated speed=55, not over speed | ||
fan.get_speed.return_value = 54 | ||
assert not fan.is_over_speed() | ||
|
||
# speed=55, maximum tolerated speed=55, not over speed | ||
fan.get_speed.return_value = 55 | ||
assert not fan.is_over_speed() | ||
|
||
# speed=56, maximum tolerated speed=55, over speed | ||
fan.get_speed.return_value = 56 | ||
assert fan.is_over_speed() | ||
|
||
# speed=56, maximum tolerated speed=60, not over speed | ||
fan.get_speed_tolerance.return_value = 20 | ||
assert not fan.is_over_speed() | ||
|
||
# speed=59, maximum tolerated speed=60, not over speed | ||
fan.get_speed.return_value = 59 | ||
assert not fan.is_over_speed() | ||
|
||
# speed=60, maximum tolerated speed=60, not over speed | ||
fan.get_speed.return_value = 60 | ||
assert not fan.is_over_speed() | ||
|
||
# speed=61, maximum tolerated speed=60, over speed | ||
fan.get_speed.return_value = 61 | ||
assert fan.is_over_speed() | ||
|
||
# speed=100, maximum tolerated speed=60, over speed | ||
fan.get_speed.return_value = 100 | ||
assert fan.is_over_speed() | ||
|
||
@staticmethod | ||
def test_fan_base(): | ||
''' | ||
Verify unimplemented methods | ||
''' | ||
fan = FanBase() | ||
not_implemented_methods = [ | ||
(fan.get_direction,), | ||
(fan.get_speed,), | ||
(fan.get_target_speed,), | ||
(fan.get_speed_tolerance,), | ||
(fan.set_speed, 50), | ||
(fan.set_status_led, 'green'), | ||
(fan.get_status_led,)] | ||
|
||
for method in not_implemented_methods: | ||
expected_exception = False | ||
try: | ||
func = method[0] | ||
args = method[1:] | ||
func(*args) | ||
except Exception as exc: | ||
expected_exception = isinstance(exc, NotImplementedError) | ||
assert expected_exception |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@spilkey-cisco This is an abstract class. Where is the HLD changes?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
My understanding is that https://github.com/sonic-net/sonic-platform-common/blob/master/sonic_fan/fan_base.py is the (obsolete, from what I can tell) fan abstract base class, and the file in this changeset follows the design from https://github.com/sonic-net/SONiC/blob/master/doc/platform_api/new_platform_api.md indicating ABCs are no longer used, 'No longer abstract base classes: All abstract methods simply raise "NotImplementedError"'. The new methods are not abstract since they have a default implementation, but can be overridden with vendor specific implementations if desired. If an HLD is needed, please advise on where it should go.
Please correct me if this understanding is inaccurate or incomplete.