|
1 | | -import imp |
| 1 | +import importlib |
2 | 2 | import sys |
3 | 3 | import os |
4 | 4 | import pytest |
|
28 | 28 | scripts_path = os.path.join(modules_path, "scripts") |
29 | 29 | sys.path.insert(0, modules_path) |
30 | 30 |
|
| 31 | +# Load the file under test |
| 32 | +determine_reboot_cause_path = os.path.join(scripts_path, 'determine-reboot-cause') |
| 33 | +loader = importlib.machinery.SourceFileLoader('determine_reboot_cause', determine_reboot_cause_path) |
| 34 | +spec = importlib.util.spec_from_loader(loader.name, loader) |
| 35 | +determine_reboot_cause = importlib.util.module_from_spec(spec) |
| 36 | +loader.exec_module(determine_reboot_cause) |
| 37 | +sys.modules['determine_reboot_cause'] = determine_reboot_cause |
| 38 | + |
| 39 | + |
31 | 40 | PROC_CMDLINE_CONTENTS = """\ |
32 | 41 | BOOT_IMAGE=/image-20191130.52/boot/vmlinuz-4.9.0-11-2-amd64 root=/dev/sda4 rw console=tty0 console=ttyS1,9600n8 quiet net.ifnames=0 biosdevname=0 loop=image-20191130.52/fs.squashfs loopfstype=squashfs apparmor=1 security=apparmor varlog_size=4096 usbcore.autosuspend=-1 module_blacklist=gpio_ich SONIC_BOOT_TYPE=warm""" |
33 | 42 |
|
|
37 | 46 | BOOT_IMAGE=/image-20191130.52/boot/vmlinuz-4.9.0-11-2-amd64 root=/dev/sda4 rw console=tty0 console=ttyS1,9600n8 quiet net.ifnames=0 biosdevname=0 loop=image-20191130.52/fs.squashfs loopfstype=squashfs apparmor=1 security=apparmor varlog_size=4096 usbcore.autosuspend=-1 module_blacklist=gpio_ich SONIC_BOOT_TYPE=warm""" |
38 | 47 |
|
39 | 48 | REBOOT_CAUSE_CONTENTS = """\ |
40 | | -User issued 'warm-reboot' command [User: admin, Time: Mon Nov 2 22:37:45 UTC 2020]""" |
| 49 | +User issued 'warm-reboot' command [User: admin, Time: Mon Nov 2 22:37:45 UTC 2020]""" |
41 | 50 |
|
42 | | -GET_SONIC_VERSION_INFO = {'commit_id': 'e59ec8291', 'build_date': 'Mon Nov 2 06:00:14 UTC 2020', 'build_number': 75, 'kernel_version': '4.9.0-11-2-amd64', 'debian_version': '9.13', 'built_by': 'sonicbld@jenkins-slave-phx-2', 'asic_type': 'mellanox', 'build_version': '20191130.52'} |
| 51 | +GET_SONIC_VERSION_INFO = {'commit_id': 'e59ec8291', 'build_date': 'Mon Nov 2 06:00:14 UTC 2020', 'build_number': 75, 'kernel_version': '4.9.0-11-2-amd64', 'debian_version': '9.13', 'built_by': 'sonicbld@jenkins-slave-phx-2', 'asic_type': 'mellanox', 'build_version': '20191130.52'} |
43 | 52 |
|
44 | 53 | REBOOT_CAUSE_WATCHDOG = "Watchdog" |
45 | 54 | GEN_TIME_WATCHDOG = "2020_10_22_03_15_08" |
|
55 | 64 | EXPECTED_WATCHDOG_REBOOT_CAUSE_DICT = {'comment': '', 'gen_time': '2020_10_22_03_15_08', 'cause': 'Watchdog', 'user': 'N/A', 'time': 'N/A'} |
56 | 65 | 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'} |
57 | 66 |
|
58 | | -imp.load_source('determine_reboot_cause', scripts_path + '/determine-reboot-cause') |
59 | | -from determine_reboot_cause import * |
60 | 67 |
|
61 | 68 | class TestDetermineRebootCause(object): |
62 | | - @classmethod |
63 | | - def setup_class(cls): |
64 | | - print("SETUP") |
65 | | - |
66 | 69 | def test_parse_warmfast_reboot_from_proc_cmdline(self): |
67 | 70 | with mock.patch("os.path.isfile") as mock_isfile: |
68 | 71 | mock_isfile.return_value = True |
69 | | - open_mocked = mock.mock_open(read_data=PROC_CMDLINE_CONTENTS) |
70 | | - with mock.patch("{}.open".format(BUILTINS), open_mocked): |
71 | | - result = parse_warmfast_reboot_from_proc_cmdline() |
| 72 | + open_mocked = mock.mock_open(read_data=PROC_CMDLINE_CONTENTS) |
| 73 | + with mock.patch("{}.open".format(BUILTINS), open_mocked): |
| 74 | + result = determine_reboot_cause.parse_warmfast_reboot_from_proc_cmdline() |
72 | 75 | assert result == EXPECTED_PARSE_WARMFAST_REBOOT_FROM_PROC_CMDLINE |
73 | | - open_mocked.assert_called_once_with("/proc/cmdline") |
| 76 | + open_mocked.assert_called_once_with("/proc/cmdline") |
74 | 77 |
|
75 | 78 | def test_find_software_reboot_cause_user(self): |
76 | | - with mock.patch("os.path.isfile") as mock_isfile: |
77 | | - mock_isfile.return_value = True |
78 | | - open_mocked = mock.mock_open(read_data=REBOOT_CAUSE_CONTENTS) |
79 | | - with mock.patch("{}.open".format(BUILTINS), open_mocked): |
80 | | - result = find_software_reboot_cause_from_reboot_cause_file() |
| 79 | + with mock.patch("os.path.isfile") as mock_isfile: |
| 80 | + mock_isfile.return_value = True |
| 81 | + open_mocked = mock.mock_open(read_data=REBOOT_CAUSE_CONTENTS) |
| 82 | + with mock.patch("{}.open".format(BUILTINS), open_mocked): |
| 83 | + result = determine_reboot_cause.find_software_reboot_cause_from_reboot_cause_file() |
81 | 84 | assert result == EXPECTED_FIND_SOFTWARE_REBOOT_CAUSE_USER |
82 | | - open_mocked.assert_called_once_with("/host/reboot-cause/reboot-cause.txt") |
| 85 | + open_mocked.assert_called_once_with("/host/reboot-cause/reboot-cause.txt") |
83 | 86 |
|
84 | 87 | def test_find_software_reboot_cause_first_boot(self): |
85 | 88 | with mock.patch("sonic_py_common.device_info.get_sonic_version_info", return_value=GET_SONIC_VERSION_INFO): |
86 | | - result = find_first_boot_version() |
| 89 | + result = determine_reboot_cause.find_first_boot_version() |
87 | 90 | assert result == EXPECTED_FIND_FIRSTBOOT_VERSION |
88 | 91 |
|
89 | 92 | def test_find_software_reboot_cause(self): |
90 | 93 | with mock.patch("determine_reboot_cause.find_software_reboot_cause_from_reboot_cause_file", return_value="Unknown"): |
91 | | - with mock.patch("os.path.isfile") as mock_isfile: |
92 | | - mock_isfile.return_value = False |
93 | | - result = find_software_reboot_cause() |
| 94 | + with mock.patch("os.path.isfile") as mock_isfile: |
| 95 | + mock_isfile.return_value = False |
| 96 | + result = determine_reboot_cause.find_software_reboot_cause() |
94 | 97 | assert result == "Unknown" |
95 | 98 |
|
96 | 99 | def test_find_proc_cmdline_reboot_cause(self): |
97 | 100 | with mock.patch("determine_reboot_cause.parse_warmfast_reboot_from_proc_cmdline", return_value="fast-reboot"): |
98 | | - result = find_proc_cmdline_reboot_cause() |
| 101 | + result = determine_reboot_cause.find_proc_cmdline_reboot_cause() |
99 | 102 | assert result == "fast-reboot" |
100 | 103 |
|
101 | 104 | def test_find_hardware_reboot_cause(self): |
102 | 105 | with mock.patch("determine_reboot_cause.get_reboot_cause_from_platform", return_value=("Powerloss", None)): |
103 | | - result = find_hardware_reboot_cause() |
| 106 | + result = determine_reboot_cause.find_hardware_reboot_cause() |
104 | 107 | assert result == "Powerloss (None)" |
105 | 108 |
|
106 | 109 | def test_get_reboot_cause_dict_watchdog(self): |
107 | | - reboot_cause_dict = get_reboot_cause_dict(REBOOT_CAUSE_WATCHDOG, "", GEN_TIME_WATCHDOG) |
| 110 | + reboot_cause_dict = determine_reboot_cause.get_reboot_cause_dict(REBOOT_CAUSE_WATCHDOG, "", GEN_TIME_WATCHDOG) |
108 | 111 | assert reboot_cause_dict == EXPECTED_WATCHDOG_REBOOT_CAUSE_DICT |
109 | 112 |
|
110 | 113 | def test_get_reboot_cause_dict_user(self): |
111 | | - reboot_cause_dict = get_reboot_cause_dict(REBOOT_CAUSE_USER, "", GEN_TIME_USER) |
| 114 | + reboot_cause_dict = determine_reboot_cause.get_reboot_cause_dict(REBOOT_CAUSE_USER, "", GEN_TIME_USER) |
112 | 115 | assert reboot_cause_dict == EXPECTED_USER_REBOOT_CAUSE_DICT |
113 | | - |
114 | | - @classmethod |
115 | | - def teardown_class(cls): |
116 | | - print("TEARDOWN") |
117 | | - |
0 commit comments