better download error exception

This commit is contained in:
Jim Rollenhagen 2014-01-14 12:39:08 -08:00
parent 0db0d68315
commit a33d710308
3 changed files with 19 additions and 4 deletions

@ -61,3 +61,15 @@ class HeartbeatError(errors.RESTError):
def __init__(self, details):
super(HeartbeatError, self).__init__()
self.details = details
class ImageDownloadError(errors.RESTError):
"""Error raised when an image cannot be downloaded."""
message = 'Error downloading image.'
def __init__(self, image_id, image_url):
details = 'Image with id {} not found at url {}.'.format(image_id,
image_url)
super(ImageDownloadError, self).__init__()
self.details = details

@ -48,10 +48,11 @@ def _write_image(image_info, configdrive='configdrive', device='/dev/sda'):
def _download_image(image_info):
resp = requests.get(image_info['urls'][0], stream=True)
# TODO(jimrollenhagen) try all URLs
url = image_info['urls'][0]
resp = requests.get(url, stream=True)
if resp.status_code != 200:
# TODO(jimrollenhagen) define a better exception
raise Exception
raise errors.ImageDownloadError(image_info['id'], url)
image_location = _image_location(image_info)
with open(image_location, 'wb') as f:
for chunk in resp.iter_content(1024 * 1024):

@ -120,7 +120,9 @@ class TestBaseTeethAgent(unittest.TestCase):
image_info = self._build_fake_image_info()
response = requests_mock.return_value
response.status_code = 404
self.assertRaises(Exception, standby._download_image, image_info)
self.assertRaises(errors.ImageDownloadError,
standby._download_image,
image_info)
@mock.patch('teeth_agent.standby._verify_image', autospec=True)
@mock.patch('__builtin__.open', autospec=True)