From af75e9836155183504c30c9676c16a1463923eb7 Mon Sep 17 00:00:00 2001 From: Justin Riley Date: Tue, 14 May 2024 08:45:51 -0400 Subject: [PATCH] test validate_allocations fix for object quota = 0 --- .../management/commands/validate_allocations.py | 12 +++++++++++- .../tests/functional/openstack/test_allocation.py | 15 +++++++++++++++ 2 files changed, 26 insertions(+), 1 deletion(-) diff --git a/src/coldfront_plugin_cloud/management/commands/validate_allocations.py b/src/coldfront_plugin_cloud/management/commands/validate_allocations.py index 17d7d78..85c4f45 100644 --- a/src/coldfront_plugin_cloud/management/commands/validate_allocations.py +++ b/src/coldfront_plugin_cloud/management/commands/validate_allocations.py @@ -105,6 +105,8 @@ def handle(self, *args, **options): failed_validation = Command.sync_users(project_id, allocation, allocator, options["apply"]) + obj_key = openstack.QUOTA_KEY_MAPPING['object']['keys'][attributes.QUOTA_OBJECT_GB] + for attr in attributes.ALLOCATION_QUOTA_ATTRIBUTES: if 'OpenStack' in attr.name: key = openstack.QUOTA_KEY_MAPPING_ALL_KEYS.get(attr.name, None) @@ -116,7 +118,15 @@ def handle(self, *args, **options): expected_value = allocation.get_attribute(attr.name) current_value = quota.get(key, None) - if expected_value is None and current_value: + if key == obj_key and expected_value <= 0: + expected_obj_value = 1 + current_value = allocator.object(project_id).head_account().get(obj_key) + if current_value != expected_obj_value: + failed_validation = True + msg = (f'Value for quota for {attr.name} = {current_value} does not match expected' + f' value of {expected_obj_value} on allocation {allocation_str}') + logger.warning(msg) + elif expected_value is None and current_value: msg = (f'Attribute "{attr.name}" expected on allocation {allocation_str} but not set.' f' Current quota is {current_value}.') if options['apply']: diff --git a/src/coldfront_plugin_cloud/tests/functional/openstack/test_allocation.py b/src/coldfront_plugin_cloud/tests/functional/openstack/test_allocation.py index 8d16d06..2c523e7 100644 --- a/src/coldfront_plugin_cloud/tests/functional/openstack/test_allocation.py +++ b/src/coldfront_plugin_cloud/tests/functional/openstack/test_allocation.py @@ -216,6 +216,7 @@ def test_new_allocation_with_quantity(self): allocation.get_attribute(attributes.QUOTA_OBJECT_GB), allocator.get_quota(openstack_project.id)[obj_key] ) + # setting 0 object quota in coldfront -> 1 byte quota in swift/rgw utils.set_attribute_on_allocation(allocation, attributes.QUOTA_OBJECT_GB, 0) self.assertEqual(allocation.get_attribute(attributes.QUOTA_OBJECT_GB), 0) @@ -223,6 +224,20 @@ def test_new_allocation_with_quantity(self): obj_quota = allocator.object(project_id).head_account().get(obj_key) self.assertEqual(int(obj_quota), 1) + # test validate_allocations works for object quota set to 0 + utils.set_attribute_on_allocation(allocation, attributes.QUOTA_OBJECT_GB, 3) + self.assertEqual(allocation.get_attribute(attributes.QUOTA_OBJECT_GB), 3) + tasks.activate_allocation(allocation.pk) + self.assertEqual( + allocation.get_attribute(attributes.QUOTA_OBJECT_GB), + allocator.get_quota(openstack_project.id)[obj_key] + ) + utils.set_attribute_on_allocation(allocation, attributes.QUOTA_OBJECT_GB, 0) + self.assertEqual(allocation.get_attribute(attributes.QUOTA_OBJECT_GB), 0) + call_command('validate_allocations', apply=True) + obj_quota = allocator.object(project_id).head_account().get(obj_key) + self.assertEqual(int(obj_quota), 1) + def test_reactivate_allocation(self): user = self.new_user()