Add unit tests for db_changed hook processing allowed_units

This commit is contained in:
Liam Young 2014-08-28 09:12:42 +01:00
parent 212019dc32
commit f16278bfce
2 changed files with 23 additions and 6 deletions

View File

@ -142,10 +142,11 @@ def db_changed():
else: else:
CONFIGS.write(KEYSTONE_CONF) CONFIGS.write(KEYSTONE_CONF)
if eligible_leader(CLUSTER_RES): if eligible_leader(CLUSTER_RES):
# Bugs 1353135 & 1187508. Dbs can appear to be ready before the units # Bugs 1353135 & 1187508. Dbs can appear to be ready before the
# acl entry has been added. So, if the db supports passing a list of # units acl entry has been added. So, if the db supports passing
# permitted units then check if we're in the list. # a list of permitted units then check if we're in the list.
allowed_units = relation_get('allowed_units') allowed_units = relation_get('allowed_units')
print "allowed_units:" + str(allowed_units)
if allowed_units and local_unit() not in allowed_units.split(): if allowed_units and local_unit() not in allowed_units.split():
log('Allowed_units list provided and this unit not present') log('Allowed_units list provided and this unit not present')
return return

View File

@ -26,6 +26,7 @@ TO_PATCH = [
'config', 'config',
'is_relation_made', 'is_relation_made',
'log', 'log',
'local_unit',
'filter_installed_packages', 'filter_installed_packages',
'relation_ids', 'relation_ids',
'relation_list', 'relation_list',
@ -138,7 +139,9 @@ class KeystoneRelationTests(CharmTestCase):
'pgsql-db relation incomplete. Peer not ready?' 'pgsql-db relation incomplete. Peer not ready?'
) )
def _shared_db_test(self, configs): def _shared_db_test(self, configs, unit_name):
self.relation_get.return_value = 'keystone/0 keystone/3'
self.local_unit.return_value = unit_name
configs.complete_contexts = MagicMock() configs.complete_contexts = MagicMock()
configs.complete_contexts.return_value = ['shared-db'] configs.complete_contexts.return_value = ['shared-db']
configs.write = MagicMock() configs.write = MagicMock()
@ -152,11 +155,11 @@ class KeystoneRelationTests(CharmTestCase):
@patch.object(hooks, 'CONFIGS') @patch.object(hooks, 'CONFIGS')
@patch.object(hooks, 'identity_changed') @patch.object(hooks, 'identity_changed')
def test_db_changed(self, identity_changed, configs): def test_db_changed_allowed(self, identity_changed, configs):
self.relation_ids.return_value = ['identity-service:0'] self.relation_ids.return_value = ['identity-service:0']
self.related_units.return_value = ['unit/0'] self.related_units.return_value = ['unit/0']
self._shared_db_test(configs) self._shared_db_test(configs, 'keystone/3')
self.assertEquals([call('/etc/keystone/keystone.conf')], self.assertEquals([call('/etc/keystone/keystone.conf')],
configs.write.call_args_list) configs.write.call_args_list)
self.migrate_database.assert_called_with() self.migrate_database.assert_called_with()
@ -165,6 +168,19 @@ class KeystoneRelationTests(CharmTestCase):
relation_id='identity-service:0', relation_id='identity-service:0',
remote_unit='unit/0') remote_unit='unit/0')
@patch.object(hooks, 'CONFIGS')
@patch.object(hooks, 'identity_changed')
def test_db_changed_not_allowed(self, identity_changed, configs):
self.relation_ids.return_value = ['identity-service:0']
self.related_units.return_value = ['unit/0']
self._shared_db_test(configs, 'keystone/2')
self.assertEquals([call('/etc/keystone/keystone.conf')],
configs.write.call_args_list)
self.assertFalse(self.migrate_database.called)
self.assertFalse(self.ensure_initial_admin.called)
self.assertFalse(identity_changed.called)
@patch.object(hooks, 'CONFIGS') @patch.object(hooks, 'CONFIGS')
@patch.object(hooks, 'identity_changed') @patch.object(hooks, 'identity_changed')
def test_postgresql_db_changed(self, identity_changed, configs): def test_postgresql_db_changed(self, identity_changed, configs):