added unit testing for db

This commit is contained in:
yolanda.robla@canonical.com 2014-03-27 13:26:35 +01:00
parent d43e56ed84
commit 9753c300e7
5 changed files with 62 additions and 4 deletions

View File

@ -113,7 +113,9 @@ def pgsql_db_joined():
'associated a mysql one')
log(e, level=ERROR)
raise Exception(e)
relation_set(database=config('database'))
conf = config()
relation_set(database=conf['database'])
@hooks.hook('shared-db-relation-changed')

View File

@ -113,7 +113,9 @@ def pgsql_db_joined():
'associated a mysql one')
log(e, level=ERROR)
raise Exception(e)
relation_set(database=config('database'))
conf = config()
relation_set(database=conf['database'])
@hooks.hook('shared-db-relation-changed')

View File

@ -113,7 +113,9 @@ def pgsql_db_joined():
'associated a mysql one')
log(e, level=ERROR)
raise Exception(e)
relation_set(database=config('database'))
conf = config()
relation_set(database=conf['database'])
@hooks.hook('shared-db-relation-changed')

View File

@ -113,7 +113,9 @@ def pgsql_db_joined():
'associated a mysql one')
log(e, level=ERROR)
raise Exception(e)
relation_set(database=config('database'))
conf = config()
relation_set(database=conf['database'])
@hooks.hook('shared-db-relation-changed')

View File

@ -31,6 +31,7 @@ TO_PATCH = [
'ensure_ceph_keyring',
'ensure_ceph_pool',
'juju_log',
'log',
'lsb_release',
'migrate_database',
'prepare_lvm_storage',
@ -43,6 +44,7 @@ TO_PATCH = [
'ceph_config_file',
# charmhelpers.core.hookenv
'config',
'is_relation_made',
'relation_get',
'relation_ids',
'relation_set',
@ -159,12 +161,25 @@ class TestChangedHooks(CharmTestCase):
self.CONFIGS.write.assert_called_with('/etc/cinder/cinder.conf')
self.assertTrue(self.migrate_database.called)
def test_pgsql_db_changed(self):
'It writes out cinder.conf on db changed'
self.CONFIGS.complete_contexts.return_value = ['pgsql-db']
hooks.hooks.execute(['hooks/pgsql-db-relation-changed'])
self.CONFIGS.write.assert_called_with('/etc/cinder/cinder.conf')
self.assertTrue(self.migrate_database.called)
def test_db_changed_relation_incomplete(self):
'It does not write out cinder.conf with incomplete shared-db rel'
hooks.hooks.execute(['hooks/shared-db-relation-changed'])
self.assertFalse(self.CONFIGS.write.called)
self.assertFalse(self.migrate_database.called)
def test_pgsql_db_changed_relation_incomplete(self):
'It does not write out cinder.conf with incomplete pgsql-db rel'
hooks.hooks.execute(['hooks/pgsql-db-relation-changed'])
self.assertFalse(self.CONFIGS.write.called)
self.assertFalse(self.migrate_database.called)
def test_db_changed_not_leader(self):
'It does not migrate database when not leader'
self.eligible_leader.return_value = False
@ -173,6 +188,14 @@ class TestChangedHooks(CharmTestCase):
self.CONFIGS.write.assert_called_with('/etc/cinder/cinder.conf')
self.assertFalse(self.migrate_database.called)
def test_pgsql_db_changed_not_leader(self):
'It does not migrate database when not leader'
self.eligible_leader.return_value = False
self.CONFIGS.complete_contexts.return_value = ['pgsql-db']
hooks.hooks.execute(['hooks/pgsql-db-relation-changed'])
self.CONFIGS.write.assert_called_with('/etc/cinder/cinder.conf')
self.assertFalse(self.migrate_database.called)
def test_amqp_changed(self):
'It writes out cinder.conf on amqp changed with complete relation'
self.CONFIGS.complete_contexts.return_value = ['amqp']
@ -237,11 +260,38 @@ class TestJoinedHooks(CharmTestCase):
def test_db_joined(self):
'It properly requests access to a shared-db service'
self.unit_get.return_value = 'cindernode1'
self.is_relation_made.return_value = False
hooks.hooks.execute(['hooks/shared-db-relation-joined'])
expected = {'username': 'cinder',
'hostname': 'cindernode1', 'database': 'cinder'}
self.relation_set.assert_called_with(**expected)
def test_db_joined_with_postgresql(self):
self.is_relation_made.return_value = True
with self.assertRaises(Exception) as context:
hooks.hooks.execute(['hooks/shared-db-relation-joined'])
self.assertEqual(context.exception.message,
'Attempting to associate a mysql database when there '
'is already associated a postgresql one')
def test_postgresql_db_joined(self):
'It properly requests access to a postgresql-db service'
self.unit_get.return_value = 'cindernode1'
self.is_relation_made.return_value = False
hooks.hooks.execute(['hooks/pgsql-db-relation-joined'])
expected = {'database': 'cinder'}
self.relation_set.assert_called_with(**expected)
def test_postgresql_joined_with_db(self):
self.is_relation_made.return_value = True
with self.assertRaises(Exception) as context:
hooks.hooks.execute(['hooks/pgsql-db-relation-joined'])
self.assertEqual(context.exception.message,
'Attempting to associate a postgresql database when there '
'is already associated a mysql one')
def test_amqp_joined(self):
'It properly requests access to an amqp service'
hooks.hooks.execute(['hooks/amqp-relation-joined'])