From 753b55029b120ed3109fd5506ffc3dec3bb1d961 Mon Sep 17 00:00:00 2001 From: judyjoseph <53951155+judyjoseph@users.noreply.github.com> Date: Tue, 17 Jan 2023 13:10:09 -0800 Subject: [PATCH] Chassisd do an explicit stop of the config_manager (#328) * Fix to explicit stop the config_manager * Add tests for chassisd run method. --- sonic-chassisd/scripts/chassisd | 3 +++ sonic-chassisd/tests/mock_swsscommon.py | 3 ++- .../mocked_libs/sonic_platform/__init__.py | 6 +++++ .../mocked_libs/sonic_platform/chassis.py | 26 +++++++++++++++++++ .../mocked_libs/sonic_platform/platform.py | 12 +++++++++ sonic-chassisd/tests/test_chassisd.py | 12 +++++++++ 6 files changed, 61 insertions(+), 1 deletion(-) create mode 100644 sonic-chassisd/tests/mocked_libs/sonic_platform/__init__.py create mode 100644 sonic-chassisd/tests/mocked_libs/sonic_platform/chassis.py create mode 100644 sonic-chassisd/tests/mocked_libs/sonic_platform/platform.py diff --git a/sonic-chassisd/scripts/chassisd b/sonic-chassisd/scripts/chassisd index d465c0209525..384d044f58e6 100644 --- a/sonic-chassisd/scripts/chassisd +++ b/sonic-chassisd/scripts/chassisd @@ -444,6 +444,9 @@ class ChassisdDaemon(daemon_base.DaemonBase): self.log_info("Stop daemon main loop") + if config_manager is not None: + config_manager.task_stop() + # Delete all the information from DB and then exit self.module_updater.deinit() diff --git a/sonic-chassisd/tests/mock_swsscommon.py b/sonic-chassisd/tests/mock_swsscommon.py index 58908422508e..dbc42e13f6c7 100644 --- a/sonic-chassisd/tests/mock_swsscommon.py +++ b/sonic-chassisd/tests/mock_swsscommon.py @@ -7,7 +7,8 @@ def __init__(self, db, table_name): self.mock_dict = {} def _del(self, key): - del self.mock_dict[key] + if key in self.mock_dict: + del self.mock_dict[key] pass def set(self, key, fvs): diff --git a/sonic-chassisd/tests/mocked_libs/sonic_platform/__init__.py b/sonic-chassisd/tests/mocked_libs/sonic_platform/__init__.py new file mode 100644 index 000000000000..e491d5b52166 --- /dev/null +++ b/sonic-chassisd/tests/mocked_libs/sonic_platform/__init__.py @@ -0,0 +1,6 @@ +""" + Mock implementation of sonic_platform package for unit testing +""" + +from . import chassis +from . import platform diff --git a/sonic-chassisd/tests/mocked_libs/sonic_platform/chassis.py b/sonic-chassisd/tests/mocked_libs/sonic_platform/chassis.py new file mode 100644 index 000000000000..2410afde20f8 --- /dev/null +++ b/sonic-chassisd/tests/mocked_libs/sonic_platform/chassis.py @@ -0,0 +1,26 @@ +""" + Mock implementation of sonic_platform package for unit testing +""" + +import sys +if sys.version_info.major == 3: + from unittest import mock +else: + import mock + +from sonic_platform_base.chassis_base import ChassisBase + + +class Chassis(ChassisBase): + def __init__(self): + ChassisBase.__init__(self) + self.eeprom = mock.MagicMock() + + def get_eeprom(self): + return self.eeprom + + def get_my_slot(self): + return 1 + + def get_supervisor_slot(self): + return 1 diff --git a/sonic-chassisd/tests/mocked_libs/sonic_platform/platform.py b/sonic-chassisd/tests/mocked_libs/sonic_platform/platform.py new file mode 100644 index 000000000000..a1b61e13e5a6 --- /dev/null +++ b/sonic-chassisd/tests/mocked_libs/sonic_platform/platform.py @@ -0,0 +1,12 @@ +""" + Mock implementation of sonic_platform package for unit testing +""" + +from sonic_platform_base.platform_base import PlatformBase +from sonic_platform.chassis import Chassis + + +class Platform(PlatformBase): + def __init__(self): + PlatformBase.__init__(self) + self._chassis = Chassis() diff --git a/sonic-chassisd/tests/test_chassisd.py b/sonic-chassisd/tests/test_chassisd.py index 484fe34a4992..1e12b8bbdefb 100644 --- a/sonic-chassisd/tests/test_chassisd.py +++ b/sonic-chassisd/tests/test_chassisd.py @@ -14,6 +14,11 @@ daemon_base.db_connect = MagicMock() test_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(test_path, 'mocked_libs') +sys.path.insert(0, mocked_libs_path) + modules_path = os.path.dirname(test_path) scripts_path = os.path.join(modules_path, "scripts") sys.path.insert(0, modules_path) @@ -503,3 +508,10 @@ def test_signal_handler(): assert daemon_chassisd.log_info.call_count == 0 assert daemon_chassisd.stop.set.call_count == 0 assert exit_code == 0 + +def test_daemon_run(): + # Test the chassisd run + daemon_chassisd = ChassisdDaemon(SYSLOG_IDENTIFIER) + daemon_chassisd.stop = MagicMock() + daemon_chassisd.stop.wait.return_value = True + daemon_chassisd.run()