From 25379d36740b75c3d43633ed13070aa18c02b381 Mon Sep 17 00:00:00 2001 From: sujinmkang Date: Tue, 16 Aug 2022 17:59:41 -0700 Subject: [PATCH] Add unit test case --- scripts/determine-reboot-cause | 43 ++++++++++++++++------------ tests/determine-reboot-cause_test.py | 33 +++++++++++++++++++++ 2 files changed, 57 insertions(+), 19 deletions(-) diff --git a/scripts/determine-reboot-cause b/scripts/determine-reboot-cause index 1e28356491a3..4320b41a7a02 100755 --- a/scripts/determine-reboot-cause +++ b/scripts/determine-reboot-cause @@ -158,25 +158,7 @@ def get_reboot_cause_dict(previous_reboot_cause, comment, gen_time): return reboot_cause_dict - -def main(): - # Configure logger to log all messages INFO level and higher - sonic_logger.set_min_log_priority_info() - - sonic_logger.log_info("Starting up...") - - if not os.geteuid() == 0: - sonic_logger.log_error("User {} does not have permission to execute".format(pwd.getpwuid(os.getuid()).pw_name)) - sys.exit("This utility must be run as root") - - # Create REBOOT_CAUSE_DIR if it doesn't exist - if not os.path.exists(REBOOT_CAUSE_DIR): - os.makedirs(REBOOT_CAUSE_DIR) - - # Remove stale PREVIOUS_REBOOT_CAUSE_FILE if it exists - if os.path.exists(PREVIOUS_REBOOT_CAUSE_FILE): - os.remove(PREVIOUS_REBOOT_CAUSE_FILE) - +def determin_reboot_cause(): # This variable is kept for future-use purpose. When proc_cmd_line/vendor/software provides # any additional_reboot_info it will be stored as a "comment" in REBOOT_CAUSE_HISTORY_FILE additional_reboot_info = "N/A" @@ -217,6 +199,29 @@ def main(): # Get the reboot cause from REBOOT_CAUSE_FILE previous_reboot_cause = software_reboot_cause + return previous_reboot_cause, additional_reboot_info + + +def main(): + # Configure logger to log all messages INFO level and higher + sonic_logger.set_min_log_priority_info() + + sonic_logger.log_info("Starting up...") + + if not os.geteuid() == 0: + sonic_logger.log_error("User {} does not have permission to execute".format(pwd.getpwuid(os.getuid()).pw_name)) + sys.exit("This utility must be run as root") + + # Create REBOOT_CAUSE_DIR if it doesn't exist + if not os.path.exists(REBOOT_CAUSE_DIR): + os.makedirs(REBOOT_CAUSE_DIR) + + # Remove stale PREVIOUS_REBOOT_CAUSE_FILE if it exists + if os.path.exists(PREVIOUS_REBOOT_CAUSE_FILE): + os.remove(PREVIOUS_REBOOT_CAUSE_FILE) + + previous_reboot_cause, additional_reboot_info = determin_reboot_cause() + # Current time reboot_cause_gen_time = str(datetime.datetime.now().strftime('%Y_%m_%d_%H_%M_%S')) diff --git a/tests/determine-reboot-cause_test.py b/tests/determine-reboot-cause_test.py index 7d22a512f8ee..743823dc5e26 100644 --- a/tests/determine-reboot-cause_test.py +++ b/tests/determine-reboot-cause_test.py @@ -117,3 +117,36 @@ def test_get_reboot_cause_dict_user(self): def test_get_reboot_cause_dict_kernel_panic(self): reboot_cause_dict = determine_reboot_cause.get_reboot_cause_dict(REBOOT_CAUSE_KERNEL_PANIC, "", GEN_TIME_KERNEL_PANIC) assert reboot_cause_dict == EXPECTED_KERNEL_PANIC_REBOOT_CAUSE_DICT + + def test_determine_reboot_cause_hardware(self): + with mock.patch("determine_reboot_cause.find_proc_cmdline_reboot_cause", return_value="Unknown"): + with mock.patch("determine_reboot_cause.find_software_reboot_cause", return_value="Power Cycle"): + with mock.patch("determine_reboot_cause.find_hardware_reboot_cause", return_value="Unknown"): + previous_reboot_cause, additional_info = determine_reboot_cause.determine_reboot_cause() + assert previous_reboot_cause == "Power Cycle" + assert additional_info == "N/A" + + def test_determine_reboot_cause_software(self): + with mock.patch("determine_reboot_cause.find_proc_cmdline_reboot_cause", return_value="Unknown"): + with mock.patch("determine_reboot_cause.find_software_reboot_cause", return_value="Unknown"): + with mock.patch("determine_reboot_cause.find_hardware_reboot_cause", return_value=EXPECTED_FIND_SOFTWARE_REBOOT_CAUSE_USER): + previous_reboot_cause, additional_info = determine_reboot_cause.determine_reboot_cause() + assert previous_reboot_cause == EXPECTED_FIND_SOFTWARE_REBOOT_CAUSE_USER + assert additional_info == "N/A" + + def test_determine_reboot_cause_cmdline(self): + with mock.patch("determine_reboot_cause.find_proc_cmdline_reboot_cause", return_value=EXPECTED_PARSE_WARMFAST_REBOOT_FROM_PROC_CMDLINE): + with mock.patch("determine_reboot_cause.find_software_reboot_cause", return_value="Unknown"): + with mock.patch("determine_reboot_cause.find_hardware_reboot_cause", return_value=EXPECTED_FIND_SOFTWARE_REBOOT_CAUSE_USER): + previous_reboot_cause, additional_info = determine_reboot_cause.determine_reboot_cause() + assert previous_reboot_cause == EXPECTED_FIND_SOFTWARE_REBOOT_CAUSE_USER + assert additional_info == "N/A" + + def test_determine_reboot_cause_cmdline_hardware(self): + with mock.patch("determine_reboot_cause.find_proc_cmdline_reboot_cause", return_value=EXPECTED_PARSE_WARMFAST_REBOOT_FROM_PROC_CMDLINE): + with mock.patch("determine_reboot_cause.find_software_reboot_cause", return_value=REBOOT_CAUSE_WATCHDOG): + with mock.patch("determine_reboot_cause.find_hardware_reboot_cause", return_value=EXPECTED_FIND_SOFTWARE_REBOOT_CAUSE_USER): + previous_reboot_cause, additional_info = determine_reboot_cause.determine_reboot_cause() + assert previous_reboot_cause == REBOOT_CAUSE_WATCHDOG + assert additional_info == EXPECTED_FIND_SOFTWARE_REBOOT_CAUSE_USER +