From 9ba52a2ec66a2a2b5e24d6553f0f3af798b0018c Mon Sep 17 00:00:00 2001 From: Qi Luo Date: Fri, 21 May 2021 15:24:55 -0700 Subject: [PATCH] Mock path early so it will applied to sonic_py_common, mock more swsscommon classes (#187) #### Description Existing test will mix true swsscommon package with the mocked one in sonic_py_common #### Motivation and Context This is blocking https://github.com/Azure/sonic-buildimage/pull/7655 #### How Has This Been Tested? Unit test --- .../tests/mocked_libs/swsscommon/swsscommon.py | 2 ++ sonic-thermalctld/tests/test_thermalctld.py | 18 ++++++++++++------ 2 files changed, 14 insertions(+), 6 deletions(-) diff --git a/sonic-thermalctld/tests/mocked_libs/swsscommon/swsscommon.py b/sonic-thermalctld/tests/mocked_libs/swsscommon/swsscommon.py index 6947a8601819..13c49dec139a 100644 --- a/sonic-thermalctld/tests/mocked_libs/swsscommon/swsscommon.py +++ b/sonic-thermalctld/tests/mocked_libs/swsscommon/swsscommon.py @@ -2,6 +2,8 @@ Mock implementation of swsscommon package for unit testing ''' +from swsssdk import ConfigDBConnector, SonicDBConfig, SonicV2Connector + STATE_DB = '' diff --git a/sonic-thermalctld/tests/test_thermalctld.py b/sonic-thermalctld/tests/test_thermalctld.py index 7c272826b7d6..063e11c22387 100644 --- a/sonic-thermalctld/tests/test_thermalctld.py +++ b/sonic-thermalctld/tests/test_thermalctld.py @@ -10,18 +10,24 @@ import mock import pytest -from sonic_py_common import daemon_base - -from .mock_platform import MockChassis, MockFan, MockPsu, MockSfp, MockThermal - -daemon_base.db_connect = mock.MagicMock() - tests_path = os.path.dirname(os.path.abspath(__file__)) # Add mocked_libs path so that the file under test can load mocked modules from there mocked_libs_path = os.path.join(tests_path, 'mocked_libs') sys.path.insert(0, mocked_libs_path) + +import swsscommon +# Check we are using the mocked package +assert len(swsscommon.__path__) == 1 +assert(os.path.samefile(swsscommon.__path__[0], os.path.join(mocked_libs_path, 'swsscommon'))) + +from sonic_py_common import daemon_base + +from .mock_platform import MockChassis, MockFan, MockPsu, MockSfp, MockThermal + +daemon_base.db_connect = mock.MagicMock() + # Add path to the file under test so that we can load it modules_path = os.path.dirname(tests_path) scripts_path = os.path.join(modules_path, 'scripts')