From aaeba3fe50548beb9204ea31724337d915da9da3 Mon Sep 17 00:00:00 2001 From: Vaibhav Hemant Dixit Date: Wed, 30 Dec 2020 18:53:53 +0000 Subject: [PATCH 1/2] Handle unkown expceptions encountered in platform lib --- src/sonic-host-services/scripts/determine-reboot-cause | 3 +++ 1 file changed, 3 insertions(+) diff --git a/src/sonic-host-services/scripts/determine-reboot-cause b/src/sonic-host-services/scripts/determine-reboot-cause index 501b279ec192..fdc05f6284f0 100755 --- a/src/sonic-host-services/scripts/determine-reboot-cause +++ b/src/sonic-host-services/scripts/determine-reboot-cause @@ -111,6 +111,9 @@ def find_hardware_reboot_cause(): except ImportError: sonic_logger.log_warning("sonic_platform package not installed. Unable to detect hardware reboot causes.") hardware_reboot_cause_major, hardware_reboot_cause_minor = REBOOT_CAUSE_NON_HARDWARE, "N/A" + except: + sonic_logger.log_warning("Unknown exception caught in platform library. Unable to detect hardware reboot causes.") + hardware_reboot_cause_major, hardware_reboot_cause_minor = REBOOT_CAUSE_NON_HARDWARE, "N/A" if hardware_reboot_cause_major: sonic_logger.log_info("Platform api indicates reboot cause {}".format(hardware_reboot_cause_major)) From 1eb56bf3c6d3f62c7c15f0be5fcfb1b2de3db029 Mon Sep 17 00:00:00 2001 From: Vaibhav Hemant Dixit Date: Wed, 30 Dec 2020 20:50:30 +0000 Subject: [PATCH 2/2] Add mock function to bypass the platform module code for unti tests --- src/sonic-host-services/scripts/determine-reboot-cause | 10 ++++++---- .../tests/determine-reboot-cause_test.py | 5 +++-- 2 files changed, 9 insertions(+), 6 deletions(-) diff --git a/src/sonic-host-services/scripts/determine-reboot-cause b/src/sonic-host-services/scripts/determine-reboot-cause index fdc05f6284f0..8439c78a7016 100755 --- a/src/sonic-host-services/scripts/determine-reboot-cause +++ b/src/sonic-host-services/scripts/determine-reboot-cause @@ -100,7 +100,7 @@ def find_proc_cmdline_reboot_cause(): return proc_cmdline_reboot_cause -def find_hardware_reboot_cause(): +def get_reboot_cause_from_platform(): # Find hardware reboot cause using sonic_platform library try: import sonic_platform @@ -111,10 +111,12 @@ def find_hardware_reboot_cause(): except ImportError: sonic_logger.log_warning("sonic_platform package not installed. Unable to detect hardware reboot causes.") hardware_reboot_cause_major, hardware_reboot_cause_minor = REBOOT_CAUSE_NON_HARDWARE, "N/A" - except: - sonic_logger.log_warning("Unknown exception caught in platform library. Unable to detect hardware reboot causes.") - hardware_reboot_cause_major, hardware_reboot_cause_minor = REBOOT_CAUSE_NON_HARDWARE, "N/A" + return hardware_reboot_cause_major, hardware_reboot_cause_minor + + +def find_hardware_reboot_cause(): + hardware_reboot_cause_major, hardware_reboot_cause_minor = get_reboot_cause_from_platform() if hardware_reboot_cause_major: sonic_logger.log_info("Platform api indicates reboot cause {}".format(hardware_reboot_cause_major)) else: diff --git a/src/sonic-host-services/tests/determine-reboot-cause_test.py b/src/sonic-host-services/tests/determine-reboot-cause_test.py index 31f36dd1309d..afd74957b6f4 100644 --- a/src/sonic-host-services/tests/determine-reboot-cause_test.py +++ b/src/sonic-host-services/tests/determine-reboot-cause_test.py @@ -99,8 +99,9 @@ def test_find_proc_cmdline_reboot_cause(self): assert result == "fast-reboot" def test_find_hardware_reboot_cause(self): - result = find_hardware_reboot_cause() - assert result == "Non-Hardware (N/A)" + with mock.patch("determine_reboot_cause.get_reboot_cause_from_platform", return_value=("Powerloss", None)): + result = find_hardware_reboot_cause() + assert result == "Powerloss (None)" def test_get_reboot_cause_dict_watchdog(self): reboot_cause_dict = get_reboot_cause_dict(REBOOT_CAUSE_WATCHDOG, "", GEN_TIME_WATCHDOG)