1
"""Tests for log_check script."""
7
from tests import TestCase
10
class TestCheckFile(TestCase):
13
file_path = '/tmp/file.log'
15
def test_calls_check_call(self):
16
with patch.object(lc.subprocess, 'check_call') as m_checkcall:
17
lc.check_file(self.regex, self.file_path)
19
m_checkcall.assert_called_once_with(
20
['sudo', 'egrep', self.regex, self.file_path])
22
def test_fails_after_attempting_multiple_times(self):
23
with patch.object(lc.subprocess, 'check_call') as m_checkcall:
24
m_checkcall.side_effect = subprocess.CalledProcessError(
25
1, ['sudo', 'egrep', self.regex, self.file_path])
26
with patch.object(lc.time, 'sleep') as m_sleep:
28
lc.check_file(self.regex, self.file_path),
29
lc.check_result.failure)
30
self.assertEqual(m_sleep.call_count, 10)
32
def test_fails_when_meeting_unexpected_outcome(self):
33
with patch.object(lc.subprocess, 'check_call') as m_checkcall:
34
m_checkcall.side_effect = subprocess.CalledProcessError(
35
-1, ['sudo', 'egrep', self.regex, self.file_path])
37
lc.check_file(self.regex, self.file_path),
38
lc.check_result.exception)
40
def test_succeeds_when_regex_found(self):
41
with patch.object(lc.subprocess, 'check_call'):
43
lc.check_file(self.regex, self.file_path),
44
lc.check_result.success)
47
class TestRaiseIfFileNotFound(TestCase):
49
def test_raises_when_file_not_found(self):
50
with self.assertRaises(ValueError):
51
lc.raise_if_file_not_found('/thisfilewontexists')
53
def test_does_not_raise_when_file_not_found(self):
54
lc.raise_if_file_not_found('/')
57
class TestParseArgs(TestCase):
59
def test_basic_args(self):
60
args = ['test .*', '/tmp/log.file']
61
parsed = lc.parse_args(args)
62
self.assertEqual(parsed.regex, 'test .*')
63
self.assertEqual(parsed.file_path, '/tmp/log.file')