From f09bd311a1c51d487c6588a4e5039a2f7e4059e3 Mon Sep 17 00:00:00 2001 From: liuh-80 <58683130+liuh-80@users.noreply.github.com> Date: Thu, 17 Mar 2022 13:56:22 +0800 Subject: [PATCH] Fix UT failed cause by change pycommon to use swsscommon (#246) Fix UT failed cause by change sonic_py_common to use swss_common #### Description Fix UT failed cause by change sonic_py_common to use swss_common: In current UT, we implement swsscommon mock class under sonic-pcied/tests/mocked_libs/swsscommon and load these mock libs by add the mock path in front of real swsscommon lib path with following code: # 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) However, because we change sonic_py_common to use swsscommon, so real version of swsscommon been load before we load mock lib. then the UT break with following error message: ______________ TestDaemonPcied.test_update_pcie_devices_status_db ______________ self = @mock.patch('pcied.load_platform_pcieutil', mock.MagicMock()) def test_update_pcie_devices_status_db(self): > daemon_pcied = pcied.DaemonPcied(SYSLOG_IDENTIFIER) tests/test_DaemonPcied.py:160: _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ scripts/pcied:93: in __init__ self.device_table = swsscommon.Table(self.state_db, PCIE_DEVICE_TABLE_NAME) _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ self = args = (, 'PCIE_DEVICE') def __init__(self, *args): > this = _swsscommon.new_Table(*args) E NotImplementedError: Wrong number or type of arguments for overloaded function 'new_Table'. E Possible C/C++ prototypes are: E swss::Table::Table(swss::DBConnector const *,std::string const &) E swss::Table::Table(swss::RedisPipeline *,std::string const &,bool) #### Motivation and Context pyswss will be deprecate, so sonic_py_common will changed to use swss_common. Some UT in this project failed because this change. #### How Has This Been Tested? Pass all UT and sonic-buildimage E2E test. #### Additional Information (Optional) --- sonic-pcied/tests/mocked_libs/swsscommon/swsscommon.py | 9 +++++++++ sonic-pcied/tests/test_DaemonPcied.py | 6 +++--- sonic-pcied/tests/test_pcied.py | 2 +- 3 files changed, 13 insertions(+), 4 deletions(-) diff --git a/sonic-pcied/tests/mocked_libs/swsscommon/swsscommon.py b/sonic-pcied/tests/mocked_libs/swsscommon/swsscommon.py index 6947a8601..e5aa2a2ba 100644 --- a/sonic-pcied/tests/mocked_libs/swsscommon/swsscommon.py +++ b/sonic-pcied/tests/mocked_libs/swsscommon/swsscommon.py @@ -52,3 +52,12 @@ def __repr__(self): def __str__(self): return repr(self.fv_dict) + +class ConfigDBConnector: + pass + +class SonicDBConfig: + pass + +class SonicV2Connector: + pass diff --git a/sonic-pcied/tests/test_DaemonPcied.py b/sonic-pcied/tests/test_DaemonPcied.py index c8c17a276..2c3c953e7 100644 --- a/sonic-pcied/tests/test_DaemonPcied.py +++ b/sonic-pcied/tests/test_DaemonPcied.py @@ -11,14 +11,11 @@ else: import mock -from sonic_py_common import daemon_base - from .mock_platform import MockPcieUtil SYSLOG_IDENTIFIER = 'pcied_test' NOT_AVAILABLE = 'N/A' -daemon_base.db_connect = mock.MagicMock() tests_path = os.path.dirname(os.path.abspath(__file__)) @@ -26,6 +23,9 @@ mocked_libs_path = os.path.join(tests_path, "mocked_libs") sys.path.insert(0, mocked_libs_path) +from sonic_py_common import daemon_base +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") diff --git a/sonic-pcied/tests/test_pcied.py b/sonic-pcied/tests/test_pcied.py index a99331846..3850d38ed 100644 --- a/sonic-pcied/tests/test_pcied.py +++ b/sonic-pcied/tests/test_pcied.py @@ -10,7 +10,6 @@ else: from mock import MagicMock, patch, mock_open -from sonic_py_common import daemon_base, device_info from .mock_platform import MockPcieUtil tests_path = os.path.dirname(os.path.abspath(__file__)) @@ -18,6 +17,7 @@ # 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) +from sonic_py_common import daemon_base, device_info # Add path to the file under test so that we can load it modules_path = os.path.dirname(tests_path)