Skip to content

Commit a40bbc8

Browse files
Fix test flakiness due to time.time() calls
In the reboot_test.py, the assert calls check for the exact timestamp to match. However sometimes there is a delay between when the call is made and when the assert is checked. If this delay is greater than 1 second, the timestamps diverge and the assert fails. This is a flakiness introduced by the test framework and hence we should mock it and make it more robust. Signed-off-by: arista-hpandya <hpandya@arista.com>
1 parent 2c5bf36 commit a40bbc8

1 file changed

Lines changed: 10 additions & 5 deletions

File tree

tests/host_modules/reboot_test.py

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -204,13 +204,14 @@ def test_execute_reboot_success(self):
204204
with (
205205
mock.patch("reboot._run_command") as mock_run_command,
206206
mock.patch("time.sleep") as mock_sleep,
207+
mock.patch("time.time", return_value=TEST_TIMESTAMP),
207208
mock.patch("reboot.Reboot.populate_reboot_status_flag") as mock_populate_reboot_status_flag,
208209
):
209210
mock_run_command.return_value = (0, ["stdout: execute WARM reboot"], ["stderror: execute WARM reboot"])
210211
self.reboot_module.execute_reboot("WARM")
211212
mock_run_command.assert_called_once_with("sudo warm-reboot")
212213
mock_sleep.assert_called_once_with(260)
213-
mock_populate_reboot_status_flag.assert_called_once_with(False, int(time.time()), "Reboot command failed to execute", 'WARM', RebootStatus.STATUS_FAILURE)
214+
mock_populate_reboot_status_flag.assert_called_once_with(False, int(TEST_TIMESTAMP), "Reboot command failed to execute", 'WARM', RebootStatus.STATUS_FAILURE)
214215

215216
def test_execute_reboot_fail_unknown_reboot(self, caplog):
216217
with caplog.at_level(logging.ERROR):
@@ -221,6 +222,7 @@ def test_execute_reboot_fail_unknown_reboot(self, caplog):
221222
def test_execute_reboot_fail_issue_reboot_command_cold_boot(self, caplog):
222223
with (
223224
mock.patch("reboot._run_command") as mock_run_command,
225+
mock.patch("time.time", return_value=TEST_TIMESTAMP),
224226
mock.patch("reboot.Reboot.populate_reboot_status_flag") as mock_populate_reboot_status_flag,
225227
caplog.at_level(logging.ERROR),
226228
):
@@ -230,11 +232,12 @@ def test_execute_reboot_fail_issue_reboot_command_cold_boot(self, caplog):
230232
"stdout: ['stdout: execute cold reboot'], stderr: "
231233
"['stderror: execute cold reboot']")
232234
assert caplog.records[0].message == msg
233-
mock_populate_reboot_status_flag.assert_called_once_with(False, int (time.time()), "Failed to execute reboot command", 1, RebootStatus.STATUS_FAILURE)
235+
mock_populate_reboot_status_flag.assert_called_once_with(False, int(TEST_TIMESTAMP), "Failed to execute reboot command", 1, RebootStatus.STATUS_FAILURE)
234236

235237
def test_execute_reboot_fail_issue_reboot_command_halt(self, caplog):
236238
with (
237239
mock.patch("reboot._run_command") as mock_run_command,
240+
mock.patch("time.time", return_value=TEST_TIMESTAMP),
238241
mock.patch("reboot.Reboot.populate_reboot_status_flag") as mock_populate_reboot_status_flag,
239242
caplog.at_level(logging.ERROR),
240243
):
@@ -244,7 +247,7 @@ def test_execute_reboot_fail_issue_reboot_command_halt(self, caplog):
244247
"stdout: ['stdout: execute halt reboot'], stderr: "
245248
"['stderror: execute halt reboot']")
246249
assert caplog.records[0].message == msg
247-
mock_populate_reboot_status_flag.assert_called_once_with(False, int (time.time()), "Failed to execute reboot command", 3, RebootStatus.STATUS_FAILURE)
250+
mock_populate_reboot_status_flag.assert_called_once_with(False, int(TEST_TIMESTAMP), "Failed to execute reboot command", 3, RebootStatus.STATUS_FAILURE)
248251

249252
def test_execute_reboot_success_halt(self):
250253
with (
@@ -265,6 +268,7 @@ def test_execute_reboot_fail_halt_timeout(self, caplog):
265268
with (
266269
mock.patch("reboot._run_command") as mock_run_command,
267270
mock.patch("time.sleep") as mock_sleep,
271+
mock.patch("time.time", return_value=TEST_TIMESTAMP),
268272
mock.patch("reboot.Reboot.is_halt_command_running", return_value=True) as mock_is_halt_command_running,
269273
mock.patch("reboot.Reboot.is_container_running", return_value=True) as mock_is_container_running,
270274
mock.patch("reboot.Reboot.populate_reboot_status_flag") as mock_populate_reboot_status_flag,
@@ -276,11 +280,12 @@ def test_execute_reboot_fail_halt_timeout(self, caplog):
276280
mock_sleep.assert_called_with(5)
277281
mock_is_halt_command_running.assert_called()
278282
assert any("HALT reboot failed: Services are still running" in record.message for record in caplog.records)
279-
mock_populate_reboot_status_flag.assert_called_once_with(False, int(time.time()), 'Halt reboot did not complete', 3, RebootStatus.STATUS_FAILURE)
283+
mock_populate_reboot_status_flag.assert_called_once_with(False, int(TEST_TIMESTAMP), 'Halt reboot did not complete', 3, RebootStatus.STATUS_FAILURE)
280284

281285
def test_execute_reboot_fail_issue_reboot_command_warm(self, caplog):
282286
with (
283287
mock.patch("reboot._run_command") as mock_run_command,
288+
mock.patch("time.time", return_value=TEST_TIMESTAMP),
284289
mock.patch("reboot.Reboot.populate_reboot_status_flag") as mock_populate_reboot_status_flag,
285290
caplog.at_level(logging.ERROR),
286291
):
@@ -290,7 +295,7 @@ def test_execute_reboot_fail_issue_reboot_command_warm(self, caplog):
290295
"stdout: ['stdout: execute WARM reboot'], stderr: "
291296
"['stderror: execute WARM reboot']")
292297
assert caplog.records[0].message == msg
293-
mock_populate_reboot_status_flag.assert_called_once_with(False, int (time.time()), "Failed to execute reboot command", 'WARM', RebootStatus.STATUS_FAILURE)
298+
mock_populate_reboot_status_flag.assert_called_once_with(False, int(TEST_TIMESTAMP), "Failed to execute reboot command", 'WARM', RebootStatus.STATUS_FAILURE)
294299

295300
def test_issue_reboot_success_cold_boot(self):
296301
with (

0 commit comments

Comments
 (0)