~wuwenbin2/onosfw/neutron-gateway

« back to all changes in this revision

Viewing changes to unit_tests/test_actions_git_reinstall.py

  • Committer: wuwenbin2
  • Date: 2015-12-31 03:51:32 UTC
  • Revision ID: git-v1:1d9e201a55e6c4996df4bf5beb62557f5f28b5ed
neutron-gateway

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
from mock import patch, MagicMock
 
2
 
 
3
with patch('charmhelpers.core.hookenv.config') as config:
 
4
    config.return_value = 'neutron'
 
5
    import neutron_utils as utils  # noqa
 
6
 
 
7
from test_utils import (
 
8
    CharmTestCase
 
9
)
 
10
 
 
11
# Need to do some early patching to get the module loaded.
 
12
_register_configs = utils.register_configs
 
13
_restart_map = utils.restart_map
 
14
 
 
15
utils.register_configs = MagicMock()
 
16
utils.restart_map = MagicMock()
 
17
 
 
18
with patch('charmhelpers.core.hookenv.status_set'):
 
19
    import git_reinstall
 
20
 
 
21
# Unpatch it now that its loaded.
 
22
utils.register_configs = _register_configs
 
23
utils.restart_map = _restart_map
 
24
 
 
25
TO_PATCH = [
 
26
    'config',
 
27
]
 
28
 
 
29
 
 
30
openstack_origin_git = \
 
31
    """repositories:
 
32
         - {name: requirements,
 
33
            repository: 'git://git.openstack.org/openstack/requirements',
 
34
            branch: stable/juno}
 
35
         - {name: neutron,
 
36
            repository: 'git://git.openstack.org/openstack/neutron',
 
37
            branch: stable/juno}"""
 
38
 
 
39
 
 
40
class TestNeutronAPIActions(CharmTestCase):
 
41
 
 
42
    def setUp(self):
 
43
        super(TestNeutronAPIActions, self).setUp(git_reinstall, TO_PATCH)
 
44
        self.config.side_effect = self.test_config.get
 
45
 
 
46
    @patch.object(git_reinstall, 'action_set')
 
47
    @patch.object(git_reinstall, 'action_fail')
 
48
    @patch.object(git_reinstall, 'git_install')
 
49
    @patch.object(git_reinstall, 'config_changed')
 
50
    def test_git_reinstall(self, config_changed, git_install, action_fail,
 
51
                           action_set):
 
52
        self.test_config.set('openstack-origin-git', openstack_origin_git)
 
53
 
 
54
        git_reinstall.git_reinstall()
 
55
 
 
56
        git_install.assert_called_with(openstack_origin_git)
 
57
        self.assertTrue(git_install.called)
 
58
        self.assertTrue(config_changed.called)
 
59
        self.assertFalse(action_set.called)
 
60
        self.assertFalse(action_fail.called)
 
61
 
 
62
    @patch.object(git_reinstall, 'action_set')
 
63
    @patch.object(git_reinstall, 'action_fail')
 
64
    @patch.object(git_reinstall, 'git_install')
 
65
    @patch.object(git_reinstall, 'config_changed')
 
66
    @patch('charmhelpers.contrib.openstack.utils.config')
 
67
    def test_git_reinstall_not_configured(self, _config, config_changed,
 
68
                                          git_install, action_fail,
 
69
                                          action_set):
 
70
        _config.return_value = None
 
71
 
 
72
        git_reinstall.git_reinstall()
 
73
 
 
74
        msg = 'openstack-origin-git is not configured'
 
75
        action_fail.assert_called_with(msg)
 
76
        self.assertFalse(git_install.called)
 
77
        self.assertFalse(action_set.called)
 
78
 
 
79
    @patch.object(git_reinstall, 'action_set')
 
80
    @patch.object(git_reinstall, 'action_fail')
 
81
    @patch.object(git_reinstall, 'git_install')
 
82
    @patch.object(git_reinstall, 'config_changed')
 
83
    @patch('traceback.format_exc')
 
84
    @patch('charmhelpers.contrib.openstack.utils.config')
 
85
    def test_git_reinstall_exception(self, _config, format_exc,
 
86
                                     config_changed, git_install, action_fail,
 
87
                                     action_set):
 
88
        _config.return_value = openstack_origin_git
 
89
        e = OSError('something bad happened')
 
90
        git_install.side_effect = e
 
91
        traceback = (
 
92
            "Traceback (most recent call last):\n"
 
93
            "  File \"actions/git_reinstall.py\", line 37, in git_reinstall\n"
 
94
            "    git_install(config(\'openstack-origin-git\'))\n"
 
95
            "  File \"/usr/lib/python2.7/dist-packages/mock.py\", line 964, in __call__\n"  # noqa
 
96
            "    return _mock_self._mock_call(*args, **kwargs)\n"
 
97
            "  File \"/usr/lib/python2.7/dist-packages/mock.py\", line 1019, in _mock_call\n"  # noqa
 
98
            "    raise effect\n"
 
99
            "OSError: something bad happened\n")
 
100
        format_exc.return_value = traceback
 
101
 
 
102
        git_reinstall.git_reinstall()
 
103
 
 
104
        msg = 'git-reinstall resulted in an unexpected error'
 
105
        action_fail.assert_called_with(msg)
 
106
        action_set.assert_called_with({'traceback': traceback})