|
| 1 | +import pytest |
| 2 | +import re |
| 3 | +from tests.common.helpers.assertions import pytest_assert |
| 4 | +from tests.common.platform.processes_utils import wait_critical_processes |
| 5 | + |
| 6 | +pytestmark = [ |
| 7 | + pytest.mark.disable_loganalyzer, |
| 8 | + pytest.mark.topology('any'), |
| 9 | +] |
| 10 | + |
| 11 | +COMMAND_TIMEOUT = 90 # seconds |
| 12 | + |
| 13 | + |
| 14 | +def check_if_platform_reboot_enabled(duthost) -> bool: |
| 15 | + platform = get_command_result(duthost, "sonic-cfggen -H -v DEVICE_METADATA.localhost.platform") |
| 16 | + return check_if_dut_file_exist(duthost, "/usr/share/sonic/device/{}/platform_reboot".format(platform)) |
| 17 | + |
| 18 | + |
| 19 | +def mock_systemctl_reboot(duthost): |
| 20 | + if not check_if_dut_file_exist(duthost, "/sbin/reboot.bak"): |
| 21 | + # Check exist to avoid override original reboot file. |
| 22 | + execute_command(duthost, "sudo mv /sbin/reboot /sbin/reboot.bak") |
| 23 | + execute_command(duthost, "sudo echo \"\" > /sbin/reboot") |
| 24 | + execute_command(duthost, "sudo chmod +x /sbin/reboot") |
| 25 | + execute_command_ignore_error(duthost, "sudo /usr/local/bin/watchdogutil disarm") |
| 26 | + |
| 27 | + # Disable watch dog to avoid reboot too early. |
| 28 | + execute_command( |
| 29 | + duthost, |
| 30 | + "sudo sed -i 's#/usr/local/bin/watchdogutil#/usr/local/bin/disabled_watchdogutil#g' /usr/local/bin/reboot") |
| 31 | + |
| 32 | + |
| 33 | +def restore_systemctl_reboot_and_reboot(duthost): |
| 34 | + if not check_if_dut_file_exist(duthost, "/sbin/reboot.bak"): |
| 35 | + return |
| 36 | + execute_command(duthost, "sudo rm /sbin/reboot") |
| 37 | + execute_command(duthost, "sudo mv /sbin/reboot.bak /sbin/reboot") |
| 38 | + execute_command( |
| 39 | + duthost, |
| 40 | + "sudo sed -i 's#/usr/local/bin/disabled_watchdogutil#/usr/local/bin/watchdogutil#g' /usr/local/bin/reboot") |
| 41 | + execute_command(duthost, "sudo reboot") |
| 42 | + |
| 43 | + timeout = None |
| 44 | + if duthost.is_supervisor_node(): |
| 45 | + timeout = 900 |
| 46 | + elif duthost.is_multi_asic: |
| 47 | + timeout = 420 |
| 48 | + wait_critical_processes(duthost, timeout=timeout) |
| 49 | + |
| 50 | + |
| 51 | +def mock_reboot_config_file(duthost): |
| 52 | + if ( |
| 53 | + check_if_dut_file_exist(duthost, "/etc/sonic/reboot.conf") |
| 54 | + and not check_if_dut_file_exist(duthost, "/etc/sonic/reboot.conf.bak") |
| 55 | + ): |
| 56 | + execute_command(duthost, "sudo mv /etc/sonic/reboot.conf /etc/sonic/reboot.conf.bak") |
| 57 | + execute_command( |
| 58 | + duthost, |
| 59 | + "echo -e \"blocking_mode=true\\nshow_timer=true\" > /etc/sonic/reboot.conf") |
| 60 | + |
| 61 | + |
| 62 | +def mock_reboot_config_file_with_0_timeout(duthost): |
| 63 | + if ( |
| 64 | + check_if_dut_file_exist(duthost, "/etc/sonic/reboot.conf") |
| 65 | + and not check_if_dut_file_exist(duthost, "/etc/sonic/reboot.conf.bak") |
| 66 | + ): |
| 67 | + execute_command(duthost, "sudo mv /etc/sonic/reboot.conf /etc/sonic/reboot.conf.bak") |
| 68 | + execute_command( |
| 69 | + duthost, |
| 70 | + "echo -e \"blocking_mode=true\\nblocking_mode_timeout=0\\nshow_timer=true\" > /etc/sonic/reboot.conf") |
| 71 | + |
| 72 | + |
| 73 | +def restore_reboot_config_file(duthost): |
| 74 | + execute_command(duthost, "sudo rm /etc/sonic/reboot.conf") |
| 75 | + if check_if_dut_file_exist(duthost, "/etc/sonic/reboot.conf.bak"): |
| 76 | + execute_command(duthost, "sudo mv /etc/sonic/reboot.conf.bak /etc/sonic/reboot.conf") |
| 77 | + |
| 78 | + |
| 79 | +def execute_command(duthost, cmd): |
| 80 | + result = duthost.shell(cmd) |
| 81 | + pytest_assert(result["rc"] == 0, "Unexpected rc: {}".format(result["rc"])) |
| 82 | + |
| 83 | + |
| 84 | +def execute_command_ignore_error(duthost, cmd): |
| 85 | + duthost.shell(cmd, module_ignore_errors=True) |
| 86 | + |
| 87 | + |
| 88 | +def get_command_result(duthost, cmd): |
| 89 | + result = duthost.shell(cmd, module_ignore_errors=True) |
| 90 | + return result["stdout"] |
| 91 | + |
| 92 | + |
| 93 | +def check_if_dut_file_exist(duthost, filepath) -> bool: |
| 94 | + result = duthost.shell(f"test -f {filepath} && echo true || echo false", module_ignore_errors=True) |
| 95 | + return "true" in result["stdout"] |
| 96 | + |
| 97 | + |
| 98 | +class TestRebootBlockingModeCLI: |
| 99 | + @pytest.fixture(autouse=True, scope="function") |
| 100 | + def setup_teardown( |
| 101 | + self, |
| 102 | + duthosts, |
| 103 | + enum_rand_one_per_hwsku_hostname |
| 104 | + ): |
| 105 | + duthost = duthosts[enum_rand_one_per_hwsku_hostname] |
| 106 | + if check_if_platform_reboot_enabled(duthost): |
| 107 | + pytest.skip("Skip test because platform reboot is enabled.") |
| 108 | + |
| 109 | + mock_systemctl_reboot(duthost) |
| 110 | + yield |
| 111 | + restore_systemctl_reboot_and_reboot(duthost) |
| 112 | + |
| 113 | + def test_non_blocking_mode( |
| 114 | + self, |
| 115 | + duthosts, |
| 116 | + enum_rand_one_per_hwsku_hostname |
| 117 | + ): |
| 118 | + duthost = duthosts[enum_rand_one_per_hwsku_hostname] |
| 119 | + result = get_command_result( |
| 120 | + duthost, |
| 121 | + f"sudo timeout {COMMAND_TIMEOUT}s bash -c 'sudo reboot; echo \"ExpectedFinished\"'") |
| 122 | + pytest_assert("ExpectedFinished" in result, "Reboot didn't exited as expected.") |
| 123 | + |
| 124 | + def test_blocking_mode( |
| 125 | + self, |
| 126 | + duthosts, |
| 127 | + enum_rand_one_per_hwsku_hostname |
| 128 | + ): |
| 129 | + duthost = duthosts[enum_rand_one_per_hwsku_hostname] |
| 130 | + result = get_command_result( |
| 131 | + duthost, |
| 132 | + f"sudo timeout {COMMAND_TIMEOUT}s bash -c 'sudo reboot -b -v; echo \"UnexpectedFinished\"'") |
| 133 | + pytest_assert("UnexpectedFinished" not in result, "Reboot script didn't blocked as expected.") |
| 134 | + pattern = r".*\n[.]+$" |
| 135 | + pytest_assert(re.search(pattern, result), "Cannot find dots as expected in output: {}".format(result)) |
| 136 | + |
| 137 | + |
| 138 | +class TestRebootBlockingModeConfigFile: |
| 139 | + @pytest.fixture(autouse=True, scope="function") |
| 140 | + def setup_teardown( |
| 141 | + self, |
| 142 | + duthosts, |
| 143 | + enum_rand_one_per_hwsku_hostname |
| 144 | + ): |
| 145 | + duthost = duthosts[enum_rand_one_per_hwsku_hostname] |
| 146 | + if check_if_platform_reboot_enabled(duthost): |
| 147 | + pytest.skip("Skip test because platform reboot is enabled.") |
| 148 | + |
| 149 | + mock_systemctl_reboot(duthost) |
| 150 | + yield |
| 151 | + |
| 152 | + restore_reboot_config_file(duthost) |
| 153 | + restore_systemctl_reboot_and_reboot(duthost) |
| 154 | + |
| 155 | + def test_timeout_for_blocking_mode( |
| 156 | + self, |
| 157 | + duthosts, |
| 158 | + enum_rand_one_per_hwsku_hostname |
| 159 | + ): |
| 160 | + duthost = duthosts[enum_rand_one_per_hwsku_hostname] |
| 161 | + mock_reboot_config_file_with_0_timeout(duthost) |
| 162 | + result = get_command_result( |
| 163 | + duthost, |
| 164 | + f"sudo timeout {COMMAND_TIMEOUT}s bash -c 'sudo reboot; echo \"ExpectedFinished\"'") |
| 165 | + pytest_assert("ExpectedFinished" in result, "Reboot didn't exited as expected.") |
0 commit comments