Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 5 additions & 3 deletions host_modules/debug_info.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,14 +40,16 @@


DEBUG_INFO_FLAG = "debug_info"
DEFAULT_HOSTNAME = "switch"

logger = logging.getLogger(__name__)

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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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")
Expand Down
17 changes: 14 additions & 3 deletions tests/debug_info_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down Expand Up @@ -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")

Expand All @@ -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):
Expand Down
Loading