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.
[sonic-py-common] Add unit test framework (sonic-net#5238)
**- Why I did it** To install the framework for adding unit tests to the sonic-py-common package and report coverage. ** How I did it ** - Incorporate pytest and pytest-cov into sonic-py-common package build - Updgrade version of 'mock' installed to version 3.0.5, the last version which supports Python 2. This fixes a bug where the file object returned from `mock_open()` was not iterable (see https://bugs.python.org/issue32933) - Add support for Python 3 setuptools and pytest in sonic-slave-buster environment - Add tests for `device_info.get_machine_info()` and `device_info.get_platform()` functions - Also add a .gitignore in the root of the sonic-py-common directory, move all related ignores from main .gitignore file, and add ignores for files and dirs generated by pytest-cov
- Loading branch information
Showing
9 changed files
with
119 additions
and
16 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,13 @@ | ||
**/*.pyc | ||
|
||
# Distribution / packaging | ||
*.egg-info/ | ||
.eggs/ | ||
build/ | ||
dist/ | ||
|
||
# Unit test / coverage reports | ||
.cache | ||
.coverage | ||
coverage.xml | ||
htmlcov/ |
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=sonic_py_common --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
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
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,75 @@ | ||
import os | ||
import sys | ||
|
||
# TODO: Remove this if/else block once we no longer support Python 2 | ||
if sys.version_info.major == 3: | ||
from unittest import mock | ||
else: | ||
# Expect the 'mock' package for python 2 | ||
# https://pypi.python.org/pypi/mock | ||
import mock | ||
|
||
from sonic_py_common import device_info | ||
|
||
|
||
# TODO: Remove this if/else block once we no longer support Python 2 | ||
if sys.version_info.major == 3: | ||
BUILTINS = "builtins" | ||
else: | ||
BUILTINS = "__builtin__" | ||
|
||
MACHINE_CONF_CONTENTS = """\ | ||
onie_version=2016.11-5.1.0008-9600 | ||
onie_vendor_id=33049 | ||
onie_machine_rev=0 | ||
onie_arch=x86_64 | ||
onie_config_version=1 | ||
onie_build_date="2017-04-26T11:01+0300" | ||
onie_partition_type=gpt | ||
onie_kernel_version=4.10.11 | ||
onie_firmware=auto | ||
onie_switch_asic=mlnx | ||
onie_skip_ethmgmt_macs=yes | ||
onie_machine=mlnx_msn2700 | ||
onie_platform=x86_64-mlnx_msn2700-r0""" | ||
|
||
EXPECTED_GET_MACHINE_INFO_RESULT = { | ||
'onie_arch': 'x86_64', | ||
'onie_skip_ethmgmt_macs': 'yes', | ||
'onie_platform': 'x86_64-mlnx_msn2700-r0', | ||
'onie_machine_rev': '0', | ||
'onie_version': '2016.11-5.1.0008-9600', | ||
'onie_machine': 'mlnx_msn2700', | ||
'onie_config_version': '1', | ||
'onie_partition_type': 'gpt', | ||
'onie_build_date': '"2017-04-26T11:01+0300"', | ||
'onie_switch_asic': 'mlnx', | ||
'onie_vendor_id': '33049', | ||
'onie_firmware': 'auto', | ||
'onie_kernel_version': '4.10.11' | ||
} | ||
|
||
|
||
class TestDeviceInfo(object): | ||
@classmethod | ||
def setup_class(cls): | ||
print("SETUP") | ||
|
||
def test_get_machine_info(self): | ||
with mock.patch("os.path.isfile") as mock_isfile: | ||
mock_isfile.return_value = True | ||
open_mocked = mock.mock_open(read_data=MACHINE_CONF_CONTENTS) | ||
with mock.patch("{}.open".format(BUILTINS), open_mocked): | ||
result = device_info.get_machine_info() | ||
assert result == EXPECTED_GET_MACHINE_INFO_RESULT | ||
open_mocked.assert_called_once_with("/host/machine.conf") | ||
|
||
def test_get_platform(self): | ||
with mock.patch("sonic_py_common.device_info.get_machine_info") as get_machine_info_mocked: | ||
get_machine_info_mocked.return_value = EXPECTED_GET_MACHINE_INFO_RESULT | ||
result = device_info.get_platform() | ||
assert result == "x86_64-mlnx_msn2700-r0" | ||
|
||
@classmethod | ||
def teardown_class(cls): | ||
print("TEARDOWN") |