Add more info to checksum exception

If an image cannot be downloaded for some reason, it is helpful for
operators to have the image path, checksum, and calculated checksums
available easily from the API.

Change-Id: I6a2fb46726245cebd730b5c51d4f25f8465f1658
This commit is contained in:
Josh Gachnang
2015-08-26 12:47:43 -07:00
parent 9783c6477d
commit e17129dbad
4 changed files with 22 additions and 13 deletions

View File

@@ -150,10 +150,14 @@ class ImageChecksumError(RESTError):
"""Error raised when an image fails to verify against its checksum."""
message = 'Error verifying image checksum'
details_str = ('Image failed to verify against checksum. location: {0}; '
'image ID: {1}; image checksum: {2}; verification '
'checksum: {3}')
def __init__(self, image_id):
details = 'Image with id {0} failed to verify against checksum.'
details = details.format(image_id)
def __init__(self, image_id, image_location, checksum,
calculated_checksum):
details = self.details_str.format(image_location, image_id, checksum,
calculated_checksum)
super(ImageChecksumError, self).__init__(details)

View File

@@ -157,8 +157,7 @@ def _download_image(image_info):
LOG.info("Image downloaded from {0} in {1} seconds".format(image_location,
totaltime))
if not _verify_image(image_info, image_location):
raise errors.ImageChecksumError(image_info['id'])
_verify_image(image_info, image_location)
def _verify_image(image_info, image_location):
@@ -175,10 +174,12 @@ def _verify_image(image_info, image_location):
hash_digest = hash_.hexdigest()
if hash_digest == checksum:
return True
log_msg = ('Image verification failed. Location: {0};'
'image hash: {1}; verification hash: {2}')
LOG.warning(log_msg.format(image_location, checksum, hash_digest))
return False
LOG.error(errors.ImageChecksumError.details_str.format(
image_location, image_info['id'], checksum, hash_digest))
raise errors.ImageChecksumError(image_location, image_info['id'], checksum,
hash_digest)
def _validate_image_info(ext, image_info=None, **kwargs):

View File

@@ -236,7 +236,8 @@ class TestStandbyExtension(test_base.BaseTestCase):
image_info = self._build_fake_image_info()
response = requests_mock.return_value
response.status_code = 200
verify_mock.return_value = False
verify_mock.side_effect = errors.ImageChecksumError(
'foo', '/bar/foo', 'incorrect', 'correct')
self.assertRaises(errors.ImageChecksumError,
standby._download_image,
image_info)
@@ -266,8 +267,9 @@ class TestStandbyExtension(test_base.BaseTestCase):
open_mock.return_value.__enter__.return_value = file_mock
image_location = '/foo/bar'
verified = standby._verify_image(image_info, image_location)
self.assertFalse(verified)
self.assertRaises(errors.ImageChecksumError,
standby._verify_image,
image_info, image_location)
self.assertEqual(md5_mock.call_count, 1)
@mock.patch('ironic_python_agent.hardware.dispatch_to_managers',

View File

@@ -106,7 +106,9 @@ class TestErrors(test_base.BaseTestCase):
(errors.LookupAgentInterfaceError(DETAILS), SAME_DETAILS),
(errors.ImageDownloadError('image_id', DETAILS),
DIFF_CL_DETAILS),
(errors.ImageChecksumError('image_id'), DIFF_CL_DETAILS),
(errors.ImageChecksumError(
'image_id', '/foo/image_id', 'incorrect', 'correct'),
DIFF_CL_DETAILS),
(errors.ImageWriteError('device', 'exit_code', 'stdout',
'stderr'),
DIFF_CL_DETAILS),