From bd7332ec0da020aa033c698dc50ee9089d6423f0 Mon Sep 17 00:00:00 2001 From: James Page Date: Fri, 8 Nov 2013 09:34:39 +0800 Subject: [PATCH 1/2] Tidy lint and duplicated unit tests --- hooks/glance_contexts.py | 2 - unit_tests/test_glance_contexts.py | 1 + unit_tests/test_glance_relations.py | 131 +++++++++------------------- unit_tests/test_glance_utils.py | 1 + unit_tests/test_utils.py | 3 + 5 files changed, 48 insertions(+), 90 deletions(-) diff --git a/hooks/glance_contexts.py b/hooks/glance_contexts.py index 53405b16..48824a4a 100644 --- a/hooks/glance_contexts.py +++ b/hooks/glance_contexts.py @@ -1,8 +1,6 @@ from charmhelpers.core.hookenv import ( is_relation_made, relation_ids, - related_units, - relation_get, service_name, ) diff --git a/unit_tests/test_glance_contexts.py b/unit_tests/test_glance_contexts.py index d86ae8a3..634a286a 100644 --- a/unit_tests/test_glance_contexts.py +++ b/unit_tests/test_glance_contexts.py @@ -15,6 +15,7 @@ TO_PATCH = [ class TestGlanceContexts(CharmTestCase): + def setUp(self): super(TestGlanceContexts, self).setUp(contexts, TO_PATCH) diff --git a/unit_tests/test_glance_relations.py b/unit_tests/test_glance_relations.py index de42baa2..c7dc0a17 100644 --- a/unit_tests/test_glance_relations.py +++ b/unit_tests/test_glance_relations.py @@ -32,7 +32,7 @@ TO_PATCH = [ 'apt_update', 'restart_on_change', 'service_stop', - #charmhelpers.contrib.openstack.utils + # charmhelpers.contrib.openstack.utils 'configure_installation_source', 'get_os_codename_package', 'openstack_upgrade_available', @@ -55,6 +55,7 @@ TO_PATCH = [ class GlanceRelationTests(CharmTestCase): + def setUp(self): super(GlanceRelationTests, self).setUp(relations, TO_PATCH) self.config.side_effect = self.test_config.get @@ -374,93 +375,6 @@ class GlanceRelationTests(CharmTestCase): 'ha_changed: hacluster subordinate is not fully clustered.' ) - @patch.object(relations, 'keystone_joined') - @patch.object(relations, 'CONFIGS') - def test_configure_https_enable_with_identity_service(self, configs, keystone_joined): - configs.complete_contexts = MagicMock() - configs.complete_contexts.return_value = ['https'] - configs.write = MagicMock() - self.relation_ids.return_value = ['identity-service:0'] - relations.configure_https() - cmd = ['a2ensite', 'openstack_https_frontend'] - self.check_call.assert_called_with(cmd) - keystone_joined.assert_called_with(relation_id='identity-service:0') - - @patch.object(relations, 'keystone_joined') - @patch.object(relations, 'CONFIGS') - def test_configure_https_disable_with_keystone_joined(self, configs, keystone_joined): - configs.complete_contexts = MagicMock() - configs.complete_contexts.return_value = [''] - configs.write = MagicMock() - self.relation_ids.return_value = ['identity-service:0'] - relations.configure_https() - cmd = ['a2dissite', 'openstack_https_frontend'] - self.check_call.assert_called_with(cmd) - keystone_joined.assert_called_with(relation_id='identity-service:0') - - @patch.object(relations, 'image_service_joined') - @patch.object(relations, 'CONFIGS') - def test_configure_https_enable_with_image_service(self, configs, image_service_joined): - configs.complete_contexts = MagicMock() - configs.complete_contexts.return_value = ['https'] - configs.write = MagicMock() - self.relation_ids.return_value = ['image-service:0'] - relations.configure_https() - cmd = ['a2ensite', 'openstack_https_frontend'] - self.check_call.assert_called_with(cmd) - image_service_joined.assert_called_with(relation_id='image-service:0') - - @patch.object(relations, 'image_service_joined') - @patch.object(relations, 'CONFIGS') - def test_configure_https_disable_with_image_service(self, configs, image_service_joined): - configs.complete_contexts = MagicMock() - configs.complete_contexts.return_value = [''] - configs.write = MagicMock() - self.relation_ids.return_value = ['image-service:0'] - relations.configure_https() - cmd = ['a2dissite', 'openstack_https_frontend'] - self.check_call.assert_called_with(cmd) - image_service_joined.assert_called_with(relation_id='image-service:0') - - def test_amqp_joined(self): - relations.amqp_joined() - self.relation_set.assert_called_with(username='glance', vhost='openstack') - - @patch.object(relations, 'CONFIGS') - def test_amqp_changed_missing_relation_data(self, configs): - configs.complete_contexts = MagicMock() - configs.complete_contexts.return_value = [] - relations.amqp_changed() - self.juju_log.assert_called() - - @patch.object(relations, 'CONFIGS') - def test_amqp_changed_relation_data(self, configs): - configs.complete_contexts = MagicMock() - configs.complete_contexts.return_value = ['amqp'] - configs.write = MagicMock() - relations.amqp_changed() - self.assertEquals([call('/etc/glance/glance-api.conf')], - configs.write.call_args_list) - self.assertFalse(self.juju_log.called) - - @patch.object(relations, 'keystone_joined') - def test_ha_relation_changed_not_leader(self, joined): - self.relation_get.return_value = True - self.eligible_leader.return_value = False - relations.ha_relation_changed() - self.assertTrue(self.juju_log.called) - self.assertFalse(joined.called) - - @patch.object(relations, 'image_service_joined') - @patch.object(relations, 'keystone_joined') - def test_ha_relation_changed_leader(self, ks_joined, image_joined): - self.relation_get.return_value = True - self.eligible_leader.return_value = True - self.relation_ids.side_effect = [['identity:0'], ['image:1']] - relations.ha_relation_changed() - ks_joined.assert_called_with('identity:0') - image_joined.assert_called_with('image:1') - @patch.object(relations, 'keystone_joined') @patch.object(relations, 'CONFIGS') def test_configure_https_enable_with_identity_service( @@ -513,6 +427,47 @@ class GlanceRelationTests(CharmTestCase): self.check_call.assert_called_with(cmd) image_service_joined.assert_called_with(relation_id='image-service:0') + def test_amqp_joined(self): + relations.amqp_joined() + self.relation_set.assert_called_with( + username='glance', + vhost='openstack') + + @patch.object(relations, 'CONFIGS') + def test_amqp_changed_missing_relation_data(self, configs): + configs.complete_contexts = MagicMock() + configs.complete_contexts.return_value = [] + relations.amqp_changed() + self.juju_log.assert_called() + + @patch.object(relations, 'CONFIGS') + def test_amqp_changed_relation_data(self, configs): + configs.complete_contexts = MagicMock() + configs.complete_contexts.return_value = ['amqp'] + configs.write = MagicMock() + relations.amqp_changed() + self.assertEquals([call('/etc/glance/glance-api.conf')], + configs.write.call_args_list) + self.assertFalse(self.juju_log.called) + + @patch.object(relations, 'keystone_joined') + def test_ha_relation_changed_not_leader(self, joined): + self.relation_get.return_value = True + self.eligible_leader.return_value = False + relations.ha_relation_changed() + self.assertTrue(self.juju_log.called) + self.assertFalse(joined.called) + + @patch.object(relations, 'image_service_joined') + @patch.object(relations, 'keystone_joined') + def test_ha_relation_changed_leader(self, ks_joined, image_joined): + self.relation_get.return_value = True + self.eligible_leader.return_value = True + self.relation_ids.side_effect = [['identity:0'], ['image:1']] + relations.ha_relation_changed() + ks_joined.assert_called_with('identity:0') + image_joined.assert_called_with('image:1') + @patch.object(relations, 'CONFIGS') def test_relation_broken(self, configs): relations.relation_broken() diff --git a/unit_tests/test_glance_utils.py b/unit_tests/test_glance_utils.py index d32a090a..8fe4ee7f 100644 --- a/unit_tests/test_glance_utils.py +++ b/unit_tests/test_glance_utils.py @@ -26,6 +26,7 @@ TO_PATCH = [ class TestGlanceUtils(CharmTestCase): + def setUp(self): super(TestGlanceUtils, self).setUp(utils, TO_PATCH) self.config.side_effect = self.test_config.get_all diff --git a/unit_tests/test_utils.py b/unit_tests/test_utils.py index e7ff6e22..c9c7bace 100644 --- a/unit_tests/test_utils.py +++ b/unit_tests/test_utils.py @@ -45,6 +45,7 @@ def get_default_config(): class CharmTestCase(unittest.TestCase): + def setUp(self, obj, patches): super(CharmTestCase, self).setUp() self.patches = patches @@ -65,6 +66,7 @@ class CharmTestCase(unittest.TestCase): class TestConfig(object): + def __init__(self): self.config = get_default_config() @@ -86,6 +88,7 @@ class TestConfig(object): class TestRelation(object): + def __init__(self, relation_data={}): self.relation_data = relation_data From 39af9a948d4ac43a5a527010bb2def6f0ffe1390 Mon Sep 17 00:00:00 2001 From: James Page Date: Fri, 8 Nov 2013 09:40:58 +0800 Subject: [PATCH 2/2] Add all target to Makefile, echo annotate lint check --- Makefile | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/Makefile b/Makefile index 1fd305c4..80ea4e03 100644 --- a/Makefile +++ b/Makefile @@ -1,12 +1,18 @@ #!/usr/bin/make lint: + @echo -n "Running flake8 tests: " @flake8 --exclude hooks/charmhelpers hooks @flake8 unit_tests + @echo "OK" + @echo -n "Running charm proof: " @charm proof + @echo "OK" sync: @charm-helper-sync -c charm-helpers.yaml test: @$(PYTHON) /usr/bin/nosetests --nologcapture --with-coverage unit_tests + +all: test lint