Skip to content

Commit 25379d3

Browse files
committed
Add unit test case
1 parent a56133b commit 25379d3

File tree

2 files changed

+57
-19
lines changed

2 files changed

+57
-19
lines changed

scripts/determine-reboot-cause

+24-19
Original file line numberDiff line numberDiff line change
@@ -158,25 +158,7 @@ def get_reboot_cause_dict(previous_reboot_cause, comment, gen_time):
158158

159159
return reboot_cause_dict
160160

161-
162-
def main():
163-
# Configure logger to log all messages INFO level and higher
164-
sonic_logger.set_min_log_priority_info()
165-
166-
sonic_logger.log_info("Starting up...")
167-
168-
if not os.geteuid() == 0:
169-
sonic_logger.log_error("User {} does not have permission to execute".format(pwd.getpwuid(os.getuid()).pw_name))
170-
sys.exit("This utility must be run as root")
171-
172-
# Create REBOOT_CAUSE_DIR if it doesn't exist
173-
if not os.path.exists(REBOOT_CAUSE_DIR):
174-
os.makedirs(REBOOT_CAUSE_DIR)
175-
176-
# Remove stale PREVIOUS_REBOOT_CAUSE_FILE if it exists
177-
if os.path.exists(PREVIOUS_REBOOT_CAUSE_FILE):
178-
os.remove(PREVIOUS_REBOOT_CAUSE_FILE)
179-
161+
def determin_reboot_cause():
180162
# This variable is kept for future-use purpose. When proc_cmd_line/vendor/software provides
181163
# any additional_reboot_info it will be stored as a "comment" in REBOOT_CAUSE_HISTORY_FILE
182164
additional_reboot_info = "N/A"
@@ -217,6 +199,29 @@ def main():
217199
# Get the reboot cause from REBOOT_CAUSE_FILE
218200
previous_reboot_cause = software_reboot_cause
219201

202+
return previous_reboot_cause, additional_reboot_info
203+
204+
205+
def main():
206+
# Configure logger to log all messages INFO level and higher
207+
sonic_logger.set_min_log_priority_info()
208+
209+
sonic_logger.log_info("Starting up...")
210+
211+
if not os.geteuid() == 0:
212+
sonic_logger.log_error("User {} does not have permission to execute".format(pwd.getpwuid(os.getuid()).pw_name))
213+
sys.exit("This utility must be run as root")
214+
215+
# Create REBOOT_CAUSE_DIR if it doesn't exist
216+
if not os.path.exists(REBOOT_CAUSE_DIR):
217+
os.makedirs(REBOOT_CAUSE_DIR)
218+
219+
# Remove stale PREVIOUS_REBOOT_CAUSE_FILE if it exists
220+
if os.path.exists(PREVIOUS_REBOOT_CAUSE_FILE):
221+
os.remove(PREVIOUS_REBOOT_CAUSE_FILE)
222+
223+
previous_reboot_cause, additional_reboot_info = determin_reboot_cause()
224+
220225
# Current time
221226
reboot_cause_gen_time = str(datetime.datetime.now().strftime('%Y_%m_%d_%H_%M_%S'))
222227

tests/determine-reboot-cause_test.py

+33
Original file line numberDiff line numberDiff line change
@@ -117,3 +117,36 @@ def test_get_reboot_cause_dict_user(self):
117117
def test_get_reboot_cause_dict_kernel_panic(self):
118118
reboot_cause_dict = determine_reboot_cause.get_reboot_cause_dict(REBOOT_CAUSE_KERNEL_PANIC, "", GEN_TIME_KERNEL_PANIC)
119119
assert reboot_cause_dict == EXPECTED_KERNEL_PANIC_REBOOT_CAUSE_DICT
120+
121+
def test_determine_reboot_cause_hardware(self):
122+
with mock.patch("determine_reboot_cause.find_proc_cmdline_reboot_cause", return_value="Unknown"):
123+
with mock.patch("determine_reboot_cause.find_software_reboot_cause", return_value="Power Cycle"):
124+
with mock.patch("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"
128+
129+
def test_determine_reboot_cause_software(self):
130+
with mock.patch("determine_reboot_cause.find_proc_cmdline_reboot_cause", return_value="Unknown"):
131+
with mock.patch("determine_reboot_cause.find_software_reboot_cause", return_value="Unknown"):
132+
with mock.patch("determine_reboot_cause.find_hardware_reboot_cause", return_value=EXPECTED_FIND_SOFTWARE_REBOOT_CAUSE_USER):
133+
previous_reboot_cause, additional_info = determine_reboot_cause.determine_reboot_cause()
134+
assert previous_reboot_cause == EXPECTED_FIND_SOFTWARE_REBOOT_CAUSE_USER
135+
assert additional_info == "N/A"
136+
137+
def test_determine_reboot_cause_cmdline(self):
138+
with mock.patch("determine_reboot_cause.find_proc_cmdline_reboot_cause", return_value=EXPECTED_PARSE_WARMFAST_REBOOT_FROM_PROC_CMDLINE):
139+
with mock.patch("determine_reboot_cause.find_software_reboot_cause", return_value="Unknown"):
140+
with mock.patch("determine_reboot_cause.find_hardware_reboot_cause", return_value=EXPECTED_FIND_SOFTWARE_REBOOT_CAUSE_USER):
141+
previous_reboot_cause, additional_info = determine_reboot_cause.determine_reboot_cause()
142+
assert previous_reboot_cause == EXPECTED_FIND_SOFTWARE_REBOOT_CAUSE_USER
143+
assert additional_info == "N/A"
144+
145+
def test_determine_reboot_cause_cmdline_hardware(self):
146+
with mock.patch("determine_reboot_cause.find_proc_cmdline_reboot_cause", return_value=EXPECTED_PARSE_WARMFAST_REBOOT_FROM_PROC_CMDLINE):
147+
with mock.patch("determine_reboot_cause.find_software_reboot_cause", return_value=REBOOT_CAUSE_WATCHDOG):
148+
with mock.patch("determine_reboot_cause.find_hardware_reboot_cause", return_value=EXPECTED_FIND_SOFTWARE_REBOOT_CAUSE_USER):
149+
previous_reboot_cause, additional_info = determine_reboot_cause.determine_reboot_cause()
150+
assert previous_reboot_cause == REBOOT_CAUSE_WATCHDOG
151+
assert additional_info == EXPECTED_FIND_SOFTWARE_REBOOT_CAUSE_USER
152+

0 commit comments

Comments
 (0)