From f9048484bde2bd4193934aa9766c89b963788e31 Mon Sep 17 00:00:00 2001 From: Bradley Jones Date: Wed, 8 Apr 2015 12:57:38 +0100 Subject: [PATCH] Refactor project overview tests Use decorators for creating stubs and remove duplicated code Change-Id: I16764ae417f5cf544dd6d6521707591d52fb5172 Closes-bug: 1441591 --- .../dashboards/project/overview/tests.py | 232 +++++------------- 1 file changed, 67 insertions(+), 165 deletions(-) diff --git a/openstack_dashboard/dashboards/project/overview/tests.py b/openstack_dashboard/dashboards/project/overview/tests.py index d8db373219..785041136d 100644 --- a/openstack_dashboard/dashboards/project/overview/tests.py +++ b/openstack_dashboard/dashboards/project/overview/tests.py @@ -34,25 +34,39 @@ INDEX_URL = reverse('horizon:project:overview:index') class UsageViewTests(test.TestCase): - def _stub_nova_api_calls(self, nova_stu_enabled=True): - self.mox.StubOutWithMock(api.nova, 'usage_get') - self.mox.StubOutWithMock(api.nova, 'tenant_absolute_limits') - self.mox.StubOutWithMock(api.nova, 'extension_supported') + @test.create_stubs({api.nova: ('usage_get', + 'tenant_absolute_limits', + 'extension_supported')}) + def _stub_nova_api_calls(self, nova_stu_enabled=True, + tenant_limits_exception=False, + stu_exception=False): + api.nova.extension_supported( + 'SimpleTenantUsage', IsA(http.HttpRequest)) \ + .AndReturn(nova_stu_enabled) api.nova.extension_supported( 'SimpleTenantUsage', IsA(http.HttpRequest)) \ .AndReturn(nova_stu_enabled) + if tenant_limits_exception: + api.nova.tenant_absolute_limits(IsA(http.HttpRequest))\ + .AndRaise(tenant_limits_exception) + else: + api.nova.tenant_absolute_limits(IsA(http.HttpRequest)) \ + .AndReturn(self.limits['absolute']) + + if nova_stu_enabled: + self._nova_stu_enabled(stu_exception) + + @test.create_stubs({api.cinder: ('tenant_absolute_limits',)}) def _stub_cinder_api_calls(self): - self.mox.StubOutWithMock(api.cinder, 'tenant_absolute_limits') api.cinder.tenant_absolute_limits(IsA(http.HttpRequest)) \ .AndReturn(self.cinder_limits['absolute']) + @test.create_stubs({api.neutron: ('is_extension_supported',), + api.network: ('floating_ip_supported', + 'tenant_floating_ip_list', + 'security_group_list')}) def _stub_neutron_api_calls(self, neutron_sg_enabled=True): - self.mox.StubOutWithMock(api.neutron, 'is_extension_supported') - self.mox.StubOutWithMock(api.network, 'floating_ip_supported') - self.mox.StubOutWithMock(api.network, 'tenant_floating_ip_list') - if neutron_sg_enabled: - self.mox.StubOutWithMock(api.network, 'security_group_list') api.neutron.is_extension_supported( IsA(http.HttpRequest), 'security-group').AndReturn(neutron_sg_enabled) @@ -64,6 +78,35 @@ class UsageViewTests(test.TestCase): api.network.security_group_list(IsA(http.HttpRequest)) \ .AndReturn(self.q_secgroups.list()) + def _nova_stu_enabled(self, exception=False): + now = timezone.now() + start = datetime.datetime(now.year, now.month, 1, 0, 0, 0, 0) + end = datetime.datetime(now.year, now.month, now.day, 23, 59, 59, 0) + + if exception: + api.nova.usage_get(IsA(http.HttpRequest), self.tenant.id, + start, end) \ + .AndRaise(exception) + else: + api.nova.usage_get(IsA(http.HttpRequest), self.tenant.id, + start, end) \ + .AndReturn(api.nova.NovaUsage(self.usages.first())) + + def _common_assertions(self, nova_stu_enabled, + maxTotalFloatingIps=float("inf")): + res = self.client.get(reverse('horizon:project:overview:index')) + usages = res.context['usage'] + self.assertTemplateUsed(res, 'project/overview/usage.html') + self.assertTrue(isinstance(usages, usage.ProjectUsage)) + self.assertEqual(nova_stu_enabled, + res.context['simple_tenant_usage_enabled']) + if nova_stu_enabled: + self.assertContains(res, 'form-inline') + else: + self.assertNotContains(res, 'form-inline') + self.assertEqual(usages.limits['maxTotalFloatingIps'], + maxTotalFloatingIps) + def test_usage(self): self._test_usage(nova_stu_enabled=True) @@ -71,40 +114,12 @@ class UsageViewTests(test.TestCase): self._test_usage(nova_stu_enabled=False) def _test_usage(self, nova_stu_enabled): - now = timezone.now() - usage_obj = api.nova.NovaUsage(self.usages.first()) self._stub_nova_api_calls(nova_stu_enabled) - api.nova.extension_supported( - 'SimpleTenantUsage', IsA(http.HttpRequest)) \ - .AndReturn(nova_stu_enabled) - - if nova_stu_enabled: - api.nova.usage_get(IsA(http.HttpRequest), self.tenant.id, - datetime.datetime(now.year, - now.month, - 1, 0, 0, 0, 0), - datetime.datetime(now.year, - now.month, - now.day, 23, 59, 59, 0)) \ - .AndReturn(usage_obj) - - api.nova.tenant_absolute_limits(IsA(http.HttpRequest)) \ - .AndReturn(self.limits['absolute']) self._stub_neutron_api_calls() self._stub_cinder_api_calls() self.mox.ReplayAll() - res = self.client.get(reverse('horizon:project:overview:index')) - usages = res.context['usage'] - self.assertTemplateUsed(res, 'project/overview/usage.html') - self.assertTrue(isinstance(usages, usage.ProjectUsage)) - self.assertEqual(nova_stu_enabled, - res.context['simple_tenant_usage_enabled']) - if nova_stu_enabled: - self.assertContains(res, 'form-inline') - else: - self.assertNotContains(res, 'form-inline') - self.assertEqual(usages.limits['maxTotalFloatingIps'], float("inf")) + self._common_assertions(nova_stu_enabled) def test_usage_nova_network(self): self._test_usage_nova_network(nova_stu_enabled=True) @@ -112,25 +127,10 @@ class UsageViewTests(test.TestCase): def test_usage_nova_network_disabled(self): self._test_usage_nova_network(nova_stu_enabled=False) + @test.create_stubs({api.base: ('is_service_enabled',)}) def _test_usage_nova_network(self, nova_stu_enabled): - now = timezone.now() - usage_obj = api.nova.NovaUsage(self.usages.first()) - self.mox.StubOutWithMock(api.base, 'is_service_enabled') self._stub_nova_api_calls(nova_stu_enabled) - api.nova.extension_supported( - 'SimpleTenantUsage', IsA(http.HttpRequest)) \ - .AndReturn(nova_stu_enabled) - if nova_stu_enabled: - api.nova.usage_get(IsA(http.HttpRequest), self.tenant.id, - datetime.datetime(now.year, - now.month, - 1, 0, 0, 0, 0), - datetime.datetime(now.year, - now.month, - now.day, 23, 59, 59, 0)) \ - .AndReturn(usage_obj) - api.nova.tenant_absolute_limits(IsA(http.HttpRequest)) \ - .AndReturn(self.limits['absolute']) + api.base.is_service_enabled(IsA(http.HttpRequest), 'network') \ .MultipleTimes().AndReturn(False) api.base.is_service_enabled(IsA(http.HttpRequest), 'volume') \ @@ -138,35 +138,11 @@ class UsageViewTests(test.TestCase): self.mox.ReplayAll() - res = self.client.get(reverse('horizon:project:overview:index')) - usages = res.context['usage'] - self.assertTemplateUsed(res, 'project/overview/usage.html') - self.assertTrue(isinstance(usages, usage.ProjectUsage)) - self.assertEqual(nova_stu_enabled, - res.context['simple_tenant_usage_enabled']) - if nova_stu_enabled: - self.assertContains(res, 'form-inline') - else: - self.assertNotContains(res, 'form-inline') - self.assertEqual(usages.limits['maxTotalFloatingIps'], 10) + self._common_assertions(nova_stu_enabled, maxTotalFloatingIps=10) def test_unauthorized(self): - exc = self.exceptions.nova_unauthorized - now = timezone.now() - self._stub_nova_api_calls() - api.nova.extension_supported( - 'SimpleTenantUsage', IsA(http.HttpRequest)) \ - .AndReturn(True) - api.nova.usage_get(IsA(http.HttpRequest), self.tenant.id, - datetime.datetime(now.year, - now.month, - 1, 0, 0, 0, 0), - datetime.datetime(now.year, - now.month, - now.day, 23, 59, 59, 0)) \ - .AndRaise(exc) - api.nova.tenant_absolute_limits(IsA(http.HttpRequest))\ - .AndReturn(self.limits['absolute']) + self._stub_nova_api_calls( + stu_exception=self.exceptions.nova_unauthorized) self._stub_neutron_api_calls() self._stub_cinder_api_calls() self.mox.ReplayAll() @@ -184,21 +160,7 @@ class UsageViewTests(test.TestCase): self._test_usage_csv(nova_stu_enabled=False) def _test_usage_csv(self, nova_stu_enabled=True): - now = timezone.now() - usage_obj = api.nova.NovaUsage(self.usages.first()) self._stub_nova_api_calls(nova_stu_enabled) - api.nova.extension_supported( - 'SimpleTenantUsage', IsA(http.HttpRequest)) \ - .AndReturn(nova_stu_enabled) - start = datetime.datetime(now.year, now.month, 1, 0, 0, 0, 0) - end = datetime.datetime(now.year, now.month, now.day, 23, 59, 59, 0) - - if nova_stu_enabled: - api.nova.usage_get(IsA(http.HttpRequest), - self.tenant.id, - start, end).AndReturn(usage_obj) - api.nova.tenant_absolute_limits(IsA(http.HttpRequest))\ - .AndReturn(self.limits['absolute']) self._stub_neutron_api_calls() self._stub_cinder_api_calls() self.mox.ReplayAll() @@ -208,18 +170,7 @@ class UsageViewTests(test.TestCase): self.assertTrue(isinstance(res.context['usage'], usage.ProjectUsage)) def test_usage_exception_usage(self): - now = timezone.now() - start = datetime.datetime(now.year, now.month, 1, 0, 0, 0, 0) - end = datetime.datetime(now.year, now.month, now.day, 23, 59, 59, 0) - self._stub_nova_api_calls() - api.nova.extension_supported( - 'SimpleTenantUsage', IsA(http.HttpRequest)) \ - .AndReturn(True) - api.nova.usage_get(IsA(http.HttpRequest), - self.tenant.id, - start, end).AndRaise(self.exceptions.nova) - api.nova.tenant_absolute_limits(IsA(http.HttpRequest))\ - .AndReturn(self.limits['absolute']) + self._stub_nova_api_calls(stu_exception=self.exceptions.nova) self._stub_neutron_api_calls() self._stub_cinder_api_calls() self.mox.ReplayAll() @@ -229,19 +180,7 @@ class UsageViewTests(test.TestCase): self.assertEqual(res.context['usage'].usage_list, []) def test_usage_exception_quota(self): - now = timezone.now() - usage_obj = api.nova.NovaUsage(self.usages.first()) - self._stub_nova_api_calls() - api.nova.extension_supported( - 'SimpleTenantUsage', IsA(http.HttpRequest)) \ - .AndReturn(True) - start = datetime.datetime(now.year, now.month, 1, 0, 0, 0, 0) - end = datetime.datetime(now.year, now.month, now.day, 23, 59, 59, 0) - api.nova.usage_get(IsA(http.HttpRequest), - self.tenant.id, - start, end).AndReturn(usage_obj) - api.nova.tenant_absolute_limits(IsA(http.HttpRequest))\ - .AndRaise(self.exceptions.nova) + self._stub_nova_api_calls(tenant_limits_exception=self.exceptions.nova) self._stub_neutron_api_calls() self._stub_cinder_api_calls() self.mox.ReplayAll() @@ -251,19 +190,7 @@ class UsageViewTests(test.TestCase): self.assertEqual(res.context['usage'].quotas, {}) def test_usage_default_tenant(self): - now = timezone.now() - usage_obj = api.nova.NovaUsage(self.usages.first()) self._stub_nova_api_calls() - api.nova.extension_supported( - 'SimpleTenantUsage', IsA(http.HttpRequest)) \ - .AndReturn(True) - start = datetime.datetime(now.year, now.month, 1, 0, 0, 0, 0) - end = datetime.datetime(now.year, now.month, now.day, 23, 59, 59, 0) - api.nova.usage_get(IsA(http.HttpRequest), - self.tenant.id, - start, end).AndReturn(usage_obj) - api.nova.tenant_absolute_limits(IsA(http.HttpRequest))\ - .AndReturn(self.limits['absolute']) self._stub_neutron_api_calls() self._stub_cinder_api_calls() self.mox.ReplayAll() @@ -284,26 +211,14 @@ class UsageViewTests(test.TestCase): def test_usage_with_neutron_floating_ip_disabled(self): self._test_usage_with_neutron(neutron_fip_enabled=False) + @test.create_stubs({api.neutron: ('tenant_quota_get', + 'is_extension_supported'), + api.network: ('floating_ip_supported', + 'tenant_floating_ip_list', + 'security_group_list')}) def _test_usage_with_neutron_prepare(self): - now = timezone.now() - usage_obj = api.nova.NovaUsage(self.usages.first()) self._stub_nova_api_calls() self._stub_cinder_api_calls() - api.nova.extension_supported( - 'SimpleTenantUsage', IsA(http.HttpRequest)) \ - .AndReturn(True) - self.mox.StubOutWithMock(api.neutron, 'tenant_quota_get') - self.mox.StubOutWithMock(api.neutron, 'is_extension_supported') - self.mox.StubOutWithMock(api.network, 'floating_ip_supported') - self.mox.StubOutWithMock(api.network, 'tenant_floating_ip_list') - self.mox.StubOutWithMock(api.network, 'security_group_list') - start = datetime.datetime(now.year, now.month, 1, 0, 0, 0, 0) - end = datetime.datetime(now.year, now.month, now.day, 23, 59, 59, 0) - api.nova.usage_get(IsA(http.HttpRequest), - self.tenant.id, - start, end).AndReturn(usage_obj) - api.nova.tenant_absolute_limits(IsA(http.HttpRequest))\ - .AndReturn(self.limits['absolute']) def _test_usage_with_neutron(self, neutron_sg_enabled=True, neutron_fip_enabled=True): @@ -370,22 +285,9 @@ class UsageViewTests(test.TestCase): def test_usage_without_cinder(self): self._test_usage_cinder(cinder_enabled=False) + @test.create_stubs({api.base: ('is_service_enabled',)}) def _test_usage_cinder(self, cinder_enabled): - now = timezone.now() - usage_obj = api.nova.NovaUsage(self.usages.first()) - self.mox.StubOutWithMock(api.base, 'is_service_enabled') self._stub_nova_api_calls(True) - api.nova.extension_supported( - 'SimpleTenantUsage', IsA(http.HttpRequest)) \ - .AndReturn(True) - - start = datetime.datetime(now.year, now.month, 1, 0, 0, 0, 0) - end = datetime.datetime(now.year, now.month, now.day, 23, 59, 59, 0) - api.nova.usage_get(IsA(http.HttpRequest), - self.tenant.id, - start, end).AndReturn(usage_obj) - api.nova.tenant_absolute_limits(IsA(http.HttpRequest)) \ - .AndReturn(self.limits['absolute']) if cinder_enabled: self._stub_cinder_api_calls()