From 6127b44d0ae4449bc2bf744e8092bb612b5559cb Mon Sep 17 00:00:00 2001 From: Stephen Finucane Date: Thu, 11 Jul 2024 11:43:56 +0100 Subject: [PATCH] quota: Migrate 'quota *' to SDK This is done for both the compute and block storage services. The network service was already using SDK. Change-Id: Id7a6943b8b4d7d1330e7d7f13705f027d1b67189 Signed-off-by: Stephen Finucane Depends-on: https://review.opendev.org/c/openstack/openstacksdk/+/923864 --- openstackclient/common/quota.py | 213 ++-- .../tests/unit/common/test_quota.py | 913 +++++++++--------- .../tests/unit/compute/v2/fakes.py | 134 --- .../tests/unit/network/v2/fakes.py | 92 -- openstackclient/tests/unit/volume/v2/fakes.py | 88 -- openstackclient/tests/unit/volume/v3/fakes.py | 4 - requirements.txt | 2 +- 7 files changed, 559 insertions(+), 887 deletions(-) diff --git a/openstackclient/common/quota.py b/openstackclient/common/quota.py index 5591069b98..c408e97bac 100644 --- a/openstackclient/common/quota.py +++ b/openstackclient/common/quota.py @@ -19,6 +19,7 @@ import itertools import logging import sys +from openstack import exceptions as sdk_exceptions from osc_lib.command import command from osc_lib import exceptions from osc_lib import utils @@ -104,11 +105,8 @@ def _xform_get_quota(data, value, keys): def get_project(app, project): if project is not None: - identity_client = app.client_manager.identity - project = utils.find_resource( - identity_client.projects, - project, - ) + identity_client = app.client_manager.sdk_connection.identity + project = identity_client.find_project(project, ignore_missing=False) project_id = project.id project_name = project.name elif app.client_manager.auth_ref: @@ -134,16 +132,18 @@ def get_compute_quotas( default=False, ): try: - client = app.client_manager.compute + client = app.client_manager.sdk_connection.compute if default: - quota = client.quotas.defaults(project_id) + quota = client.get_quota_set_defaults(project_id) else: - quota = client.quotas.get(project_id, detail=detail) - except Exception as e: - if type(e).__name__ == 'EndpointNotFound': - return {} - raise - return quota._info + quota = client.get_quota_set(project_id, usage=detail) + except sdk_exceptions.EndpointNotFound: + return {} + data = quota.to_dict() + if not detail: + del data['usage'] + del data['reservation'] + return data def get_volume_quotas( @@ -154,17 +154,18 @@ def get_volume_quotas( default=False, ): try: - client = app.client_manager.volume + client = app.client_manager.sdk_connection.volume if default: - quota = client.quotas.defaults(project_id) + quota = client.get_quota_set_defaults(project_id) else: - quota = client.quotas.get(project_id, usage=detail) - except Exception as e: - if type(e).__name__ == 'EndpointNotFound': - return {} - else: - raise - return quota._info + quota = client.get_quota_set(project_id, usage=detail) + except sdk_exceptions.EndpointNotFound: + return {} + data = quota.to_dict() + if not detail: + del data['usage'] + del data['reservation'] + return data def get_network_quotas( @@ -242,37 +243,35 @@ class ListQuota(command.Lister): return parser def _list_quota_compute(self, parsed_args, project_ids): - compute_client = self.app.client_manager.compute + compute_client = self.app.client_manager.sdk_connection.compute result = [] - for p in project_ids: + for project_id in project_ids: try: - data = compute_client.quotas.get(p) - except Exception as ex: - if ( - type(ex).__name__ == 'NotFound' - or ex.http_status >= 400 - and ex.http_status <= 499 - ): - # Project not found, move on to next one - LOG.warning(f"Project {p} not found: {ex}") - continue - else: - raise + project_data = compute_client.get_quota_set(project_id) + except ( + sdk_exceptions.NotFoundException, + sdk_exceptions.ForbiddenException, + ) as exc: + # Project not found, move on to next one + LOG.warning(f"Project {project_id} not found: {exc}") + continue - result_data = _xform_get_quota( - data, - p, + project_result = _xform_get_quota( + project_data, + project_id, COMPUTE_QUOTAS.keys(), ) - default_data = compute_client.quotas.defaults(p) - result_default = _xform_get_quota( + + default_data = compute_client.get_quota_set_defaults(project_id) + default_result = _xform_get_quota( default_data, - p, + project_id, COMPUTE_QUOTAS.keys(), ) - if result_default != result_data: - result += result_data + + if default_result != project_result: + result += project_result columns = ( 'id', @@ -306,33 +305,35 @@ class ListQuota(command.Lister): ) def _list_quota_volume(self, parsed_args, project_ids): - volume_client = self.app.client_manager.volume + volume_client = self.app.client_manager.sdk_connection.volume result = [] - for p in project_ids: + for project_id in project_ids: try: - data = volume_client.quotas.get(p) - except Exception as ex: - if type(ex).__name__ == 'NotFound': - # Project not found, move on to next one - LOG.warning(f"Project {p} not found: {ex}") - continue - else: - raise + project_data = volume_client.get_quota_set(project_id) + except ( + sdk_exceptions.NotFoundException, + sdk_exceptions.ForbiddenException, + ) as exc: + # Project not found, move on to next one + LOG.warning(f"Project {project_id} not found: {exc}") + continue - result_data = _xform_get_quota( - data, - p, + project_result = _xform_get_quota( + project_data, + project_id, VOLUME_QUOTAS.keys(), ) - default_data = volume_client.quotas.defaults(p) - result_default = _xform_get_quota( + + default_data = volume_client.get_quota_set_defaults(project_id) + default_result = _xform_get_quota( default_data, - p, + project_id, VOLUME_QUOTAS.keys(), ) - if result_default != result_data: - result += result_data + + if default_result != project_result: + result += project_result columns = ( 'id', @@ -359,33 +360,35 @@ class ListQuota(command.Lister): ) def _list_quota_network(self, parsed_args, project_ids): - client = self.app.client_manager.network + network_client = self.app.client_manager.network result = [] - for p in project_ids: + for project_id in project_ids: try: - data = client.get_quota(p) - except Exception as ex: - if type(ex).__name__ == 'NotFound': - # Project not found, move on to next one - LOG.warning(f"Project {p} not found: {ex}") - continue - else: - raise + project_data = network_client.get_quota(project_id) + except ( + sdk_exceptions.NotFoundException, + sdk_exceptions.ForbiddenException, + ) as exc: + # Project not found, move on to next one + LOG.warning(f"Project {project_id} not found: {exc}") + continue - result_data = _xform_get_quota( - data, - p, + project_result = _xform_get_quota( + project_data, + project_id, NETWORK_KEYS, ) - default_data = client.get_quota_default(p) - result_default = _xform_get_quota( + + default_data = network_client.get_quota_default(project_id) + default_result = _xform_get_quota( default_data, - p, + project_id, NETWORK_KEYS, ) - if result_default != result_data: - result += result_data + + if default_result != project_result: + result += project_result columns = ( 'id', @@ -419,7 +422,8 @@ class ListQuota(command.Lister): def take_action(self, parsed_args): project_ids = [ - p.id for p in self.app.client_manager.identity.projects.list() + p.id + for p in self.app.client_manager.sdk_connection.identity.projects() ] if parsed_args.compute: return self._list_quota_compute(parsed_args, project_ids) @@ -561,8 +565,8 @@ class SetQuota(common.NetDetectionMixin, command.Command): msg = _('--force cannot be used with --class or --default') raise exceptions.CommandError(msg) - compute_client = self.app.client_manager.compute - volume_client = self.app.client_manager.volume + compute_client = self.app.client_manager.sdk_connection.compute + volume_client = self.app.client_manager.sdk_connection.volume compute_kwargs = {} for k, v in COMPUTE_QUOTAS.items(): @@ -570,7 +574,7 @@ class SetQuota(common.NetDetectionMixin, command.Command): if value is not None: compute_kwargs[k] = value - if parsed_args.force is True: + if compute_kwargs and parsed_args.force is True: compute_kwargs['force'] = parsed_args.force volume_kwargs = {} @@ -604,12 +608,12 @@ class SetQuota(common.NetDetectionMixin, command.Command): if parsed_args.quota_class or parsed_args.default: if compute_kwargs: - compute_client.quota_classes.update( + compute_client.update_quota_class_set( parsed_args.project or 'default', **compute_kwargs, ) if volume_kwargs: - volume_client.quota_classes.update( + volume_client.update_quota_class_set( parsed_args.project or 'default', **volume_kwargs, ) @@ -625,9 +629,9 @@ class SetQuota(common.NetDetectionMixin, command.Command): project = project_info['id'] if compute_kwargs: - compute_client.quotas.update(project, **compute_kwargs) + compute_client.update_quota_set(project, **compute_kwargs) if volume_kwargs: - volume_client.quotas.update(project, **volume_kwargs) + volume_client.update_quota_set(project, **volume_kwargs) if ( network_kwargs and self.app.client_manager.is_network_endpoint_enabled() @@ -765,14 +769,24 @@ and ``server-group-members`` output for a given quota class.""" if 'id' in info: del info['id'] - # Remove the 'location' field for resources from openstacksdk - if 'location' in info: - del info['location'] + # Remove the sdk-derived fields + for field in ('location', 'name', 'force'): + if field in info: + del info[field] if not parsed_args.usage: result = [{'resource': k, 'limit': v} for k, v in info.items()] else: - result = [{'resource': k, **v} for k, v in info.items()] + result = [ + { + 'resource': k, + 'limit': v or 0, + 'in_use': info['usage'].get(k, 0), + 'reserved': info['reservation'].get(k, 0), + } + for k, v in info.items() + if k not in ('usage', 'reservation') + ] columns = ( 'resource', @@ -850,21 +864,20 @@ class DeleteQuota(command.Command): return parser def take_action(self, parsed_args): - identity_client = self.app.client_manager.identity - project = utils.find_resource( - identity_client.projects, - parsed_args.project, + identity_client = self.app.client_manager.sdk_connection.identity + project = identity_client.find_project( + parsed_args.project, ignore_missing=False ) # compute quotas if parsed_args.service in {'all', 'compute'}: - compute_client = self.app.client_manager.compute - compute_client.quotas.delete(project.id) + compute_client = self.app.client_manager.sdk_connection.compute + compute_client.revert_quota_set(project.id) # volume quotas if parsed_args.service in {'all', 'volume'}: - volume_client = self.app.client_manager.volume - volume_client.quotas.delete(project.id) + volume_client = self.app.client_manager.sdk_connection.volume + volume_client.revert_quota_set(project.id) # network quotas (but only if we're not using nova-network, otherwise # we already deleted the quotas in the compute step) diff --git a/openstackclient/tests/unit/common/test_quota.py b/openstackclient/tests/unit/common/test_quota.py index be85d27b2a..e18161d4e4 100644 --- a/openstackclient/tests/unit/common/test_quota.py +++ b/openstackclient/tests/unit/common/test_quota.py @@ -10,61 +10,38 @@ # License for the specific language governing permissions and limitations # under the License. -import copy from unittest import mock -from osc_lib import exceptions +from openstack.block_storage.v3 import quota_set as _volume_quota_set +from openstack.compute.v2 import quota_set as _compute_quota_set +from openstack.identity.v3 import project as _project +from openstack.network.v2 import quota as _network_quota_set +from openstack.test import fakes as sdk_fakes +from openstack import exceptions as sdk_exceptions from openstackclient.common import quota from openstackclient.tests.unit.compute.v2 import fakes as compute_fakes -from openstackclient.tests.unit import fakes -from openstackclient.tests.unit.identity.v2_0 import fakes as identity_fakes -from openstackclient.tests.unit.identity.v3 import fakes as identity_fakes_v3 +from openstackclient.tests.unit.identity.v3 import fakes as identity_fakes from openstackclient.tests.unit.network.v2 import fakes as network_fakes -from openstackclient.tests.unit.volume.v2 import fakes as volume_fakes +from openstackclient.tests.unit import utils +from openstackclient.tests.unit.volume.v3 import fakes as volume_fakes -class FakeQuotaResource(fakes.FakeResource): - _keys = {'property': 'value'} - - def set_keys(self, args): - self._keys.update(args) - - def unset_keys(self, keys): - for key in keys: - self._keys.pop(key, None) - - def get_keys(self): - return self._keys - - -class TestQuota(compute_fakes.TestComputev2): +class TestQuota( + identity_fakes.FakeClientMixin, + compute_fakes.FakeClientMixin, + network_fakes.FakeClientMixin, + volume_fakes.FakeClientMixin, + utils.TestCommand, +): def setUp(self): super().setUp() - # Set up common projects - self.projects = identity_fakes_v3.FakeProject.create_projects(count=2) - self.projects_mock = self.identity_client.projects - self.projects_mock.reset_mock() - self.projects_mock.get.return_value = self.projects[0] - - self.compute_quotas_mock = self.compute_client.quotas - self.compute_quotas_mock.reset_mock() - self.compute_quotas_class_mock = self.compute_client.quota_classes - self.compute_quotas_class_mock.reset_mock() - - self.volume_quotas_mock = self.volume_client.quotas - self.volume_quotas_mock.reset_mock() - self.volume_quotas_class_mock = self.volume_client.quota_classes - self.volume_quotas_class_mock.reset_mock() - - self.app.client_manager.auth_ref = mock.Mock() - self.app.client_manager.auth_ref.service_catalog = mock.Mock() - self.service_catalog_mock = ( - self.app.client_manager.auth_ref.service_catalog + self.projects = list( + sdk_fakes.generate_fake_resources(_project.Project, count=2) ) - self.service_catalog_mock.reset_mock() - self.app.client_manager.auth_ref.project_id = identity_fakes.project_id + self.app.client_manager.auth_ref = mock.Mock() + self.app.client_manager.auth_ref.project_id = self.projects[1].id class TestQuotaList(TestQuota): @@ -110,22 +87,20 @@ class TestQuotaList(TestQuota): def setUp(self): super().setUp() - # Work with multiple projects in this class - self.projects_mock.get.side_effect = self.projects - self.projects_mock.list.return_value = self.projects + self.identity_sdk_client.get_project.side_effect = self.projects[0] + self.identity_sdk_client.projects.return_value = self.projects self.compute_quotas = [ - compute_fakes.create_one_comp_quota(), - compute_fakes.create_one_comp_quota(), + sdk_fakes.generate_fake_resource(_compute_quota_set.QuotaSet), + sdk_fakes.generate_fake_resource(_compute_quota_set.QuotaSet), ] - self.compute_default_quotas = [ - compute_fakes.create_one_default_comp_quota(), - compute_fakes.create_one_default_comp_quota(), - ] - self.compute_client.quotas.defaults = mock.Mock( - side_effect=self.compute_default_quotas, + self.default_compute_quotas = sdk_fakes.generate_fake_resource( + _compute_quota_set.QuotaSet + ) + # the defaults are global hence use of return_value here + self.compute_sdk_client.get_quota_set_defaults.return_value = ( + self.default_compute_quotas ) - self.compute_reference_data = ( self.projects[0].id, self.compute_quotas[0].cores, @@ -141,17 +116,16 @@ class TestQuotaList(TestQuota): ) self.network_quotas = [ - network_fakes.FakeQuota.create_one_net_quota(), - network_fakes.FakeQuota.create_one_net_quota(), + sdk_fakes.generate_fake_resource(_network_quota_set.Quota), + sdk_fakes.generate_fake_resource(_network_quota_set.Quota), ] - self.network_default_quotas = [ - network_fakes.FakeQuota.create_one_default_net_quota(), - network_fakes.FakeQuota.create_one_default_net_quota(), - ] - self.network_client.get_quota_default = mock.Mock( - side_effect=self.network_default_quotas, + self.default_network_quotas = sdk_fakes.generate_fake_resource( + _network_quota_set.QuotaDefault + ) + # the defaults are global hence use of return_value here + self.network_client.get_quota_default.return_value = ( + self.default_network_quotas ) - self.network_reference_data = ( self.projects[0].id, self.network_quotas[0].floating_ips, @@ -166,17 +140,16 @@ class TestQuotaList(TestQuota): ) self.volume_quotas = [ - volume_fakes.create_one_vol_quota(), - volume_fakes.create_one_vol_quota(), + sdk_fakes.generate_fake_resource(_volume_quota_set.QuotaSet), + sdk_fakes.generate_fake_resource(_volume_quota_set.QuotaSet), ] - self.volume_default_quotas = [ - volume_fakes.create_one_default_vol_quota(), - volume_fakes.create_one_default_vol_quota(), - ] - self.volume_client.quotas.defaults = mock.Mock( - side_effect=self.volume_default_quotas, + self.default_volume_quotas = sdk_fakes.generate_fake_resource( + _volume_quota_set.QuotaSet + ) + # the defaults are global hence use of return_value here + self.volume_sdk_client.get_quota_set_defaults.return_value = ( + self.default_volume_quotas ) - self.volume_reference_data = ( self.projects[0].id, self.volume_quotas[0].backups, @@ -191,9 +164,7 @@ class TestQuotaList(TestQuota): def test_quota_list_compute(self): # Two projects with non-default quotas - self.compute_client.quotas.get = mock.Mock( - side_effect=self.compute_quotas, - ) + self.compute_sdk_client.get_quota_set.side_effect = self.compute_quotas arglist = [ '--compute', @@ -212,12 +183,10 @@ class TestQuotaList(TestQuota): def test_quota_list_compute_default(self): # One of the projects is at defaults - self.compute_client.quotas.get = mock.Mock( - side_effect=[ - self.compute_quotas[0], - compute_fakes.create_one_default_comp_quota(), - ], - ) + self.compute_sdk_client.get_quota_set.side_effect = [ + self.compute_quotas[0], + self.default_compute_quotas, + ] arglist = [ '--compute', @@ -234,14 +203,12 @@ class TestQuotaList(TestQuota): self.assertEqual(self.compute_reference_data, ret_quotas[0]) self.assertEqual(1, len(ret_quotas)) - def test_quota_list_compute_no_project_not_found(self): + def test_quota_list_compute_project_not_found(self): # Make one of the projects disappear - self.compute_client.quotas.get = mock.Mock( - side_effect=[ - self.compute_quotas[0], - exceptions.NotFound("NotFound"), - ], - ) + self.compute_sdk_client.get_quota_set.side_effect = [ + self.compute_quotas[0], + sdk_exceptions.NotFoundException("NotFound"), + ] arglist = [ '--compute', @@ -258,14 +225,12 @@ class TestQuotaList(TestQuota): self.assertEqual(self.compute_reference_data, ret_quotas[0]) self.assertEqual(1, len(ret_quotas)) - def test_quota_list_compute_no_project_4xx(self): - # Make one of the projects disappear - self.compute_client.quotas.get = mock.Mock( - side_effect=[ - self.compute_quotas[0], - exceptions.BadRequest("Bad request"), - ], - ) + def test_quota_list_compute_project_inaccessible(self): + # Make one of the projects inaccessible + self.compute_sdk_client.get_quota_set.side_effect = [ + self.compute_quotas[0], + sdk_exceptions.ForbiddenException("Forbidden"), + ] arglist = [ '--compute', @@ -282,13 +247,10 @@ class TestQuotaList(TestQuota): self.assertEqual(self.compute_reference_data, ret_quotas[0]) self.assertEqual(1, len(ret_quotas)) - def test_quota_list_compute_no_project_5xx(self): - # Make one of the projects disappear - self.compute_client.quotas.get = mock.Mock( - side_effect=[ - self.compute_quotas[0], - exceptions.HTTPNotImplemented("Not implemented??"), - ], + def test_quota_list_compute_server_error(self): + # Make the server "break" + self.compute_sdk_client.get_quota_set.side_effect = ( + sdk_exceptions.HttpException("Not implemented?") ) arglist = [ @@ -300,16 +262,14 @@ class TestQuotaList(TestQuota): parsed_args = self.check_parser(self.cmd, arglist, verifylist) self.assertRaises( - exceptions.HTTPNotImplemented, + sdk_exceptions.HttpException, self.cmd.take_action, parsed_args, ) def test_quota_list_network(self): # Two projects with non-default quotas - self.network_client.get_quota = mock.Mock( - side_effect=self.network_quotas, - ) + self.network_client.get_quota.side_effect = self.network_quotas arglist = [ '--network', @@ -328,12 +288,10 @@ class TestQuotaList(TestQuota): def test_quota_list_network_default(self): # Two projects with non-default quotas - self.network_client.get_quota = mock.Mock( - side_effect=[ - self.network_quotas[0], - network_fakes.FakeQuota.create_one_default_net_quota(), - ], - ) + self.network_client.get_quota.side_effect = [ + self.network_quotas[0], + self.default_network_quotas, + ] arglist = [ '--network', @@ -352,12 +310,10 @@ class TestQuotaList(TestQuota): def test_quota_list_network_no_project(self): # Two projects with non-default quotas - self.network_client.get_quota = mock.Mock( - side_effect=[ - self.network_quotas[0], - exceptions.NotFound("NotFound"), - ], - ) + self.network_client.get_quota.side_effect = [ + self.network_quotas[0], + sdk_exceptions.NotFoundException("NotFound"), + ] arglist = [ '--network', @@ -376,9 +332,7 @@ class TestQuotaList(TestQuota): def test_quota_list_volume(self): # Two projects with non-default quotas - self.volume_client.quotas.get = mock.Mock( - side_effect=self.volume_quotas, - ) + self.volume_sdk_client.get_quota_set.side_effect = self.volume_quotas arglist = [ '--volume', @@ -397,36 +351,10 @@ class TestQuotaList(TestQuota): def test_quota_list_volume_default(self): # Two projects with non-default quotas - self.volume_client.quotas.get = mock.Mock( - side_effect=[ - self.volume_quotas[0], - volume_fakes.create_one_default_vol_quota(), - ], - ) - - arglist = [ - '--volume', + self.volume_sdk_client.get_quota_set.side_effect = [ + self.volume_quotas[0], + self.default_volume_quotas, ] - verifylist = [ - ('volume', True), - ] - parsed_args = self.check_parser(self.cmd, arglist, verifylist) - - columns, data = self.cmd.take_action(parsed_args) - ret_quotas = list(data) - - self.assertEqual(self.volume_column_header, columns) - self.assertEqual(self.volume_reference_data, ret_quotas[0]) - self.assertEqual(1, len(ret_quotas)) - - def test_quota_list_volume_no_project(self): - # Two projects with non-default quotas - self.volume_client.quotas.get = mock.Mock( - side_effect=[ - self.volume_quotas[0], - volume_fakes.create_one_default_vol_quota(), - ], - ) arglist = [ '--volume', @@ -448,80 +376,75 @@ class TestQuotaSet(TestQuota): def setUp(self): super().setUp() - self.compute_quotas_mock.update.return_value = FakeQuotaResource( - None, - copy.deepcopy(compute_fakes.QUOTA), - loaded=True, - ) - self.compute_quotas_class_mock.update.return_value = FakeQuotaResource( - None, - copy.deepcopy(compute_fakes.QUOTA), - loaded=True, - ) - - self.volume_quotas_mock.update.return_value = FakeQuotaResource( - None, - copy.deepcopy(compute_fakes.QUOTA), - loaded=True, - ) - self.volume_quotas_class_mock.update.return_value = FakeQuotaResource( - None, - copy.deepcopy(compute_fakes.QUOTA), - loaded=True, - ) + self.identity_sdk_client.find_project.return_value = self.projects[0] self.cmd = quota.SetQuota(self.app, None) def test_quota_set(self): + floating_ip_num = 100 + fix_ip_num = 100 + injected_file_num = 100 + injected_file_size_num = 10240 + injected_path_size_num = 255 + key_pair_num = 100 + core_num = 20 + ram_num = 51200 + instance_num = 10 + property_num = 128 + secgroup_rule_num = 20 + secgroup_num = 10 + servgroup_num = 10 + servgroup_members_num = 10 + arglist = [ '--floating-ips', - str(compute_fakes.floating_ip_num), + str(floating_ip_num), '--fixed-ips', - str(compute_fakes.fix_ip_num), + str(fix_ip_num), '--injected-files', - str(compute_fakes.injected_file_num), + str(injected_file_num), '--injected-file-size', - str(compute_fakes.injected_file_size_num), + str(injected_file_size_num), '--injected-path-size', - str(compute_fakes.injected_path_size_num), + str(injected_path_size_num), '--key-pairs', - str(compute_fakes.key_pair_num), + str(key_pair_num), '--cores', - str(compute_fakes.core_num), + str(core_num), '--ram', - str(compute_fakes.ram_num), + str(ram_num), '--instances', - str(compute_fakes.instance_num), + str(instance_num), '--properties', - str(compute_fakes.property_num), + str(property_num), '--secgroup-rules', - str(compute_fakes.secgroup_rule_num), + str(secgroup_rule_num), '--secgroups', - str(compute_fakes.secgroup_num), + str(secgroup_num), '--server-groups', - str(compute_fakes.servgroup_num), + str(servgroup_num), '--server-group-members', - str(compute_fakes.servgroup_members_num), + str(servgroup_members_num), self.projects[0].name, ] verifylist = [ - ('floating_ips', compute_fakes.floating_ip_num), - ('fixed_ips', compute_fakes.fix_ip_num), - ('injected_files', compute_fakes.injected_file_num), + ('floating_ips', floating_ip_num), + ('fixed_ips', fix_ip_num), + ('injected_files', injected_file_num), ( 'injected_file_content_bytes', - compute_fakes.injected_file_size_num, + injected_file_size_num, ), - ('injected_file_path_bytes', compute_fakes.injected_path_size_num), - ('key_pairs', compute_fakes.key_pair_num), - ('cores', compute_fakes.core_num), - ('ram', compute_fakes.ram_num), - ('instances', compute_fakes.instance_num), - ('metadata_items', compute_fakes.property_num), - ('security_group_rules', compute_fakes.secgroup_rule_num), - ('security_groups', compute_fakes.secgroup_num), - ('server_groups', compute_fakes.servgroup_num), - ('server_group_members', compute_fakes.servgroup_members_num), + ('injected_file_path_bytes', injected_path_size_num), + ('key_pairs', key_pair_num), + ('cores', core_num), + ('ram', ram_num), + ('instances', instance_num), + ('metadata_items', property_num), + ('security_group_rules', secgroup_rule_num), + ('security_groups', secgroup_num), + ('server_groups', servgroup_num), + ('server_group_members', servgroup_members_num), ('force', False), ('project', self.projects[0].name), ] @@ -531,52 +454,59 @@ class TestQuotaSet(TestQuota): result = self.cmd.take_action(parsed_args) kwargs = { - 'floating_ips': compute_fakes.floating_ip_num, - 'fixed_ips': compute_fakes.fix_ip_num, - 'injected_files': compute_fakes.injected_file_num, - 'injected_file_content_bytes': compute_fakes.injected_file_size_num, # noqa: E501 - 'injected_file_path_bytes': compute_fakes.injected_path_size_num, - 'key_pairs': compute_fakes.key_pair_num, - 'cores': compute_fakes.core_num, - 'ram': compute_fakes.ram_num, - 'instances': compute_fakes.instance_num, - 'metadata_items': compute_fakes.property_num, - 'security_group_rules': compute_fakes.secgroup_rule_num, - 'security_groups': compute_fakes.secgroup_num, - 'server_groups': compute_fakes.servgroup_num, - 'server_group_members': compute_fakes.servgroup_members_num, + 'floating_ips': floating_ip_num, + 'fixed_ips': fix_ip_num, + 'injected_files': injected_file_num, + 'injected_file_content_bytes': injected_file_size_num, # noqa: E501 + 'injected_file_path_bytes': injected_path_size_num, + 'key_pairs': key_pair_num, + 'cores': core_num, + 'ram': ram_num, + 'instances': instance_num, + 'metadata_items': property_num, + 'security_group_rules': secgroup_rule_num, + 'security_groups': secgroup_num, + 'server_groups': servgroup_num, + 'server_group_members': servgroup_members_num, } - self.compute_quotas_mock.update.assert_called_once_with( + self.compute_sdk_client.update_quota_set.assert_called_once_with( self.projects[0].id, **kwargs ) self.assertIsNone(result) def test_quota_set_volume(self): + gigabytes = 1000 + volumes = 11 + snapshots = 10 + backups = 10 + backup_gigabytes = 1000 + per_volume_gigabytes = -1 + arglist = [ '--gigabytes', - str(volume_fakes.QUOTA['gigabytes']), + str(gigabytes), '--snapshots', - str(volume_fakes.QUOTA['snapshots']), + str(snapshots), '--volumes', - str(volume_fakes.QUOTA['volumes']), + str(volumes), '--backups', - str(volume_fakes.QUOTA['backups']), + str(backups), '--backup-gigabytes', - str(volume_fakes.QUOTA['backup_gigabytes']), + str(backup_gigabytes), '--per-volume-gigabytes', - str(volume_fakes.QUOTA['per_volume_gigabytes']), + str(per_volume_gigabytes), self.projects[0].name, ] verifylist = [ - ('gigabytes', volume_fakes.QUOTA['gigabytes']), - ('snapshots', volume_fakes.QUOTA['snapshots']), - ('volumes', volume_fakes.QUOTA['volumes']), - ('backups', volume_fakes.QUOTA['backups']), - ('backup_gigabytes', volume_fakes.QUOTA['backup_gigabytes']), + ('gigabytes', gigabytes), + ('snapshots', snapshots), + ('volumes', volumes), + ('backups', backups), + ('backup_gigabytes', backup_gigabytes), ( 'per_volume_gigabytes', - volume_fakes.QUOTA['per_volume_gigabytes'], + per_volume_gigabytes, ), ('project', self.projects[0].name), ] @@ -585,47 +515,54 @@ class TestQuotaSet(TestQuota): result = self.cmd.take_action(parsed_args) kwargs = { - 'gigabytes': volume_fakes.QUOTA['gigabytes'], - 'snapshots': volume_fakes.QUOTA['snapshots'], - 'volumes': volume_fakes.QUOTA['volumes'], - 'backups': volume_fakes.QUOTA['backups'], - 'backup_gigabytes': volume_fakes.QUOTA['backup_gigabytes'], - 'per_volume_gigabytes': volume_fakes.QUOTA['per_volume_gigabytes'], + 'gigabytes': gigabytes, + 'snapshots': snapshots, + 'volumes': volumes, + 'backups': backups, + 'backup_gigabytes': backup_gigabytes, + 'per_volume_gigabytes': per_volume_gigabytes, } - self.volume_quotas_mock.update.assert_called_once_with( + self.volume_sdk_client.update_quota_set.assert_called_once_with( self.projects[0].id, **kwargs ) self.assertIsNone(result) def test_quota_set_volume_with_volume_type(self): + gigabytes = 1000 + volumes = 11 + snapshots = 10 + backups = 10 + backup_gigabytes = 1000 + per_volume_gigabytes = -1 + arglist = [ '--gigabytes', - str(volume_fakes.QUOTA['gigabytes']), + str(gigabytes), '--snapshots', - str(volume_fakes.QUOTA['snapshots']), + str(snapshots), '--volumes', - str(volume_fakes.QUOTA['volumes']), + str(volumes), '--backups', - str(volume_fakes.QUOTA['backups']), + str(backups), '--backup-gigabytes', - str(volume_fakes.QUOTA['backup_gigabytes']), + str(backup_gigabytes), '--per-volume-gigabytes', - str(volume_fakes.QUOTA['per_volume_gigabytes']), + str(per_volume_gigabytes), '--volume-type', 'volume_type_backend', self.projects[0].name, ] verifylist = [ - ('gigabytes', volume_fakes.QUOTA['gigabytes']), - ('snapshots', volume_fakes.QUOTA['snapshots']), - ('volumes', volume_fakes.QUOTA['volumes']), - ('backups', volume_fakes.QUOTA['backups']), - ('backup_gigabytes', volume_fakes.QUOTA['backup_gigabytes']), + ('gigabytes', gigabytes), + ('snapshots', snapshots), + ('volumes', volumes), + ('backups', backups), + ('backup_gigabytes', backup_gigabytes), ( 'per_volume_gigabytes', - volume_fakes.QUOTA['per_volume_gigabytes'], + per_volume_gigabytes, ), ('volume_type', 'volume_type_backend'), ('project', self.projects[0].name), @@ -635,54 +572,64 @@ class TestQuotaSet(TestQuota): result = self.cmd.take_action(parsed_args) kwargs = { - 'gigabytes_volume_type_backend': volume_fakes.QUOTA['gigabytes'], - 'snapshots_volume_type_backend': volume_fakes.QUOTA['snapshots'], - 'volumes_volume_type_backend': volume_fakes.QUOTA['volumes'], - 'backups': volume_fakes.QUOTA['backups'], - 'backup_gigabytes': volume_fakes.QUOTA['backup_gigabytes'], - 'per_volume_gigabytes': volume_fakes.QUOTA['per_volume_gigabytes'], + 'gigabytes_volume_type_backend': gigabytes, + 'snapshots_volume_type_backend': snapshots, + 'volumes_volume_type_backend': volumes, + 'backups': backups, + 'backup_gigabytes': backup_gigabytes, + 'per_volume_gigabytes': per_volume_gigabytes, } - self.volume_quotas_mock.update.assert_called_once_with( + self.volume_sdk_client.update_quota_set.assert_called_once_with( self.projects[0].id, **kwargs ) self.assertIsNone(result) def test_quota_set_network(self): + subnet = 10 + network = 10 + floatingip = 50 + subnetpool = -1 + security_group_rule = 100 + security_group = 10 + router = 10 + rbac_policy = -1 + port = 50 + arglist = [ '--subnets', - str(network_fakes.QUOTA['subnet']), + str(subnet), '--networks', - str(network_fakes.QUOTA['network']), + str(network), '--floating-ips', - str(network_fakes.QUOTA['floatingip']), + str(floatingip), '--subnetpools', - str(network_fakes.QUOTA['subnetpool']), + str(subnetpool), '--secgroup-rules', - str(network_fakes.QUOTA['security_group_rule']), + str(security_group_rule), '--secgroups', - str(network_fakes.QUOTA['security_group']), + str(security_group), '--routers', - str(network_fakes.QUOTA['router']), + str(router), '--rbac-policies', - str(network_fakes.QUOTA['rbac_policy']), + str(rbac_policy), '--ports', - str(network_fakes.QUOTA['port']), + str(port), self.projects[0].name, ] verifylist = [ - ('subnet', network_fakes.QUOTA['subnet']), - ('network', network_fakes.QUOTA['network']), - ('floatingip', network_fakes.QUOTA['floatingip']), - ('subnetpool', network_fakes.QUOTA['subnetpool']), + ('subnet', subnet), + ('network', network), + ('floatingip', floatingip), + ('subnetpool', subnetpool), ( 'security_group_rule', - network_fakes.QUOTA['security_group_rule'], + security_group_rule, ), - ('security_group', network_fakes.QUOTA['security_group']), - ('router', network_fakes.QUOTA['router']), - ('rbac_policy', network_fakes.QUOTA['rbac_policy']), - ('port', network_fakes.QUOTA['port']), + ('security_group', security_group), + ('router', router), + ('rbac_policy', rbac_policy), + ('port', port), ('force', False), ('project', self.projects[0].name), ] @@ -691,15 +638,15 @@ class TestQuotaSet(TestQuota): result = self.cmd.take_action(parsed_args) kwargs = { 'check_limit': True, - 'subnet': network_fakes.QUOTA['subnet'], - 'network': network_fakes.QUOTA['network'], - 'floatingip': network_fakes.QUOTA['floatingip'], - 'subnetpool': network_fakes.QUOTA['subnetpool'], - 'security_group_rule': network_fakes.QUOTA['security_group_rule'], - 'security_group': network_fakes.QUOTA['security_group'], - 'router': network_fakes.QUOTA['router'], - 'rbac_policy': network_fakes.QUOTA['rbac_policy'], - 'port': network_fakes.QUOTA['port'], + 'subnet': subnet, + 'network': network, + 'floatingip': floatingip, + 'subnetpool': subnetpool, + 'security_group_rule': security_group_rule, + 'security_group': security_group, + 'router': router, + 'rbac_policy': rbac_policy, + 'port': port, } self.network_client.update_quota.assert_called_once_with( self.projects[0].id, **kwargs @@ -707,56 +654,71 @@ class TestQuotaSet(TestQuota): self.assertIsNone(result) def test_quota_set_with_class(self): + floating_ip_num = 100 + fix_ip_num = 100 + injected_file_num = 100 + injected_file_size_num = 10240 + injected_path_size_num = 255 + key_pair_num = 100 + core_num = 20 + ram_num = 51200 + instance_num = 10 + property_num = 128 + servgroup_num = 10 + servgroup_members_num = 10 + volumes = 11 + network = 10 + arglist = [ '--injected-files', - str(compute_fakes.injected_file_num), + str(injected_file_num), '--injected-file-size', - str(compute_fakes.injected_file_size_num), + str(injected_file_size_num), '--injected-path-size', - str(compute_fakes.injected_path_size_num), + str(injected_path_size_num), '--key-pairs', - str(compute_fakes.key_pair_num), + str(key_pair_num), '--cores', - str(compute_fakes.core_num), + str(core_num), '--ram', - str(compute_fakes.ram_num), + str(ram_num), '--instances', - str(compute_fakes.instance_num), + str(instance_num), '--properties', - str(compute_fakes.property_num), + str(property_num), '--server-groups', - str(compute_fakes.servgroup_num), + str(servgroup_num), '--server-group-members', - str(compute_fakes.servgroup_members_num), + str(servgroup_members_num), '--gigabytes', - str(compute_fakes.floating_ip_num), + str(floating_ip_num), '--snapshots', - str(compute_fakes.fix_ip_num), + str(fix_ip_num), '--volumes', - str(volume_fakes.QUOTA['volumes']), + str(volumes), '--network', - str(network_fakes.QUOTA['network']), + str(network), '--class', self.projects[0].name, ] verifylist = [ - ('injected_files', compute_fakes.injected_file_num), + ('injected_files', injected_file_num), ( 'injected_file_content_bytes', - compute_fakes.injected_file_size_num, + injected_file_size_num, ), - ('injected_file_path_bytes', compute_fakes.injected_path_size_num), - ('key_pairs', compute_fakes.key_pair_num), - ('cores', compute_fakes.core_num), - ('ram', compute_fakes.ram_num), - ('instances', compute_fakes.instance_num), - ('metadata_items', compute_fakes.property_num), - ('server_groups', compute_fakes.servgroup_num), - ('server_group_members', compute_fakes.servgroup_members_num), - ('gigabytes', compute_fakes.floating_ip_num), - ('snapshots', compute_fakes.fix_ip_num), - ('volumes', volume_fakes.QUOTA['volumes']), - ('network', network_fakes.QUOTA['network']), + ('injected_file_path_bytes', injected_path_size_num), + ('key_pairs', key_pair_num), + ('cores', core_num), + ('ram', ram_num), + ('instances', instance_num), + ('metadata_items', property_num), + ('server_groups', servgroup_num), + ('server_group_members', servgroup_members_num), + ('gigabytes', floating_ip_num), + ('snapshots', fix_ip_num), + ('volumes', volumes), + ('network', network), ('quota_class', True), ('project', self.projects[0].name), ] @@ -765,82 +727,97 @@ class TestQuotaSet(TestQuota): result = self.cmd.take_action(parsed_args) kwargs_compute = { - 'injected_files': compute_fakes.injected_file_num, - 'injected_file_content_bytes': compute_fakes.injected_file_size_num, # noqa: E501 - 'injected_file_path_bytes': compute_fakes.injected_path_size_num, - 'key_pairs': compute_fakes.key_pair_num, - 'cores': compute_fakes.core_num, - 'ram': compute_fakes.ram_num, - 'instances': compute_fakes.instance_num, - 'metadata_items': compute_fakes.property_num, - 'server_groups': compute_fakes.servgroup_num, - 'server_group_members': compute_fakes.servgroup_members_num, + 'injected_files': injected_file_num, + 'injected_file_content_bytes': injected_file_size_num, # noqa: E501 + 'injected_file_path_bytes': injected_path_size_num, + 'key_pairs': key_pair_num, + 'cores': core_num, + 'ram': ram_num, + 'instances': instance_num, + 'metadata_items': property_num, + 'server_groups': servgroup_num, + 'server_group_members': servgroup_members_num, } kwargs_volume = { - 'gigabytes': compute_fakes.floating_ip_num, - 'snapshots': compute_fakes.fix_ip_num, - 'volumes': volume_fakes.QUOTA['volumes'], + 'gigabytes': floating_ip_num, + 'snapshots': fix_ip_num, + 'volumes': volumes, } - self.compute_quotas_class_mock.update.assert_called_with( + self.compute_sdk_client.update_quota_class_set.assert_called_with( self.projects[0].name, **kwargs_compute ) - self.volume_quotas_class_mock.update.assert_called_with( + self.volume_sdk_client.update_quota_class_set.assert_called_with( self.projects[0].name, **kwargs_volume ) self.assertNotCalled(self.network_client.update_quota) self.assertIsNone(result) def test_quota_set_default(self): + floating_ip_num = 100 + fix_ip_num = 100 + injected_file_num = 100 + injected_file_size_num = 10240 + injected_path_size_num = 255 + key_pair_num = 100 + core_num = 20 + ram_num = 51200 + instance_num = 10 + property_num = 128 + servgroup_num = 10 + servgroup_members_num = 10 + volumes = 11 + network = 10 + arglist = [ '--injected-files', - str(compute_fakes.injected_file_num), + str(injected_file_num), '--injected-file-size', - str(compute_fakes.injected_file_size_num), + str(injected_file_size_num), '--injected-path-size', - str(compute_fakes.injected_path_size_num), + str(injected_path_size_num), '--key-pairs', - str(compute_fakes.key_pair_num), + str(key_pair_num), '--cores', - str(compute_fakes.core_num), + str(core_num), '--ram', - str(compute_fakes.ram_num), + str(ram_num), '--instances', - str(compute_fakes.instance_num), + str(instance_num), '--properties', - str(compute_fakes.property_num), + str(property_num), '--server-groups', - str(compute_fakes.servgroup_num), + str(servgroup_num), '--server-group-members', - str(compute_fakes.servgroup_members_num), + str(servgroup_members_num), '--gigabytes', - str(compute_fakes.floating_ip_num), + str(floating_ip_num), '--snapshots', - str(compute_fakes.fix_ip_num), + str(fix_ip_num), '--volumes', - str(volume_fakes.QUOTA['volumes']), + str(volumes), '--network', - str(network_fakes.QUOTA['network']), + str(network), '--default', ] verifylist = [ - ('injected_files', compute_fakes.injected_file_num), + ('injected_files', injected_file_num), ( 'injected_file_content_bytes', - compute_fakes.injected_file_size_num, + injected_file_size_num, ), - ('injected_file_path_bytes', compute_fakes.injected_path_size_num), - ('key_pairs', compute_fakes.key_pair_num), - ('cores', compute_fakes.core_num), - ('ram', compute_fakes.ram_num), - ('instances', compute_fakes.instance_num), - ('metadata_items', compute_fakes.property_num), - ('server_groups', compute_fakes.servgroup_num), - ('server_group_members', compute_fakes.servgroup_members_num), - ('gigabytes', compute_fakes.floating_ip_num), - ('snapshots', compute_fakes.fix_ip_num), - ('volumes', volume_fakes.QUOTA['volumes']), - ('network', network_fakes.QUOTA['network']), + ('injected_file_path_bytes', injected_path_size_num), + ('key_pairs', key_pair_num), + ('cores', core_num), + ('ram', ram_num), + ('instances', instance_num), + ('metadata_items', property_num), + ('server_groups', servgroup_num), + ('server_group_members', servgroup_members_num), + ('gigabytes', floating_ip_num), + ('snapshots', fix_ip_num), + ('volumes', volumes), + ('network', network), ('default', True), ] parsed_args = self.check_parser(self.cmd, arglist, verifylist) @@ -848,53 +825,59 @@ class TestQuotaSet(TestQuota): result = self.cmd.take_action(parsed_args) kwargs_compute = { - 'injected_files': compute_fakes.injected_file_num, - 'injected_file_content_bytes': compute_fakes.injected_file_size_num, # noqa: E501 - 'injected_file_path_bytes': compute_fakes.injected_path_size_num, - 'key_pairs': compute_fakes.key_pair_num, - 'cores': compute_fakes.core_num, - 'ram': compute_fakes.ram_num, - 'instances': compute_fakes.instance_num, - 'metadata_items': compute_fakes.property_num, - 'server_groups': compute_fakes.servgroup_num, - 'server_group_members': compute_fakes.servgroup_members_num, + 'injected_files': injected_file_num, + 'injected_file_content_bytes': injected_file_size_num, # noqa: E501 + 'injected_file_path_bytes': injected_path_size_num, + 'key_pairs': key_pair_num, + 'cores': core_num, + 'ram': ram_num, + 'instances': instance_num, + 'metadata_items': property_num, + 'server_groups': servgroup_num, + 'server_group_members': servgroup_members_num, } kwargs_volume = { - 'gigabytes': compute_fakes.floating_ip_num, - 'snapshots': compute_fakes.fix_ip_num, - 'volumes': volume_fakes.QUOTA['volumes'], + 'gigabytes': floating_ip_num, + 'snapshots': fix_ip_num, + 'volumes': volumes, } - self.compute_quotas_class_mock.update.assert_called_with( + self.compute_sdk_client.update_quota_class_set.assert_called_with( 'default', **kwargs_compute ) - self.volume_quotas_class_mock.update.assert_called_with( + self.volume_sdk_client.update_quota_class_set.assert_called_with( 'default', **kwargs_volume ) self.assertNotCalled(self.network_client.update_quota) self.assertIsNone(result) def test_quota_set_with_force(self): + core_num = 20 + ram_num = 51200 + instance_num = 10 + volumes = 11 + subnet = 10 + arglist = [ '--cores', - str(compute_fakes.core_num), + str(core_num), '--ram', - str(compute_fakes.ram_num), + str(ram_num), '--instances', - str(compute_fakes.instance_num), + str(instance_num), '--volumes', - str(volume_fakes.QUOTA['volumes']), + str(volumes), '--subnets', - str(network_fakes.QUOTA['subnet']), + str(subnet), '--force', self.projects[0].name, ] verifylist = [ - ('cores', compute_fakes.core_num), - ('ram', compute_fakes.ram_num), - ('instances', compute_fakes.instance_num), - ('volumes', volume_fakes.QUOTA['volumes']), - ('subnet', network_fakes.QUOTA['subnet']), + ('cores', core_num), + ('ram', ram_num), + ('instances', instance_num), + ('volumes', volumes), + ('subnet', subnet), ('force', True), ('project', self.projects[0].name), ] @@ -904,22 +887,22 @@ class TestQuotaSet(TestQuota): result = self.cmd.take_action(parsed_args) kwargs_compute = { - 'cores': compute_fakes.core_num, - 'ram': compute_fakes.ram_num, - 'instances': compute_fakes.instance_num, + 'cores': core_num, + 'ram': ram_num, + 'instances': instance_num, 'force': True, } kwargs_volume = { - 'volumes': volume_fakes.QUOTA['volumes'], + 'volumes': volumes, } kwargs_network = { - 'subnet': network_fakes.QUOTA['subnet'], + 'subnet': subnet, 'force': True, } - self.compute_quotas_mock.update.assert_called_once_with( + self.compute_sdk_client.update_quota_set.assert_called_once_with( self.projects[0].id, **kwargs_compute ) - self.volume_quotas_mock.update.assert_called_once_with( + self.volume_sdk_client.update_quota_set.assert_called_once_with( self.projects[0].id, **kwargs_volume ) self.network_client.update_quota.assert_called_once_with( @@ -930,18 +913,18 @@ class TestQuotaSet(TestQuota): def test_quota_set_with_no_force(self): arglist = [ '--subnets', - str(network_fakes.QUOTA['subnet']), + str(10), '--volumes', - str(volume_fakes.QUOTA['volumes']), + str(30), '--cores', - str(compute_fakes.core_num), + str(20), '--no-force', self.projects[0].name, ] verifylist = [ - ('subnet', network_fakes.QUOTA['subnet']), - ('volumes', volume_fakes.QUOTA['volumes']), - ('cores', compute_fakes.core_num), + ('subnet', 10), + ('volumes', 30), + ('cores', 20), ('force', False), ('project', self.projects[0].name), ] @@ -950,19 +933,19 @@ class TestQuotaSet(TestQuota): result = self.cmd.take_action(parsed_args) kwargs_compute = { - 'cores': compute_fakes.core_num, + 'cores': 20, } kwargs_volume = { - 'volumes': volume_fakes.QUOTA['volumes'], + 'volumes': 30, } kwargs_network = { - 'subnet': network_fakes.QUOTA['subnet'], + 'subnet': 10, 'check_limit': True, } - self.compute_quotas_mock.update.assert_called_once_with( + self.compute_sdk_client.update_quota_set.assert_called_once_with( self.projects[0].id, **kwargs_compute ) - self.volume_quotas_mock.update.assert_called_once_with( + self.volume_sdk_client.update_quota_set.assert_called_once_with( self.projects[0].id, **kwargs_volume ) self.network_client.update_quota.assert_called_once_with( @@ -975,47 +958,36 @@ class TestQuotaShow(TestQuota): def setUp(self): super().setUp() - self.compute_quota = compute_fakes.create_one_comp_quota() - self.compute_quotas_mock.get.return_value = self.compute_quota - self.compute_default_quota = ( - compute_fakes.create_one_default_comp_quota() + self.identity_sdk_client.find_project.return_value = self.projects[0] + + self.compute_sdk_client.get_quota_set.return_value = ( + sdk_fakes.generate_fake_resource(_compute_quota_set.QuotaSet) ) - self.compute_quotas_mock.defaults.return_value = ( - self.compute_default_quota + self.default_compute_quotas = sdk_fakes.generate_fake_resource( + _compute_quota_set.QuotaSet ) - self.compute_quotas_class_mock.get.return_value = FakeQuotaResource( - None, - copy.deepcopy(compute_fakes.QUOTA), - loaded=True, + self.compute_sdk_client.get_quota_set_defaults.return_value = ( + self.default_compute_quotas ) - self.volume_quota = volume_fakes.create_one_vol_quota() - self.volume_quotas_mock.get.return_value = self.volume_quota - self.volume_default_quota = volume_fakes.create_one_default_vol_quota() - self.volume_quotas_mock.defaults.return_value = ( - self.volume_default_quota + self.volume_sdk_client.get_quota_set.return_value = ( + sdk_fakes.generate_fake_resource(_volume_quota_set.QuotaSet) ) - self.volume_quotas_class_mock.get.return_value = FakeQuotaResource( - None, - copy.deepcopy(volume_fakes.QUOTA), - loaded=True, + self.default_volume_quotas = sdk_fakes.generate_fake_resource( + _volume_quota_set.QuotaSet + ) + self.volume_sdk_client.get_quota_set_defaults.return_value = ( + self.default_volume_quotas ) - fake_network_endpoint = fakes.FakeResource( - None, - copy.deepcopy(identity_fakes.ENDPOINT), - loaded=True, + self.network_client.get_quota.return_value = ( + sdk_fakes.generate_fake_resource(_network_quota_set.Quota) ) - - self.service_catalog_mock.get_endpoints.return_value = { - 'network': fake_network_endpoint - } - - self.network_client.get_quota = mock.Mock( - return_value=network_fakes.QUOTA, + self.default_network_quotas = sdk_fakes.generate_fake_resource( + _network_quota_set.QuotaDefault ) - self.network_client.get_quota_default = mock.Mock( - return_value=network_fakes.QUOTA, + self.network_client.get_quota_default.return_value = ( + self.default_network_quotas ) self.cmd = quota.ShowQuota(self.app, None) @@ -1032,11 +1004,11 @@ class TestQuotaShow(TestQuota): self.cmd.take_action(parsed_args) - self.compute_quotas_mock.get.assert_called_once_with( + self.compute_sdk_client.get_quota_set.assert_called_once_with( self.projects[0].id, - detail=False, + usage=False, ) - self.volume_quotas_mock.get.assert_called_once_with( + self.volume_sdk_client.get_quota_set.assert_called_once_with( self.projects[0].id, usage=False, ) @@ -1059,11 +1031,11 @@ class TestQuotaShow(TestQuota): self.cmd.take_action(parsed_args) - self.compute_quotas_mock.get.assert_called_once_with( + self.compute_sdk_client.get_quota_set.assert_called_once_with( self.projects[0].id, - detail=False, + usage=False, ) - self.volume_quotas_mock.get.assert_not_called() + self.volume_sdk_client.get_quota_set.assert_not_called() self.network_client.get_quota.assert_not_called() def test_quota_show__with_volume(self): @@ -1079,8 +1051,8 @@ class TestQuotaShow(TestQuota): self.cmd.take_action(parsed_args) - self.compute_quotas_mock.get.assert_not_called() - self.volume_quotas_mock.get.assert_called_once_with( + self.compute_sdk_client.get_quota_set.assert_not_called() + self.volume_sdk_client.get_quota_set.assert_called_once_with( self.projects[0].id, usage=False, ) @@ -1099,8 +1071,8 @@ class TestQuotaShow(TestQuota): self.cmd.take_action(parsed_args) - self.compute_quotas_mock.get.assert_not_called() - self.volume_quotas_mock.get.assert_not_called() + self.compute_sdk_client.get_quota_set.assert_not_called() + self.volume_sdk_client.get_quota_set.assert_not_called() self.network_client.get_quota.assert_called_once_with( self.projects[0].id, details=False, @@ -1120,10 +1092,10 @@ class TestQuotaShow(TestQuota): self.cmd.take_action(parsed_args) - self.compute_quotas_mock.defaults.assert_called_once_with( + self.compute_sdk_client.get_quota_set_defaults.assert_called_once_with( self.projects[0].id, ) - self.volume_quotas_mock.defaults.assert_called_once_with( + self.volume_sdk_client.get_quota_set_defaults.assert_called_once_with( self.projects[0].id, ) self.network_client.get_quota_default.assert_called_once_with( @@ -1132,15 +1104,6 @@ class TestQuotaShow(TestQuota): self.assertNotCalled(self.network_client.get_quota) def test_quota_show__with_usage(self): - # update mocks to return detailed quota instead - self.compute_quota = compute_fakes.create_one_comp_detailed_quota() - self.compute_quotas_mock.get.return_value = self.compute_quota - self.volume_quota = volume_fakes.create_one_detailed_quota() - self.volume_quotas_mock.get.return_value = self.volume_quota - self.network_client.get_quota.return_value = ( - network_fakes.FakeQuota.create_one_net_detailed_quota() - ) - arglist = [ '--usage', self.projects[0].name, @@ -1153,11 +1116,11 @@ class TestQuotaShow(TestQuota): self.cmd.take_action(parsed_args) - self.compute_quotas_mock.get.assert_called_once_with( + self.compute_sdk_client.get_quota_set.assert_called_once_with( self.projects[0].id, - detail=True, + usage=True, ) - self.volume_quotas_mock.get.assert_called_once_with( + self.volume_sdk_client.get_quota_set.assert_called_once_with( self.projects[0].id, usage=True, ) @@ -1175,14 +1138,14 @@ class TestQuotaShow(TestQuota): self.cmd.take_action(parsed_args) - self.compute_quotas_mock.get.assert_called_once_with( - identity_fakes.project_id, detail=False + self.compute_sdk_client.get_quota_set.assert_called_once_with( + self.projects[1].id, usage=False ) - self.volume_quotas_mock.get.assert_called_once_with( - identity_fakes.project_id, usage=False + self.volume_sdk_client.get_quota_set.assert_called_once_with( + self.projects[1].id, usage=False ) self.network_client.get_quota.assert_called_once_with( - identity_fakes.project_id, details=False + self.projects[1].id, details=False ) self.assertNotCalled(self.network_client.get_quota_default) @@ -1193,6 +1156,12 @@ class TestQuotaDelete(TestQuota): def setUp(self): super().setUp() + self.identity_sdk_client.find_project.return_value = self.projects[0] + + self.compute_sdk_client.revert_quota_set.return_value = None + self.volume_sdk_client.revert_quota_set.return_value = None + self.network_client.delete_quota.return_value = None + self.cmd = quota.DeleteQuota(self.app, None) def test_delete(self): @@ -1210,11 +1179,13 @@ class TestQuotaDelete(TestQuota): result = self.cmd.take_action(parsed_args) self.assertIsNone(result) - self.projects_mock.get.assert_called_once_with(self.projects[0].id) - self.compute_quotas_mock.delete.assert_called_once_with( + self.identity_sdk_client.find_project.assert_called_once_with( + self.projects[0].id, ignore_missing=False + ) + self.compute_sdk_client.revert_quota_set.assert_called_once_with( self.projects[0].id, ) - self.volume_quotas_mock.delete.assert_called_once_with( + self.volume_sdk_client.revert_quota_set.assert_called_once_with( self.projects[0].id, ) self.network_client.delete_quota.assert_called_once_with( @@ -1237,11 +1208,13 @@ class TestQuotaDelete(TestQuota): result = self.cmd.take_action(parsed_args) self.assertIsNone(result) - self.projects_mock.get.assert_called_once_with(self.projects[0].id) - self.compute_quotas_mock.delete.assert_called_once_with( + self.identity_sdk_client.find_project.assert_called_once_with( + self.projects[0].id, ignore_missing=False + ) + self.compute_sdk_client.revert_quota_set.assert_called_once_with( self.projects[0].id, ) - self.volume_quotas_mock.delete.assert_not_called() + self.volume_sdk_client.revert_quota_set.assert_not_called() self.network_client.delete_quota.assert_not_called() def test_delete__volume(self): @@ -1260,9 +1233,11 @@ class TestQuotaDelete(TestQuota): result = self.cmd.take_action(parsed_args) self.assertIsNone(result) - self.projects_mock.get.assert_called_once_with(self.projects[0].id) - self.compute_quotas_mock.delete.assert_not_called() - self.volume_quotas_mock.delete.assert_called_once_with( + self.identity_sdk_client.find_project.assert_called_once_with( + self.projects[0].id, ignore_missing=False + ) + self.compute_sdk_client.revert_quota_set.assert_not_called() + self.volume_sdk_client.revert_quota_set.assert_called_once_with( self.projects[0].id, ) self.network_client.delete_quota.assert_not_called() @@ -1283,9 +1258,11 @@ class TestQuotaDelete(TestQuota): result = self.cmd.take_action(parsed_args) self.assertIsNone(result) - self.projects_mock.get.assert_called_once_with(self.projects[0].id) - self.compute_quotas_mock.delete.assert_not_called() - self.volume_quotas_mock.delete.assert_not_called() + self.identity_sdk_client.find_project.assert_called_once_with( + self.projects[0].id, ignore_missing=False + ) + self.compute_sdk_client.revert_quota_set.assert_not_called() + self.volume_sdk_client.revert_quota_set.assert_not_called() self.network_client.delete_quota.assert_called_once_with( self.projects[0].id, ) diff --git a/openstackclient/tests/unit/compute/v2/fakes.py b/openstackclient/tests/unit/compute/v2/fakes.py index 5a19ad56ae..1e1bc40751 100644 --- a/openstackclient/tests/unit/compute/v2/fakes.py +++ b/openstackclient/tests/unit/compute/v2/fakes.py @@ -46,42 +46,6 @@ from openstackclient.tests.unit.network.v2 import fakes as network_fakes from openstackclient.tests.unit import utils from openstackclient.tests.unit.volume.v3 import fakes as volume_fakes -floating_ip_num = 100 -fix_ip_num = 100 -injected_file_num = 100 -injected_file_size_num = 10240 -injected_path_size_num = 255 -key_pair_num = 100 -core_num = 20 -ram_num = 51200 -instance_num = 10 -property_num = 128 -secgroup_rule_num = 20 -secgroup_num = 10 -servgroup_num = 10 -servgroup_members_num = 10 -project_name = 'project_test' -QUOTA = { - 'project': project_name, - 'floating-ips': floating_ip_num, - 'fix-ips': fix_ip_num, - 'injected-files': injected_file_num, - 'injected-file-size': injected_file_size_num, - 'injected-path-size': injected_path_size_num, - 'key-pairs': key_pair_num, - 'cores': core_num, - 'ram': ram_num, - 'instances': instance_num, - 'properties': property_num, - 'secgroup_rules': secgroup_rule_num, - 'secgroups': secgroup_num, - 'server-groups': servgroup_num, - 'server-group-members': servgroup_members_num, -} - -QUOTA_columns = tuple(sorted(QUOTA)) -QUOTA_data = tuple(QUOTA[x] for x in sorted(QUOTA)) - class FakeComputev2Client: def __init__(self, **kwargs): @@ -105,12 +69,6 @@ class FakeComputev2Client: self.flavor_access = mock.Mock() self.flavor_access.resource_class = fakes.FakeResource(None, {}) - self.quotas = mock.Mock() - self.quotas.resource_class = fakes.FakeResource(None, {}) - - self.quota_classes = mock.Mock() - self.quota_classes.resource_class = fakes.FakeResource(None, {}) - self.usage = mock.Mock() self.usage.resource_class = fakes.FakeResource(None, {}) @@ -1077,98 +1035,6 @@ def create_usages(attrs=None, count=2): return usages -def create_one_comp_quota(attrs=None): - """Create one quota""" - - attrs = attrs or {} - - quota_attrs = { - 'id': 'project-id-' + uuid.uuid4().hex, - 'cores': 20, - 'fixed_ips': 30, - 'injected_files': 100, - 'injected_file_content_bytes': 10240, - 'injected_file_path_bytes': 255, - 'instances': 50, - 'key_pairs': 20, - 'metadata_items': 10, - 'ram': 51200, - 'server_groups': 10, - 'server_group_members': 10, - } - - quota_attrs.update(attrs) - quota = fakes.FakeResource(info=copy.deepcopy(quota_attrs), loaded=True) - - quota.project_id = quota_attrs['id'] - - return quota - - -def create_one_default_comp_quota(attrs=None): - """Create one quota""" - - attrs = attrs or {} - - quota_attrs = { - 'id': 'project-id-' + uuid.uuid4().hex, - 'cores': 10, - 'fixed_ips': 10, - 'injected_files': 100, - 'injected_file_content_bytes': 10240, - 'injected_file_path_bytes': 255, - 'instances': 20, - 'key_pairs': 20, - 'metadata_items': 10, - 'ram': 51200, - 'server_groups': 10, - 'server_group_members': 10, - } - - quota_attrs.update(attrs) - quota = fakes.FakeResource(info=copy.deepcopy(quota_attrs), loaded=True) - - quota.project_id = quota_attrs['id'] - - return quota - - -def create_one_comp_detailed_quota(attrs=None): - """Create one quota""" - - attrs = attrs or {} - - quota_attrs = { - 'id': 'project-id-' + uuid.uuid4().hex, - 'cores': {'reserved': 0, 'in_use': 0, 'limit': 20}, - 'fixed_ips': {'reserved': 0, 'in_use': 0, 'limit': 30}, - 'injected_files': {'reserved': 0, 'in_use': 0, 'limit': 100}, - 'injected_file_content_bytes': { - 'reserved': 0, - 'in_use': 0, - 'limit': 10240, - }, - 'injected_file_path_bytes': { - 'reserved': 0, - 'in_use': 0, - 'limit': 255, - }, - 'instances': {'reserved': 0, 'in_use': 0, 'limit': 50}, - 'key_pairs': {'reserved': 0, 'in_use': 0, 'limit': 20}, - 'metadata_items': {'reserved': 0, 'in_use': 0, 'limit': 10}, - 'ram': {'reserved': 0, 'in_use': 0, 'limit': 51200}, - 'server_groups': {'reserved': 0, 'in_use': 0, 'limit': 10}, - 'server_group_members': {'reserved': 0, 'in_use': 0, 'limit': 10}, - } - - quota_attrs.update(attrs) - quota = fakes.FakeResource(info=copy.deepcopy(quota_attrs), loaded=True) - - quota.project_id = quota_attrs['id'] - - return quota - - def create_limits(attrs=None): """Create a fake limits object.""" attrs = attrs or {} diff --git a/openstackclient/tests/unit/network/v2/fakes.py b/openstackclient/tests/unit/network/v2/fakes.py index 28af24287b..999e1027cd 100644 --- a/openstackclient/tests/unit/network/v2/fakes.py +++ b/openstackclient/tests/unit/network/v2/fakes.py @@ -42,22 +42,6 @@ from openstackclient.tests.unit.identity.v3 import fakes as identity_fakes from openstackclient.tests.unit import utils -QUOTA = { - "subnet": 10, - "network": 10, - "floatingip": 50, - "subnetpool": -1, - "security_group_rule": 100, - "security_group": 10, - "router": 10, - "rbac_policy": -1, - "port": 50, - "vip": 10, - "member": 10, - "healthmonitor": 10, - "l7policy": 5, -} - RULE_TYPE_BANDWIDTH_LIMIT = 'bandwidth-limit' RULE_TYPE_DSCP_MARKING = 'dscp-marking' RULE_TYPE_MINIMUM_BANDWIDTH = 'minimum-bandwidth' @@ -990,82 +974,6 @@ class FakeNetworkServiceProvider: return service_providers -class FakeQuota: - """Fake quota""" - - @staticmethod - def create_one_net_quota(attrs=None): - """Create one quota""" - attrs = attrs or {} - - quota_attrs = { - 'floating_ips': 20, - 'networks': 25, - 'ports': 11, - 'rbac_policies': 15, - 'routers': 40, - 'security_groups': 10, - 'security_group_rules': 100, - 'subnets': 20, - 'subnet_pools': 30, - } - - quota_attrs.update(attrs) - - quota = fakes.FakeResource( - info=copy.deepcopy(quota_attrs), loaded=True - ) - return quota - - @staticmethod - def create_one_default_net_quota(attrs=None): - """Create one quota""" - attrs = attrs or {} - - quota_attrs = { - 'floatingip': 30, - 'network': 20, - 'port': 10, - 'rbac_policy': 25, - 'router': 30, - 'security_group': 30, - 'security_group_rule': 200, - 'subnet': 10, - 'subnetpool': 20, - } - - quota_attrs.update(attrs) - - quota = fakes.FakeResource( - info=copy.deepcopy(quota_attrs), loaded=True - ) - return quota - - @staticmethod - def create_one_net_detailed_quota(attrs=None): - """Create one quota""" - attrs = attrs or {} - - quota_attrs = { - 'floating_ips': {'used': 0, 'reserved': 0, 'limit': 20}, - 'networks': {'used': 0, 'reserved': 0, 'limit': 25}, - 'ports': {'used': 0, 'reserved': 0, 'limit': 11}, - 'rbac_policies': {'used': 0, 'reserved': 0, 'limit': 15}, - 'routers': {'used': 0, 'reserved': 0, 'limit': 40}, - 'security_groups': {'used': 0, 'reserved': 0, 'limit': 10}, - 'security_group_rules': {'used': 0, 'reserved': 0, 'limit': 100}, - 'subnets': {'used': 0, 'reserved': 0, 'limit': 20}, - 'subnet_pools': {'used': 0, 'reserved': 0, 'limit': 30}, - } - - quota_attrs.update(attrs) - - quota = fakes.FakeResource( - info=copy.deepcopy(quota_attrs), loaded=True - ) - return quota - - class FakeFloatingIPPortForwarding: """Fake one or more Port forwarding""" diff --git a/openstackclient/tests/unit/volume/v2/fakes.py b/openstackclient/tests/unit/volume/v2/fakes.py index 389b10b0f9..1c183728c0 100644 --- a/openstackclient/tests/unit/volume/v2/fakes.py +++ b/openstackclient/tests/unit/volume/v2/fakes.py @@ -33,19 +33,6 @@ from openstackclient.tests.unit.identity.v3 import fakes as identity_fakes from openstackclient.tests.unit import utils -QUOTA = { - "gigabytes": 1000, - "volumes": 11, - "snapshots": 10, - "backups": 10, - "backup_gigabytes": 1000, - "per_volume_gigabytes": -1, - "gigabytes_volume_type_backend": -1, - "volumes_volume_type_backend": -1, - "snapshots_volume_type_backend": -1, -} - - class FakeVolumeClient: def __init__(self, **kwargs): self.auth_token = kwargs['token'] @@ -68,10 +55,6 @@ class FakeVolumeClient: self.pools.resource_class = fakes.FakeResource(None, {}) self.qos_specs = mock.Mock() self.qos_specs.resource_class = fakes.FakeResource(None, {}) - self.quota_classes = mock.Mock() - self.quota_classes.resource_class = fakes.FakeResource(None, {}) - self.quotas = mock.Mock() - self.quotas.resource_class = fakes.FakeResource(None, {}) self.restores = mock.Mock() self.restores.resource_class = fakes.FakeResource(None, {}) self.services = mock.Mock() @@ -991,74 +974,3 @@ def create_one_encryption_volume_type(attrs=None): info=copy.deepcopy(encryption_info), loaded=True ) return encryption_type - - -def create_one_vol_quota(attrs=None): - """Create one quota""" - attrs = attrs or {} - - quota_attrs = { - 'id': 'project-id-' + uuid.uuid4().hex, - 'backups': 100, - 'backup_gigabytes': 100, - 'gigabytes': 10, - 'per_volume_gigabytes': 10, - 'snapshots': 0, - 'volumes': 10, - } - - quota_attrs.update(attrs) - - quota = fakes.FakeResource(info=copy.deepcopy(quota_attrs), loaded=True) - quota.project_id = quota_attrs['id'] - - return quota - - -def create_one_default_vol_quota(attrs=None): - """Create one quota""" - attrs = attrs or {} - - quota_attrs = { - 'id': 'project-id-' + uuid.uuid4().hex, - 'backups': 100, - 'backup_gigabytes': 100, - 'gigabytes': 100, - 'per_volume_gigabytes': 100, - 'snapshots': 100, - 'volumes': 100, - } - - quota_attrs.update(attrs) - - quota = fakes.FakeResource(info=copy.deepcopy(quota_attrs), loaded=True) - quota.project_id = quota_attrs['id'] - - return quota - - -def create_one_detailed_quota(attrs=None): - """Create one quota""" - attrs = attrs or {} - - quota_attrs = { - 'volumes': {'limit': 3, 'in_use': 1, 'reserved': 0}, - 'per_volume_gigabytes': {'limit': -1, 'in_use': 0, 'reserved': 0}, - 'snapshots': {'limit': 10, 'in_use': 0, 'reserved': 0}, - 'gigabytes': {'limit': 1000, 'in_use': 5, 'reserved': 0}, - 'backups': {'limit': 10, 'in_use': 0, 'reserved': 0}, - 'backup_gigabytes': {'limit': 1000, 'in_use': 0, 'reserved': 0}, - 'volumes_lvmdriver-1': {'limit': -1, 'in_use': 1, 'reserved': 0}, - 'gigabytes_lvmdriver-1': {'limit': -1, 'in_use': 5, 'reserved': 0}, - 'snapshots_lvmdriver-1': {'limit': -1, 'in_use': 0, 'reserved': 0}, - 'volumes___DEFAULT__': {'limit': -1, 'in_use': 0, 'reserved': 0}, - 'gigabytes___DEFAULT__': {'limit': -1, 'in_use': 0, 'reserved': 0}, - 'snapshots___DEFAULT__': {'limit': -1, 'in_use': 0, 'reserved': 0}, - 'groups': {'limit': 10, 'in_use': 0, 'reserved': 0}, - 'id': uuid.uuid4().hex, - } - quota_attrs.update(attrs) - - quota = fakes.FakeResource(info=copy.deepcopy(quota_attrs), loaded=True) - - return quota diff --git a/openstackclient/tests/unit/volume/v3/fakes.py b/openstackclient/tests/unit/volume/v3/fakes.py index e792827047..208cd7622d 100644 --- a/openstackclient/tests/unit/volume/v3/fakes.py +++ b/openstackclient/tests/unit/volume/v3/fakes.py @@ -58,10 +58,6 @@ class FakeVolumeClient: self.group_types.resource_class = fakes.FakeResource(None, {}) self.messages = mock.Mock() self.messages.resource_class = fakes.FakeResource(None, {}) - self.quota_classes = mock.Mock() - self.quota_classes.resource_class = fakes.FakeResource(None, {}) - self.quotas = mock.Mock() - self.quotas.resource_class = fakes.FakeResource(None, {}) self.resource_filters = mock.Mock() self.resource_filters.resource_class = fakes.FakeResource(None, {}) self.restores = mock.Mock() diff --git a/requirements.txt b/requirements.txt index a20ce69f87..341525ea76 100644 --- a/requirements.txt +++ b/requirements.txt @@ -7,7 +7,7 @@ pbr!=2.1.0,>=2.0.0 # Apache-2.0 cryptography>=2.7 # BSD/Apache-2.0 cliff>=3.5.0 # Apache-2.0 iso8601>=0.1.11 # MIT -openstacksdk>=3.2.0 # Apache-2.0 +openstacksdk>=3.3.0 # Apache-2.0 osc-lib>=2.3.0 # Apache-2.0 oslo.i18n>=3.15.3 # Apache-2.0 python-keystoneclient>=3.22.0 # Apache-2.0