Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 10 additions & 5 deletions tests/host_modules/reboot_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -204,13 +204,14 @@ def test_execute_reboot_success(self):
with (
mock.patch("reboot._run_command") as mock_run_command,
mock.patch("time.sleep") as mock_sleep,
mock.patch("time.time", return_value=TEST_TIMESTAMP),
mock.patch("reboot.Reboot.populate_reboot_status_flag") as mock_populate_reboot_status_flag,
):
mock_run_command.return_value = (0, ["stdout: execute WARM reboot"], ["stderror: execute WARM reboot"])
self.reboot_module.execute_reboot("WARM")
mock_run_command.assert_called_once_with("sudo warm-reboot")
mock_sleep.assert_called_once_with(260)
mock_populate_reboot_status_flag.assert_called_once_with(False, int(time.time()), "Reboot command failed to execute", 'WARM', RebootStatus.STATUS_FAILURE)
mock_populate_reboot_status_flag.assert_called_once_with(False, int(TEST_TIMESTAMP), "Reboot command failed to execute", 'WARM', RebootStatus.STATUS_FAILURE)

def test_execute_reboot_fail_unknown_reboot(self, caplog):
with caplog.at_level(logging.ERROR):
Expand All @@ -221,6 +222,7 @@ def test_execute_reboot_fail_unknown_reboot(self, caplog):
def test_execute_reboot_fail_issue_reboot_command_cold_boot(self, caplog):
with (
mock.patch("reboot._run_command") as mock_run_command,
mock.patch("time.time", return_value=TEST_TIMESTAMP),
mock.patch("reboot.Reboot.populate_reboot_status_flag") as mock_populate_reboot_status_flag,
caplog.at_level(logging.ERROR),
):
Expand All @@ -230,11 +232,12 @@ def test_execute_reboot_fail_issue_reboot_command_cold_boot(self, caplog):
"stdout: ['stdout: execute cold reboot'], stderr: "
"['stderror: execute cold reboot']")
assert caplog.records[0].message == msg
mock_populate_reboot_status_flag.assert_called_once_with(False, int (time.time()), "Failed to execute reboot command", 1, RebootStatus.STATUS_FAILURE)
mock_populate_reboot_status_flag.assert_called_once_with(False, int(TEST_TIMESTAMP), "Failed to execute reboot command", 1, RebootStatus.STATUS_FAILURE)

def test_execute_reboot_fail_issue_reboot_command_halt(self, caplog):
with (
mock.patch("reboot._run_command") as mock_run_command,
mock.patch("time.time", return_value=TEST_TIMESTAMP),
mock.patch("reboot.Reboot.populate_reboot_status_flag") as mock_populate_reboot_status_flag,
caplog.at_level(logging.ERROR),
):
Expand All @@ -244,7 +247,7 @@ def test_execute_reboot_fail_issue_reboot_command_halt(self, caplog):
"stdout: ['stdout: execute halt reboot'], stderr: "
"['stderror: execute halt reboot']")
assert caplog.records[0].message == msg
mock_populate_reboot_status_flag.assert_called_once_with(False, int (time.time()), "Failed to execute reboot command", 3, RebootStatus.STATUS_FAILURE)
mock_populate_reboot_status_flag.assert_called_once_with(False, int(TEST_TIMESTAMP), "Failed to execute reboot command", 3, RebootStatus.STATUS_FAILURE)

def test_execute_reboot_success_halt(self):
with (
Expand All @@ -265,6 +268,7 @@ def test_execute_reboot_fail_halt_timeout(self, caplog):
with (
mock.patch("reboot._run_command") as mock_run_command,
mock.patch("time.sleep") as mock_sleep,
mock.patch("time.time", return_value=TEST_TIMESTAMP),
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggestion (pre-existing issue): time.sleep is mocked here but time.monotonic() is not. The production code uses time.monotonic() for the halt timeout loop (while time.monotonic() - start_time < timeout). Since sleep is a no-op but monotonic is real, this test spins for ~60 real wall-clock seconds waiting for the loop to exit.

Consider also mocking time.monotonic to make this test fast:

Suggested change
mock.patch("time.time", return_value=TEST_TIMESTAMP),
mock.patch("time.time", return_value=TEST_TIMESTAMP),
mock.patch("time.monotonic", side_effect=[0, 61]),

This makes the loop iterate once (0 -> start_time=0) then exit immediately on the second call (61 > 60 timeout). It would cut this test from ~60s to near-instant.

mock.patch("reboot.Reboot.is_halt_command_running", return_value=True) as mock_is_halt_command_running,
mock.patch("reboot.Reboot.is_container_running", return_value=True) as mock_is_container_running,
mock.patch("reboot.Reboot.populate_reboot_status_flag") as mock_populate_reboot_status_flag,
Expand All @@ -276,11 +280,12 @@ def test_execute_reboot_fail_halt_timeout(self, caplog):
mock_sleep.assert_called_with(5)
mock_is_halt_command_running.assert_called()
assert any("HALT reboot failed: Services are still running" in record.message for record in caplog.records)
mock_populate_reboot_status_flag.assert_called_once_with(False, int(time.time()), 'Halt reboot did not complete', 3, RebootStatus.STATUS_FAILURE)
mock_populate_reboot_status_flag.assert_called_once_with(False, int(TEST_TIMESTAMP), 'Halt reboot did not complete', 3, RebootStatus.STATUS_FAILURE)

def test_execute_reboot_fail_issue_reboot_command_warm(self, caplog):
with (
mock.patch("reboot._run_command") as mock_run_command,
mock.patch("time.time", return_value=TEST_TIMESTAMP),
mock.patch("reboot.Reboot.populate_reboot_status_flag") as mock_populate_reboot_status_flag,
caplog.at_level(logging.ERROR),
):
Expand All @@ -290,7 +295,7 @@ def test_execute_reboot_fail_issue_reboot_command_warm(self, caplog):
"stdout: ['stdout: execute WARM reboot'], stderr: "
"['stderror: execute WARM reboot']")
assert caplog.records[0].message == msg
mock_populate_reboot_status_flag.assert_called_once_with(False, int (time.time()), "Failed to execute reboot command", 'WARM', RebootStatus.STATUS_FAILURE)
mock_populate_reboot_status_flag.assert_called_once_with(False, int(TEST_TIMESTAMP), "Failed to execute reboot command", 'WARM', RebootStatus.STATUS_FAILURE)

def test_issue_reboot_success_cold_boot(self):
with (
Expand Down
Loading