From 9297a294b5e6cde0cbfa05d51b185070476dd6c9 Mon Sep 17 00:00:00 2001 From: Qi Luo Date: Mon, 24 May 2021 08:42:49 -0700 Subject: [PATCH] Mock path early so it will applied to sonic_py_common, mock platform_chassis (#188) #### Description Following https://github.com/Azure/sonic-platform-daemons/pull/187 to fix the same issue with other packages #### Motivation and Context This is blocking Azure/sonic-buildimage#7655 #### How Has This Been Tested? Unit test --- sonic-psud/tests/mocked_libs/swsscommon/swsscommon.py | 2 ++ sonic-psud/tests/test_DaemonPsud.py | 5 +++-- sonic-psud/tests/test_psud.py | 3 ++- sonic-syseepromd/tests/mocked_libs/swsscommon/swsscommon.py | 5 +++++ sonic-syseepromd/tests/test_syseepromd.py | 6 +++--- 5 files changed, 15 insertions(+), 6 deletions(-) diff --git a/sonic-psud/tests/mocked_libs/swsscommon/swsscommon.py b/sonic-psud/tests/mocked_libs/swsscommon/swsscommon.py index 6947a8601..13c49dec1 100644 --- a/sonic-psud/tests/mocked_libs/swsscommon/swsscommon.py +++ b/sonic-psud/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-psud/tests/test_DaemonPsud.py b/sonic-psud/tests/test_DaemonPsud.py index 0234ec79f..06033f36e 100644 --- a/sonic-psud/tests/test_DaemonPsud.py +++ b/sonic-psud/tests/test_DaemonPsud.py @@ -10,14 +10,12 @@ from unittest import mock else: import mock -from sonic_py_common import daemon_base from .mock_platform import MockChassis, MockFan, MockPsu SYSLOG_IDENTIFIER = 'psud_test' NOT_AVAILABLE = 'N/A' -daemon_base.db_connect = mock.MagicMock() tests_path = os.path.dirname(os.path.abspath(__file__)) @@ -25,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-psud/tests/test_psud.py b/sonic-psud/tests/test_psud.py index a66a8c29a..405ec30c6 100644 --- a/sonic-psud/tests/test_psud.py +++ b/sonic-psud/tests/test_psud.py @@ -11,7 +11,7 @@ import mock from sonic_py_common import daemon_base -from .mock_platform import MockPsu +from .mock_platform import MockPsu, MockChassis tests_path = os.path.dirname(os.path.abspath(__file__)) @@ -179,6 +179,7 @@ def test_log_on_status_changed(): mock_logger.log_warning.assert_called_with(abnormal_log) +@mock.patch('psud.platform_chassis', mock.MagicMock()) @mock.patch('psud.DaemonPsud.run') def test_main(mock_run): mock_run.return_value = False diff --git a/sonic-syseepromd/tests/mocked_libs/swsscommon/swsscommon.py b/sonic-syseepromd/tests/mocked_libs/swsscommon/swsscommon.py index 8a0a87692..13c49dec1 100644 --- a/sonic-syseepromd/tests/mocked_libs/swsscommon/swsscommon.py +++ b/sonic-syseepromd/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 = '' @@ -23,6 +25,9 @@ def get(self, key): return self.mock_dict[key] return None + def get_size(self): + return (len(self.mock_dict)) + class FieldValuePairs: fv_dict = {} diff --git a/sonic-syseepromd/tests/test_syseepromd.py b/sonic-syseepromd/tests/test_syseepromd.py index e25b94ce3..4ac106b8a 100644 --- a/sonic-syseepromd/tests/test_syseepromd.py +++ b/sonic-syseepromd/tests/test_syseepromd.py @@ -9,19 +9,19 @@ from unittest import mock else: import mock -from sonic_py_common import daemon_base SYSLOG_IDENTIFIER = 'syseepromd_test' NOT_AVAILABLE = 'N/A' -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) +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')