Skip to content

Commit

Permalink
test(monitor): mock file write for recovery tests
Browse files Browse the repository at this point in the history
Fixes jamesoff#1356
The tests for `did_recovery` and `did_recovered` touch a file that they
leave around.
While we could use a temporary file, in this context, it seems like
mocking subprocess.Popen should be sufficient.
  • Loading branch information
wyardley committed Feb 9, 2025
1 parent 2c6edcb commit 74133e5
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 16 deletions.
1 change: 0 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ docs/vendor
monitor.log
monitor.pid
output.json
did_recover*
simplemonitor.egg-info
dist
/html
Expand Down
25 changes: 10 additions & 15 deletions tests/test_monitor.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
# type: ignore
import datetime
import os
import platform
import time
import unittest
from pathlib import Path
from unittest import mock

import arrow

Expand Down Expand Up @@ -364,24 +364,19 @@ def test_compound_fail(self):
"compound monitor did not report failures properly",
)

def test_recovery(self):
@mock.patch("subprocess.Popen")
def test_recovery(self, mock_popen):
m = MonitorFail("fail1", {"recover_command": "touch did_recovery"})
try:
os.unlink("did_recovery")
except FileNotFoundError:
pass
m.run_test()
m.attempt_recover()
# throws an exception if the file isn't there
os.stat("did_recovery")
# Make sure we called the fake command
mock_popen.assert_called_once_with(["touch", "did_recovery"])

def test_recovered(self):
@mock.patch("subprocess.Popen")
def test_recovered(self, mock_popen):
m = MonitorFail("fail9", {"recovered_command": "touch did_recovered"})
try:
os.unlink("did_recovered")
except FileNotFoundError:
pass
for i in range(0, 6):
for _ in range(0, 6):
m.run_test()
m.run_recovered()
os.stat("did_recovered")
# Make sure we called the fake command
mock_popen.assert_called_once_with(["touch", "did_recovered"])

0 comments on commit 74133e5

Please sign in to comment.