Skip to content
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

[thermalctld] Refactor to allow for greater unit test coverage; Add more unit tests #157

Merged
merged 43 commits into from
Mar 18, 2021
Merged

[thermalctld] Refactor to allow for greater unit test coverage; Add more unit tests #157

merged 43 commits into from
Mar 18, 2021

Conversation

jleveque
Copy link
Contributor

@jleveque jleveque commented Feb 25, 2021

Description

  • Refactor thermalctld to reduce the amount of code in infinite loops, thus allowing us better unit test coverage

  • Refactor mock_platform.py such that it inherits from sonic-platform-common in order to ensure it is aligned with the current API definitions (this introduces a test-time dependency on the sonic-platform-common package)

  • Eliminate the need to check for a THERMALCTLD_UNIT_TESTING environment variable in daemon code

  • Increase pytest verbosity to prevent truncation of error messages

  • Miscellaneous cleanup:

    • Fixes to grammar
    • Remove unnecessary punctuation from log messages
  • Increase overall unit test unit test coverage from 73% to 93%

Previous unit test coverage:

----------- coverage: platform linux, python 3.7.3-final-0 -----------
Name                  Stmts   Miss  Cover
-----------------------------------------
scripts/thermalctld     424    113    73%

Unit test coverage with this patch:

----------- coverage: platform linux, python 3.7.3-final-0 -----------
Name                  Stmts   Miss  Cover
-----------------------------------------
scripts/thermalctld     434     29    93%

Motivation and Context

As part of ongoing efforts to increase unit test coverage >= 90% across the SONiC codebase in order to prevent regressions

How Has This Been Tested?

The refactored thermalctld has been tested on a physical DUT and the unit test results can be examined via the Azure Pipelines check builds in this PR.

@sonic-net sonic-net deleted a comment from lgtm-com bot Feb 26, 2021
@sonic-net sonic-net deleted a comment from lgtm-com bot Feb 26, 2021
@sonic-net sonic-net deleted a comment from lgtm-com bot Feb 26, 2021
@sonic-net sonic-net deleted a comment from lgtm-com bot Feb 26, 2021
@sonic-net sonic-net deleted a comment from lgtm-com bot Feb 26, 2021
@sonic-net sonic-net deleted a comment from lgtm-com bot Feb 26, 2021
@sonic-net sonic-net deleted a comment from lgtm-com bot Feb 26, 2021
@sonic-net sonic-net deleted a comment from lgtm-com bot Feb 26, 2021
@sonic-net sonic-net deleted a comment from lgtm-com bot Feb 26, 2021
@sonic-net sonic-net deleted a comment from lgtm-com bot Feb 26, 2021
@sonic-net sonic-net deleted a comment from lgtm-com bot Feb 26, 2021
@sonic-net sonic-net deleted a comment from lgtm-com bot Feb 26, 2021
@sonic-net sonic-net deleted a comment from lgtm-com bot Feb 26, 2021
@sonic-net sonic-net deleted a comment from lgtm-com bot Feb 26, 2021
@sonic-net sonic-net deleted a comment from lgtm-com bot Feb 26, 2021
@sonic-net sonic-net deleted a comment from lgtm-com bot Feb 26, 2021
@sonic-net sonic-net deleted a comment from lgtm-com bot Feb 26, 2021
@sonic-net sonic-net deleted a comment from lgtm-com bot Feb 26, 2021
@sonic-net sonic-net deleted a comment from lgtm-com bot Feb 26, 2021
@sonic-net sonic-net deleted a comment from lgtm-com bot Feb 26, 2021
@sonic-net sonic-net deleted a comment from lgtm-com bot Feb 26, 2021
@sonic-net sonic-net deleted a comment from lgtm-com bot Feb 26, 2021
@sonic-net sonic-net deleted a comment from lgtm-com bot Feb 26, 2021
@sonic-net sonic-net deleted a comment from lgtm-com bot Feb 26, 2021
@sonic-net sonic-net deleted a comment from lgtm-com bot Feb 26, 2021
@sonic-net sonic-net deleted a comment from lgtm-com bot Feb 26, 2021
@sonic-net sonic-net deleted a comment from lgtm-com bot Feb 26, 2021
@@ -180,7 +180,11 @@ def test_update_fan_with_exception(self):
assert fan.get_status_led() == MockFan.STATUS_LED_COLOR_RED
assert fan_updater.log_warning.call_count == 1

fan_updater.log_warning.assert_called_with("Failed to update fan status - Failed to get speed")
# TODO: Clean this up once we no longer need to support Python 2
if sys.version_info.major == 3:
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How about we add a helper function like below:

def get_exception_message(msg):
    if sys.version_info.major == 3:
        return 'Exception(\'{},\')'.format(msg)
    else:
        return 'Exception(\'{}\')'.format(msg)

And we don't have to change everywhere when removing python2 support.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for the suggestion. However, I believe this unit test code will not change much, if at all before we remove Python 2 support. And with things the way they are, all we have to do is delete lines. If we make the change you suggested, there will actually be a bit more work needed when removing Python 2 support. Therefore, I prefer we just leave things as-is for the time being.

@jleveque jleveque merged commit 8509f43 into sonic-net:master Mar 18, 2021
@jleveque jleveque deleted the increase_thermalctld_coverage branch March 18, 2021 17:10
@@ -23,7 +23,8 @@
tests_require=[
'mock>=2.0.0; python_version < "3.3"',
'pytest',
'pytest-cov'
'pytest-cov',
'sonic-platform-common'
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@jleveque this dependency is causing sonic-net/sonic-buildimage#7143 sonic-buildimage to fail.
Do we need to remove it or add something ?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

PR to add dependency in sonic-buildimage here

jleveque added a commit to sonic-net/sonic-buildimage that referenced this pull request Mar 30, 2021
Unit tests for thermalctld depend on sonic-platform-common as of sonic-net/sonic-platform-daemons#157
daall pushed a commit to sonic-net/sonic-buildimage that referenced this pull request Mar 31, 2021
Unit tests for thermalctld depend on sonic-platform-common as of sonic-net/sonic-platform-daemons#157
raphaelt-nvidia pushed a commit to raphaelt-nvidia/sonic-buildimage that referenced this pull request May 23, 2021
carl-nokia pushed a commit to carl-nokia/sonic-buildimage that referenced this pull request Aug 7, 2021
vdahiya12 pushed a commit to vdahiya12/sonic-platform-daemons that referenced this pull request Apr 4, 2022
- Make `_read_eeprom_specific_bytes` Python3 and Python2 compatible
- Change a stray `self.eep_dict.iteritems()` call to `self.eep_dict.items()`
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants