From b89c7051649a72df898ff0f5396e1f32303da8ed Mon Sep 17 00:00:00 2001 From: Akihiro Motoki Date: Mon, 26 Oct 2020 16:53:17 +0900 Subject: [PATCH] Refactor quota related unit tests openstack_dashboard/dashboards/project/floating_ips/tests.py: The floating IP panel implementation calls openstack_dashboard.usage.quotas.tenant_quota_usages, but its unit test mocks functions called inside tenant_quota_usages. tenant_quota_usages itself should be mocked. openstack_dashboard/test/unit/usage/test_quotas.py: Mocked methods were not verified test_tenant_quota_usages_non_legacy. Verifications for called methods are added. Mocking floating_ip_supported is dropped as it is not called and there is no need to mock it. Change-Id: Iad66c882738fd90eb6dd6de3df96459c10a29aa2 --- .../dashboards/project/floating_ips/tests.py | 32 ++++------------ .../test/unit/usage/test_quotas.py | 37 ++++++++++++++++++- 2 files changed, 43 insertions(+), 26 deletions(-) diff --git a/openstack_dashboard/dashboards/project/floating_ips/tests.py b/openstack_dashboard/dashboards/project/floating_ips/tests.py index d900b5c13b..0bedcd9507 100644 --- a/openstack_dashboard/dashboards/project/floating_ips/tests.py +++ b/openstack_dashboard/dashboards/project/floating_ips/tests.py @@ -351,19 +351,13 @@ class FloatingIpViewTests(test.TestCase): ) @test.create_mocks({api.neutron: ('floating_ip_pools_list', - 'tenant_floating_ip_list', - 'is_extension_supported', - 'is_router_enabled', - 'tenant_quota_get'), - api.base: ('is_service_enabled',)}) + 'is_extension_supported'), + quotas: ('tenant_quota_usages',)}) @test.update_settings(OPENSTACK_NEUTRON_NETWORK={'enable_quotas': True}) def test_correct_quotas_displayed(self): - self.mock_is_service_enabled.return_value = True self.mock_is_extension_supported.side_effect = [False, True, False] - self.mock_is_router_enabled.return_value = True - self.mock_tenant_quota_get.return_value = self.neutron_quotas.first() - self.mock_tenant_floating_ip_list.return_value = \ - self.floating_ips.list() + self.mock_tenant_quota_usages.return_value = \ + self.neutron_quota_usages.first() self.mock_floating_ip_pools_list.return_value = self.pools.list() url = reverse('%s:allocate' % NAMESPACE) @@ -371,19 +365,9 @@ class FloatingIpViewTests(test.TestCase): self.assertEqual(res.context['usages']['floatingip']['quota'], self.neutron_quotas.first().get('floatingip').limit) - self.mock_is_service_enabled.assert_called_once_with( - test.IsHttpRequest(), 'network') - self.assertEqual(3, self.mock_is_extension_supported.call_count) - self.mock_is_extension_supported.assert_has_calls([ - mock.call(test.IsHttpRequest(), 'dns-integration'), - mock.call(test.IsHttpRequest(), 'quotas'), - mock.call(test.IsHttpRequest(), 'quota_details'), - ]) - self.mock_is_router_enabled.assert_called_once_with( - test.IsHttpRequest()) - self.mock_tenant_quota_get.assert_called_once_with( - test.IsHttpRequest(), self.tenant.id) - self.mock_tenant_floating_ip_list.assert_called_once_with( - test.IsHttpRequest()) + self.mock_is_extension_supported.assert_called_once_with( + test.IsHttpRequest(), 'dns-integration') + self.mock_tenant_quota_usages.assert_called_once_with( + test.IsHttpRequest(), targets=('floatingip',)) self.mock_floating_ip_pools_list.assert_called_once_with( test.IsHttpRequest()) diff --git a/openstack_dashboard/test/unit/usage/test_quotas.py b/openstack_dashboard/test/unit/usage/test_quotas.py index 382bf0841f..d663525ec1 100644 --- a/openstack_dashboard/test/unit/usage/test_quotas.py +++ b/openstack_dashboard/test/unit/usage/test_quotas.py @@ -461,13 +461,13 @@ class QuotaTests(test.APITestCase): # quotas._get_tenant_network_usages) @test.create_mocks({api.base: ('is_service_enabled',), cinder: ('is_volume_service_enabled',), - api.neutron: ('floating_ip_supported', - 'is_extension_supported', + api.neutron: ('is_extension_supported', 'is_quotas_extension_supported', 'tenant_quota_detail_get')}) def test_tenant_quota_usages_non_legacy(self): self._mock_service_enabled(network_enabled=True) self.mock_is_extension_supported.return_value = True + self.mock_is_quotas_extension_supported.return_value = True test_data = [ ("network", self.networks.list(), 10), @@ -510,6 +510,39 @@ class QuotaTests(test.APITestCase): self.assertAvailableQuotasEqual(expected, quota_usages.usages, msg=msg) + self.assert_mock_multiple_calls_with_same_arguments( + self.mock_is_service_enabled, len(test_data), + mock.call(test.IsHttpRequest(), 'network')) + # NOTE: is_volume_service_enabled() needs to be mocked + # as _mock_service_enabled() requires it, but it is never called here. + self.mock_is_volume_service_enabled.assert_not_called() + self.mock_is_extension_supported.assert_has_calls([ + # network + mock.call(test.IsHttpRequest(), 'quota_details'), + # subnet + mock.call(test.IsHttpRequest(), 'quota_details'), + # port + mock.call(test.IsHttpRequest(), 'quota_details'), + # router + mock.call(test.IsHttpRequest(), 'router'), + mock.call(test.IsHttpRequest(), 'quota_details'), + # floating IP + mock.call(test.IsHttpRequest(), 'quota_details'), + # security group + mock.call(test.IsHttpRequest(), 'security-group'), + mock.call(test.IsHttpRequest(), 'quota_details'), + # security group rule + mock.call(test.IsHttpRequest(), 'security-group'), + mock.call(test.IsHttpRequest(), 'quota_details'), + ]) + self.assertEqual(10, self.mock_is_extension_supported.call_count) + self.assert_mock_multiple_calls_with_same_arguments( + self.mock_is_quotas_extension_supported, len(test_data), + mock.call(test.IsHttpRequest())) + self.assert_mock_multiple_calls_with_same_arguments( + self.mock_tenant_quota_detail_get, len(test_data), + mock.call(test.IsHttpRequest(), self.request.user.tenant_id)) + @test.create_mocks({api.base: ('is_service_enabled',), cinder: ('is_volume_service_enabled',), api.neutron: ('floating_ip_supported',