~1chb1n/charms/trusty/nova-cloud-controller/15.10-stable-flip-tests-helper-syncs

« back to all changes in this revision

Viewing changes to unit_tests/test_nova_cc_hooks.py

[yolanda] Add postgresql support
Rejig templates to use includes, add Postgresql context for neutron and quantum config files.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
from mock import MagicMock, patch
2
 
 
 
1
from mock import call, MagicMock, patch
3
2
from test_utils import CharmTestCase
4
3
 
5
 
import nova_cc_utils as utils
 
4
with patch('charmhelpers.core.hookenv.config') as config:
 
5
    config.return_value = 'neutron'
 
6
    import nova_cc_utils as utils
6
7
 
7
8
_reg = utils.register_configs
8
9
_map = utils.restart_map
15
16
utils.register_configs = _reg
16
17
utils.restart_map = _map
17
18
 
18
 
 
19
19
TO_PATCH = [
20
20
    'api_port',
21
21
    'apt_update',
29
29
    'determine_packages',
30
30
    'determine_ports',
31
31
    'open_port',
 
32
    'is_relation_made',
 
33
    'log',
32
34
    'relation_get',
33
35
    'relation_set',
 
36
    'relation_ids',
34
37
    'ssh_compute_add',
35
38
    'ssh_known_hosts_b64',
36
39
    'ssh_authorized_keys_b64',
42
45
    'eligible_leader',
43
46
    'keystone_ca_cert_b64',
44
47
    'neutron_plugin',
 
48
    'migrate_database',
45
49
]
46
50
 
47
51
 
63
67
 
64
68
    def setUp(self):
65
69
        super(NovaCCHooksTests, self).setUp(hooks, TO_PATCH)
 
70
 
66
71
        self.config.side_effect = self.test_config.get
67
72
        self.relation_get.side_effect = self.test_relation.get
68
73
        self.charm_dir.return_value = '/var/lib/juju/charms/nova/charm'
138
143
            quantum_url='http://nova-cc-host1:9696', quantum_plugin='nvp',
139
144
            relation_id=None,
140
145
            **FAKE_KS_AUTH_CFG)
 
146
 
 
147
    def test_db_joined(self):
 
148
        self.unit_get.return_value = 'nova.foohost.com'
 
149
        self.is_relation_made.return_value = False
 
150
        hooks.db_joined()
 
151
        self.relation_set.assert_called_with(nova_database='nova',
 
152
                                             nova_username='nova',
 
153
                                             nova_hostname='nova.foohost.com')
 
154
        self.unit_get.assert_called_with('private-address')
 
155
 
 
156
    def test_postgresql_nova_db_joined(self):
 
157
        self.is_relation_made.return_value = False
 
158
        hooks.pgsql_nova_db_joined()
 
159
        self.relation_set.assert_called_with(database='nova')
 
160
 
 
161
    def test_postgresql_neutron_db_joined(self):
 
162
        self.is_relation_made.return_value = False
 
163
        hooks.pgsql_neutron_db_joined()
 
164
        self.relation_set.assert_called_with(database='neutron')
 
165
 
 
166
    def test_db_joined_with_postgresql(self):
 
167
        self.is_relation_made.return_value = True
 
168
 
 
169
        with self.assertRaises(Exception) as context:
 
170
            hooks.db_joined()
 
171
        self.assertEqual(context.exception.message,
 
172
                         'Attempting to associate a mysql database when there is already '
 
173
                         'associated a postgresql one')
 
174
 
 
175
    def test_postgresql_nova_joined_with_db(self):
 
176
        self.is_relation_made.return_value = True
 
177
 
 
178
        with self.assertRaises(Exception) as context:
 
179
            hooks.pgsql_nova_db_joined()
 
180
        self.assertEqual(context.exception.message,
 
181
                         'Attempting to associate a postgresql database when there is already '
 
182
                         'associated a mysql one')
 
183
 
 
184
    def test_postgresql_neutron_joined_with_db(self):
 
185
        self.is_relation_made.return_value = True
 
186
 
 
187
        with self.assertRaises(Exception) as context:
 
188
            hooks.pgsql_neutron_db_joined()
 
189
        self.assertEqual(context.exception.message,
 
190
                         'Attempting to associate a postgresql database when there is already '
 
191
                         'associated a mysql one')
 
192
 
 
193
    @patch.object(hooks, 'CONFIGS')
 
194
    def test_db_changed_missing_relation_data(self, configs):
 
195
        configs.complete_contexts = MagicMock()
 
196
        configs.complete_contexts.return_value = []
 
197
        hooks.db_changed()
 
198
        self.log.assert_called_with(
 
199
            'shared-db relation incomplete. Peer not ready?'
 
200
        )
 
201
 
 
202
    @patch.object(hooks, 'CONFIGS')
 
203
    def test_postgresql_nova_db_changed_missing_relation_data(self, configs):
 
204
        configs.complete_contexts = MagicMock()
 
205
        configs.complete_contexts.return_value = []
 
206
        hooks.postgresql_nova_db_changed()
 
207
        self.log.assert_called_with(
 
208
            'pgsql-nova-db relation incomplete. Peer not ready?'
 
209
        )
 
210
 
 
211
    def _shared_db_test(self, configs):
 
212
        configs.complete_contexts = MagicMock()
 
213
        configs.complete_contexts.return_value = ['shared-db']
 
214
        configs.write = MagicMock()
 
215
        hooks.db_changed()
 
216
 
 
217
    def _postgresql_db_test(self, configs):
 
218
        configs.complete_contexts = MagicMock()
 
219
        configs.complete_contexts.return_value = ['pgsql-nova-db']
 
220
        configs.write = MagicMock()
 
221
        hooks.postgresql_nova_db_changed()
 
222
 
 
223
    @patch.object(hooks, 'CONFIGS')
 
224
    def test_db_changed(self, configs):
 
225
        self._shared_db_test(configs)
 
226
        self.assertEquals([call('/etc/nova/nova.conf')],
 
227
                          configs.write.call_args_list)
 
228
        self.migrate_database.assert_called_with()
 
229
 
 
230
    @patch.object(hooks, 'CONFIGS')
 
231
    def test_postgresql_db_changed(self, configs):
 
232
        self._postgresql_db_test(configs)
 
233
        self.assertEquals([call('/etc/nova/nova.conf')],
 
234
                          configs.write.call_args_list)
 
235
        self.migrate_database.assert_called_with()