Skip to content

Commit 8543ddf

Browse files
committed
update the reboot cause logic and update the unit test
1 parent 53ad7cd commit 8543ddf

File tree

2 files changed

+50
-28
lines changed

2 files changed

+50
-28
lines changed

scripts/determine-reboot-cause

+16-15
Original file line numberDiff line numberDiff line change
@@ -175,28 +175,29 @@ def determine_reboot_cause():
175175
software_reboot_cause = find_software_reboot_cause()
176176

177177
# The main decision logic of the reboot cause:
178-
# If there is a reboot cause indicated by /proc/cmdline, it should be warmreboot/fastreboot
178+
# If there is a valid hardware reboot cause indicated by platform API,
179+
# check the software reboot cause to add additional rebot cause.
180+
# If there is a reboot cause indicated by /proc/cmdline, and/or warmreboot/fastreboot/softreboot
179181
# the software_reboot_cause which is the content of /hosts/reboot-cause/reboot-cause.txt
180-
# will be treated as the reboot cause
181-
# Elif there is a reboot cause indicated by platform API,
182-
# the hardware_reboot_cause will be treated as the reboot cause
182+
# will be treated as the additional reboot cause
183+
# Elif there is a cmdline reboot cause,
184+
# the software_reboot_cause will be treated as the reboot cause if it's not unknown
185+
# otherwise, the cmdline_reboot_cause will be treated as the reboot cause if it's not none
183186
# Else the software_reboot_cause will be treated as the reboot cause
184-
if proc_cmdline_reboot_cause is not None:
185-
if not hardware_reboot_cause.startswith(REBOOT_CAUSE_NON_HARDWARE):
186-
# Add the hardware_reboot_cause as actual reboot cause
187-
previous_reboot_cause = hardware_reboot_cause
188-
additional_reboot_info = software_reboot_cause
189-
else:
190-
previous_reboot_cause = software_reboot_cause
191-
elif hardware_reboot_cause is not None:
187+
if not hardware_reboot_cause.startswith(REBOOT_CAUSE_NON_HARDWARE):
188+
previous_reboot_cause = hardware_reboot_cause
192189
# Check if any software reboot was issued before this hardware reboot happened
193190
if software_reboot_cause is not REBOOT_CAUSE_UNKNOWN:
194-
previous_reboot_cause = hardware_reboot_cause
195191
additional_reboot_info = software_reboot_cause
192+
elif proc_cmdline_reboot_cause is not None:
193+
additional_reboot_info = proc_cmdline_reboot_cause
194+
elif proc_cmdline_reboot_cause is not None:
195+
if software_reboot_cause is not REBOOT_CAUSE_UNKNOWN:
196+
# Get the reboot cause from REBOOT_CAUSE_FILE
197+
previous_reboot_cause = software_reboot_cause
196198
else:
197-
previous_reboot_cause = hardware_reboot_cause
199+
previous_reboot_cause = proc_cmdline_reboot_cause
198200
else:
199-
# Get the reboot cause from REBOOT_CAUSE_FILE
200201
previous_reboot_cause = software_reboot_cause
201202

202203
return previous_reboot_cause, additional_reboot_info

tests/determine-reboot-cause_test.py

+34-13
Original file line numberDiff line numberDiff line change
@@ -54,11 +54,16 @@
5454
GEN_TIME_KERNEL_PANIC = "2021_3_28_13_48_49"
5555

5656

