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
This commit is contained in:
Tim Burke 2025-02-11 14:25:29 -08:00
parent 965cc2fcbc
commit 62fa6d27a6
4 changed files with 49 additions and 30 deletions

View File

@ -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:

View File

@ -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)

View File

@ -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
)

View File

@ -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))