Include transaction ID on content-check failures

Change-Id: I6b667db26ffc5dccdcadfc8c73f7accb81f03dac
This commit is contained in:
Tim Burke 2023-03-14 11:52:50 -07:00
parent d572ccfae9
commit e343298240
2 changed files with 27 additions and 9 deletions

View File

@ -419,6 +419,9 @@ class _SwiftReader:
def __init__(self, path, body, headers, checksum=True):
self._path = path
self._body = body
self._txn_id = headers.get('x-openstack-request-id')
if self._txn_id is None:
self._txn_id = headers.get('x-trans-id')
self._actual_read = 0
self._content_length = None
self._actual_md5 = None
@ -459,17 +462,20 @@ class _SwiftReader:
def _check_contents(self):
if (self._content_length is not None and
self._actual_read != self._content_length):
raise SwiftError('Error downloading {0}: read_length != '
'content_length, {1:d} != {2:d}'.format(
self._path, self._actual_read,
self._content_length))
raise SwiftError(
'Error downloading {0}: read_length != content_length, '
'{1:d} != {2:d} (txn: {3})'.format(
self._path, self._actual_read, self._content_length,
self._txn_id or 'unknown'))
if self._actual_md5 and self._expected_md5:
etag = self._actual_md5.hexdigest()
if etag != self._expected_md5:
raise SwiftError('Error downloading {0}: md5sum != etag, '
'{1} != {2}'.format(
self._path, etag, self._expected_md5))
raise SwiftError(
'Error downloading {0}: md5sum != etag, '
'{1} != {2} (txn: {3})'.format(
self._path, etag, self._expected_md5,
self._txn_id or 'unknown'))
def bytes_read(self):
return self._actual_read

View File

@ -201,7 +201,19 @@ class TestSwiftReader(unittest.TestCase):
with self.assertRaises(SwiftError) as cm:
_consume(sr)
self.assertEqual(
"'Error downloading path: read_length != content_length, 4 != 5'",
"'Error downloading path: read_length != content_length, "
"4 != 5 (txn: unknown)'",
str(cm.exception))
# Check error includes txn id if available
sr = self.sr('path', BytesIO(b'body'), {'content-length': 5,
'etag': 'bad etag',
'x-trans-id': 'uuid'})
with self.assertRaises(SwiftError) as cm:
_consume(sr)
self.assertEqual(
"'Error downloading path: read_length != content_length, "
"4 != 5 (txn: uuid)'",
str(cm.exception))
# Check error is raised if SwiftReader doesn't calculate the expected
@ -212,7 +224,7 @@ class TestSwiftReader(unittest.TestCase):
_consume(sr)
self.assertEqual(
"'Error downloading path: md5sum != etag, "
"841a2d689ad86bd1611447453c22c6fc != bad etag'",
"841a2d689ad86bd1611447453c22c6fc != bad etag (txn: unknown)'",
str(cm.exception))
sr = self.sr('path', BytesIO(b'body'), {'content-length': 4})