From 2e909f3de1286d6d9392aa1fe27312e3256c8898 Mon Sep 17 00:00:00 2001 From: mlok Date: Wed, 1 Mar 2023 13:00:17 -0500 Subject: [PATCH 1/2] [reboot-cause] Fix a broken symlink of previous-reboot-cause file removal issue Signed-off-by: mlok --- scripts/determine-reboot-cause | 2 +- tests/determine-reboot-cause_test.py | 9 +++++++++ 2 files changed, 10 insertions(+), 1 deletion(-) diff --git a/scripts/determine-reboot-cause b/scripts/determine-reboot-cause index e4d8a38c..e96580f0 100755 --- a/scripts/determine-reboot-cause +++ b/scripts/determine-reboot-cause @@ -223,7 +223,7 @@ def main(): os.makedirs(REBOOT_CAUSE_DIR) # Remove stale PREVIOUS_REBOOT_CAUSE_FILE if it exists - if os.path.exists(PREVIOUS_REBOOT_CAUSE_FILE): + if os.path.exists(PREVIOUS_REBOOT_CAUSE_FILE) or os.path.islink(PREVIOUS_REBOOT_CAUSE_FILE): os.remove(PREVIOUS_REBOOT_CAUSE_FILE) previous_reboot_cause, additional_reboot_info = determine_reboot_cause() diff --git a/tests/determine-reboot-cause_test.py b/tests/determine-reboot-cause_test.py index b4c07af4..d1bd7c0f 100644 --- a/tests/determine-reboot-cause_test.py +++ b/tests/determine-reboot-cause_test.py @@ -176,3 +176,12 @@ def test_determine_reboot_cause_software_hardware(self): assert previous_reboot_cause == EXPECTED_HARDWARE_REBOOT_CAUSE assert additional_info == EXPECTED_FIND_SOFTWARE_REBOOT_CAUSE_USER + @mock.patch('determine_reboot_cause.REBOOT_CAUSE_DIR', os.path.join(os.getcwd(), 'host/reboot-cause/')) + @mock.patch('determine_reboot_cause.REBOOT_CAUSE_HISTORY_DIR', os.path.join(os.getcwd(), 'host/reboot-cause/history/')) + @mock.patch('determine_reboot_cause.PREVIOUS_REBOOT_CAUSE_FILE', os.path.join(os.getcwd(), 'host/reboot-cause/previous-reboot-cause.json')) + @mock.patch('determine_reboot_cause.REBOOT_CAUSE_FILE', os.path.join(os.getcwd(),'host/reboot-cause/reboot-cause.txt')) + def test_determine_reboot_cause_main(self): + with mock.patch("os.geteuid", return_value=0): + determine_reboot_cause.main() + assert os.path.exists("host/reboot-cause/reboot-cause.txt") == True + assert os.path.exists("host/reboot-cause/previous-reboot-cause.json") == True From 6c11931173bc035a38da2defc307dfd0569d6d5e Mon Sep 17 00:00:00 2001 From: mlok Date: Sat, 25 Mar 2023 22:22:13 -0400 Subject: [PATCH 2/2] Address the comments and move the PREVIOUS_REBOOT_CAUSE_FILE checking to the pace before its new symlink creation --- scripts/determine-reboot-cause | 8 ++++---- tests/determine-reboot-cause_test.py | 18 ++++++++++++++++-- 2 files changed, 20 insertions(+), 6 deletions(-) diff --git a/scripts/determine-reboot-cause b/scripts/determine-reboot-cause index e96580f0..a0ddc21a 100755 --- a/scripts/determine-reboot-cause +++ b/scripts/determine-reboot-cause @@ -222,10 +222,6 @@ def main(): 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) or os.path.islink(PREVIOUS_REBOOT_CAUSE_FILE): - os.remove(PREVIOUS_REBOOT_CAUSE_FILE) - previous_reboot_cause, additional_reboot_info = determine_reboot_cause() # Current time @@ -244,6 +240,10 @@ def main(): # Write the previous reboot cause to REBOOT_CAUSE_HISTORY_FILE as a JSON format with open(REBOOT_CAUSE_HISTORY_FILE, "w") as reboot_cause_history_file: json.dump(reboot_cause_dict, reboot_cause_history_file) + + # Remove stale PREVIOUS_REBOOT_CAUSE_FILE if it exists + if os.path.exists(PREVIOUS_REBOOT_CAUSE_FILE) or os.path.islink(PREVIOUS_REBOOT_CAUSE_FILE): + os.remove(PREVIOUS_REBOOT_CAUSE_FILE) # Create a symbolic link to previous-reboot-cause.json file os.symlink(REBOOT_CAUSE_HISTORY_FILE, PREVIOUS_REBOOT_CAUSE_FILE) diff --git a/tests/determine-reboot-cause_test.py b/tests/determine-reboot-cause_test.py index d1bd7c0f..6bf52fe5 100644 --- a/tests/determine-reboot-cause_test.py +++ b/tests/determine-reboot-cause_test.py @@ -1,5 +1,6 @@ import sys import os +import shutil import pytest from swsscommon import swsscommon @@ -69,6 +70,7 @@ EXPECTED_USER_REBOOT_CAUSE_DICT = {'comment': '', 'gen_time': '2020_10_22_03_14_07', 'cause': 'reboot', 'user': 'admin', 'time': 'Thu Oct 22 03:11:08 UTC 2020'} EXPECTED_KERNEL_PANIC_REBOOT_CAUSE_DICT = {'comment': '', 'gen_time': '2021_3_28_13_48_49', 'cause': 'Kernel Panic', 'user': 'N/A', 'time': 'Sun Mar 28 13:45:12 UTC 2021'} +REBOOT_CAUSE_DIR="host/reboot-cause/" class TestDetermineRebootCause(object): def test_parse_warmfast_reboot_from_proc_cmdline(self): @@ -176,12 +178,24 @@ def test_determine_reboot_cause_software_hardware(self): assert previous_reboot_cause == EXPECTED_HARDWARE_REBOOT_CAUSE assert additional_info == EXPECTED_FIND_SOFTWARE_REBOOT_CAUSE_USER - @mock.patch('determine_reboot_cause.REBOOT_CAUSE_DIR', os.path.join(os.getcwd(), 'host/reboot-cause/')) + @mock.patch('determine_reboot_cause.REBOOT_CAUSE_DIR', os.path.join(os.getcwd(), REBOOT_CAUSE_DIR)) @mock.patch('determine_reboot_cause.REBOOT_CAUSE_HISTORY_DIR', os.path.join(os.getcwd(), 'host/reboot-cause/history/')) @mock.patch('determine_reboot_cause.PREVIOUS_REBOOT_CAUSE_FILE', os.path.join(os.getcwd(), 'host/reboot-cause/previous-reboot-cause.json')) @mock.patch('determine_reboot_cause.REBOOT_CAUSE_FILE', os.path.join(os.getcwd(),'host/reboot-cause/reboot-cause.txt')) - def test_determine_reboot_cause_main(self): + def test_determine_reboot_cause_main_without_reboot_cause_dir(self): + if os.path.exists(REBOOT_CAUSE_DIR): + shutil.rmtree(REBOOT_CAUSE_DIR) with mock.patch("os.geteuid", return_value=0): determine_reboot_cause.main() assert os.path.exists("host/reboot-cause/reboot-cause.txt") == True assert os.path.exists("host/reboot-cause/previous-reboot-cause.json") == True + + @mock.patch('determine_reboot_cause.REBOOT_CAUSE_DIR', os.path.join(os.getcwd(), REBOOT_CAUSE_DIR)) + @mock.patch('determine_reboot_cause.REBOOT_CAUSE_HISTORY_DIR', os.path.join(os.getcwd(), 'host/reboot-cause/history/')) + @mock.patch('determine_reboot_cause.PREVIOUS_REBOOT_CAUSE_FILE', os.path.join(os.getcwd(), 'host/reboot-cause/previous-reboot-cause.json')) + @mock.patch('determine_reboot_cause.REBOOT_CAUSE_FILE', os.path.join(os.getcwd(),'host/reboot-cause/reboot-cause.txt')) + def test_determine_reboot_cause_main_with_reboot_cause_dir(self): + with mock.patch("os.geteuid", return_value=0): + determine_reboot_cause.main() + assert os.path.exists("host/reboot-cause/reboot-cause.txt") == True + assert os.path.exists("host/reboot-cause/previous-reboot-cause.json") == True