From 62fa6d27a6bf84f06e7e3bfc967ca353ca3b0ebb Mon Sep 17 00:00:00 2001 From: Tim Burke Date: Tue, 11 Feb 2025 14:25:29 -0800 Subject: [PATCH] tests: Add unknown-policy container to account test Also: - Couple small clean-ups in account.backend and account.utils - Make a couple test assertions more useful. Related-Change: https://review.opendev.org/c/openstack/swift/+/940601 Change-Id: Ic14642df50592c982adfb55a0c6cfd673a5a95b8 --- swift/account/backend.py | 2 +- swift/account/reaper.py | 4 +-- swift/account/utils.py | 19 +++++------- test/unit/account/test_utils.py | 54 +++++++++++++++++++++++---------- 4 files changed, 49 insertions(+), 30 deletions(-) diff --git a/swift/account/backend.py b/swift/account/backend.py index dbeb1eaa0d..a4d7137ee6 100644 --- a/swift/account/backend.py +++ b/swift/account/backend.py @@ -367,7 +367,7 @@ class AccountBroker(DatabaseBroker): :param allow_reserved: exclude names with reserved-byte by default :returns: list of tuples of (name, object_count, bytes_used, - put_timestamp, storage_policy_index, 0) + put_timestamp, storage_policy_index, is_subdir) """ delim_force_gte = False if reverse: diff --git a/swift/account/reaper.py b/swift/account/reaper.py index 10d59ea185..0061e790ac 100644 --- a/swift/account/reaper.py +++ b/swift/account/reaper.py @@ -265,8 +265,8 @@ class AccountReaper(Daemon): container_limit, '', None, None, None, allow_reserved=True)) while containers: try: - for (container, _junk, _junk, _junk, _junk, - _junk) in containers: + for row in containers: + container = row[0] this_shard = ( int(md5(container.encode('utf-8'), usedforsecurity=False) diff --git a/swift/account/utils.py b/swift/account/utils.py index 231c207448..2a540faaeb 100644 --- a/swift/account/utils.py +++ b/swift/account/utils.py @@ -93,19 +93,16 @@ def account_listing_response(account, req, response_content_type, broker=None, 'count': object_count, 'bytes': bytes_used, 'last_modified': Timestamp(put_timestamp).isoformat} - # Add the container's storage policy to the response, unless: - # * storage_policy_index < 0, which means that - # the storage policy could not be determined - # * storage_policy_index was not found in POLICIES, - # which means the storage policy is missing from - # the Swift configuration. + # Add the container's storage policy to the response, unless + # storage_policy_index was not found in POLICIES, which means + # the storage policy is missing from the Swift configuration + # or otherwise could not be determined. + # # The storage policy should always be returned when # everything is configured correctly, but clients are - # expected to be able to handle this case regardless. - if ( - storage_policy_index >= 0 - and storage_policy_index in POLICIES - ): + # expected to be able to handle this case regardless, + # if only to support older versions of swift. + if storage_policy_index in POLICIES: container['storage_policy'] = ( POLICIES[storage_policy_index].name ) diff --git a/test/unit/account/test_utils.py b/test/unit/account/test_utils.py index 4ac806fb67..f7950280a4 100644 --- a/test/unit/account/test_utils.py +++ b/test/unit/account/test_utils.py @@ -227,6 +227,9 @@ class TestAccountUtils(TestDbBase): container_timestamp.internal, 0, 10, 100, 0) broker.put_container('bar', container_timestamp.internal, 0, 10, 100, 1) + # Can eat rows for policies not in POLICIES + broker.put_container('baz', + container_timestamp.internal, 0, 10, 100, 2) req = Request.blank('') resp = utils.account_listing_response( @@ -234,10 +237,10 @@ class TestAccountUtils(TestDbBase): self.assertEqual(resp.status_int, 200) expected = HeaderKeyDict({ 'Content-Type': 'application/json; charset=utf-8', - 'Content-Length': 233, - 'X-Account-Container-Count': 2, - 'X-Account-Object-Count': 20, - 'X-Account-Bytes-Used': 200, + 'Content-Length': str(len(resp.body)), + 'X-Account-Container-Count': 3, + 'X-Account-Object-Count': 30, + 'X-Account-Bytes-Used': 300, 'X-Timestamp': Timestamp(now).normal, 'X-PUT-Timestamp': put_timestamp.normal, 'X-Account-Storage-Policy-Zero-Container-Count': 1, @@ -246,23 +249,43 @@ class TestAccountUtils(TestDbBase): 'X-Account-Storage-Policy-One-Container-Count': 1, 'X-Account-Storage-Policy-One-Object-Count': 10, 'X-Account-Storage-Policy-One-Bytes-Used': 100, + # No POLICIES[2], so only account totals can include baz }) self.assertEqual(expected, resp.headers) expected = [{ "last_modified": container_timestamp.isoformat, "count": 10, "bytes": 100, - "name": 'foo', - 'storage_policy': POLICIES[0].name, + "name": 'bar', + 'storage_policy': POLICIES[1].name, }, { "last_modified": container_timestamp.isoformat, "count": 10, "bytes": 100, - "name": 'bar', - 'storage_policy': POLICIES[1].name, + "name": 'baz', + }, { + "last_modified": container_timestamp.isoformat, + "count": 10, + "bytes": 100, + "name": 'foo', + 'storage_policy': POLICIES[0].name, }] - self.assertEqual(sorted(json.dumps(expected).encode('ascii')), - sorted(resp.body)) + self.assertEqual(expected, json.loads(resp.body)) + + req = Request.blank('') + resp = utils.account_listing_response( + 'a', req, 'application/json', broker, delimiter='a') + self.assertEqual(resp.status_int, 200) + expected = [{ + "subdir": "ba", + }, { + "last_modified": container_timestamp.isoformat, + "count": 10, + "bytes": 100, + "name": 'foo', + 'storage_policy': POLICIES[0].name, + }] + self.assertEqual(expected, json.loads(resp.body)) @patch_policies([StoragePolicy(0, 'zero', is_default=True), StoragePolicy(1, 'one', is_default=False)]) @@ -325,14 +348,13 @@ class TestAccountUtils(TestDbBase): "last_modified": container_timestamp.isoformat, "count": 10, "bytes": 100, - "name": get_reserved_name('foo'), - 'storage_policy': POLICIES[0].name, + "name": get_reserved_name('bar'), + 'storage_policy': POLICIES[1].name, }, { "last_modified": container_timestamp.isoformat, "count": 10, "bytes": 100, - "name": get_reserved_name('bar'), - 'storage_policy': POLICIES[1].name, + "name": get_reserved_name('foo'), + 'storage_policy': POLICIES[0].name, }] - self.assertEqual(sorted(json.dumps(expected).encode('ascii')), - sorted(resp.body)) + self.assertEqual(expected, json.loads(resp.body))