Skip to content

Commit

Permalink
Mock path early so it will applied to sonic_py_common, mock platform_…
Browse files Browse the repository at this point in the history
…chassis (sonic-net#188)

#### Description
Following sonic-net/sonic-platform-daemons#187 to fix the same issue with other packages

#### Motivation and Context
This is blocking sonic-net#7655

#### How Has This Been Tested?
Unit test
  • Loading branch information
qiluo-msft authored May 24, 2021
1 parent 9ba52a2 commit 9297a29
Show file tree
Hide file tree
Showing 5 changed files with 15 additions and 6 deletions.
2 changes: 2 additions & 0 deletions sonic-psud/tests/mocked_libs/swsscommon/swsscommon.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
Mock implementation of swsscommon package for unit testing
'''

from swsssdk import ConfigDBConnector, SonicDBConfig, SonicV2Connector

STATE_DB = ''


Expand Down
5 changes: 3 additions & 2 deletions sonic-psud/tests/test_DaemonPsud.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,21 +10,22 @@
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__))

# 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")
Expand Down
3 changes: 2 additions & 1 deletion sonic-psud/tests/test_psud.py
Original file line number Diff line number Diff line change
Expand Up @@ -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__))

Expand Down Expand Up @@ -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
Expand Down
5 changes: 5 additions & 0 deletions sonic-syseepromd/tests/mocked_libs/swsscommon/swsscommon.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
Mock implementation of swsscommon package for unit testing
'''

from swsssdk import ConfigDBConnector, SonicDBConfig, SonicV2Connector

STATE_DB = ''


Expand All @@ -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 = {}
Expand Down
6 changes: 3 additions & 3 deletions sonic-syseepromd/tests/test_syseepromd.py
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand Down

0 comments on commit 9297a29

Please sign in to comment.