Improve quota object coverage

Added some basic test coverage for quota objects and
some minor tuning to make code easier to read.

Change-Id: I0a0a8ca15c8c0c46b52de4f57151c8998f418d2b
This commit is contained in:
Erik Olof Gunnar Andersson
2022-03-20 21:51:01 -07:00
parent 244028674c
commit ed5aa3f76c
3 changed files with 73 additions and 18 deletions

View File

@@ -40,9 +40,7 @@ class QuotaList(base.ListObjectMixin, base.DesignateObject):
@classmethod
def from_dict(cls, _dict):
instance = cls()
for field, value in _dict.items():
item = cls.LIST_ITEM_TYPE()
item.resource = field
@@ -52,10 +50,7 @@ class QuotaList(base.ListObjectMixin, base.DesignateObject):
return instance
def to_dict(self):
_dict = {}
for quota in self.objects:
_dict[quota.resource] = quota.hard_limit
return _dict

View File

@@ -59,32 +59,31 @@ class StorageQuota(base.Quota):
context = context.deepcopy()
context.all_tenants = True
def create_quota():
quota = objects.Quota(
tenant_id=tenant_id, resource=resource, hard_limit=hard_limit)
self.storage.create_quota(context, quota)
def update_quota(quota):
quota.hard_limit = hard_limit
self.storage.update_quota(context, quota)
if resource not in list(self.get_default_quotas(context).keys()):
raise exceptions.QuotaResourceUnknown("%s is not a valid quota "
"resource", resource)
try:
create_quota()
self._create_quota(context, tenant_id, resource, hard_limit)
except exceptions.Duplicate:
quota = self.storage.find_quota(context, {
'tenant_id': tenant_id,
'resource': resource,
})
update_quota(quota)
self._update_quota(context, quota, hard_limit)
return {resource: hard_limit}
def _create_quota(self, context, project_id, resource, hard_limit):
quota = objects.Quota(
tenant_id=project_id, resource=resource, hard_limit=hard_limit
)
self.storage.create_quota(context, quota)
def _update_quota(self, context, quota, hard_limit):
quota.hard_limit = hard_limit
self.storage.update_quota(context, quota)
@transaction
def reset_quotas(self, context, tenant_id):
context = context.deepcopy()

View File

@@ -0,0 +1,61 @@
# Licensed under the Apache License, Version 2.0 (the "License"); you may
# not use this file except in compliance with the License. You may obtain
# a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations
# under the License.
from oslo_log import log as logging
import oslotest.base
from designate import objects
LOG = logging.getLogger(__name__)
class QuotaTest(oslotest.base.BaseTestCase):
def test_quota(self):
quota = objects.Quota(
tenant_id='123', resource='dns', hard_limit=100
)
self.assertEqual('123', quota.tenant_id)
self.assertEqual('dns', quota.resource)
self.assertEqual(100, quota.hard_limit)
def test_quota_list(self):
quotas = objects.QuotaList()
quotas.append(objects.Quota(tenant_id='123', resource='dns1'))
quotas.append(objects.Quota(tenant_id='123', resource='dns2'))
quotas.append(objects.Quota(tenant_id='123', resource='dns3'))
self.assertEqual('dns1', quotas[0].resource)
self.assertEqual('dns2', quotas[1].resource)
self.assertEqual('dns3', quotas[2].resource)
def test_quota_list_from_dict(self):
quotas = objects.QuotaList().from_dict({
'zones': 100,
'zone_recordsets': 101,
'zone_records': 102,
'recordset_records': 103,
'api_export_size': 104,
})
self.assertEqual('zones', quotas[0].resource)
self.assertEqual(100, quotas[0].hard_limit)
self.assertEqual('api_export_size', quotas[4].resource)
self.assertEqual(104, quotas[4].hard_limit)
def test_quota_list_to_dict(self):
quotas = objects.QuotaList().from_dict({
'zones': 100,
'zone_recordsets': 101,
})
self.assertEqual(100, quotas.to_dict()['zones'])
self.assertEqual(101, quotas.to_dict()['zone_recordsets'])