Skip to content

Commit

Permalink
Add unit test case
Browse files Browse the repository at this point in the history
  • Loading branch information
sujinmkang committed Aug 17, 2022
1 parent a56133b commit 25379d3
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 19 deletions.
43 changes: 24 additions & 19 deletions scripts/determine-reboot-cause
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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'))

Expand Down
33 changes: 33 additions & 0 deletions tests/determine-reboot-cause_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit 25379d3

Please sign in to comment.