~james-page/charms/trusty/neutron-openvswitch/lp1531102

« back to all changes in this revision

Viewing changes to unit_tests/test_neutron_ovs_hooks.py

Deploy from source

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
 
2
1
from mock import MagicMock, patch, call
 
2
import yaml
 
3
 
3
4
from test_utils import CharmTestCase
4
5
 
5
 
 
6
6
with patch('charmhelpers.core.hookenv.config') as config:
7
7
    config.return_value = 'neutron'
8
8
    import neutron_ovs_utils as utils
21
21
TO_PATCH = [
22
22
    'apt_update',
23
23
    'apt_install',
 
24
    'apt_purge',
24
25
    'config',
25
26
    'CONFIGS',
26
27
    'determine_packages',
 
28
    'determine_dvr_packages',
 
29
    'get_shared_secret',
 
30
    'git_install',
27
31
    'log',
 
32
    'relation_ids',
28
33
    'relation_set',
 
34
    'configure_ovs',
 
35
    'use_dvr',
29
36
]
30
37
NEUTRON_CONF_DIR = "/etc/neutron"
31
38
 
44
51
        hooks.hooks.execute([
45
52
            'hooks/{}'.format(hookname)])
46
53
 
47
 
    def test_install_hook(self):
 
54
    @patch.object(hooks, 'git_install_requested')
 
55
    def test_install_hook(self, git_requested):
 
56
        git_requested.return_value = False
48
57
        _pkgs = ['foo', 'bar']
49
58
        self.determine_packages.return_value = [_pkgs]
50
59
        self._call_hook('install')
53
62
            call(_pkgs, fatal=True),
54
63
        ])
55
64
 
56
 
    def test_config_changed(self):
57
 
        self._call_hook('config-changed')
58
 
        self.assertTrue(self.CONFIGS.write_all.called)
 
65
    @patch.object(hooks, 'git_install_requested')
 
66
    def test_install_hook_git(self, git_requested):
 
67
        git_requested.return_value = True
 
68
        _pkgs = ['foo', 'bar']
 
69
        self.determine_packages.return_value = _pkgs
 
70
        openstack_origin_git = {
 
71
            'repositories': [
 
72
                {'name': 'requirements',
 
73
                 'repository': 'git://git.openstack.org/openstack/requirements',  # noqa
 
74
                 'branch': 'stable/juno'},
 
75
                {'name': 'neutron',
 
76
                 'repository': 'git://git.openstack.org/openstack/neutron',
 
77
                 'branch': 'stable/juno'}
 
78
            ],
 
79
            'directory': '/mnt/openstack-git',
 
80
        }
 
81
        projects_yaml = yaml.dump(openstack_origin_git)
 
82
        self.test_config.set('openstack-origin-git', projects_yaml)
 
83
        self._call_hook('install')
 
84
        self.apt_update.assert_called_with()
 
85
        self.assertTrue(self.determine_packages)
 
86
        self.git_install.assert_called_with(projects_yaml)
 
87
 
 
88
    @patch.object(hooks, 'git_install_requested')
 
89
    def test_config_changed(self, git_requested):
 
90
        git_requested.return_value = False
 
91
        self.relation_ids.return_value = ['relid']
 
92
        _zmq_joined = self.patch('zeromq_configuration_relation_joined')
 
93
        self._call_hook('config-changed')
 
94
        self.assertTrue(self.CONFIGS.write_all.called)
 
95
        self.assertTrue(_zmq_joined.called_with('relid'))
 
96
        self.configure_ovs.assert_called_with()
 
97
 
 
98
    @patch.object(hooks, 'git_install_requested')
 
99
    @patch.object(hooks, 'config_value_changed')
 
100
    def test_config_changed_git(self, config_val_changed, git_requested):
 
101
        git_requested.return_value = True
 
102
        self.relation_ids.return_value = ['relid']
 
103
        _zmq_joined = self.patch('zeromq_configuration_relation_joined')
 
104
        openstack_origin_git = {
 
105
            'repositories': [
 
106
                {'name': 'requirements',
 
107
                 'repository':
 
108
                 'git://git.openstack.org/openstack/requirements',
 
109
                 'branch': 'stable/juno'},
 
110
                {'name': 'neutron',
 
111
                 'repository': 'git://git.openstack.org/openstack/neutron',
 
112
                 'branch': 'stable/juno'}
 
113
            ],
 
114
            'directory': '/mnt/openstack-git',
 
115
        }
 
116
        projects_yaml = yaml.dump(openstack_origin_git)
 
117
        self.test_config.set('openstack-origin-git', projects_yaml)
 
118
        self._call_hook('config-changed')
 
119
        self.git_install.assert_called_with(projects_yaml)
 
120
        self.assertTrue(self.CONFIGS.write_all.called)
 
121
        self.assertTrue(_zmq_joined.called_with('relid'))
 
122
        self.configure_ovs.assert_called_with()
 
123
 
 
124
    @patch.object(hooks, 'git_install_requested')
 
125
    def test_config_changed_dvr(self, git_requested):
 
126
        git_requested.return_value = False
 
127
        self.determine_dvr_packages.return_value = ['dvr']
 
128
        self._call_hook('config-changed')
 
129
        self.apt_update.assert_called_with()
 
130
        self.assertTrue(self.CONFIGS.write_all.called)
 
131
        self.apt_install.assert_has_calls([
 
132
            call(['dvr'], fatal=True),
 
133
        ])
 
134
        self.configure_ovs.assert_called_with()
 
135
 
 
136
    @patch.object(hooks, 'neutron_plugin_joined')
 
137
    def test_neutron_plugin_api(self, _plugin_joined):
 
138
        self.relation_ids.return_value = ['rid']
 
139
        self._call_hook('neutron-plugin-api-relation-changed')
 
140
        self.configure_ovs.assert_called_with()
 
141
        self.assertTrue(self.CONFIGS.write_all.called)
 
142
        _plugin_joined.assert_called_with(relation_id='rid')
 
143
 
 
144
    @patch.object(hooks, 'git_install_requested')
 
145
    def test_neutron_plugin_joined(self, git_requested):
 
146
        git_requested.return_value = False
 
147
        self.get_shared_secret.return_value = 'secret'
 
148
        self._call_hook('neutron-plugin-relation-joined')
 
149
        rel_data = {
 
150
            'metadata-shared-secret': 'secret',
 
151
        }
 
152
        self.relation_set.assert_called_with(
 
153
            relation_id=None,
 
154
            **rel_data
 
155
        )
59
156
 
60
157
    def test_amqp_joined(self):
61
158
        self._call_hook('amqp-relation-joined')