~andrewjbeach/juju-ci-tools/get-juju-dict

« back to all changes in this revision

Viewing changes to tests/test_log_check.py

  • Committer: Christopher Lee
  • Date: 2016-08-03 06:06:52 UTC
  • mto: This revision was merged to the branch mainline in revision 1535.
  • Revision ID: chris.lee@canonical.com-20160803060652-t15epgkgkfkzjvhg
Put check logic into separate file with tests.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
"""Tests for log_check script."""
 
2
 
 
3
from mock import patch
 
4
import subprocess
 
5
 
 
6
import log_check as lc
 
7
from tests import TestCase
 
8
 
 
9
 
 
10
class TestCheckFile(TestCase):
 
11
 
 
12
    regex = 'test123'
 
13
    file_path = '/tmp/file.log'
 
14
 
 
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)
 
18
 
 
19
            m_checkcall.assert_called_once_with(
 
20
                ['sudo', 'egrep', self.regex, self.file_path])
 
21
 
 
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:
 
27
                self.assertEqual(
 
28
                    lc.check_file(self.regex, self.file_path),
 
29
                    lc.check_result.failure)
 
30
                self.assertEqual(m_sleep.call_count, 10)
 
31
 
 
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])
 
36
            self.assertEqual(
 
37
                lc.check_file(self.regex, self.file_path),
 
38
                lc.check_result.exception)
 
39
 
 
40
    def test_succeeds_when_regex_found(self):
 
41
        with patch.object(lc.subprocess, 'check_call'):
 
42
            self.assertEqual(
 
43
                lc.check_file(self.regex, self.file_path),
 
44
                lc.check_result.success)
 
45
 
 
46
 
 
47
class TestRaiseIfFileNotFound(TestCase):
 
48
 
 
49
    def test_raises_when_file_not_found(self):
 
50
        with self.assertRaises(ValueError):
 
51
            lc.raise_if_file_not_found('/thisfilewontexists')
 
52
 
 
53
    def test_does_not_raise_when_file_not_found(self):
 
54
        lc.raise_if_file_not_found('/')
 
55
 
 
56
 
 
57
class TestParseArgs(TestCase):
 
58
 
 
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')