~sidnei/charms/precise/squid-forwardproxy/trunk

« back to all changes in this revision

Viewing changes to tests/test_utils.py

  • Committer: Tarmac
  • Author(s): Diogo Baeder de Paula Pinto
  • Date: 2013-02-21 21:39:48 UTC
  • mfrom: (22.1.23 squid-forwardproxy)
  • Revision ID: tarmac-20130221213948-1bqcf9nitufy57j6
[r=sidnei] Increasing code coverage; more migration to charmsupport

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
from contextlib import contextmanager
2
 
 
3
1
from testtools import TestCase
4
 
from mock import patch, call, MagicMock
 
2
from mock import patch, call
5
3
 
6
4
import utils
7
5
 
8
6
 
9
 
@contextmanager
10
 
def patch_open():
11
 
    '''Patch open() to allow mocking both open() itself and the file that is
12
 
    yielded.
13
 
 
14
 
    Yields the mock for "open" and "file", respectively.'''
15
 
    mock_open = MagicMock(spec=open)
16
 
    mock_file = MagicMock(spec=file)
17
 
 
18
 
    @contextmanager
19
 
    def stub_open(*args, **kwargs):
20
 
        mock_open(*args, **kwargs)
21
 
        yield mock_file
22
 
 
23
 
    with patch('__builtin__.open', stub_open):
24
 
        yield mock_open, mock_file
25
 
 
26
 
 
27
7
class AptLockIsNoMore(Exception):
28
8
    pass
29
9
 
88
68
                                       '-odpkg::options::=--force-confold',
89
69
                                       'install', 'foo', 'bar'])
90
70
 
91
 
    @patch('subprocess.check_output')
92
 
    def test_gets_a_config(self, check_output):
93
 
        check_output.return_value = ' some result '
94
 
 
95
 
        result = utils.config_get('foo')
96
 
 
97
 
        self.assertEqual(result, 'some result')
98
 
        check_output.assert_called_with(['config-get', 'foo'])
99
 
 
100
71
    @patch('subprocess.check_call')
101
72
    def test_logs_something_with_juju(self, check_call):
102
73
        utils.juju_log('not critical', 'some message')