57+
REBOOT_CAUSE_UNKNOWN = "Unknown"
58+
REBOOT_CAUSE_NON_HARDWARE = "Non-Hardware"
59+
EXPECTED_NON_HARDWARE_REBOOT_CAUSE = {REBOOT_CAUSE_NON_HARDWARE, "N/A"}
60+
REBOOT_CAUSE_HARDWARE_OTHER = "Hardware - Other"
61+
EXPECTED_HARDWARE_REBOOT_CAUSE = {REBOOT_CAUSE_HARDWARE_OTHER, ""}
62+
5763
EXPECTED_PARSE_WARMFAST_REBOOT_FROM_PROC_CMDLINE = "warm-reboot"
5864
EXPECTED_FIND_SOFTWARE_REBOOT_CAUSE_USER = "User issued 'warm-reboot' command [User: admin, Time: Mon Nov 2 22:37:45 UTC 2020]"
5965
EXPECTED_FIND_FIRSTBOOT_VERSION = " (First boot of SONiC version 20191130.52)"
6066
EXPECTED_FIND_SOFTWARE_REBOOT_CAUSE_FIRSTBOOT = "Unknown (First boot of SONiC version 20191130.52)"
61-
EXPECTED_HARDWARE_REBOOT_CAUSE = {"warm-reboot", ""}
6267

6368
EXPECTED_WATCHDOG_REBOOT_CAUSE_DICT = {'comment': '', 'gen_time': '2020_10_22_03_15_08', 'cause': 'Watchdog', 'user': 'N/A', 'time': 'N/A'}
6469
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'}
@@ -119,33 +124,49 @@ def test_get_reboot_cause_dict_kernel_panic(self):
119124
assert reboot_cause_dict == EXPECTED_KERNEL_PANIC_REBOOT_CAUSE_DICT
120125

121126
def test_determine_reboot_cause_hardware(self):
122-
with mock.patch("determine_reboot_cause.determine_reboot_cause.find_proc_cmdline_reboot_cause", return_value="Unknown"):
123-
with mock.patch("determine_reboot_cause.determine_reboot_cause.find_software_reboot_cause", return_value="Power Cycle"):
124-
with mock.patch("determine_reboot_cause.determine_reboot_cause.find_hardware_reboot_cause", return_value="Unknown"):
125-
previous_reboot_cause, additional_info = determine_reboot_cause.determine_reboot_cause()
126-
assert previous_reboot_cause == "Power Cycle"
127-
assert additional_info == "N/A"
127+
with mock.patch("determine_reboot_cause.find_proc_cmdline_reboot_cause", return_value=None):
128+
with mock.patch("determine_reboot_cause.find_software_reboot_cause", return_value=REBOOT_CAUSE_UNKNOWN):
129+
with mock.patch("determine_reboot_cause.find_hardware_reboot_cause", return_value=EXPECTED_HARDWARE_REBOOT_CAUSE):
130+
previous_reboot_cause, additional_reboot_info = determine_reboot_cause.determine_reboot_cause()
131+
assert previous_reboot_cause == EXPECTED_HARDWARE_REBOOT_CAUSE
132+
assert additional_reboot_info == "N/A"
128133

129134
def test_determine_reboot_cause_software(self):
130-
with mock.patch("determine_reboot_cause.determine_reboot_cause.find_proc_cmdline_reboot_cause", return_value="Unknown"):
135+
with mock.patch("determine_reboot_cause.determine_reboot_cause.find_proc_cmdline_reboot_cause", return_value=None):
131136
with mock.patch("determine_reboot_cause.determine_reboot_cause.find_software_reboot_cause", return_value=EXPECTED_FIND_SOFTWARE_REBOOT_CAUSE_USER):
132-
with mock.patch("determine_reboot_cause.determine_reboot_cause.find_hardware_reboot_cause", return_value="Unknown"):
137+
with mock.patch("determine_reboot_cause.determine_reboot_cause.find_hardware_reboot_cause", return_value=EXPECTED_NON_HARDWARE_REBOOT_CAUSE):
133138
previous_reboot_cause, additional_info = determine_reboot_cause.determine_reboot_cause()
134139
assert previous_reboot_cause == EXPECTED_FIND_SOFTWARE_REBOOT_CAUSE_USER
135140
assert additional_info == "N/A"
136141

