~barryprice/juju-deployer/LP1892423

« back to all changes in this revision

Viewing changes to deployer/tests/test_utils.py

  • Committer: Adam Gandelman
  • Date: 2013-09-03 20:44:14 UTC
  • mfrom: (69.3.45 darwin)
  • Revision ID: adamg@canonical.com-20130903204414-xsqqz2gp83dp5d2o
MergeĀ lp:juju-deployer/darwin.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
from mock import patch, MagicMock
 
2
from subprocess import CalledProcessError
 
3
from .base import Base
 
4
from deployer.utils import dict_merge, _check_call, ErrorExit
 
5
 
 
6
 
 
7
class UtilTests(Base):
 
8
 
 
9
    def test_relation_list_merge(self):
 
10
        self.assertEqual(
 
11
            dict_merge(
 
12
                {'relations': [['m1', 'x1']]},
 
13
                {'relations': [['m2', 'x2']]}),
 
14
            {'relations': [['m1', 'x1'], ['m2', 'x2']]})
 
15
 
 
16
    def test_no_rels_in_target(self):
 
17
        self.assertEqual(
 
18
            dict_merge(
 
19
                {'a': 1},
 
20
                {'relations': [['m1', 'x1'], ['m2', 'x2']]}),
 
21
            {'a': 1, 'relations': [['m1', 'x1'], ['m2', 'x2']]})
 
22
 
 
23
    @patch('subprocess.check_output')
 
24
    def test_check_call_fail_no_retry(self, check_output):
 
25
        _e = CalledProcessError(returncode=1, cmd=['fail'])
 
26
        check_output.side_effect = _e
 
27
        self.assertRaises(
 
28
            ErrorExit, _check_call, params=['fail'], log=MagicMock())
 
29
 
 
30
    @patch('time.sleep')
 
31
    @patch('subprocess.check_output')
 
32
    def test_check_call_fail_retry(self, check_output, sleep):
 
33
        _e = CalledProcessError(returncode=1, cmd=['fail'])
 
34
        check_output.side_effect = _e
 
35
        self.assertRaises(
 
36
            ErrorExit, _check_call, params=['fail'], log=MagicMock(), max_retry=3)
 
37
        # 1 failure + 3 retries
 
38
        self.assertEquals(len(check_output.call_args_list), 4)
 
39
 
 
40
    @patch('time.sleep')
 
41
    @patch('subprocess.check_output')
 
42
    def test_check_call_succeed_after_retry(self, check_output, sleep):
 
43
        # call succeeds after the 3rd try
 
44
        _e = CalledProcessError(returncode=1, cmd=['maybe_fail'])
 
45
        check_output.side_effect = [
 
46
            _e, _e, 'good', _e ]
 
47
        output = _check_call(params=['magybe_fail'], log=MagicMock(), max_retry=3)
 
48
        self.assertEquals(output, 'good')
 
49
        # 1 failure + 3 retries
 
50
        self.assertEquals(len(check_output.call_args_list), 3)