diff --git a/host_modules/debug_info.py b/host_modules/debug_info.py index 4ab309f8..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,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 = DEFAULT_HOSTNAME + self._board_type = "" super(DebugArtifactCollector, self).__init__(mod_name) @staticmethod @@ -99,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 @@ -360,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 70ec050e..7fdd28ea 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 == DEFAULT_HOSTNAME + 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() @@ -71,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") @@ -91,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):