Skip to content

Commit 741a9df

Browse files
authored
Handle NotImplementedError in determine-reboot-cause that will be thrown on VS Chassis platform (sonic-net#211)
* scripts/determine-reboot-cause: add an except branch to handle NotImplementedError that will be thrown on VS Chassis platform * tests/determine-reboot-cause_test.py: supplement a new test case to improve coverage * fix a typo * make error message more specific * consolidate the case where sonic_platform is not implemented or installed
1 parent 47f6feb commit 741a9df

File tree

2 files changed

+12
-8
lines changed

2 files changed

+12
-8
lines changed

scripts/determine-reboot-cause

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -113,8 +113,8 @@ def get_reboot_cause_from_platform():
113113
chassis = platform.get_chassis()
114114
hardware_reboot_cause_major, hardware_reboot_cause_minor = chassis.get_reboot_cause()
115115
sonic_logger.log_info("Platform api returns reboot cause {}, {}".format(hardware_reboot_cause_major, hardware_reboot_cause_minor))
116-
except (ImportError, FileNotFoundError):
117-
sonic_logger.log_warning("sonic_platform package not installed. Unable to detect hardware reboot causes.")
116+
except (ImportError, FileNotFoundError, NotImplementedError):
117+
sonic_logger.log_warning("sonic_platform package not installed or not implemented. Unable to detect hardware reboot causes.")
118118
hardware_reboot_cause_major, hardware_reboot_cause_minor = REBOOT_CAUSE_NON_HARDWARE, "N/A"
119119

120120
return hardware_reboot_cause_major, hardware_reboot_cause_minor
@@ -143,7 +143,7 @@ def get_reboot_cause_dict(previous_reboot_cause, comment, gen_time):
143143
If user issued a command to reboot device, then user, command and time will be
144144
stored into a dictionary.
145145
146-
If device was rebooted due to the kernel panic, then the string `Kernel Panic`
146+
If device was rebooted due to the kernel panic, then the string `Kernel Panic`
147147
and time will be stored into a dictionary.
148148
"""
149149
reboot_cause_dict = {}
@@ -165,7 +165,7 @@ def get_reboot_cause_dict(previous_reboot_cause, comment, gen_time):
165165
if match is not None:
166166
reboot_cause_dict['cause'] = "Kernel Panic"
167167
reboot_cause_dict['time'] = match.group(1)
168-
168+
169169
return reboot_cause_dict
170170

171171
def determine_reboot_cause():
@@ -185,12 +185,12 @@ def determine_reboot_cause():
185185
software_reboot_cause = find_software_reboot_cause()
186186

187187
# The main decision logic of the reboot cause:
188-
# If there is a valid hardware reboot cause indicated by platform API,
188+
# If there is a valid hardware reboot cause indicated by platform API,
189189
# check the software reboot cause to add additional reboot cause.
190190
# If there is a reboot cause indicated by /proc/cmdline, and/or warmreboot/fastreboot/softreboot
191191
# the software_reboot_cause which is the content of /hosts/reboot-cause/reboot-cause.txt
192192
# will be treated as the additional reboot cause
193-
# Elif there is a cmdline reboot cause,
193+
# Elif there is a cmdline reboot cause,
194194
# the software_reboot_cause will be treated as the reboot cause if it's not unknown
195195
# otherwise, the cmdline_reboot_cause will be treated as the reboot cause if it's not none
196196
# Else the software_reboot_cause will be treated as the reboot cause
@@ -266,7 +266,7 @@ def main():
266266
# Write the previous reboot cause to REBOOT_CAUSE_HISTORY_FILE as a JSON format
267267
with open(REBOOT_CAUSE_HISTORY_FILE, "w") as reboot_cause_history_file:
268268
json.dump(reboot_cause_dict, reboot_cause_history_file)
269-
269+
270270
# Remove stale PREVIOUS_REBOOT_CAUSE_FILE if it exists
271271
if os.path.exists(PREVIOUS_REBOOT_CAUSE_FILE) or os.path.islink(PREVIOUS_REBOOT_CAUSE_FILE):
272272
os.remove(PREVIOUS_REBOOT_CAUSE_FILE)

tests/determine-reboot-cause_test.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,10 @@ def test_find_hardware_reboot_cause_with_minor(self):
130130
result = determine_reboot_cause.find_hardware_reboot_cause()
131131
assert result == "Powerloss (under-voltage)"
132132

133+
def test_find_hardware_reboot_cause_not_installed_or_not_implemented(self):
134+
result = determine_reboot_cause.find_hardware_reboot_cause()
135+
assert result == REBOOT_CAUSE_NON_HARDWARE + " (N/A)"
136+
133137
def test_get_reboot_cause_dict_watchdog(self):
134138
reboot_cause_dict = determine_reboot_cause.get_reboot_cause_dict(REBOOT_CAUSE_WATCHDOG, "", GEN_TIME_WATCHDOG)
135139
assert reboot_cause_dict == EXPECTED_WATCHDOG_REBOOT_CAUSE_DICT
@@ -210,7 +214,7 @@ def test_determine_reboot_cause_main_with_reboot_cause_dir(self):
210214
with mock.patch("os.geteuid", return_value=0):
211215
determine_reboot_cause.main()
212216
assert os.path.exists("host/reboot-cause/reboot-cause.txt") == True
213-
assert os.path.exists("host/reboot-cause/previous-reboot-cause.json") == True
217+
assert os.path.exists("host/reboot-cause/previous-reboot-cause.json") == True
214218

215219
def create_mock_platform_json(self, dpus):
216220
"""Helper function to create a mock platform.json file."""

0 commit comments

Comments
 (0)