Skip to content

Commit

Permalink
Mock path early so it will applied to sonic_py_common, mock more swss…
Browse files Browse the repository at this point in the history
…common classes (#187)

#### Description
Existing test will mix true swsscommon package with the mocked one in sonic_py_common

#### Motivation and Context
This is blocking #7655

#### How Has This Been Tested?
Unit test
  • Loading branch information
qiluo-msft authored May 21, 2021
1 parent 1adf47b commit 9ba52a2
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 6 deletions.
2 changes: 2 additions & 0 deletions sonic-thermalctld/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
18 changes: 12 additions & 6 deletions sonic-thermalctld/tests/test_thermalctld.py
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand Down

0 comments on commit 9ba52a2

Please sign in to comment.