github.com/juju/juju@v0.0.0-20240327075706-a90865de2538/acceptancetests/tests/test_log_check.py (about) 1 """Tests for log_check script.""" 2 3 try: 4 from mock import patch 5 except ImportError: 6 from unittest.mock import patch 7 import subprocess 8 9 import log_check as lc 10 from tests import TestCase 11 12 13 class TestCheckFile(TestCase): 14 15 regex = 'test123' 16 file_path = '/tmp/file.log' 17 18 @patch('sys.stdout') 19 def test_calls_check_call(self, _): 20 with patch.object(lc.subprocess, 'check_call') as m_checkcall: 21 lc.check_file(self.regex, self.file_path) 22 23 m_checkcall.assert_called_once_with( 24 ['sudo', 'egrep', self.regex, self.file_path]) 25 26 @patch('sys.stdout') 27 def test_fails_after_attempting_multiple_times(self, _): 28 with patch.object(lc.subprocess, 'check_call') as m_checkcall: 29 m_checkcall.side_effect = subprocess.CalledProcessError( 30 1, ['sudo', 'egrep', self.regex, self.file_path]) 31 with patch.object(lc.time, 'sleep') as m_sleep: 32 self.assertEqual( 33 lc.check_file(self.regex, self.file_path), 34 lc.check_result.failure) 35 self.assertEqual(m_sleep.call_count, 10) 36 37 @patch('sys.stdout') 38 def test_fails_when_meeting_unexpected_outcome(self, _): 39 with patch.object(lc.subprocess, 'check_call') as m_checkcall: 40 m_checkcall.side_effect = subprocess.CalledProcessError( 41 -1, ['sudo', 'egrep', self.regex, self.file_path]) 42 self.assertEqual( 43 lc.check_file(self.regex, self.file_path), 44 lc.check_result.exception) 45 46 @patch('sys.stdout') 47 def test_succeeds_when_regex_found(self, _): 48 with patch.object(lc.subprocess, 'check_call'): 49 self.assertEqual( 50 lc.check_file(self.regex, self.file_path), 51 lc.check_result.success) 52 53 54 class TestRaiseIfFileNotFound(TestCase): 55 56 def test_raises_when_file_not_found(self): 57 with self.assertRaises(ValueError): 58 lc.raise_if_file_not_found('/thisfilewontexists') 59 60 def test_does_not_raise_when_file_not_found(self): 61 lc.raise_if_file_not_found('/') 62 63 64 class TestParseArgs(TestCase): 65 66 def test_basic_args(self): 67 args = ['test .*', '/tmp/log.file'] 68 parsed = lc.parse_args(args) 69 self.assertEqual(parsed.regex, 'test .*') 70 self.assertEqual(parsed.file_path, '/tmp/log.file')