From 9753c300e74701e7db97f0c308096cdce005ae2a Mon Sep 17 00:00:00 2001 From: "yolanda.robla@canonical.com" <> Date: Thu, 27 Mar 2014 13:26:35 +0100 Subject: [PATCH] added unit testing for db --- hooks/cinder_hooks.py | 4 ++- hooks/pgsql-db-relation-broken | 4 ++- hooks/pgsql-db-relation-changed | 4 ++- hooks/pgsql-db-relation-joined | 4 ++- unit_tests/test_cinder_hooks.py | 50 +++++++++++++++++++++++++++++++++ 5 files changed, 62 insertions(+), 4 deletions(-) diff --git a/hooks/cinder_hooks.py b/hooks/cinder_hooks.py index 4889ebaa..b995cbea 100755 --- a/hooks/cinder_hooks.py +++ b/hooks/cinder_hooks.py @@ -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') diff --git a/hooks/pgsql-db-relation-broken b/hooks/pgsql-db-relation-broken index 4889ebaa..b995cbea 100755 --- a/hooks/pgsql-db-relation-broken +++ b/hooks/pgsql-db-relation-broken @@ -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') diff --git a/hooks/pgsql-db-relation-changed b/hooks/pgsql-db-relation-changed index 4889ebaa..b995cbea 100755 --- a/hooks/pgsql-db-relation-changed +++ b/hooks/pgsql-db-relation-changed @@ -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') diff --git a/hooks/pgsql-db-relation-joined b/hooks/pgsql-db-relation-joined index 4889ebaa..b995cbea 100755 --- a/hooks/pgsql-db-relation-joined +++ b/hooks/pgsql-db-relation-joined @@ -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') diff --git a/unit_tests/test_cinder_hooks.py b/unit_tests/test_cinder_hooks.py index 03b81b4d..e7c4afaa 100644 --- a/unit_tests/test_cinder_hooks.py +++ b/unit_tests/test_cinder_hooks.py @@ -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'])