137-
def test_determine_reboot_cause_cmdline(self):
142+
def test_determine_reboot_cause_cmdline_software(self):
138143
with mock.patch("determine_reboot_cause.determine_reboot_cause.find_proc_cmdline_reboot_cause", return_value=EXPECTED_PARSE_WARMFAST_REBOOT_FROM_PROC_CMDLINE):
139144
with mock.patch("determine_reboot_cause.determine_reboot_cause.find_software_reboot_cause", return_value=EXPECTED_FIND_SOFTWARE_REBOOT_CAUSE_USER):
140-
with mock.patch("determine_reboot_cause.determine_reboot_cause.find_hardware_reboot_cause", return_value="Unknown"):
145+
with mock.patch("determine_reboot_cause.determine_reboot_cause.find_hardware_reboot_cause", return_value=EXPECTED_NON_HARDWARE_REBOOT_CAUSE):
141146
previous_reboot_cause, additional_info = determine_reboot_cause.determine_reboot_cause()
142147
assert previous_reboot_cause == EXPECTED_FIND_SOFTWARE_REBOOT_CAUSE_USER
143148
assert additional_info == "N/A"
144149

150+
def test_determine_reboot_cause_cmdline_no_software(self):
151+
with mock.patch("determine_reboot_cause.determine_reboot_cause.find_proc_cmdline_reboot_cause", return_value=EXPECTED_PARSE_WARMFAST_REBOOT_FROM_PROC_CMDLINE):
152+
with mock.patch("determine_reboot_cause.determine_reboot_cause.find_software_reboot_cause", return_value=REBOOT_CAUSE_UNKNOWN):
153+
with mock.patch("determine_reboot_cause.determine_reboot_cause.find_hardware_reboot_cause", return_value=EXPECTED_NON_HARDWARE_REBOOT_CAUSE):
154+
previous_reboot_cause, additional_info = determine_reboot_cause.determine_reboot_cause()
155+
assert previous_reboot_cause == EXPECTED_PARSE_WARMFAST_REBOOT_FROM_PROC_CMDLINE
156+
assert additional_info == "N/A"
157+
145158
def test_determine_reboot_cause_cmdline_hardware(self):
146159
with mock.patch("determine_reboot_cause.determine_reboot_cause.find_proc_cmdline_reboot_cause", return_value=EXPECTED_PARSE_WARMFAST_REBOOT_FROM_PROC_CMDLINE):
147-
with mock.patch("determine_reboot_cause.determine_reboot_cause.find_software_reboot_cause", return_value=EXPECTED_FIND_SOFTWARE_REBOOT_CAUSE_USER):
148-
with mock.patch("determine_reboot_cause.determine_reboot_cause.find_hardware_reboot_cause", return_value=REBOOT_CAUSE_WATCHDOG):
160+
with mock.patch("determine_reboot_cause.determine_reboot_cause.find_software_reboot_cause", return_value=REBOOT_CAUSE_UNKNOWN):
161+
with mock.patch("determine_reboot_cause.determine_reboot_cause.find_hardware_reboot_cause", return_value=EXPECTED_HARDWARE_REBOOT_CAUSE):
162+
previous_reboot_cause, additional_info = determine_reboot_cause.determine_reboot_cause()
163+
assert previous_reboot_cause == EXPECTED_HARDWARE_REBOOT_CAUSE
164+
assert additional_info == EXPECTED_PARSE_WARMFAST_REBOOT_FROM_PROC_CMDLINE
165+
166+
def test_determine_reboot_cause_software_hardware(self):
167+
with mock.patch("determine_reboot_cause.find_proc_cmdline_reboot_cause", return_value=EXPECTED_PARSE_WARMFAST_REBOOT_FROM_PROC_CMDLINE):
168+
with mock.patch("determine_reboot_cause.find_software_reboot_cause", return_value=EXPECTED_FIND_SOFTWARE_REBOOT_CAUSE_USER):
169+
with mock.patch("determine_reboot_cause.find_hardware_reboot_cause", return_value=EXPECTED_HARDWARE_REBOOT_CAUSE):
149170
previous_reboot_cause, additional_info = determine_reboot_cause.determine_reboot_cause()
150171
assert previous_reboot_cause == REBOOT_CAUSE_WATCHDOG
151172
assert additional_info == EXPECTED_FIND_SOFTWARE_REBOOT_CAUSE_USER

0 commit comments

Comments
 (0)