diff --git a/sonic-thermalctld/tests/mock_swsscommon.py b/sonic-thermalctld/tests/mock_swsscommon.py index ade0d3541..102063b6b 100644 --- a/sonic-thermalctld/tests/mock_swsscommon.py +++ b/sonic-thermalctld/tests/mock_swsscommon.py @@ -2,33 +2,7 @@ Mock implementation of swsscommon package for unit testing ''' -STATE_DB = '' -CHASSIS_STATE_DB = '' +from .mocked_libs.swsscommon import swsscommon - -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 +Table = swsscommon.Table +FieldValuePairs = swsscommon.FieldValuePairs diff --git a/sonic-thermalctld/tests/mocked_libs/swsscommon/swsscommon.py b/sonic-thermalctld/tests/mocked_libs/swsscommon/swsscommon.py index 13c49dec1..f75d60a74 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: @@ -27,6 +28,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: diff --git a/sonic-thermalctld/tests/test_thermalctld.py b/sonic-thermalctld/tests/test_thermalctld.py index 097fe1be0..20d596806 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__)) @@ -34,6 +32,15 @@ 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) + load_source('thermalctld', os.path.join(scripts_path, 'thermalctld')) import thermalctld