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.
[ledd] Minor refactor; add unit tests (sonic-net#143)
- Refactor ledd: - Remove useless try/catch from around imports - Move argument parsing out of `DaemonLedd.run()` method and into `main()` function, a more appropriate location - Fix LGTM alert for unreachable code - Add unit tests and report coverage: - Test passing good and bad command-line arguments to ledd process Unit test coverage with this patch: ``` ----------- coverage: platform linux, python 3.7.3-final-0 ----------- Name Stmts Miss Cover ---------------------------------- scripts/ledd 66 34 48% Coverage HTML written to dir htmlcov Coverage XML written to file coverage.xml ```
- Loading branch information
Showing
7 changed files
with
128 additions
and
30 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
[pytest] | ||
addopts = --cov=scripts --cov-report html --cov-report term --cov-report xml |
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,2 @@ | ||
[aliases] | ||
test=pytest |
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
Empty file.
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,28 @@ | ||
STATE_DB = '' | ||
|
||
|
||
class Table: | ||
def __init__(self, db, table_name): | ||
self.table_name = table_name | ||
self.mock_dict = {} | ||
|
||
def _del(self, key): | ||
del self.mock_dict[key] | ||
pass | ||
|
||
def set(self, key, fvs): | ||
self.mock_dict[key] = fvs.fv_dict | ||
pass | ||
|
||
def get(self, key): | ||
if key in self.mock_dict: | ||
return self.mock_dict[key] | ||
return None | ||
|
||
|
||
class FieldValuePairs(dict): | ||
def __init__(self, len): | ||
self.fv_dict = {} | ||
|
||
def __setitem__(self, key, val_tuple): | ||
self.fv_dict[val_tuple[0]] = val_tuple[1] |
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,68 @@ | ||
import os | ||
import sys | ||
from imp import load_source | ||
|
||
import pytest | ||
# TODO: Clean this up once we no longer need to support Python 2 | ||
if sys.version_info.major == 3: | ||
from unittest.mock import Mock, MagicMock, patch | ||
else: | ||
from mock import Mock, MagicMock, patch | ||
from sonic_py_common import daemon_base | ||
|
||
SYSLOG_IDENTIFIER = 'ledd_test' | ||
NOT_AVAILABLE = 'N/A' | ||
|
||
daemon_base.db_connect = MagicMock() | ||
|
||
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["LEDD_UNIT_TESTING"] = "1" | ||
load_source('ledd', scripts_path + '/ledd') | ||
import ledd | ||
|
||
|
||
def test_help_args(capsys): | ||
for flag in ['-h', '--help']: | ||
with patch.object(sys, 'argv', ['ledd', flag]): | ||
with pytest.raises(SystemExit) as pytest_wrapped_e: | ||
ledd.main() | ||
assert pytest_wrapped_e.type == SystemExit | ||
assert pytest_wrapped_e.value.code == 0 | ||
out, err = capsys.readouterr() | ||
assert out.rstrip() == ledd.USAGE_HELP.rstrip() | ||
|
||
|
||
def test_version_args(capsys): | ||
for flag in ['-v', '--version']: | ||
with patch.object(sys, 'argv', ['ledd', flag]): | ||
with pytest.raises(SystemExit) as pytest_wrapped_e: | ||
ledd.main() | ||
assert pytest_wrapped_e.type == SystemExit | ||
assert pytest_wrapped_e.value.code == 0 | ||
out, err = capsys.readouterr() | ||
assert out.rstrip() == 'ledd version {}'.format(ledd.VERSION) | ||
|
||
|
||
def test_bad_args(capsys): | ||
for flag in ['-n', '--nonexistent']: | ||
with patch.object(sys, 'argv', ['ledd', flag]): | ||
with pytest.raises(SystemExit) as pytest_wrapped_e: | ||
ledd.main() | ||
assert pytest_wrapped_e.type == SystemExit | ||
assert pytest_wrapped_e.value.code == 1 | ||
out, err = capsys.readouterr() | ||
assert out.rstrip().endswith(ledd.USAGE_HELP.rstrip()) | ||
|
||
|
||
class TestDaemonLedd(object): | ||
""" | ||
Test cases to cover functionality in DaemonLedd class | ||
""" | ||
|
||
def test_run(self): | ||
daemon_ledd = ledd.DaemonLedd(SYSLOG_IDENTIFIER) | ||
# TODO: Add more coverage |