Skip to content

Commit

Permalink
[sonic-py-common] Clear environment variables before running device_i…
Browse files Browse the repository at this point in the history
…nfo tests (#7273)

#### Why I did it

To ensure any environment variables which are configured in the build/test environment do not influence the behavior of sonic-py-common during unit tests. For example, variables which might be set by continuous integration pipelines.

#### How I did it

Add class-scoped pytest fixture to `TestDeviceInfo` class which stashes the current environment variables, clears them and yields. Once all the test cases in the class finish, the fixture will restore the original environment variables.

Also remove unnecessary unittest-style setup and teardown functions from interface_test.py
  • Loading branch information
jleveque authored Jun 28, 2021
1 parent 5b01069 commit 9f114ca
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 11 deletions.
11 changes: 8 additions & 3 deletions src/sonic-py-common/tests/device_info_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
# https://pypi.python.org/pypi/mock
import mock

import pytest

from sonic_py_common import device_info

from .mock_swsssdk import SonicV2Connector
Expand Down Expand Up @@ -51,9 +53,12 @@
}

class TestDeviceInfo(object):
@classmethod
def setup_class(cls):
print("SETUP")
@pytest.fixture(scope="class", autouse=True)
def sanitize_environment(self):
# Clear environment variables, in case a variable is set in the test
# environment (e.g., PLATFORM) which could modify the behavior of sonic-py-common
with mock.patch.dict(os.environ, {}, clear=True):
yield

def test_get_machine_info(self):
with mock.patch("os.path.isfile") as mock_isfile:
Expand Down
8 changes: 0 additions & 8 deletions src/sonic-py-common/tests/interface_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,6 @@
from sonic_py_common import interface

class TestInterface(object):
@classmethod
def setup_class(cls):
print("SETUP")

def test_get_interface_table_name(self):
result = interface.get_interface_table_name("Ethernet0")
assert result == "INTERFACE"
Expand Down Expand Up @@ -35,7 +31,3 @@ def test_get_port_table_name(self):
assert result == "VLAN_INTERFACE"
result = interface.get_port_table_name("Loopback0")
assert result == "LOOPBACK_INTERFACE"

@classmethod
def teardown_class(cls):
print("TEARDOWN")

0 comments on commit 9f114ca

Please sign in to comment.