log meaningful error message on download exception

This changes:
    "IOError: [Errno 28] No space left on device"
to read (for example):
    "Error writing to
/var/lib/instances/_base/89e3812ae38b76b8f658e56a75f584de3bfb6941.part:
IOError: [Errno 28] No space left on device"

Co-Authored-By: Michael Still (mikal@stillhq.com)
Change-Id: I86305d100513b7f9c8cf50f5432524ec45460617
Closes-Bug: 1431631
This commit is contained in:
Joe Julian
2015-03-12 17:38:01 -07:00
committed by Michael Still
parent 77cadd2c48
commit eeea6092ff
2 changed files with 26 additions and 0 deletions

View File

@@ -28,6 +28,7 @@ import glanceclient.exc
from oslo_config import cfg
from oslo_log import log as logging
from oslo_serialization import jsonutils
from oslo_utils import excutils
from oslo_utils import netutils
from oslo_utils import timeutils
import six
@@ -364,6 +365,10 @@ class GlanceImageService(object):
try:
for chunk in image_chunks:
data.write(chunk)
except Exception as ex:
with excutils.save_and_reraise_exception():
LOG.error(_LE("Error writing to %(path)s: %(exception)s"),
{'path': dst_path, 'exception': ex})
finally:
if close_file:
data.close()

View File

@@ -15,6 +15,7 @@
import datetime
import StringIO
import glanceclient.exc
import mock
@@ -500,6 +501,26 @@ class TestDownloadNoDirectUri(test.NoDBTestCase):
)
self.assertFalse(data.close.called)
@mock.patch('__builtin__.open')
@mock.patch('nova.image.glance.GlanceImageService.show')
def test_download_data_dest_path_write_fails(self, show_mock, open_mock):
client = mock.MagicMock()
client.call.return_value = [1, 2, 3]
ctx = mock.sentinel.ctx
service = glance.GlanceImageService(client)
# NOTE(mikal): data is a file like object, which in our case always
# raises an exception when we attempt to write to the file.
class FakeDiskException(Exception):
pass
class Exceptionator(StringIO.StringIO):
def write(self, _):
raise FakeDiskException('Disk full!')
self.assertRaises(FakeDiskException, service.download, ctx,
mock.sentinel.image_id, data=Exceptionator())
@mock.patch('nova.image.glance.GlanceImageService._get_transfer_module')
@mock.patch('nova.image.glance.GlanceImageService.show')
def test_download_direct_file_uri(self, show_mock, get_tran_mock):