From e61b5fe2ad302f31d6323b6c197bce51d9efcf3e Mon Sep 17 00:00:00 2001 From: Hemanth Kumar Tirupati Date: Wed, 8 Jul 2026 00:49:11 +0300 Subject: [PATCH 1/2] Lazy connect to config_db during first collect instead of during init Signed-off-by: Hemanth Kumar Tirupati --- host_modules/debug_info.py | 3 ++- tests/debug_info_test.py | 13 ++++++++++++- 2 files changed, 14 insertions(+), 2 deletions(-) diff --git a/host_modules/debug_info.py b/host_modules/debug_info.py index 4ab309f8..cb4fd369 100644 --- a/host_modules/debug_info.py +++ b/host_modules/debug_info.py @@ -47,7 +47,8 @@ class DebugArtifactCollector(host_service.HostModule): """DBus endpoint that collects debug artifacts.""" def __init__(self, mod_name): - self._hostname, self._board_type = DebugArtifactCollector.get_device_metadata() + self._hostname = "switch" + self._board_type = "" super(DebugArtifactCollector, self).__init__(mod_name) @staticmethod diff --git a/tests/debug_info_test.py b/tests/debug_info_test.py index 70ec050e..8cc191d2 100644 --- a/tests/debug_info_test.py +++ b/tests/debug_info_test.py @@ -43,7 +43,18 @@ def setup_method(self): def teardown_method(self): self.tmpdir.cleanup() - + + def test_init_does_not_read_device_metadata(self): + metadata_mock_path = ( + "debug_info.DebugArtifactCollector.get_device_metadata") + with mock.patch(metadata_mock_path) as mock_get_metadata, \ + mock.patch("debug_info.super"): + debug_info_module = DebugArtifactCollector(MOD_NAME) + + assert debug_info_module._hostname == "switch" + assert debug_info_module._board_type == "" + mock_get_metadata.assert_not_called() + def test_run_command_success(self): with mock.patch("debug_info.subprocess.Popen") as mp: proc = mock.Mock() From 1ed5c6eaf3a5a0ffb49b60d702e9e9f440abcaea Mon Sep 17 00:00:00 2001 From: Hemanth Kumar Tirupati Date: Fri, 10 Jul 2026 03:26:58 +0300 Subject: [PATCH 2/2] Create constant for default switch name Signed-off-by: Hemanth Kumar Tirupati --- host_modules/debug_info.py | 7 ++++--- tests/debug_info_test.py | 6 +++--- 2 files changed, 7 insertions(+), 6 deletions(-) diff --git a/host_modules/debug_info.py b/host_modules/debug_info.py index cb4fd369..08788180 100644 --- a/host_modules/debug_info.py +++ b/host_modules/debug_info.py @@ -40,6 +40,7 @@ DEBUG_INFO_FLAG = "debug_info" +DEFAULT_HOSTNAME = "switch" logger = logging.getLogger(__name__) @@ -47,7 +48,7 @@ class DebugArtifactCollector(host_service.HostModule): """DBus endpoint that collects debug artifacts.""" def __init__(self, mod_name): - self._hostname = "switch" + self._hostname = DEFAULT_HOSTNAME self._board_type = "" super(DebugArtifactCollector, self).__init__(mod_name) @@ -100,7 +101,7 @@ def get_device_metadata() -> str: Get hostname and board_type from CONFIG_DB using a single swsscommon call. """ - hostname = "switch" + hostname = DEFAULT_HOSTNAME board_type = "" try: # Connect to CONFIG_DB @@ -361,7 +362,7 @@ def collect(self, options): except json.JSONDecodeError: return 1, "invalid input: " + options[0] - if not self._board_type or self._hostname == "switch": + if not self._board_type or self._hostname == DEFAULT_HOSTNAME: self._hostname, self._board_type = DebugArtifactCollector.get_device_metadata() timestamp = datetime.now().strftime("%Y%m%d_%H%M%S%f") diff --git a/tests/debug_info_test.py b/tests/debug_info_test.py index 8cc191d2..7fdd28ea 100644 --- a/tests/debug_info_test.py +++ b/tests/debug_info_test.py @@ -51,7 +51,7 @@ def test_init_does_not_read_device_metadata(self): mock.patch("debug_info.super"): debug_info_module = DebugArtifactCollector(MOD_NAME) - assert debug_info_module._hostname == "switch" + assert debug_info_module._hostname == DEFAULT_HOSTNAME assert debug_info_module._board_type == "" mock_get_metadata.assert_not_called() @@ -82,7 +82,7 @@ def test_get_device_metadata_failure(self): instance = mock_conn.return_value instance.get_all.side_effect = Exception("db error") hostname, board_type = self.debug_info_module.get_device_metadata() - assert hostname == "switch" + assert hostname == DEFAULT_HOSTNAME assert board_type == "" mock_warn.assert_called_with("Failed to read hostname/board_type from CONFIG_DB: db error") @@ -102,7 +102,7 @@ def test_get_device_metadata_missing_fields(self): instance = mock_conn.return_value instance.get_all.return_value = {} hostname, board_type = self.debug_info_module.get_device_metadata() - assert hostname == "switch" + assert hostname == DEFAULT_HOSTNAME assert board_type == "" def test_collect_invalid_json(self):