diff --git a/sonic-thermalctld/tests/mock_swsscommon.py b/sonic-thermalctld/tests/mock_swsscommon.py deleted file mode 100644 index ade0d3541..000000000 --- a/sonic-thermalctld/tests/mock_swsscommon.py +++ /dev/null @@ -1,34 +0,0 @@ -''' - Mock implementation of swsscommon package for unit testing -''' - -STATE_DB = '' -CHASSIS_STATE_DB = '' - - -class Table: - def __init__(self, db, table_name): - self.table_name = table_name - self.mock_dict = {} - - def _del(self, key): - del self.mock_dict[key] - pass - - def set(self, key, fvs): - self.mock_dict[key] = fvs.fv_dict - pass - - def get(self, key): - if key in self.mock_dict: - return self.mock_dict[key] - return None - - def get_size(self): - return (len(self.mock_dict)) - - -class FieldValuePairs: - def __init__(self, fvs): - self.fv_dict = dict(fvs) - pass diff --git a/sonic-thermalctld/tests/mocked_libs/swsscommon/swsscommon.py b/sonic-thermalctld/tests/mocked_libs/swsscommon/swsscommon.py index 13c49dec1..41e5e0fda 100644 --- a/sonic-thermalctld/tests/mocked_libs/swsscommon/swsscommon.py +++ b/sonic-thermalctld/tests/mocked_libs/swsscommon/swsscommon.py @@ -5,6 +5,7 @@ from swsssdk import ConfigDBConnector, SonicDBConfig, SonicV2Connector STATE_DB = '' +CHASSIS_STATE_DB = '' class Table: @@ -28,6 +29,9 @@ def get(self, key): def get_size(self): return (len(self.mock_dict)) + def getKeys(self): + return list(self.mock_dict.keys()) + class FieldValuePairs: fv_dict = {} diff --git a/sonic-thermalctld/tests/test_thermalctld.py b/sonic-thermalctld/tests/test_thermalctld.py index 097fe1be0..ec6faf4b7 100644 --- a/sonic-thermalctld/tests/test_thermalctld.py +++ b/sonic-thermalctld/tests/test_thermalctld.py @@ -1,13 +1,11 @@ import os import sys import multiprocessing -from imp import load_source # TODO: Replace with importlib once we no longer need to support Python 2 +import importlib.machinery +import importlib.util + +from unittest import mock -# TODO: Clean this up once we no longer need to support Python 2 -if sys.version_info.major == 3: - from unittest import mock -else: - import mock import pytest tests_path = os.path.dirname(os.path.abspath(__file__)) @@ -25,7 +23,10 @@ from sonic_py_common import daemon_base from .mock_platform import MockChassis, MockFan, MockModule, MockPsu, MockSfp, MockThermal -from .mock_swsscommon import Table +from .mocked_libs.swsscommon import swsscommon + +Table = swsscommon.Table +FieldValuePairs = swsscommon.FieldValuePairs daemon_base.db_connect = mock.MagicMock() @@ -34,6 +35,16 @@ scripts_path = os.path.join(modules_path, 'scripts') sys.path.insert(0, modules_path) +# Replacement for imp.load_source from the Python3.12 docs: +# https://docs.python.org/3/whatsnew/3.12.html#imp +def load_source(modname, filename): + loader = importlib.machinery.SourceFileLoader(modname, filename) + spec = importlib.util.spec_from_file_location(modname, filename, loader=loader) + module = importlib.util.module_from_spec(spec) + sys.modules[module.__name__] = module + loader.exec_module(module) + return module + load_source('thermalctld', os.path.join(scripts_path, 'thermalctld')) import thermalctld